提交 d1dc0b7e authored 作者: zw.wang's avatar zw.wang

feat: [tasks] 新增在线时长和离线时长

上级 3d52fc30
import tempfile
import os
import pytz
import pandas as pd
from apscheduler.schedulers.blocking import BlockingScheduler
from datetime import datetime, timedelta
......@@ -137,20 +138,33 @@ class Tasks:
"""
headers = [
'摄像头序列号', '摄像头名称', 'IndexCode',
'总时长', '完成', '失败', '未处理', '未知'
'总时长', '完成', '失败', '未处理', '未知', '在线时长', '离线时长'
]
columns = [
'device_code', 'camera_name', 'point_index_code',
'total', 'done', 'failed', 'untreated', 'unknown'
'total', 'done', 'failed', 'untreated', 'unknown',
'online_duration', 'offline_duration'
]
end_time = datetime.utcnow().astimezone(tz).replace(hour=0, minute=0, second=0)
query_str = '''
select *
from one_week.isc_camera_status
where time > '{}'
and time < '{}'
and camera_code = '{}';
'''
influxdb.reconnect()
end_time_cst = datetime.utcnow().astimezone(tz).replace(hour=0, minute=0, second=0)
end_time = end_time_cst.astimezone(pytz.utc)
start_time = end_time - timedelta(days=1)
file_name = 'isc-daily-{}.xlsx'.format(end_time.strftime('%Y%m%d'))
file_name = 'isc-daily-{}.xlsx'.format(end_time_cst.strftime('%Y%m%d'))
camera_count = 0
daily_total_duration = daily_done_duration = 0
daily_failed_duration = daily_retry_duration = 0
results = []
influxdb.reconnect()
for camera in get_camera_info():
done = failed = untreated = unknown = total = 0
......@@ -168,14 +182,26 @@ class Tasks:
untreated += duration
else:
unknown += duration
# TODO 在线和离线时间
camera['done'] = get_time_str(done)
camera['failed'] = get_time_str(failed)
camera['untreated'] = get_time_str(untreated)
camera['unknown'] = get_time_str(unknown)
camera['total'] = get_time_str(total)
res = influxdb.query(query_str.format(
start_time.strftime('%Y-%m-%d %H:%M:%S'),
end_time.strftime('%Y-%m-%d %H:%M:%S'),
camera['device_code']))
online_seconds = offline_seconds = 0
if res and res['isc_camera_status']:
df = pd.DataFrame(res['isc_camera_status'])
df.index = df['time']
online_seconds = len(df[df['value'] == 1]) * 190
offline_seconds = len(df[df['value'] == 0]) * 190
camera['online_duration'] = get_time_str(online_seconds)
camera['offline_duration'] = get_time_str(offline_seconds)
results.append(camera)
daily_total_duration += total
......@@ -192,7 +218,7 @@ class Tasks:
send_markdown(
'isc-daily',
daily_text.format(
end_time.strftime('%Y-%m-%d'),
end_time_cst.strftime('%Y-%m-%d'),
camera_count,
get_time_str(daily_total_duration),
get_time_str(daily_done_duration),
......
......@@ -18,7 +18,8 @@ requires = [
'retrying',
'oss2',
'apscheduler',
'openpyxl'
'openpyxl',
'pandas'
]
setuptools.setup(
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论