iwla/plugins/display/top_visitors.py

92 lines
2.9 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/>.
#
import time
from iwla import IWLA
from iplugin import IPlugin
from display import *
"""
Display hook
Create TOP visitors block
Plugin requirements :
None
Conf values needed :
None
Output files :
OUTPUT_ROOT/year/month/index.html
Statistics creation :
None
Statistics update :
None
Statistics deletion :
None
"""
class IWLADisplayTopVisitors(IPlugin):
def __init__(self, iwla):
super(IWLADisplayTopVisitors, self).__init__(iwla)
self.API_VERSION = 1
def hook(self):
hits = self.iwla.getValidVisitors()
display = self.iwla.getDisplay()
display_visitor_ip = self.iwla.getConfValue('display_visitor_ip', False)
total = [0]*5
for super_hit in hits.values():
total[1] += super_hit['viewed_pages'][0]
total[2] += super_hit['viewed_hits'][0]
total[3] += super_hit['bandwidth'][0]
top_bandwidth = [(k,v['bandwidth'][0]) for (k,v) in hits.items()]
top_bandwidth = sorted(top_bandwidth, key=lambda t: t[1], reverse=True)
top_visitors = [hits[h[0]] for h in top_bandwidth[:10]]
index = self.iwla.getDisplayIndex()
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])
table.setColsCSSClass(['', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', ''])
for super_hit in top_visitors:
row = [
super_hit['remote_addr'],
super_hit['viewed_pages'][0],
super_hit['viewed_hits'][0],
super_hit['bandwidth'][0],
time.asctime(super_hit['last_access'])
]
total[1] -= super_hit['viewed_pages'][0]
total[2] -= super_hit['viewed_hits'][0]
total[3] -= super_hit['bandwidth'][0]
table.appendRow(row, super_hit['remote_ip'])
if total[1] or total[2] or total[3]:
total[0] = self.iwla._(u'Others')
total[4] = ''
table.appendRow(total)
table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
index.appendBlock(table)