优化运行信息接口、优化次数接口
This commit is contained in:
@@ -173,49 +173,6 @@ class Sf_Dashboard_Connect(http.Controller):
|
||||
res['message'] = '前端请求机床数据失败,原因:%s' % e
|
||||
return json.JSONEncoder().encode(res)
|
||||
|
||||
# @http.route('/api/logs/list', type='http', auth='public', methods=['GET', 'POST'], csrf=False,
|
||||
# cors="*")
|
||||
# def logs_list(self, **kw):
|
||||
# """
|
||||
# 拿到日志数据返回给大屏展示
|
||||
# :param kw:
|
||||
# :return:
|
||||
# """
|
||||
# res = {'status': 1, 'message': '成功', 'data': []}
|
||||
# logging.info('前端请求日志数据的参数为:%s' % kw)
|
||||
#
|
||||
# try:
|
||||
# # 获取请求的日志数据
|
||||
# logs_obj = request.env['maintenance.equipment.oee.log.detail'].sudo()
|
||||
# # 获取请求的机床数据
|
||||
# machine_list = ast.literal_eval(kw['machine_list'])
|
||||
# begin_time_str = kw['begin_time'].strip('"')
|
||||
# end_time_str = kw['end_time'].strip('"')
|
||||
#
|
||||
# begin_time = datetime.strptime(begin_time_str, '%Y-%m-%d %H:%M:%S')
|
||||
# end_time = datetime.strptime(end_time_str, '%Y-%m-%d %H:%M:%S')
|
||||
#
|
||||
# print('begin_time: %s' % begin_time)
|
||||
# for item in machine_list:
|
||||
# log_datas = logs_obj.search(
|
||||
# [('equipment_code', '=', item), ('time', '>=', begin_time), ('time', '<=', end_time)])
|
||||
# print('log_datas: %s' % log_datas)
|
||||
# for log_data in log_datas:
|
||||
# res['data'].append({
|
||||
# 'equipment_code': log_data.equipment_code,
|
||||
# 'time': log_data.time.strftime('%Y-%m-%d %H:%M:%S'),
|
||||
# 'state': log_data.state
|
||||
#
|
||||
# })
|
||||
#
|
||||
# return json.JSONEncoder().encode(res)
|
||||
#
|
||||
# except Exception as e:
|
||||
# logging.info('前端请求日志数据失败,原因:%s' % e)
|
||||
# res['status'] = -1
|
||||
# res['message'] = '前端请求日志数据失败,原因:%s' % e
|
||||
# return json.JSONEncoder().encode(res)
|
||||
|
||||
@http.route('/api/logs/list', type='http', auth='public', methods=['GET', 'POST'], csrf=False, cors="*")
|
||||
def logs_list(self, **kw):
|
||||
"""
|
||||
@@ -227,9 +184,9 @@ class Sf_Dashboard_Connect(http.Controller):
|
||||
logging.info('前端请求日志数据的参数为:%s' % kw)
|
||||
|
||||
try:
|
||||
# 获取请求的日志数据
|
||||
logs_obj = request.env['maintenance.equipment.oee.log.detail'].sudo()
|
||||
# 获取请求的机床数据
|
||||
# 连接数据库
|
||||
conn = psycopg2.connect(**db_config)
|
||||
cur = conn.cursor()
|
||||
machine_list = ast.literal_eval(kw['machine_list'])
|
||||
begin_time_str = kw['begin_time'].strip('"')
|
||||
end_time_str = kw['end_time'].strip('"')
|
||||
@@ -240,19 +197,25 @@ class Sf_Dashboard_Connect(http.Controller):
|
||||
print('begin_time: %s' % begin_time)
|
||||
|
||||
for item in machine_list:
|
||||
log_datas = logs_obj.search(
|
||||
[('equipment_code', '=', item), ('time', '>=', begin_time), ('time', '<=', end_time)])
|
||||
print('log_datas: %s' % log_datas)
|
||||
sql = '''
|
||||
SELECT time, device_state, program_name
|
||||
FROM device_data
|
||||
WHERE device_name = %s AND time >= %s AND time <= %s
|
||||
ORDER BY time ASC;
|
||||
'''
|
||||
# 执行SQL命令,使用参数绑定
|
||||
cur.execute(sql, (item, begin_time, end_time))
|
||||
results = cur.fetchall()
|
||||
|
||||
# 将数据按照 equipment_code 进行分组
|
||||
if item not in res['data']:
|
||||
res['data'][item] = []
|
||||
|
||||
for log_data in log_datas:
|
||||
for result in results:
|
||||
res['data'][item].append({
|
||||
'time': log_data.time.strftime('%Y-%m-%d %H:%M:%S'),
|
||||
'state': log_data.state,
|
||||
'production_name': log_data.production_name,
|
||||
'time': result[0].strftime('%Y-%m-%d %H:%M:%S'),
|
||||
'state': result[1],
|
||||
'production_name': result[2],
|
||||
})
|
||||
|
||||
return json.dumps(res) # 注意使用 json.dumps 而不是直接用 json.JSONEncoder().encode()
|
||||
@@ -624,7 +587,7 @@ class Sf_Dashboard_Connect(http.Controller):
|
||||
|
||||
# 查询pg库来获得待机次数
|
||||
@http.route('/api/IdleAlarmCount', type='http', auth='public', methods=['GET', 'POST'], csrf=False, cors="*")
|
||||
def idle_count(self, **kw):
|
||||
def idle_alarm_count(self, **kw):
|
||||
"""
|
||||
查询设备的待机次数
|
||||
"""
|
||||
@@ -637,69 +600,50 @@ class Sf_Dashboard_Connect(http.Controller):
|
||||
try:
|
||||
# 获取请求的机床数据
|
||||
machine_list = ast.literal_eval(kw['machine_list'])
|
||||
idle_times = []
|
||||
idle_dict = {}
|
||||
|
||||
total_alarm_time = 0
|
||||
alarm_count_num = 0
|
||||
for item in machine_list:
|
||||
sql = '''
|
||||
SELECT idle_start_time,alarm_time,alarm_repair_time FROM device_data WHERE device_name = %s;
|
||||
SELECT COUNT(*)
|
||||
FROM (
|
||||
SELECT DISTINCT ON (idle_start_time) idle_start_time
|
||||
FROM device_data
|
||||
WHERE device_name = %s AND idle_start_time IS NOT NULL
|
||||
ORDER BY idle_start_time, time
|
||||
) subquery;
|
||||
'''
|
||||
|
||||
sql2 = '''
|
||||
SELECT DISTINCT ON (alarm_time) alarm_time, alarm_repair_time
|
||||
FROM device_data
|
||||
WHERE device_name = %s AND alarm_time IS NOT NULL
|
||||
ORDER BY alarm_time, time;
|
||||
|
||||
'''
|
||||
# 执行SQL命令
|
||||
cur.execute(sql, (item,))
|
||||
result = cur.fetchall()
|
||||
# # print('result', result)
|
||||
#
|
||||
# # 将查询结果添加到idle_times列表中
|
||||
# idle_times = [row[0] for row in result if row[0] is not None]
|
||||
#
|
||||
# # 对结果去重
|
||||
# unique_idle_times = set(idle_times)
|
||||
# # print('unique_idle_times', unique_idle_times)
|
||||
#
|
||||
# # 统计去重后的数量
|
||||
# idle_count = len(unique_idle_times)
|
||||
# # idle_dict[item] = idle_count
|
||||
#
|
||||
# res['data'][item] = idle_count
|
||||
|
||||
total_alarm_time = 0
|
||||
alarm_count = 0
|
||||
alarm_time_list = []
|
||||
idle_times = []
|
||||
alarm_times = []
|
||||
print('result========', result)
|
||||
|
||||
cur.execute(sql2, (item,))
|
||||
result2 = cur.fetchall()
|
||||
print('result2========', result2)
|
||||
#
|
||||
for row in result:
|
||||
idle_start_time = row[0]
|
||||
alarm_time = row[1]
|
||||
alarm_repair_time = row[2]
|
||||
|
||||
alarm_time_list.append(alarm_time) # 将时长累加,以秒为单位
|
||||
idle_times.append(idle_start_time)
|
||||
# if alarm_repair_time is not None:
|
||||
# alarm_times.append(alarm_repair_time)
|
||||
alarm_times.append(alarm_repair_time)
|
||||
|
||||
# 对结果去重
|
||||
unique_total_alarm_time = set(alarm_time_list)
|
||||
unique_idle_times = set(idle_times)
|
||||
unique_alarm_times = set(alarm_times)
|
||||
|
||||
# 统计去重后的数量
|
||||
idle_count = len(unique_idle_times)
|
||||
|
||||
for alarm_time in unique_total_alarm_time:
|
||||
if alarm_time is not None:
|
||||
total_alarm_time += abs(float(alarm_time))
|
||||
|
||||
alarm_count = len(unique_alarm_times) if unique_alarm_times else 0
|
||||
alarm_count = alarm_count if total_alarm_time else 0
|
||||
|
||||
# 存储待机次数和总待机时长(单位:秒)
|
||||
res['data'][item] = {
|
||||
'idle_count': idle_count,
|
||||
'total_alarm_time': total_alarm_time / 3600, # 以秒为单位
|
||||
'alarm_count': alarm_count
|
||||
}
|
||||
res['data'][item] = {'idle_count': row[0]}
|
||||
alarm_count = []
|
||||
for row in result2:
|
||||
alarm_count.append(row[0])
|
||||
total_alarm_time += abs(float(row[0]))
|
||||
if len(list(set(alarm_count))) == 1:
|
||||
if list(set(alarm_count))[0] is None:
|
||||
alarm_count_num = 0
|
||||
else:
|
||||
alarm_count_num = 1
|
||||
else:
|
||||
alarm_count_num = len(list(set(alarm_count)))
|
||||
res['data'][item]['total_alarm_time'] = total_alarm_time / 3600
|
||||
res['data'][item]['alarm_count_num'] = alarm_count_num
|
||||
|
||||
# 返回统计结果
|
||||
return json.dumps(res)
|
||||
@@ -712,7 +656,7 @@ class Sf_Dashboard_Connect(http.Controller):
|
||||
|
||||
# 查询pg库来获得异常情况
|
||||
@http.route('/api/alarm/logs', type='http', auth='public', methods=['GET', 'POST'], csrf=False, cors="*")
|
||||
def idle_count(self, **kw):
|
||||
def alarm_logs(self, **kw):
|
||||
"""
|
||||
查询设备的异常情况
|
||||
"""
|
||||
@@ -871,4 +815,3 @@ class Sf_Dashboard_Connect(http.Controller):
|
||||
# 返回数据
|
||||
res['data'] = oee_data
|
||||
return json.dumps(res)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user