Add hours_stats plugin

This commit is contained in:
Grégory Soutadé 2015-03-02 19:44:10 +01:00
parent b563609b24
commit f853ad808e
2 changed files with 184 additions and 0 deletions

View File

@ -0,0 +1,88 @@
# -*- coding: utf-8 -*-
#
# Copyright Grégory Soutadé 2015
# This file is part of iwla
# iwla is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# iwla is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with iwla. If not, see <http://www.gnu.org/licenses/>.
#
from iwla import IWLA
from iplugin import IPlugin
from display import *
"""
Display hook
Display statistics by hour/week day
Plugin requirements :
post_analysis/hours_stats
Conf values needed :
None
Output files :
OUTPUT_ROOT/year/month/index.html
Statistics creation :
None
Statistics update :
None
Statistics deletion :
None
"""
class IWLADisplayHoursStats(IPlugin):
def __init__(self, iwla):
super(IWLADisplayHoursStats, self).__init__(iwla)
self.API_VERSION = 1
self.requires = ['IWLAPostAnalysisHoursStats']
def hook(self):
display = self.iwla.getDisplay()
month_stats = self.iwla.getMonthStats()
hours_stats = month_stats.get('hours_stats', {})
if not hours_stats:
for i in range(0, 24):
hours_stats[i] = {'pages':0, 'hits':0, 'bandwidth':0}
days_stats = month_stats.get('days_stats', {})
if not days_stats:
for i in range(0, 7):
days_stats[i] = {'pages':0, 'hits':0, 'bandwidth':0}
index = self.iwla.getDisplayIndex()
# By Day
title = self.iwla._(u'By day')
days = [self.iwla._('Mon'), self.iwla._('Tue'), self.iwla._('Wed'), self.iwla._('Thu'), self.iwla._('Fri'), self.iwla._('Sat'), self.iwla._('Sun')]
table = display.createBlock(DisplayHTMLBlockTableWithGraph, title, [self.iwla._('Day'), self.iwla._('Pages'), self.iwla._('Hits'), self.iwla._('Bandwidth')], days, 7, range(1,4))
table.setColsCSSClass(['', 'iwla_page', 'iwla_hit', 'iwla_bandwidth'])
for i in range(0,7):
table.appendRow([days[i], days_stats[i]['pages'], days_stats[i]['hits'], days_stats[i]['bandwidth']])
table.setCellValue(i, 3, bytesToStr(days_stats[i]['bandwidth']))
index.appendBlock(table)
# By Hours
title = self.iwla._(u'By Hours')
hours = ['%02d' % i for i in range(0, 24)]
table = display.createBlock(DisplayHTMLBlockTableWithGraph, title, [self.iwla._('Hours'), self.iwla._('Pages'), self.iwla._('Hits'), self.iwla._('Bandwidth')], hours, 24, range(1,4))
table.setColsCSSClass(['', 'iwla_page', 'iwla_hit', 'iwla_bandwidth'])
for i in range(0,24):
table.appendRow([hours[i], hours_stats[i]['pages'], hours_stats[i]['hits'], hours_stats[i]['bandwidth']])
table.setCellValue(i, 3, bytesToStr(hours_stats[i]['bandwidth']))
index.appendBlock(table)

View File

@ -0,0 +1,96 @@
# -*- coding: utf-8 -*-
#
# Copyright Grégory Soutadé 2015
# This file is part of iwla
# iwla is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# iwla is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with iwla. If not, see <http://www.gnu.org/licenses/>.
#
from iwla import IWLA
from iplugin import IPlugin
"""
Post analysis hook
Count pages, hits and bandwidth by hour/week day
Plugin requirements :
None
Conf values needed :
None
Output files :
None
Statistics creation :
month_stats:
hours_stats =>
00 .. 23 =>
pages
hits
bandwidth
days_stats =>
0 .. 6 =>
pages
hits
bandwidth
Statistics update :
None
Statistics deletion :
None
"""
class IWLAPostAnalysisHoursStats(IPlugin):
def __init__(self, iwla):
super(IWLAPostAnalysisHoursStats, self).__init__(iwla)
self.API_VERSION = 1
def hook(self):
stats = self.iwla.getCurrentVisists()
month_stats = self.iwla.getMonthStats()
hours_stats = month_stats.get('hours_stats', {})
if not hours_stats:
for i in range(0, 24):
hours_stats[i] = {'pages':0, 'hits':0, 'bandwidth':0}
days_stats = month_stats.get('days_stats', {})
if not days_stats:
for i in range(0, 7):
days_stats[i] = {'pages':0, 'hits':0, 'bandwidth':0}
for super_hit in stats.values():
if super_hit['robot']: continue
for r in super_hit['requests'][::-1]:
if not self.iwla.isValidForCurrentAnalysis(r):
break
if not self.iwla.hasBeenViewed(r): continue
key = r['is_page'] and 'pages' or 'hits'
t = r['time_decoded']
hours_stats[t.tm_hour][key] += 1
hours_stats[t.tm_hour]['bandwidth'] += int(r['body_bytes_sent'])
days_stats[t.tm_wday][key] += 1
days_stats[t.tm_wday]['bandwidth'] += int(r['body_bytes_sent'])
month_stats['hours_stats'] = hours_stats
month_stats['days_stats'] = days_stats