dashboard.py
4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# import json
from pyramid.view import (
view_config,
)
# from pyramid.httpexceptions import (
# HTTPFound,
# )
from ..views import BaseView
from ..models import EisDBSession
from ..models import Eis, Chart, ChartItem, Slide
#from ..tools import row2dict
class dashboard(BaseView):
def cek_value(self,value,devider,simbol):
if value<devider:
return "{0:,.0f}".format(value)
else:
return "{0:,.2f} {1}".format(value/devider,simbol)
@view_config(route_name='eis-dashboard', renderer='templates/dashboard.pt')
def view_app(self):
tahun = self.ses['tahun']
datas = EisDBSession.query(Slide).filter(Slide.status==1).order_by(Slide.order_id)
if not datas:
datas = {}
return dict(project='EIS', datas=datas)
@view_config(route_name='eis-dashboard-act', renderer='json')
def view_act(self):
tahun = self.ses['tahun']
req = self.req
params = req.params
id = 'id' in params and params['id'] or 0
json_data = {}
json_data['success']=False
if req.matchdict['act']=='grid':
rows = EisDBSession.query(Eis).filter(Eis.id==id)
if not rows:
return json_data
for row in rows:
row_dicted = row.to_dict()
amt_hari = float(row_dicted['amt_hari'])
amt_minggu = float(row_dicted['amt_minggu']) #+amt_hari
amt_bulan = float(row_dicted['amt_bulan']) #+amt_hari
amt_tahun = float(row_dicted['amt_tahun']) #+amt_bulan
json_data['success']= True
json_data['tahun'] = self.cek_value(amt_tahun,1000000000, 'M')
json_data['bulan'] = self.cek_value(amt_bulan,1000000000, 'M')
json_data['minggu'] = self.cek_value(amt_minggu,1000000000, 'M')
json_data['hari'] = self.cek_value(amt_hari,1000000000, 'M')
return json_data
#######################################################################
# GRAFIK LINE/BAR
#######################################################################
elif req.matchdict['act']=='linebar':
rows = EisDBSession.query(Chart).filter(Chart.id==id).first()
if not rows:
return json_data
json_data['label'] = rows.label.split(',')
rows = EisDBSession.query(ChartItem).filter(ChartItem.chart_id==id).\
order_by(ChartItem.id)
for row in rows:
json_data[row.source_type] = [row.value_1/row.chart.devider,row.value_2/row.chart.devider,row.value_3/row.chart.devider,
row.value_4/row.chart.devider,row.value_5/row.chart.devider,row.value_6/row.chart.devider,
row.value_7/row.chart.devider,row.value_8/row.chart.devider,row.value_9/row.chart.devider,
row.value10/row.chart.devider,row.value11/row.chart.devider,row.value12/row.chart.devider,]
json_data['success']= True
return json_data
#######################################################################
# GRAFIK LINGKARAN
#######################################################################
elif req.matchdict['act']=='pie':
rows = EisDBSession.query(Chart).filter(Chart.id==id).first()
if not rows:
return json_data
json_data['label'] = rows.label.split(',')
rows = EisDBSession.query(ChartItem).filter(ChartItem.chart_id==id).\
order_by(ChartItem.id)
json_data['rows'] = {}
for row in rows:
anama = {}
anama['nama'] = row.nama
anama['color'] = row.color
anama ['highlight'] = row.highlight
anama ['value'] = row.value_1/row.chart.devider
json_data['rows'][row.nama] =anama
json_data['success']= True
return json_data