iwla/plugins/display/istats_diff.py

116 lines
3.6 KiB
Python
Raw Normal View History

2014-12-31 14:22:46 +01:00
# -*- 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
from display import *
import logging
import re
2014-12-31 14:22:46 +01:00
"""
Display hook interface
2014-12-31 14:22:46 +01:00
Enlight new and updated statistics
Plugin requirements :
None
Conf values needed :
None
Output files :
None
Statistics creation :
None
Statistics update :
None
Statistics deletion :
None
"""
class IWLADisplayStatsDiff(IPlugin):
def __init__(self, iwla):
super(IWLADisplayStatsDiff, self).__init__(iwla)
self.API_VERSION = 1
self.month_stats_key = None
# Set >= if month_stats[self.month_stats_key] is a list or a tuple
self.stats_index = -1
self.display_index = 1
2014-12-31 14:22:46 +01:00
self.filename = None
self.block_name = None
self.logger = logging.getLogger(__name__)
self.tag_re = re.compile(r'<[^>]+>')
2014-12-31 14:22:46 +01:00
def load(self):
if not self.month_stats_key or not self.filename or\
not self.block_name:
self.logger('Bad parametrization')
return False
month_stats = self.iwla.getMonthStats()
self.cur_stats = {k:v for (k,v) in month_stats.get(self.month_stats_key, {}).items()}
return True
# from https://tutorialedge.net/python/removing-html-from-string/
def remove_tags(self, text):
return self.tag_re.sub('', text)
2014-12-31 14:22:46 +01:00
def hook(self):
display = self.iwla.getDisplay()
month_stats = self.iwla.getMonthStats()
path = self.iwla.getCurDisplayPath(self.filename)
page = display.getPage(path)
if not page:
self.logger.error('No page for %s' % (path))
return
2014-12-31 14:22:46 +01:00
title = self.iwla._(self.block_name)
block = page.getBlock(title)
if not block:
self.logger.error('Block %s not found' % (title))
return
stats_diff = {}
for (k, v) in month_stats[self.month_stats_key].items():
new_value = self.cur_stats.get(k, 0)
if new_value:
if self.stats_index != -1:
if new_value[self.stats_index] != v[self.stats_index]:
diff_value = v[self.stats_index] - new_value[self.stats_index]
stats_diff[k] = ['iwla_update', diff_value]
2014-12-31 14:22:46 +01:00
else:
if new_value != v:
diff_value = v - new_value
stats_diff[k] = ['iwla_update', diff_value]
2014-12-31 14:22:46 +01:00
else:
stats_diff[k] = ['iwla_new', 0]
2014-12-31 14:22:46 +01:00
for (idx, row) in enumerate(block.rows):
clear_text = self.remove_tags(row[0])
if clear_text in stats_diff.keys():
(cls, diff) = stats_diff[clear_text]
block.setCellCSSClass(idx, 0, cls)
if diff:
value = block.getCellValue(idx, self.display_index)
value += ' (+%d)' % diff
block.setCellValue(idx, self.display_index, value)