|
1
|
|
|
# Copyright 2013 Mathias WOLFF |
|
2
|
|
|
# This file is part of pyfreebilling. |
|
3
|
|
|
# |
|
4
|
|
|
# pyfreebilling is free software: you can redistribute it and/or modify |
|
5
|
|
|
# it under the terms of the GNU General Public License as published by |
|
6
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
|
7
|
|
|
# (at your option) any later version. |
|
8
|
|
|
# |
|
9
|
|
|
# pyfreebilling is distributed in the hope that it will be useful, |
|
10
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12
|
|
|
# GNU General Public License for more details. |
|
13
|
|
|
# |
|
14
|
|
|
# You should have received a copy of the GNU General Public License |
|
15
|
|
|
# along with pyfreebilling. If not, see <http://www.gnu.org/licenses/> |
|
16
|
|
|
|
|
17
|
|
|
# -*- coding: utf-8 -*- |
|
18
|
|
|
from datetime import timedelta |
|
19
|
|
|
from django.utils.translation import ugettext_lazy as _ |
|
20
|
|
|
import datetime |
|
21
|
|
|
|
|
22
|
|
|
from qsstats import QuerySetStats |
|
23
|
|
|
from admin_tools.dashboard import modules |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
class BaseChart(modules.DashboardModule): |
|
27
|
|
|
""" |
|
28
|
|
|
Dashboard module with user registration charts. |
|
29
|
|
|
|
|
30
|
|
|
With default values it is suited best for 2-column dashboard layouts. |
|
31
|
|
|
""" |
|
32
|
|
|
title = _('Stats chart') |
|
33
|
|
|
template = 'admin/pyfreebill/stats/chart.html' |
|
34
|
|
|
chart_size = "580x100" |
|
35
|
|
|
days = None |
|
36
|
|
|
interval = 'days' |
|
37
|
|
|
queryset = None |
|
38
|
|
|
date_field = 'date' |
|
39
|
|
|
|
|
40
|
|
|
def is_empty(self): |
|
41
|
|
|
return False |
|
42
|
|
|
|
|
43
|
|
|
def __init__(self, *args, **kwargs): |
|
44
|
|
|
super(BaseChart, self).__init__(*args, **kwargs) |
|
45
|
|
|
|
|
46
|
|
|
if self.days is None: |
|
47
|
|
|
self.days = {'days': 30, 'weeks': 30*7, 'months': 30*12}[self.interval] |
|
48
|
|
|
|
|
49
|
|
|
self.data = self.get_data(self.interval, self.days) |
|
50
|
|
|
self.prepare_template_data(self.data) |
|
51
|
|
|
|
|
52
|
|
|
def get_caption(self, dt): |
|
53
|
|
|
return { |
|
54
|
|
|
'days': dt.day, |
|
55
|
|
|
'months': dt.strftime("%b"), |
|
56
|
|
|
'weeks': dt.strftime('%W'), |
|
57
|
|
|
}[self.interval] |
|
58
|
|
|
|
|
59
|
|
|
# @cached(60*5) |
|
60
|
|
|
def get_data(self, interval, days): |
|
61
|
|
|
""" Returns an array with new users count per interval """ |
|
62
|
|
|
stats = QuerySetStats(self.queryset, self.date_field) |
|
63
|
|
|
today = datetime.date.today() |
|
64
|
|
|
begin = today - datetime.timedelta(days=days-1) |
|
65
|
|
|
return stats.time_series(begin, today, interval) |
|
66
|
|
|
|
|
67
|
|
|
def prepare_template_data(self, data): |
|
68
|
|
|
""" Prepares data for template (it is passed as module attributes) """ |
|
69
|
|
|
self.captions = [self.get_caption(t[0]) for t in data] |
|
70
|
|
|
self.values = [t[1] for t in data] |
|
71
|
|
|
self.max_value = max(self.values) |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
class BaseCharts(modules.Group): |
|
76
|
|
|
""" Group module with 3 default registration charts """ |
|
77
|
|
|
title = _('Stats') |
|
78
|
|
|
chart_model = BaseChart |
|
79
|
|
|
|
|
80
|
|
|
def __init__(self, *args, **kwargs): |
|
81
|
|
|
kwargs.setdefault('children', self.get_charts()) |
|
82
|
|
|
super(BaseCharts, self).__init__(*args, **kwargs) |
|
83
|
|
|
|
|
84
|
|
|
def get_charts(self): |
|
85
|
|
|
""" Returns 3 basic chart modules (per-day, per-week and per-month) """ |
|
86
|
|
|
return [ |
|
87
|
|
|
self.chart_model(_('By Day'), interval='days'), |
|
88
|
|
|
self.chart_model(_('By Week'), interval='weeks'), |
|
89
|
|
|
self.chart_model(_('By Month'), interval='months'), |
|
90
|
|
|
] |
|
91
|
|
|
|