iwla

iwla Git Source Tree

Root/plugins/display/istats_diff.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
23from display import *
24import logging
25import re
26
27"""
28Display hook interface
29
30Enlight new and updated statistics
31
32Plugin requirements :
33 None
34
35Conf values needed :
36 None
37
38Output files :
39 None
40
41Statistics creation :
42 None
43
44Statistics update :
45 None
46
47Statistics deletion :
48 None
49"""
50
51class IWLADisplayStatsDiff(IPlugin):
52 def __init__(self, iwla):
53 super(IWLADisplayStatsDiff, self).__init__(iwla)
54 self.API_VERSION = 1
55 self.month_stats_key = None
56 # Set >= if month_stats[self.month_stats_key] is a list or a tuple
57 self.stats_index = -1
58 self.display_index = 1
59 self.filename = None
60 self.block_name = None
61 self.logger = logging.getLogger(__name__)
62 self.tag_re = re.compile(r'<[^>]+>')
63
64 def load(self):
65 if not self.month_stats_key or not self.filename or\
66 not self.block_name:
67 self.logger('Bad parametrization')
68 return False
69 month_stats = self.iwla.getMonthStats()
70 self.cur_stats = {k:v for (k,v) in month_stats.get(self.month_stats_key, {}).items()}
71 return True
72
73 # from https://tutorialedge.net/python/removing-html-from-string/
74 def remove_tags(self, text):
75 return self.tag_re.sub('', text)
76
77 def hook(self):
78 display = self.iwla.getDisplay()
79 month_stats = self.iwla.getMonthStats()
80
81 path = self.iwla.getCurDisplayPath(self.filename)
82 page = display.getPage(path)
83 if not page:
84 self.logger.error('No page for %s' % (path))
85 return
86 title = self.iwla._(self.block_name)
87 block = page.getBlock(title)
88 if not block:
89 self.logger.error('Block %s not found' % (title))
90 return
91
92 stats_diff = {}
93 for (k, v) in month_stats[self.month_stats_key].items():
94 new_value = self.cur_stats.get(k, 0)
95 if new_value:
96 if self.stats_index != -1:
97 if new_value[self.stats_index] != v[self.stats_index]:
98 diff_value = v[self.stats_index] - new_value[self.stats_index]
99 stats_diff[k] = ['iwla_update', diff_value]
100 else:
101 if new_value != v:
102 diff_value = v - new_value
103 stats_diff[k] = ['iwla_update', diff_value]
104 else:
105 stats_diff[k] = ['iwla_new', 0]
106
107 for (idx, row) in enumerate(block.rows):
108 clear_text = self.remove_tags(row[0])
109 if clear_text in stats_diff.keys():
110 (cls, diff) = stats_diff[clear_text]
111 block.setCellCSSClass(idx, 0, cls)
112 if diff:
113 value = block.getCellValue(idx, self.display_index)
114 value += ' (+%d)' % diff
115 block.setCellValue(idx, self.display_index, value)

Archive Download this file

Branches

Tags