iwla/plugins/post_analysis/hours_stats.py
2016-02-04 20:44:36 +01:00

97 lines
2.6 KiB
Python

# -*- 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.getCurrentVisits()
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