121 lines
3.9 KiB
Python
121 lines
3.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 re
|
|
|
|
from iwla import IWLA
|
|
from iplugin import IPlugin
|
|
from display import *
|
|
|
|
import awstats_data
|
|
|
|
"""
|
|
Display hook
|
|
|
|
Add geo statistics
|
|
|
|
Plugin requirements :
|
|
post_analysis/ip_to_geo
|
|
|
|
Conf values needed :
|
|
create_geo_page*
|
|
|
|
Output files :
|
|
OUTPUT_ROOT/year/month/index.html
|
|
|
|
Statistics creation :
|
|
None
|
|
|
|
Statistics update :
|
|
None
|
|
|
|
Statistics deletion :
|
|
None
|
|
"""
|
|
|
|
class IWLADisplayTopGeo(IPlugin):
|
|
def __init__(self, iwla):
|
|
super(IWLADisplayTopGeo, self).__init__(iwla)
|
|
self.API_VERSION = 1
|
|
self.requires = ['IWLAPostAnalysisIPToGeo']
|
|
|
|
def load(self):
|
|
self.icon_path = self.iwla.getConfValue('icon_path', '/')
|
|
self.create_geo_page = self.iwla.getConfValue('create_geo_page_page', True)
|
|
|
|
display = self.iwla.getDisplay()
|
|
display.addColumnFilter(self.iwla._(u'Host'), self.FlagFilter, {'self':self})
|
|
|
|
return True
|
|
|
|
@staticmethod # Needed to have unbound methd
|
|
def FlagFilter(host, self):
|
|
cc = None
|
|
host_name = host.split(' ')[0] # hostname or ip
|
|
if host_name in self.valid_visitors.keys():
|
|
cc = self.valid_visitors[host_name]['country_code']
|
|
else:
|
|
for visitor in self.valid_visitors.values():
|
|
if visitor['remote_addr'] == host_name:
|
|
cc = visitor['country_code']
|
|
break
|
|
if not cc or cc == 'ip': return None
|
|
icon = '<img src="/%s/flags/%s.png"/>' % (self.icon_path, cc)
|
|
return '%s %s' % (icon ,host)
|
|
|
|
def hook(self):
|
|
display = self.iwla.getDisplay()
|
|
geo = self.iwla.getMonthStats()['geo']
|
|
geo = sorted(geo.items(), key=lambda t: t[1], reverse=True)
|
|
self.valid_visitors = self.iwla.getValidVisitors()
|
|
|
|
# All in a page
|
|
if self.create_geo_page:
|
|
title = createCurTitle(self.iwla, u'All Coutries')
|
|
filename = 'geo.html'
|
|
path = self.iwla.getCurDisplayPath(filename)
|
|
|
|
page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
|
|
table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Countries'), ['', self.iwla._(u'Country'), self.iwla._(u'Visitors')])
|
|
table.setColsCSSClass(['', '', 'iwla_hit'])
|
|
for (cc, visitors) in geo:
|
|
icon = '<img src="/%s/flags/%s.png"/>' % (self.icon_path, cc)
|
|
table.appendRow([icon, cc, visitors])
|
|
table.computeRatio(2)
|
|
page.appendBlock(table)
|
|
|
|
display.addPage(page)
|
|
|
|
# Countries in index
|
|
title = self.iwla._(u'Countries')
|
|
if self.create_geo_page:
|
|
link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'Details'))
|
|
title = '%s - %s' % (title, link)
|
|
|
|
index = self.iwla.getDisplayIndex()
|
|
|
|
table = display.createBlock(DisplayHTMLBlockTable, title, ['', self.iwla._(u'Countries'), self.iwla._(u'Visitors')])
|
|
table.setColsCSSClass(['', '', 'iwla_hit'])
|
|
for (cc, visitors) in geo[:10]:
|
|
icon = '<img src="/%s/flags/%s.png"/>' % (self.icon_path, cc)
|
|
table.appendRow([icon, cc, visitors])
|
|
table.computeRatio(2)
|
|
index.appendBlock(table)
|