Accept Merge Request #2014: (feature/制造功能优化 -> develop)

Merge Request: 调整设备可用率接口

Created By: @马广威
Accepted By: @马广威
URL: https://jikimo-hn.coding.net/p/jikimo_sfs/d/jikimo_sf/git/merge/2014?initial=true
This commit is contained in:
马广威
2025-04-18 14:40:31 +08:00
committed by Coding
2 changed files with 439 additions and 272 deletions

View File

@@ -6,7 +6,7 @@
<field name="arch" type="xml">
<form>
<group>
<field name="file_data" widget="binary" options="{'accepted_file_extensions': '.xlsx'}"/>
<field name="file_data" widget="binary" options="{'accepted_file_extensions': '.xls,.xlsx'}"/>
</group>
<footer>
<button string="确认导入" name="import_data" type="object" class="btn-primary"/>

View File

@@ -1122,169 +1122,169 @@ class Sf_Dashboard_Connect(http.Controller):
return request.not_found()
# 设备运行率
@http.route('/api/RunningTime', type='http', auth='public', methods=['GET', 'POST'], csrf=False, cors="*")
def RunningTime(self, **kw):
"""
获取设备运行时长
"""
res = {'status': 1, 'message': '成功', 'data': {}}
# 连接数据库
conn = psycopg2.connect(**db_config)
# 获取请求的机床数据
machine_list = ast.literal_eval(kw['machine_list'])
def fetch_result_as_dict(cursor):
"""辅助函数:将查询结果转为字典"""
columns = [desc[0] for desc in cursor.description]
return dict(zip(columns, cursor.fetchone())) if cursor.rowcount != 0 else None
# 初始化当天、当月和有记录以来的总时长
day_total_running_time = 0
day_total_process_time = 0
day_work_rate = 0
month_total_running_time = 0
month_total_process_time = 0
month_work_rate = 0
all_time_total_running_time = 0
all_time_total_process_time = 0
all_time_work_rate = 0
for item in machine_list:
# 获取当天第一条记录排除device_state等于离线的记录
with conn.cursor() as cur:
cur.execute("""
SELECT * FROM device_data
WHERE device_name = %s
AND time >= CURRENT_DATE -- 今日 00:00:00
AND time < CURRENT_DATE + 1 -- 明日 00:00:00
AND device_state in ('待机', '警告', '运行中')
ORDER BY time ASC
LIMIT 1;
""", (item,))
first_today = fetch_result_as_dict(cur)
# print("当天第一条记录(非离线):", first_today)
# 获取当天最新一条记录排除device_state等于离线的记录
with conn.cursor() as cur:
cur.execute("""
SELECT * FROM device_data
WHERE device_name = %s
AND time >= CURRENT_DATE -- 今日 00:00:00
AND time < CURRENT_DATE + 1 -- 明日 00:00:00
AND device_state in ('待机', '警告', '运行中')
ORDER BY time DESC
LIMIT 1;
""", (item,))
last_today = fetch_result_as_dict(cur)
# print("当天最新一条记录(非离线):", last_today)
# 计算当天运行时长
if first_today and last_today:
running_time = convert_to_seconds(last_today['run_time']) - convert_to_seconds(first_today['run_time'])
process_time = convert_to_seconds(last_today['process_time']) - convert_to_seconds(
first_today['process_time'])
day_total_running_time += abs(running_time)
day_total_process_time += abs(process_time)
# 获取当月第一条记录排除device_state等于离线的记录
with conn.cursor() as cur:
cur.execute("""
SELECT * FROM device_data
WHERE device_name = %s
AND time >= DATE_TRUNC('MONTH', CURRENT_DATE)
AND time < DATE_TRUNC('MONTH', CURRENT_DATE) + INTERVAL '1 MONTH'
AND device_state in ('待机', '警告', '运行中')
ORDER BY time ASC
LIMIT 1;
""", (item,))
first_month = fetch_result_as_dict(cur)
# print("当月第一条记录:", first_month)
# 获取当月最新一条记录
with conn.cursor() as cur:
cur.execute("""
SELECT * FROM device_data
WHERE device_name = %s
AND time >= DATE_TRUNC('MONTH', CURRENT_DATE)
AND time < DATE_TRUNC('MONTH', CURRENT_DATE) + INTERVAL '1 MONTH'
AND device_state in ('待机', '警告', '运行中')
ORDER BY time DESC
LIMIT 1;
""", (item,))
last_month = fetch_result_as_dict(cur)
# print("当月最新一条记录(非离线):", last_month)
# 计算当月运行时长
if first_month and last_month:
month_running_time = convert_to_seconds(last_month['run_time']) - convert_to_seconds(
first_month['run_time'])
month_process_time = convert_to_seconds(last_month['process_time']) - convert_to_seconds(
first_month['process_time'])
month_total_running_time += abs(month_running_time)
month_total_process_time += abs(month_process_time)
# 获取有记录以来的第一条记录排除device_state等于离线的记录
with conn.cursor() as cur:
cur.execute("""
SELECT * FROM device_data
WHERE device_name = %s
AND device_state in ('待机', '警告', '运行中')
ORDER BY time ASC
LIMIT 1;
""", (item,))
first_all_time = fetch_result_as_dict(cur)
# print("有记录以来的第一条记录(非离线):", first_all_time)
# 获取有记录以来的最新一条记录排除device_state等于离线的记录
with conn.cursor() as cur:
cur.execute("""
SELECT * FROM device_data
WHERE device_name = %s
AND device_state in ('待机', '警告', '运行中')
ORDER BY time DESC
LIMIT 1;
""", (item,))
last_all_time = fetch_result_as_dict(cur)
# print("有记录以来的最新一条记录(非离线):", last_all_time)
# 计算有记录以来的运行时长
if first_all_time and last_all_time:
all_time_running_time = convert_to_seconds(last_all_time['run_time']) - convert_to_seconds(
first_all_time['run_time'])
all_time_process_time = convert_to_seconds(last_all_time['process_time']) - convert_to_seconds(
first_all_time['process_time'])
all_time_total_running_time += abs(all_time_running_time)
all_time_total_process_time += abs(all_time_process_time)
# 计算当天工作效率
if day_total_running_time > day_total_process_time:
day_work_rate = day_total_process_time / day_total_running_time if day_total_running_time != 0 else 0
else:
day_work_rate = day_total_running_time / day_total_process_time if day_total_process_time != 0 else 0
print("当天工作效率: %s" % day_work_rate)
# 计算当月工作效率
if month_total_running_time > month_total_process_time:
month_work_rate = month_total_process_time / month_total_running_time if month_total_running_time != 0 else 0
else:
month_work_rate = month_total_running_time / month_total_process_time if month_total_process_time != 0 else 0
print("当月工作效率: %s" % month_work_rate)
# 计算有记录以来的工作效率
if all_time_total_running_time > all_time_total_process_time:
all_time_work_rate = all_time_total_process_time / all_time_total_running_time if all_time_total_running_time != 0 else 0
else:
all_time_work_rate = all_time_total_running_time / all_time_total_process_time if all_time_total_process_time != 0 else 0
print("有记录以来的工作效率: %s" % all_time_work_rate)
conn.close()
# 返回数据
res['data']['day_work_rate'] = day_work_rate
res['data']['month_work_rate'] = month_work_rate
res['data']['all_time_work_rate'] = all_time_work_rate
return json.dumps(res)
# @http.route('/api/RunningTime', type='http', auth='public', methods=['GET', 'POST'], csrf=False, cors="*")
# def RunningTime(self, **kw):
# """
# 获取设备运行时长
# """
# res = {'status': 1, 'message': '成功', 'data': {}}
# # 连接数据库
# conn = psycopg2.connect(**db_config)
# # 获取请求的机床数据
# machine_list = ast.literal_eval(kw['machine_list'])
#
# def fetch_result_as_dict(cursor):
# """辅助函数:将查询结果转为字典"""
# columns = [desc[0] for desc in cursor.description]
# return dict(zip(columns, cursor.fetchone())) if cursor.rowcount != 0 else None
#
# # 初始化当天、当月和有记录以来的总时长
# day_total_running_time = 0
# day_total_process_time = 0
# day_work_rate = 0
# month_total_running_time = 0
# month_total_process_time = 0
# month_work_rate = 0
# all_time_total_running_time = 0
# all_time_total_process_time = 0
# all_time_work_rate = 0
#
# for item in machine_list:
# # 获取当天第一条记录排除device_state等于离线的记录
# with conn.cursor() as cur:
# cur.execute("""
# SELECT * FROM device_data
# WHERE device_name = %s
# AND time >= CURRENT_DATE -- 今日 00:00:00
# AND time < CURRENT_DATE + 1 -- 明日 00:00:00
# AND device_state in ('待机', '警告', '运行中')
# ORDER BY time ASC
# LIMIT 1;
# """, (item,))
# first_today = fetch_result_as_dict(cur)
# # print("当天第一条记录(非离线):", first_today)
#
# # 获取当天最新一条记录排除device_state等于离线的记录
# with conn.cursor() as cur:
# cur.execute("""
# SELECT * FROM device_data
# WHERE device_name = %s
# AND time >= CURRENT_DATE -- 今日 00:00:00
# AND time < CURRENT_DATE + 1 -- 明日 00:00:00
# AND device_state in ('待机', '警告', '运行中')
# ORDER BY time DESC
# LIMIT 1;
# """, (item,))
# last_today = fetch_result_as_dict(cur)
# # print("当天最新一条记录(非离线):", last_today)
#
# # 计算当天运行时长
# if first_today and last_today:
# running_time = convert_to_seconds(last_today['run_time']) - convert_to_seconds(first_today['run_time'])
# process_time = convert_to_seconds(last_today['process_time']) - convert_to_seconds(
# first_today['process_time'])
# day_total_running_time += abs(running_time)
# day_total_process_time += abs(process_time)
#
# # 获取当月第一条记录排除device_state等于离线的记录
# with conn.cursor() as cur:
# cur.execute("""
# SELECT * FROM device_data
# WHERE device_name = %s
# AND time >= DATE_TRUNC('MONTH', CURRENT_DATE)
# AND time < DATE_TRUNC('MONTH', CURRENT_DATE) + INTERVAL '1 MONTH'
# AND device_state in ('待机', '警告', '运行中')
# ORDER BY time ASC
# LIMIT 1;
# """, (item,))
# first_month = fetch_result_as_dict(cur)
# # print("当月第一条记录:", first_month)
#
# # 获取当月最新一条记录
# with conn.cursor() as cur:
# cur.execute("""
# SELECT * FROM device_data
# WHERE device_name = %s
# AND time >= DATE_TRUNC('MONTH', CURRENT_DATE)
# AND time < DATE_TRUNC('MONTH', CURRENT_DATE) + INTERVAL '1 MONTH'
# AND device_state in ('待机', '警告', '运行中')
# ORDER BY time DESC
# LIMIT 1;
# """, (item,))
# last_month = fetch_result_as_dict(cur)
# # print("当月最新一条记录(非离线):", last_month)
#
# # 计算当月运行时长
# if first_month and last_month:
# month_running_time = convert_to_seconds(last_month['run_time']) - convert_to_seconds(
# first_month['run_time'])
# month_process_time = convert_to_seconds(last_month['process_time']) - convert_to_seconds(
# first_month['process_time'])
# month_total_running_time += abs(month_running_time)
# month_total_process_time += abs(month_process_time)
#
# # 获取有记录以来的第一条记录排除device_state等于离线的记录
# with conn.cursor() as cur:
# cur.execute("""
# SELECT * FROM device_data
# WHERE device_name = %s
# AND device_state in ('待机', '警告', '运行中')
# ORDER BY time ASC
# LIMIT 1;
# """, (item,))
# first_all_time = fetch_result_as_dict(cur)
# # print("有记录以来的第一条记录(非离线):", first_all_time)
#
# # 获取有记录以来的最新一条记录排除device_state等于离线的记录
# with conn.cursor() as cur:
# cur.execute("""
# SELECT * FROM device_data
# WHERE device_name = %s
# AND device_state in ('待机', '警告', '运行中')
# ORDER BY time DESC
# LIMIT 1;
# """, (item,))
# last_all_time = fetch_result_as_dict(cur)
# # print("有记录以来的最新一条记录(非离线):", last_all_time)
#
# # 计算有记录以来的运行时长
# if first_all_time and last_all_time:
# all_time_running_time = convert_to_seconds(last_all_time['run_time']) - convert_to_seconds(
# first_all_time['run_time'])
# all_time_process_time = convert_to_seconds(last_all_time['process_time']) - convert_to_seconds(
# first_all_time['process_time'])
# all_time_total_running_time += abs(all_time_running_time)
# all_time_total_process_time += abs(all_time_process_time)
#
# # 计算当天工作效率
# if day_total_running_time > day_total_process_time:
# day_work_rate = day_total_process_time / day_total_running_time if day_total_running_time != 0 else 0
# else:
# day_work_rate = day_total_running_time / day_total_process_time if day_total_process_time != 0 else 0
# print("当天工作效率: %s" % day_work_rate)
#
# # 计算当月工作效率
# if month_total_running_time > month_total_process_time:
# month_work_rate = month_total_process_time / month_total_running_time if month_total_running_time != 0 else 0
# else:
# month_work_rate = month_total_running_time / month_total_process_time if month_total_process_time != 0 else 0
# print("当月工作效率: %s" % month_work_rate)
#
# # 计算有记录以来的工作效率
# if all_time_total_running_time > all_time_total_process_time:
# all_time_work_rate = all_time_total_process_time / all_time_total_running_time if all_time_total_running_time != 0 else 0
# else:
# all_time_work_rate = all_time_total_running_time / all_time_total_process_time if all_time_total_process_time != 0 else 0
# print("有记录以来的工作效率: %s" % all_time_work_rate)
#
# conn.close()
#
# # 返回数据
# res['data']['day_work_rate'] = day_work_rate
# res['data']['month_work_rate'] = month_work_rate
# res['data']['all_time_work_rate'] = all_time_work_rate
#
# return json.dumps(res)
# 设备运行时长
@http.route('/api/RunningTimeDetail', type='http', auth='public', methods=['GET', 'POST'], csrf=False, cors="*")
@@ -1411,7 +1411,218 @@ class Sf_Dashboard_Connect(http.Controller):
return json.dumps(res)
@http.route('/api/utilization/rate', type='http', auth='public', methods=['GET', 'POST'], csrf=False, cors="*")
# @http.route('/api/utilization/rate', type='http', auth='public', methods=['GET', 'POST'], csrf=False, cors="*")
# def UtilizationRate(self, **kw):
# """
# 获取稼动率
# """
# logging.info("kw=:%s" % kw)
# res = {'status': 1, 'message': '成功', 'data': {}}
# # 获取请求的机床数据
# machine_list = ast.literal_eval(kw['machine_list'])
# line = kw['line']
# orders = request.env['mrp.workorder'].sudo().search([
# ('routing_type', '=', 'CNC加工'), # 将第一个条件合并进来
# ('production_line_id.name', '=', line),
# ('state', 'in', ['done'])
# ])
#
# faulty_plans = request.env['quality.check'].sudo().search([
# ('operation_id.name', '=', 'CNC加工'),
# ('quality_state', 'in', ['fail'])
# ])
#
# # 计算时间范围
# now = datetime.now()
# today_start = now.replace(hour=0, minute=0, second=0, microsecond=0)
# month_start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
#
# total_power_on_time = 0
# month_power_on_time = 0
# today_power_on_time = 0
# today_power_on_dict = {}
# today_alarm_dict = {}
# single_machine_dict = {}
#
# today_order_data = []
# month_order_data = []
# today_check_ng = []
# month_check_ng = []
#
# total_alarm_time = 0
# today_alarm_time = 0
# month_alarm_time = 0
#
# for order in orders:
# time = order.date_finished
# if time >= today_start:
# today_order_data.append(order)
# if time >= month_start:
# month_order_data.append(order)
#
# for faulty_plan in faulty_plans:
# time = faulty_plan.write_date
# if time >= today_start:
# today_check_ng.append(faulty_plan)
# if time >= month_start:
# month_check_ng.append(faulty_plan)
#
# # 连接数据库
# conn = psycopg2.connect(**db_config)
# for item in machine_list:
# with conn.cursor() as cur:
# cur.execute("""
# (
# SELECT power_on_time, 'latest' AS record_type
# FROM device_data
# WHERE device_name = %s
# AND power_on_time IS NOT NULL
# ORDER BY time DESC
# LIMIT 1
# )
# UNION ALL
# (
# SELECT power_on_time, 'month_first' AS record_type
# FROM device_data
# WHERE device_name = %s
# AND power_on_time IS NOT NULL
# AND time >= date_trunc('month', CURRENT_DATE) -- ✅ 修复日期函数
# AND time < (date_trunc('month', CURRENT_DATE) + INTERVAL '1 month')::date
# ORDER BY time ASC
# LIMIT 1
# )
# UNION ALL
# (
# SELECT power_on_time, 'day_first' AS record_type
# FROM device_data
# WHERE device_name = %s
# AND power_on_time IS NOT NULL
# AND time::date = CURRENT_DATE -- ✅ 更高效的写法
# ORDER BY time ASC
# LIMIT 1
# );
# """, (item, item, item))
# results = cur.fetchall()
# if len(results) >= 1:
# total_power_on_time += convert_to_seconds(results[0][0])
# else:
# total_power_on_time += 0
# if len(results) >= 2:
# month_power_on_time += convert_to_seconds(results[1][0])
# else:
# month_power_on_time += 0
# if len(results) >= 3:
# today_power_on_time += convert_to_seconds(results[2][0])
# today_power_on_dict[item] = today_power_on_time
# else:
# today_power_on_time += 0
# today_power_on_dict[item] = 0
#
# with conn.cursor() as cur:
# cur.execute("""
# SELECT DISTINCT ON (alarm_start_time) alarm_time, alarm_start_time
# FROM device_data
# WHERE device_name = %s AND alarm_start_time IS NOT NULL
# ORDER BY alarm_start_time, time;
# """, (item,))
# results = cur.fetchall()
# today_data = []
# month_data = []
#
# for record in results:
# if record[0]:
# if float(record[0]) >= 28800:
# continue
# total_alarm_time += float(record[0])
# else:
# total_alarm_time += 0.0
# alarm_start = datetime.strptime(record[1], "%Y-%m-%d %H:%M:%S")
# if alarm_start >= today_start:
# today_data.append(record)
# if alarm_start >= month_start:
# month_data.append(record)
# if today_data:
# for today in today_data:
# if today[0]:
# if float(today[0]) >= 28800:
# continue
# today_alarm_time += float(today[0])
# today_alarm_dict[item] = today_alarm_time
# else:
# today_alarm_time += 0.0
# today_alarm_dict[item] = 0
# else:
# today_alarm_dict[item] = 0
# for month in month_data:
# if month[0]:
# if float(month[0]) >= 28800:
# continue
# month_alarm_time += float(month[0])
# else:
# month_alarm_time += 0.0
#
# conn.close()
#
# logging.info('报警时间总月日=============%s, %s, %s' % (total_alarm_time, month_alarm_time, today_alarm_time))
# # 计算时间开动率(累计、月、日)
# if total_power_on_time:
# total_power_on_rate = (total_power_on_time - total_alarm_time) / total_power_on_time
# else:
# total_power_on_rate = 0
# if month_power_on_time:
# month_power_on_rate = (total_power_on_time - month_power_on_time - month_alarm_time) / (
# total_power_on_time - month_power_on_time)
# else:
# month_power_on_rate = 0
# if today_power_on_time:
# today_power_on_rate = (total_power_on_time - today_power_on_time - today_alarm_time) / (
# total_power_on_time - today_power_on_time)
# else:
# today_power_on_rate = 0
# logging.info("总开动率: %s" % total_power_on_rate)
# logging.info("月开动率: %s" % month_power_on_rate)
# logging.info("日开动率: %s" % today_power_on_rate)
#
# # 计算性能开动率(累计、月、日)
# logging.info('完成工单数量: %s' % len(orders))
# total_performance_rate = len(orders) * 30 * 60 / (total_power_on_time - total_alarm_time)
# month_performance_rate = len(month_data) * 30 * 60 / (
# total_power_on_time - month_power_on_time - month_alarm_time)
# today_performance_rate = len(today_data) * 30 * 60 / (
# total_power_on_time - today_power_on_time - today_alarm_time) if today_power_on_time != 0 else 0
# logging.info("总性能率: %s" % total_performance_rate)
# logging.info("月性能率: %s" % month_performance_rate)
# logging.info("日性能率: %s" % today_performance_rate)
#
# # 计算累计合格率
# total_pass_rate = (len(orders) - len(today_check_ng)) / len(orders) if len(orders) != 0 else 0
# month_pass_rate = (len(month_order_data) - len(month_check_ng)) / len(month_order_data) if len(month_order_data) != 0 else 0
# today_pass_rate = (len(today_order_data) - len(today_check_ng)) / len(today_order_data) if len(today_order_data) != 0 else 0
# logging.info("总合格率: %s" % total_pass_rate)
# logging.info("月合格率: %s" % month_pass_rate)
# logging.info("日合格率: %s" % today_pass_rate)
#
# # # 返回数据
# # res['data'][item] = {
# # 'total_utilization_rate': total_power_on_rate * total_performance_rate * total_pass_rate,
# # 'month_utilization_rate': month_power_on_rate * month_performance_rate * month_pass_rate,
# # 'today_utilization_rate': today_power_on_rate * today_performance_rate * today_pass_rate,
# # }
# for i in machine_list:
# single_machine_utilization_rate = total_power_on_time - today_power_on_dict[i] - today_alarm_dict[i] / (
# total_power_on_time - today_power_on_dict[i])
# single_machine_dict[i] = single_machine_utilization_rate * today_performance_rate * today_pass_rate
#
# res['data'] = {
# 'total_utilization_rate': total_power_on_rate * total_performance_rate * total_pass_rate,
# 'month_utilization_rate': month_power_on_rate * month_performance_rate * month_pass_rate,
# 'today_utilization_rate': today_power_on_rate * today_performance_rate * today_pass_rate,
# 'single_machine_dict': single_machine_dict
# }
#
# return json.dumps(res)
@http.route('/api/RunningTime', type='http', auth='public', methods=['GET', 'POST'], csrf=False, cors="*")
def UtilizationRate(self, **kw):
"""
获取稼动率
@@ -1420,12 +1631,6 @@ class Sf_Dashboard_Connect(http.Controller):
res = {'status': 1, 'message': '成功', 'data': {}}
# 获取请求的机床数据
machine_list = ast.literal_eval(kw['machine_list'])
line = kw['line']
orders = request.env['mrp.workorder'].sudo().search([
('routing_type', '=', 'CNC加工'), # 将第一个条件合并进来
('production_line_id.name', '=', line),
('state', 'in', ['done'])
])
faulty_plans = request.env['quality.check'].sudo().search([
('operation_id.name', '=', 'CNC加工'),
@@ -1441,9 +1646,11 @@ class Sf_Dashboard_Connect(http.Controller):
month_power_on_time = 0
today_power_on_time = 0
today_power_on_dict = {}
today_alarm_dict = {}
single_machine_dict = {}
today_data = []
month_data = []
today_order_data = []
month_order_data = []
today_check_ng = []
month_check_ng = []
@@ -1451,20 +1658,6 @@ class Sf_Dashboard_Connect(http.Controller):
today_alarm_time = 0
month_alarm_time = 0
for order in orders:
time = datetime.strptime(order.date_finished, "%Y-%m-%d %H:%M:%S")
if time >= today_start:
today_data.append(order)
if time >= month_start:
month_data.append(order)
for faulty_plan in faulty_plans:
time = faulty_plan.write_date
if time >= today_start:
today_check_ng.append(faulty_plan)
if time >= month_start:
month_check_ng.append(faulty_plan)
# 连接数据库
conn = psycopg2.connect(**db_config)
for item in machine_list:
@@ -1501,7 +1694,6 @@ class Sf_Dashboard_Connect(http.Controller):
);
""", (item, item, item))
results = cur.fetchall()
print(results)
if len(results) >= 1:
total_power_on_time += convert_to_seconds(results[0][0])
else:
@@ -1515,7 +1707,7 @@ class Sf_Dashboard_Connect(http.Controller):
today_power_on_dict[item] = today_power_on_time
else:
today_power_on_time += 0
print(total_power_on_time, month_power_on_time, today_power_on_time)
today_power_on_dict[item] = 0
with conn.cursor() as cur:
cur.execute("""
@@ -1540,13 +1732,18 @@ class Sf_Dashboard_Connect(http.Controller):
today_data.append(record)
if alarm_start >= month_start:
month_data.append(record)
if today_data:
for today in today_data:
if today[0]:
if float(today[0]) >= 28800:
continue
today_alarm_time += float(today[0])
today_alarm_dict[item] = today_alarm_time
else:
today_alarm_time += 0.0
today_alarm_dict[item] = 0
else:
today_alarm_dict[item] = 0
for month in month_data:
if month[0]:
if float(month[0]) >= 28800:
@@ -1557,60 +1754,30 @@ class Sf_Dashboard_Connect(http.Controller):
conn.close()
print('报警时间=============', total_alarm_time, month_alarm_time, today_alarm_time)
logging.info("报警时间=%s" % total_alarm_time)
logging.info("报警时间=%s" % month_alarm_time)
logging.info("报警时间=%s" % today_alarm_time)
logging.info('报警时间总月日=============%s, %s, %s' % (total_alarm_time, month_alarm_time, today_alarm_time))
# 计算时间开动率(累计、月、日)
if total_power_on_time:
total_power_on_rate = (total_power_on_time - total_alarm_time) / total_power_on_time
else:
total_power_on_rate = 0
if month_power_on_time:
month_power_on_rate = (total_power_on_time - month_power_on_time - month_alarm_time) / month_power_on_time
month_power_on_rate = (total_power_on_time - month_power_on_time - month_alarm_time) / (
total_power_on_time - month_power_on_time)
else:
month_power_on_rate = 0
if today_power_on_time:
today_power_on_rate = (total_power_on_time - today_power_on_time - today_alarm_time) / today_power_on_time
today_power_on_rate = (total_power_on_time - today_power_on_time - today_alarm_time) / (
total_power_on_time - today_power_on_time)
else:
today_power_on_rate = 0
print("总开动率: %s" % total_power_on_rate)
print("月开动率: %s" % month_power_on_rate)
print("日开动率: %s" % today_power_on_rate)
logging.info("总开动率: %s" % total_power_on_rate)
logging.info("月开动率: %s" % month_power_on_rate)
logging.info("日开动率: %s" % today_power_on_rate)
# 计算性能开动率(累计、月、日)
print('===========',orders)
print(len(orders))
total_performance_rate = len(orders) * 30 * 60 / (total_power_on_time - total_alarm_time)
month_performance_rate = len(month_data) * 30 * 60 / (month_power_on_time - month_alarm_time)
today_performance_rate = len(today_data) * 30 * 60 / (today_power_on_time - today_alarm_time) if today_power_on_time != 0 else 0
print("总性能率: %s" % total_performance_rate)
print("月性能率: %s" % month_performance_rate)
print("日性能率: %s" % today_performance_rate)
# 计算累计合格率
total_pass_rate = (len(orders) - len(today_check_ng)) / len(orders) if len(orders) != 0 else 0
month_pass_rate = (len(month_data) - len(month_check_ng)) / len(month_data) if len(month_data) != 0 else 0
today_pass_rate = (len(today_data) - len(today_check_ng)) / len(today_data) if len(today_data) != 0 else 0
print("总合格率: %s" % total_pass_rate)
print("月合格率: %s" % month_pass_rate)
print("日合格率: %s" % today_pass_rate)
# # 返回数据
# res['data'][item] = {
# 'total_utilization_rate': total_power_on_rate * total_performance_rate * total_pass_rate,
# 'month_utilization_rate': month_power_on_rate * month_performance_rate * month_pass_rate,
# 'today_utilization_rate': today_power_on_rate * today_performance_rate * today_pass_rate,
# }
res['data'] = {
'total_utilization_rate': total_power_on_rate * total_performance_rate * total_pass_rate,
'month_utilization_rate': month_power_on_rate * month_performance_rate * month_pass_rate,
'today_utilization_rate': today_power_on_rate * today_performance_rate * today_pass_rate,
'all_time_work_rate': total_power_on_rate,
'month_work_rate': month_power_on_rate,
'day_work_rate': today_power_on_rate,
}
return json.dumps(res)