iwla

iwla Git Source Tree

Root/plugins/post_analysis/hours_stats.py

1# -*- coding: utf-8 -*-
2#
3# Copyright Grégory Soutadé 2015
4
5# This file is part of iwla
6
7# iwla is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# iwla is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with iwla. If not, see <http://www.gnu.org/licenses/>.
19#
20
21from iwla import IWLA
22from iplugin import IPlugin
23
24"""
25Post analysis hook
26
27Count pages, hits and bandwidth by hour/week day
28
29Plugin requirements :
30 None
31
32Conf values needed :
33 None
34
35Output files :
36 None
37
38Statistics creation :
39month_stats:
40 hours_stats =>
41 00 .. 23 =>
42 pages
43 hits
44 bandwidth
45
46 days_stats =>
47 0 .. 6 =>
48 pages
49 hits
50 bandwidth
51
52Statistics update :
53 None
54
55Statistics deletion :
56 None
57"""
58
59class IWLAPostAnalysisHoursStats(IPlugin):
60 def __init__(self, iwla):
61 super(IWLAPostAnalysisHoursStats, self).__init__(iwla)
62 self.API_VERSION = 1
63
64 def hook(self):
65 stats = self.iwla.getCurrentVisists()
66 month_stats = self.iwla.getMonthStats()
67
68 hours_stats = month_stats.get('hours_stats', {})
69 if not hours_stats:
70 for i in range(0, 24):
71 hours_stats[i] = {'pages':0, 'hits':0, 'bandwidth':0}
72 days_stats = month_stats.get('days_stats', {})
73 if not days_stats:
74 for i in range(0, 7):
75 days_stats[i] = {'pages':0, 'hits':0, 'bandwidth':0}
76
77 for super_hit in stats.values():
78 if super_hit['robot']: continue
79 for r in super_hit['requests'][::-1]:
80 if not self.iwla.isValidForCurrentAnalysis(r):
81 break
82
83 if not self.iwla.hasBeenViewed(r): continue
84
85 key = r['is_page'] and 'pages' or 'hits'
86
87 t = r['time_decoded']
88
89 hours_stats[t.tm_hour][key] += 1
90 hours_stats[t.tm_hour]['bandwidth'] += int(r['body_bytes_sent'])
91
92 days_stats[t.tm_wday][key] += 1
93 days_stats[t.tm_wday]['bandwidth'] += int(r['body_bytes_sent'])
94
95 month_stats['hours_stats'] = hours_stats
96 month_stats['days_stats'] = days_stats

Archive Download this file

Branches

Tags