WebStats: add the hourly sum and its progressbar

master
Valentin Lorentz 2010-11-21 15:32:29 +01:00
parent a1782854e1
commit 369d798f2c
3 changed files with 70 additions and 14 deletions

View File

@ -125,7 +125,7 @@ class WebStatsDB:
def __init__(self):
filename = conf.supybot.directories.data.dirize('WebStats.db')
alreadyExists = os.path.exists(filename)
if alreadyExists and (DEBUG or testing):
if alreadyExists and testing:
os.remove(filename)
alreadyExists = False
self._conn = sqlite3.connect(filename, check_same_thread = False)
@ -307,6 +307,32 @@ class WebStatsDB:
return min_, max_
def getChanXXlyData(self, chanName, type_):
"""Same as getChanGlobalData, but for the given
year/month/day/dayofweek/hour.
For example, getChanXXlyData('#test', 'hour') returns a list of 24
getChanGlobalData-like tuples."""
sampleQuery = """SELECT lines, words, chars, joins, parts, quits
FROM chans_cache WHERE chan=? and %s=?"""
min_, max_ = self.getChanRecordingTimeBoundaries(chanName)
typeToIndex = {"year":0, "month":1, "day":2, "dayofweek":3, "hour":4}
if type_ not in typeToIndex:
raise ValueError("Invalid type")
min_ = min_[typeToIndex[type_]]
max_ = max_[typeToIndex[type_]]
results = {}
for index in range(min_, max_+1):
query = sampleQuery % (type_)
cursor = self._conn.cursor()
cursor.execute(query, (chanName, index))
try:
results.update({index: cursor.fetchone()})
except:
self._addKeyInTmpCacheIfDoesNotExist(results, index)
cursor.close()
return results
class WebStatsHTTPServer(BaseHTTPServer.HTTPServer):
"""A simple class that set a smaller timeout to the socket"""
timeout = 0.1

View File

@ -9,25 +9,34 @@ except:
internationalizeDocstring = lambda x:x
content = \
"<h1>%s</h1><p>%s</p><p>%s</p>"
'<h1>%s</h1><p>%s</p>'
def get(useSkeleton, channel, db):
channel = '#' + channel
items = db.getChanGlobalData(channel)
bound = db.getChanRecordingTimeBoundaries(channel)
print repr(bound)
output = content % (_("Stats about %s channel"),
_("There were %n, %n, %n, %n, %n, and %n."),
_("Recording from %i/%i/%i (%i) at %s to %i/%i/%i (%i) at %s")
)
output = utils.str.format(output, channel, (items[0], 'line'), (items[1], 'word'),
output = '<h1>%s</h1>' % _('Stats about %s channel')
output %= channel
output += '<p>%s</p>' % _('There were %n, %n, %n, %n, %n, and %n.')
output = utils.str.format(output, (items[0], 'line'), (items[1], 'word'),
(items[2], 'char'), (items[3], 'join'),
(items[4], 'part'), (items[5], 'quit'),
bound[0][0], bound[0][1], bound[0][2],
bound[0][3], bound[0][4],
bound[1][0], bound[1][1], bound[1][2],
bound[1][3], bound[1][4])
(items[4], 'part'), (items[5], 'quit'))
output += '<table><tr><th>%s</th><th style="width: 100px;">%s</th></tr>'
output %= (_('Hour'), _('Lines'))
items = db.getChanXXlyData(channel, 'hour')
max_ = 0
for hour in items:
max_ = max(max_, items[hour][0])
for hour in items:
output += """<tr>
<td>%s</td>
<td class="progressbar">
<div class="text">%i</div>
<div style="width: %ipx" class="color"></div>
</td>
</tr>""" % \
(hour, items[hour][0], round(float(items[hour][0])/float(max_)*100))
output += '</table>'
if useSkeleton:
output = ''.join([skeleton.start, output, skeleton.end])
return output

View File

@ -79,6 +79,27 @@ h1 {
.chanslist li a:visited {
color: blue;
}
table {
margin-left: auto;
margin-right: auto;
}
.progressbar {
border: orange 1px solid;
height: 20px;
}
.progressbar .color {
background-color: orange;
height: 20px;
text-align: center;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
}
.progressbar .text {
position: absolute;
width: 100px;
text-align: center;
}
"""
def get(useSkeleton=True):