-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathapi_dashboard.py
417 lines (380 loc) · 15 KB
/
api_dashboard.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import numpy as np
from django.http.response import JsonResponse
from django.views.decorators.http import require_http_methods
import sys, MySQLdb
sys.path.insert(0, '..')
from mysite import db_config
from utils import functions as f
# np.set_printoptions(precision=2, suppress=True)
@require_http_methods(['GET'])
def avg_problem_percentage(request):
"""
各公司平均问题占比
:param request:
:return:
"""
# 接口返回值列表
data = []
data_quarter = ['quarter']
data_company = []
# 获取所有年所有季度
year = f.query_data_year()
quarter = []
for y in year:
q = f.query_data_quarter(y)
quarter.extend([(y, i) for i in q])
[data_quarter.append(str(y)+'Q'+str(i)) for i in q]
data.append(data_quarter)
# 查询各公司每季度数据
try:
conn = db_config.mysql_connect()
curs = conn.cursor()
for company in ('xt', 'zc', 'db', 'jk', 'jj1', 'jj2', 'jz'):
data_company = [company]
sql = f"""select round(sum(a.problem_count)/sum(a.item_count)*100,2)
from check_result_{company} a,dim_date b
where a.RISK_MARKET_ITEM='是'
and DATE_FORMAT(a.check_date,'%Y%m%d') = b.day_id
and (b.year,b.quarter) in {tuple(quarter)}
group by b.year,b.quarter"""
curs.execute(sql)
result = curs.fetchall()
[data_company.append(str(r[0])) for r in result]
data.append(data_company)
return JsonResponse(data, safe=False)
except Exception as e:
print(e)
return JsonResponse({'msg': str(e)})
finally:
curs.close()
conn.close()
@require_http_methods(['GET'])
def same_problem_top5(request):
"""
各公司同类问题Top 5统计
:param request:
:return:
"""
year = request.GET.get('year')
quarter = request.GET.get('quarter')
month = request.GET.get('month')
day = request.GET.get('day')
try:
conn = db_config.mysql_connect()
curs = conn.cursor()
# 查询风险集市相关的相同的检核项的问题占比总和
sql = f"""select check_item,count(*) cnt,sum(problem_per) from (
select check_item,problem_per,check_date from check_result_xt where risk_market_item='是' and problem_per is not null
union
select check_item,problem_per,check_date from check_result_zc where risk_market_item='是' and problem_per is not null
union
select check_item,problem_per,check_date from check_result_db where risk_market_item='是' and problem_per is not null
union
select check_item,problem_per,check_date from check_result_jk where risk_market_item='是' and problem_per is not null
union
select check_item,problem_per,check_date from check_result_jj1 where risk_market_item='是' and problem_per is not null
union
select check_item,problem_per,check_date from check_result_jj2 where risk_market_item='是' and problem_per is not null
union
select check_item,problem_per,check_date from check_result_jz where risk_market_item='是' and problem_per is not null
) a, dim_date b
where DATE_FORMAT(a.check_date,'%Y%m%d') = b.day_id
and b.year={year}
and b.quarter={quarter}
and b.month={month}
and b.day={day}
group by check_item
having count(*)>1
order by 3 desc,2 desc"""
curs.execute(sql)
result = curs.fetchall()
check_item = []
total_problem = []
[check_item.append(r[0]) for r in result]
[total_problem.append(float(str(r[2]))) for r in result]
# 取top4问题项及占比
top4_item = check_item[0:4]
top4_problem = total_problem[0:4]
other_problem = sum(total_problem[4:])
# 合并 其他项
top4_item.append('其他')
top4_problem.append(other_problem)
data = {
'name': top4_item,
'value': top4_problem
}
return JsonResponse(data)
except Exception as e:
print(e)
return JsonResponse({'msg': str(e)})
finally:
curs.close()
conn.close()
@require_http_methods(['GET'])
def subcompany_data_percentage(request):
"""
各公司数据量占比
:param request:
:return:
"""
year = request.GET.get('year')
quarter = request.GET.get('quarter')
month = request.GET.get('month')
day = request.GET.get('day')
data = []
try:
conn = db_config.mysql_connect()
curs = conn.cursor()
for company in ('xt', 'zc', 'db', 'jk', 'jj1', 'jj2', 'jz'):
sql = f"""select sum(distinct item_count) from check_result_{company} a,
(
select max(a.check_version) check_version
from check_result_{company} a,dim_date b where DATE_FORMAT(a.check_date,'%Y%m%d') = b.day_id
and b.year={year}
and b.quarter={quarter}
and b.month={month}
and b.day={day}
) b
where a.check_version=b.check_version
and a.risk_market_item='是'"""
curs.execute(sql)
result = curs.fetchone()
if result[0] is None:
data.append({'name': company, 'value': 0})
else:
data.append({'name': company, 'value': float(str(result[0]))})
return JsonResponse(data, safe=False)
except Exception as e:
print(e)
return JsonResponse({'msg': str(e)})
finally:
curs.close()
conn.close()
def count_db_rows(request):
"""统计各类数据库数据量
"""
quarter = request.GET.get('quarter')
data = [{
"name": "MySQL",
"value": np.random.randint(1000,99999),
},
{
"name": "Oracle",
"value": np.random.randint(1000,99999)
},
{
"name": "SQL server",
"value": np.random.randint(1000,99999)
},
{
"name": "HBase",
"value": np.random.randint(1000,99999)
},
]
return JsonResponse(data, safe=False)
@require_http_methods(['GET'])
def data_overview_total(request):
"""
统计风险集市相关 总数据量、总问题数据量、总问题占比
:param request:
:return:
"""
year = request.GET.get('year')
quarter = request.GET.get('quarter')
month = request.GET.get('month')
day = request.GET.get('day')
all_cnt = 0
problem_cnt = 0
try:
conn = db_config.mysql_connect()
curs = conn.cursor()
for company in ('xt', 'zc', 'db', 'jk', 'jj1', 'jj2', 'jz'):
sql = f"""select sum(a.item_count),sum(a.problem_count) from check_result_{company} a,
(
select max(a.check_version) check_version
from check_result_{company} a,dim_date b where DATE_FORMAT(a.check_date,'%Y%m%d') = b.day_id
and b.year={year}
and b.quarter={quarter}
and b.month={month}
and b.day={day}
) b
where a.check_version=b.check_version
and a.risk_market_item='是'"""
curs.execute(sql)
result = curs.fetchone()
if result[0] is None:
continue
else:
all_cnt = all_cnt + result[0]
problem_cnt = problem_cnt + result[1]
response = {
'all_cnt': all_cnt,
'problem_cnt': problem_cnt,
'problem_per': round(problem_cnt / all_cnt * 100, 2)
}
return JsonResponse(response)
except Exception as e:
print(e)
return JsonResponse({'msg': str(e)})
finally:
curs.close()
conn.close()
@require_http_methods(['GET'])
def data_overview_company(request):
"""
统计风险集市相关 各公司 检核数据量、问题数据量、问题数据占比
:param request:
:return:
"""
year = request.GET.get('year')
quarter = request.GET.get('quarter')
month = request.GET.get('month')
day = request.GET.get('day')
data = []
try:
conn = db_config.mysql_connect()
curs = conn.cursor()
for company in ('xt', 'zc', 'db', 'jk', 'jj1', 'jj2', 'jz'):
sql = f"""select sum(a.item_count),sum(a.problem_count),round(sum(a.problem_count)/sum(a.item_count)*100,2)
from check_result_{company} a,
(
select max(a.check_version) check_version
from check_result_{company} a,dim_date b where DATE_FORMAT(a.check_date,'%Y%m%d') = b.day_id
and b.year={year}
and b.quarter={quarter}
and b.month={month}
and b.day={day}
) b
where a.check_version=b.check_version
and a.risk_market_item='是'"""
curs.execute (sql)
result = curs.fetchone()
data.append([company, result[0], result[1], result[2]])
return JsonResponse(data, safe=False)
except Exception as e:
print(e)
return JsonResponse({'msg': str(e)})
finally:
curs.close()
conn.close()
@require_http_methods(['GET'])
def data_overview_company_trend(request):
"""
统计风险集市相关 各公司 检核数据量、问题数据量、问题数据占比
:param request:
:return:
"""
year = request.GET.get('year')
month = request.GET.get('month')
day = request.GET.get('day')
company = request.GET.get('company')
try:
conn = db_config.mysql_connect()
curs = conn.cursor()
sql = f"""select round(sum(problem_count)/sum(item_count)*100,2),check_version
from check_result_{company}
where risk_market_item='是'
and check_date < date_add('{year}-{month}-{day}',interval 1 day)
group by check_version
order by check_version asc"""
curs.execute(sql)
result = curs.fetchall()
result = [r[0] for r in result]
return JsonResponse(result, safe=False)
except Exception as e:
print(e)
return JsonResponse({'msg': str(e)})
finally:
curs.close()
conn.close()
@require_http_methods(['GET'])
def total_trend(request):
"""
显示集团总问题占比走势
:param request:
:return:
"""
value = []
try:
conn = db_config.mysql_connect()
curs = conn.cursor()
sql = f"""select DATE_FORMAT(a.check_date,'%Y-%m-%d'),
round(sum(a.problem_count)/sum(a.item_count)*100,2),
count(distinct company) from
(
select 'xt' company,item_count,problem_count,check_date from check_result_xt where risk_market_item='是'
union
select 'zc' company,item_count,problem_count,check_date from check_result_zc where risk_market_item='是'
union
select 'db' company,item_count,problem_count,check_date from check_result_db where risk_market_item='是'
union
select 'jk' company,item_count,problem_count,check_date from check_result_jk where risk_market_item='是'
union
select 'jj1' company,item_count,problem_count,check_date from check_result_jj1 where risk_market_item='是'
union
select 'jj2' company,item_count,problem_count,check_date from check_result_jj2 where risk_market_item='是'
union
select 'jz' company,item_count,problem_count,check_date from check_result_jz where risk_market_item='是'
) a
group by DATE_FORMAT(a.check_date,'%Y-%m-%d')
having count(distinct company)=7
order by 1 asc"""
curs.execute(sql)
result = curs.fetchall()
return JsonResponse({'datatime': [r[0] for r in result], 'value': [r[1] for r in result]}, safe=False)
except Exception as e:
print(e)
return JsonResponse({'msg': str(e)})
finally:
curs.close()
conn.close()
@require_http_methods(['GET'])
def subcompany_problem_count(request):
"""
子公司仪表盘-问题数据项统计
:param request:
:return:
"""
year = request.GET.get('year')
quarter = request.GET.get('quarter')
month = request.GET.get('month')
day = request.GET.get('day')
company = request.GET.get('company')
try:
conn = db_config.mysql_connect()
curs = conn.cursor()
# 问题占比 | 问题数据总量 | 问题数据项
sql = f"""select sum(a.item_count),sum(a.problem_count),a.check_item from (
select a.check_item,a.item_count,a.problem_count
from check_result_{company} a,
(
select max(a.check_version) check_version
from check_result_{company} a,dim_date b where DATE_FORMAT(a.check_date,'%Y%m%d') = b.day_id
and b.year={year}
and b.quarter={quarter}
and b.month={month}
and b.day={day}
) b
where a.problem_count is not null
and a.problem_count !=0
and a.risk_market_item='是'
and a.check_version=b.check_version
) a
group by a.check_item
order by 2 desc"""
curs.execute(sql)
result = curs.fetchall()
result_list = [['问题占比', '问题数据总量', '问题数据项'], ]
for i in result:
problem_per = (int(i[1]) / int(i[0]))
problem_per = round(problem_per * 100, 2)
problem_total = int(i[1])
item = i[2]
result_list.append([problem_per, problem_total, item])
return JsonResponse(result_list, safe=False)
except Exception as e:
print(e)
return JsonResponse({'msg': str(e)})
finally:
curs.close()
conn.close()