iwla

iwla Git Source Tree

Root/plugins/display/top_visitors.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
21import time
22
23from iwla import IWLA
24from iplugin import IPlugin
25from display import *
26
27"""
28Display hook
29
30Create TOP visitors block
31
32Plugin requirements :
33 None
34
35Conf values needed :
36 display_visitor_ip*
37
38Output files :
39 OUTPUT_ROOT/year/month/index.html
40
41Statistics creation :
42 None
43
44Statistics update :
45 None
46
47Statistics deletion :
48 None
49"""
50
51class IWLADisplayTopVisitors(IPlugin):
52 def __init__(self, iwla):
53 super(IWLADisplayTopVisitors, self).__init__(iwla)
54 self.API_VERSION = 1
55
56 def hook(self):
57 hits = self.iwla.getValidVisitors()
58 display = self.iwla.getDisplay()
59 display_visitor_ip = self.iwla.getConfValue('display_visitor_ip', False)
60
61 total = [0]*5
62 for super_hit in hits.values():
63 total[1] += super_hit['viewed_pages'][0]
64 total[2] += super_hit['viewed_hits'][0]
65 total[3] += super_hit['bandwidth'][0]
66
67 top_bandwidth = [(k,v['bandwidth'][0]) for (k,v) in hits.items()]
68 top_bandwidth = sorted(top_bandwidth, key=lambda t: t[1], reverse=True)
69 top_visitors = [hits[h[0]] for h in top_bandwidth[:10]]
70
71 index = self.iwla.getDisplayIndex()
72 table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Top visitors'), [self.iwla._(u'Host'), self.iwla._(u'Pages'), self.iwla._(u'Hits'), self.iwla._(u'Bandwidth'), self.iwla._(u'Last seen')], [3])
73 table.setColsCSSClass(['', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', ''])
74 for super_hit in top_visitors:
75 address = super_hit['remote_addr']
76 if display_visitor_ip and\
77 super_hit.get('dns_name_replaced', False):
78 address = '%s [%s]' % (address, super_hit['remote_ip'])
79
80 row = [
81 address,
82 super_hit['viewed_pages'][0],
83 super_hit['viewed_hits'][0],
84 super_hit['bandwidth'][0],
85 time.asctime(super_hit['last_access'])
86 ]
87 total[1] -= super_hit['viewed_pages'][0]
88 total[2] -= super_hit['viewed_hits'][0]
89 total[3] -= super_hit['bandwidth'][0]
90 table.appendRow(row)
91 if total[1] or total[2] or total[3]:
92 total[0] = self.iwla._(u'Others')
93 total[4] = ''
94 table.appendRow(total)
95 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
96 index.appendBlock(table)

Archive Download this file

Branches

Tags