Subversion Repositories basico

Rev

Rev 4 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# File: stats.py
4
# Author: Tomás Vírseda
5
# License: GPL v3
6
# Description: Stats service
7
 
8
import pygal
9
from pygal import Config
10
 
11
from .service import Service
12
 
13
 
14
class Stats(Service):
15
    def initialize(self):
16
        self.get_services()
17
 
18
    def get_html(self, filename):
19
        fin = open(filename, 'r')
20
        html = fin.read()
21
        fin.close()
22
 
23
        return html
24
 
25
 
26
    def get_services(self):
27
        self.sap = self.app.get_service('SAP')
28
 
29
    def build_pie_maincomp(self):
74 t00mlabs 30
        db = self.get_service('DB')
31
        stats  = db.get_stats()
4 t00mlabs 32
        CHART_FILE = self.app.get_var('TMP', 'local') + 'chart.svg'
33
        config = Config()
34
        config.show_legend = True
74 t00mlabs 35
        config.legend_at_bottom = True
4 t00mlabs 36
        config.print_values = True
37
        config.print_values_position = 'top'
38
        config.print_labels = True
39
        #~ config.dynamic_print_values = True
40
        config.human_readable = True
41
        config.fill = True
42
        #~ chart = pygal.XY(config)
43
        pie_chart = pygal.HorizontalBar(config)
44
        pie_chart.title = 'Main components used'
45
        for key in stats['maincomp']:
46
            value = stats['maincomp'][key]
47
            label = "%s (%d)" % (key, value)
48
            pie_chart.add(label, value)
49
        pie_chart.render_to_file(CHART_FILE)
50
 
51
        return self.get_html(CHART_FILE)
52
 
53
 
54
    def build_pie_categories(self):
74 t00mlabs 55
        db = self.get_service('DB')
56
        stats  = db.get_stats()
4 t00mlabs 57
        CHART_FILE = self.app.get_var('TMP', 'local') + 'chart.svg'
58
        config = Config()
59
        config.show_legend = True
74 t00mlabs 60
        config.legend_at_bottom = True
4 t00mlabs 61
        config.print_values = True
62
        config.print_values_position = 'top'
63
        config.print_labels = True
74 t00mlabs 64
        #~ config.rounded_bars=20
4 t00mlabs 65
        #~ config.dynamic_print_values = True
66
        config.human_readable = True
67
        config.fill = True
68
        #~ chart = pygal.XY(config)
69
        pie_chart = pygal.HorizontalBar(config)
70
        pie_chart.title = 'By categories'
71
        for key in stats['cats']:
72
            value = stats['cats'][key]
73
            label = "%s (%d)" % (key, value)
74
            pie_chart.add(label, value)
75
        pie_chart.render_to_file(CHART_FILE)
76
 
77
        return self.get_html(CHART_FILE)