boxstate.py
5.0 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
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
#!/usr/bin/env python
# encoding: utf-8
'''
@author: dongjie
@license: (C) Copyright 2013-2020, Node Supply Chain Manager Corporation Limited.
@contact: 503479457@qq.com
@software: garner
@file: boxstate.py
@time: 2020/9/17 13:45
@desc:
'''
from flask import jsonify,render_template,request,send_from_directory
from app.api import bp
from config import Config
from pyecharts.charts import Bar,Line,Grid, Liquid,Pie
from pyecharts.options.global_options import ThemeType
from pyecharts.faker import Faker
import pyecharts.options as opts
from pyecharts.commons.utils import JsCode
import urllib,json,operator,re
import os
import pymongo
import pandas as pd
def open_url(url):
resp = urllib.request.urlopen(url)
ele_json = json.loads(resp.read())
return ele_json
def format_data():
url = Config.BOX_RATIO_URL
ele_json = open_url(url)
ratio_list = []
for info in ele_json:
current_type_list = []
usecount = 0
totalcount = 0
for nummap in info['usageMap'].values():
use_list = []
# if int(nummap['usedCount'])
use_list.append(nummap['sizeStr'])
use_list.append(int(nummap['usedCount']))
usecount += int(nummap['usedCount'])
totalcount += int(nummap['totalCount'])
current_type_list.append(use_list)
current_ratio = usecount / totalcount
current_ratio = round(float(current_ratio),4)
# print (current_ratio)
# current_ratio = float(('%.2f' % current_ratio))
number = int(re.findall(r"\d+", info['cid'])[0])
# name = info['cid']
ratio_dict = {
'name':info['name'],
'type_map':current_type_list,
'ratio': current_ratio,
'box_num':totalcount,
'number': number
}
ratio_list.append(ratio_dict)
ratio_list = sorted(ratio_list, key=operator.itemgetter('number'))[0:24]
# print (ratio_list)
return ratio_list
@bp.route('/boxstate')
def boxstate():
data = format_data()
# print (type(data[0]['ratio']))
# print (data[0]['ratio'])
grid_list = []
for current in data:
# print (current['ratio'])
l = (
Liquid()
.add("lq", [current['ratio']], center=["75%", "50%"],is_outline_show=False,label_opts=opts.LabelOpts(formatter=JsCode(
"""function (param) {
return (Math.floor(param.value * 10000) / 100) + '%';
}"""
),position="inside",font_size=50))
.set_global_opts(title_opts=opts.TitleOpts(title='总容量' + str(current['box_num']),title_textstyle_opts=opts.TextStyleOpts(color="yellow"),))
.set_series_opts(type='liquidFill',radius='80%'))
c = (
Pie()
.add("", current['type_map'],center=["30%", "50%"])
# .set_colors(["blue", "green", "yellow", "red", "pink", "orange", "purple"])
.set_global_opts(legend_opts=opts.LegendOpts(is_show=False))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}",color="rgba(255, 255, 255, 1)"))
)
grid = Grid().add(l, grid_opts=opts.GridOpts()).add(c, grid_opts=opts.GridOpts())
grid_list.append(grid.dump_options())
return render_template("boxstate.html",grid=grid_list)
# 连接数据库
def conn_mongo(table,path):
mongo_client = pymongo.MongoClient(host='10.85.162.124', port=27017)
db = mongo_client['qisda']
db.authenticate("admin", "123")
if table == 'alarmagv':
cach = db.alarmagv
elif table == 'ng':
cach = db.collectNg
else:
cach = db.stockqty
data=cach.find()
mongo_client.close()
df = pd.DataFrame(data)
df = df.drop('_id', 1)
df.to_csv(path,encoding="utf_8_sig")
# 下载agv错误信息
@bp.route('/downloadagv/', methods=['GET'], strict_slashes=False)
def downloadagv():
# basepath = os.path.dirname(__file__)
c_path = "D:/qisda_charts/app/static/data/"
file_path = os.path.join(c_path, 'agverror.csv')
table = 'alarmagv'
conn_mongo(table,file_path)
if request.method == "GET":
if os.path.isfile(os.path.join(c_path, 'agverror.csv')):
return send_from_directory(c_path, 'agverror.csv', as_attachment=True)
@bp.route('/downloadstock/', methods=['GET'], strict_slashes=False)
def downloadstock():
c_path = "D:/qisda_charts/app/static/data/"
file_path = os.path.join(c_path, 'stock.csv')
table = 'stock'
conn_mongo(table, file_path)
if request.method == "GET":
if os.path.isfile(os.path.join(c_path, 'stock.csv')):
return send_from_directory(c_path, 'stock.csv', as_attachment=True)
@bp.route('/downloadng/', methods=['GET'], strict_slashes=False)
def downloadng():
c_path = "D:/qisda_charts/app/static/data/"
file_path = os.path.join(c_path, 'collectNg.csv')
table = 'ng'
conn_mongo(table, file_path)
if request.method == "GET":
if os.path.isfile(os.path.join(c_path, 'collectNg.csv')):
return send_from_directory(c_path, 'collectNg.csv', as_attachment=True)