调整设备可用率接口
This commit is contained in:
@@ -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="*")
|
||||
@@ -1621,3 +1621,163 @@ class Sf_Dashboard_Connect(http.Controller):
|
||||
# }
|
||||
#
|
||||
# return json.dumps(res)
|
||||
|
||||
@http.route('/api/RunningTime', 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'])
|
||||
|
||||
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
|
||||
|
||||
# 连接数据库
|
||||
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)
|
||||
|
||||
res['data'] = {
|
||||
'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)
|
||||
|
||||
Reference in New Issue
Block a user