Commit a2e7dd6a authored by ZZH's avatar ZZH

remove es 2023-6-5

parent f0fe05e6
from pot_libs.mysql_util.mysql_util import MysqlUtil
from pot_libs.logger import log
from unify_api.modules.product_info.procedures.hardware_pds import (
get_user_hardware_info, hardware_statistics)
......
......@@ -5,8 +5,7 @@ from unify_api.modules.product_info.components.hardware_cps import (
HardwareInfoManResq, HardwareInfoListReq, HardwareInfoListResq
)
from unify_api.modules.product_info.procedures.hardware_pds import (
company_available, get_user_hardware_info, hardware_statistics,
get_user_hardware_info_new15, hardware_statistics_new15
company_available, get_user_hardware_info_new15, hardware_statistics_new15
)
from pot_libs.logger import log
from unify_api.modules.product_info.service.hardware_info import \
......@@ -16,7 +15,8 @@ from unify_api.modules.common.procedures.cids import get_cids, get_proxy_cids
@summary("获取硬件信息")
async def post_hardware_info_list(request, body: HardwareInfoReq) -> HardwareInfoRespList:
async def post_hardware_info_list(request,
body: HardwareInfoReq) -> HardwareInfoRespList:
company_id = body.cid
page_size, page_num = body.page_size, body.page_num
log.info(
......@@ -27,18 +27,21 @@ async def post_hardware_info_list(request, body: HardwareInfoReq) -> HardwareInf
return HardwareInfoRespList.user_error()
# page_map = await get_user_hardware_info(company_id, page_num, page_size)
page_map = await get_user_hardware_info_new15(company_id, page_num, page_size)
page_map = await get_user_hardware_info_new15(company_id, page_num,
page_size)
return HardwareInfoRespList(rows=page_map["rows"], total=page_map["total"])
@summary("硬件统计信息")
async def post_hardware_info_count(request, body: HardwareInfoCountReq) -> HardwareInfoCountResp:
async def post_hardware_info_count(request,
body: HardwareInfoCountReq) -> HardwareInfoCountResp:
company_id = body.cid
# statistics_info_map = await hardware_statistics(company_id)
statistics_info_map = await hardware_statistics_new15(company_id)
return HardwareInfoCountResp(
installed_number=statistics_info_map["installed_number"],
legal_measurement_number=statistics_info_map["legal_measurement_number"],
legal_measurement_number=statistics_info_map[
"legal_measurement_number"],
start_time=statistics_info_map["start_time"],
power_capacity=statistics_info_map["power_capacity"],
)
......@@ -58,20 +61,21 @@ async def post_hardware_list_sdu(req, body: HlsReq) -> HlsResp:
@summary("管理版-关于我们-统计")
async def post_hardware_info_management(req, body: HardwareInfoManReq) -> HardwareInfoManResq:
async def post_hardware_info_management(req,
body: HardwareInfoManReq) -> HardwareInfoManResq:
product = body.product
# user_id = 88 if req.ctx else req.ctx.user_id
try:
user_id = req.ctx.user_id
except:
user_id = req.json.get("userid") # 给跑测试用的
user_id = req.json.get("userid") # 给跑测试用的
# cids = await get_cids(user_id, product)
proxy_id = body.proxy_id
cids = await get_proxy_cids(user_id, product, proxy_id)
if not cids:
return HardwareInfoManResq(
project_count=0, start_time="", detection_point_count=0
)
)
return await hardware_info_management_service(cids)
......
import json
from datetime import datetime, timedelta
from pot_libs.utils.exc_util import BusinessException
from unify_api import constants
from unify_api.modules.alarm_manager.dao.list_static_dao import \
......@@ -12,13 +10,12 @@ from unify_api.modules.home_page.procedures.count_info_pds import \
from unify_api.modules.shidianu.components.algorithm_cps import WcResp, AbcResp
from unify_api.modules.shidianu.dao.analysis_result_dao import \
query_sdu_power_wave, query_sdu_recog_record
from unify_api.modules.shidianu.procedures.output_result import get_p_list, \
get_curve_p
from unify_api.modules.shidianu.procedures.output_result import get_curve_p
from unify_api.utils.time_format import last30_day_range, \
get_start_end_by_tz_time, day_slots, get_start_end_by_tz_time_new
day_slots, get_start_end_by_tz_time_new
async def wave_curve_service_new15(point_id, req_date, product):
async def wave_curve_srv(point_id, req_date, product):
# 1,获取slots
time_slot = day_slots()
# 2. 获取sid
......
......@@ -2,14 +2,10 @@ import json
from collections import defaultdict
from datetime import datetime, timedelta
from itertools import groupby
import requests
from pot_libs.es_util.es_utils import EsUtil
from pot_libs.logger import log
from pot_libs.settings import SETTING
from pot_libs.utils.exc_util import BusinessException
from pot_libs.utils.pendulum_wrapper import my_pendulum
from unify_api.constants import POINT_1MIN_INDEX, PRODUCT, ExtendModule
from unify_api.modules.common.procedures.common_cps import point_day2month
from unify_api.modules.common.service.td_engine_service import \
......@@ -24,15 +20,19 @@ from unify_api.modules.common.procedures.points import get_meter_by_point
from pot_libs.mysql_util.mysql_util import MysqlUtil
from unify_api.modules.shidianu.procedures.power_param import power_p
from unify_api.modules.shidianu.service.IntergratePipe import hisPieceFind
from unify_api.modules.shidianu.service.electricProportion import electricProportion
from unify_api.modules.shidianu.service.electricProportion import \
electricProportion
import numpy as np
import pandas as pd
import asyncio
from unify_api.modules.shidianu.service.open_data_service import get_token
from unify_api.modules.shidianu.service.paramAnaly import paramAnalysis, params_mining
from unify_api.modules.users.procedures.user_product_auth import get_product_auth
from unify_api.utils.time_format import last_n_day, srv_time
from unify_api.modules.shidianu.service.paramAnaly import paramAnalysis, \
params_mining
from unify_api.modules.users.procedures.user_product_auth import \
get_product_auth
from unify_api.utils.time_format import (
last_n_day, srv_time, get_day_start, YMD_Hms
)
from unify_api.utils.taos_new import parse_td_columns
......@@ -143,60 +143,6 @@ def get_runtime_list(state_dict):
return runtime_list
async def get_p_list(sid, meter_sn, req_date, time_slots):
sn = meter_sn.lower()
year, month, day = req_date.split("-")
start_time = datetime(year=int(year), month=int(month), day=int(day))
end_time = start_time + timedelta(days=1)
es_start_time = start_time.strftime("%Y-%m-%dT%H:%M:%S+08:00")
es_end_time = end_time.strftime("%Y-%m-%dT%H:%M:%S+08:00")
es_index = point_day2month(start_time)
query_body = {
"size": 0,
"query": {
"bool": {
"must": [
{"term": {"sid.keyword": sid}},
{"term": {"meter_sn.keyword": {"value": str.upper(sn)}}},
{"range": {"datetime": {"gte": es_start_time, "lt": es_end_time,}}},
]
}
},
"sort": [{"datetime": {"order": "asc"}}],
"aggs": {
"datetime": {
"date_histogram": {
"field": "datetime",
"interval": "minute",
"time_zone": "+08:00",
"format": "HH:mm",
},
"aggs": {
f"p{sn}": {"stats": {"field": f"p{sn}"}},
f"i{sn}": {"stats": {"field": f"i{sn}"}},
},
}
},
}
log.info(f"query_body={query_body}")
async with EsUtil() as es:
res = await es.search_origin(body=query_body, index=es_index)
buckets = res["aggregations"]["datetime"]["buckets"]
es_result_map = {i["key_as_string"]: i for i in buckets}
p_list = []
for slot in time_slots:
if slot in es_result_map:
p = es_result_map[slot].get("p" + sn).get("max")
if p is None:
p = ""
p_list.append(round(p, 4) if type(p) in [int, float] else p)
else:
p_list.append("")
return p_list
async def get_curve_p(mtid, meter_sn, start, end, time_slots):
'''
从tdenine中获取相关数据
......@@ -218,7 +164,7 @@ async def get_curve_p(mtid, meter_sn, start, end, time_slots):
datas[slot] = data[p_field]
p_list = []
for slot in time_slots:
p = datas.get(slot,"")
p = datas.get(slot, "")
if p:
p = round(p, 4) if type(p) in [int, float] else p
p_list.append(p)
......@@ -230,20 +176,26 @@ def time_interval_class(item_obj):
return time_str
async def algorithm_result_to_front(point_id, req_date, user_id, product, detail):
meter_info = await get_meter_by_point(point_id)
async def algorithm_result_to_front(pid, req_date, user_id, product, detail):
meter_info = await get_meter_by_point(pid)
if not meter_info:
raise BusinessException(message="没有该监测点的meter信息,请联系运维人员!")
sid, meter_no = meter_info["sid"], meter_info["meter_no"]
mtid = meter_info["mtid"]
dt = datetime.strptime(req_date + " 00:00:00", "%Y-%m-%d %H:%M:%S")
time_slot = [
datetime.strftime(dt + timedelta(minutes=i), "%Y-%m-%d %H:%M:%S").split(" ")[1][:5]
datetime.strftime(dt + timedelta(minutes=i),
"%Y-%m-%d %H:%M:%S").split(" ")[1][:5]
for i in range(1440)
]
p_list = await get_p_list(sid, meter_no, req_date, time_slot)
s_dt = get_day_start(req_date, dt_fmt="YYYY-MM-DD")
s_dts, e_dts = s_dt.format(YMD_Hms), s_dt.add(days=1).format(YMD_Hms)
p_list = await get_curve_p(mtid, meter_no, s_dts, e_dts, time_slot)
async with MysqlUtil() as conn:
sql = "select * from sdu_dev_run_anal where pid=%s and cal_day=%s"
res = await conn.fetchone(sql, args=(point_id, req_date))
res = await conn.fetchone(sql, args=(pid, req_date))
if not res:
return AlgorithmOutput(
time_slot=time_slot,
......@@ -279,7 +231,7 @@ async def algorithm_result_to_front(point_id, req_date, user_id, product, detail
# 根据配置扩展模块取不同的算法结果字段
async with MysqlUtil() as conn:
point_sql = "select pid, cid from point where pid=%s"
point_map = await conn.fetchone(point_sql, args=(point_id,))
point_map = await conn.fetchone(point_sql, args=(pid,))
if not point_map:
raise BusinessException(message="没有该监测点的信息,请联系运维人员!")
cid = point_map["cid"]
......@@ -288,10 +240,12 @@ async def algorithm_result_to_front(point_id, req_date, user_id, product, detail
cid_ext_module_map = product_auth_map["product"]
if str(cid) not in cid_ext_module_map:
log.error(f"用户user_id = {user_id} 工厂cid={cid}的权限")
raise BusinessException(message=f"用户user_id = {user_id} 没有工厂cid={cid}的权限")
raise BusinessException(
message=f"用户user_id = {user_id} 没有工厂cid={cid}的权限")
act_info = json.loads(res["act_info"]) if res["act_info"] else {}
if ExtendModule.AlgorithmResultDetail.value in cid_ext_module_map[str(cid)] and detail == 1:
if ExtendModule.AlgorithmResultDetail.value in cid_ext_module_map[
str(cid)] and detail == 1:
# 如果识电U配置了2那么给权限看新字段(内部人看的详细识别数据)
act_info = json.loads(res["act_info2"]) if res["act_info2"] else {}
for event_type, event_list in act_info.items():
......@@ -304,20 +258,25 @@ async def algorithm_result_to_front(point_id, req_date, user_id, product, detail
action_list.append(item[0])
result_dict = {
"设备运行情况": json.loads(res["dev_run_info"]) if res["dev_run_info"] is not None else [],
"运行时间段": json.loads(res["dev_run_tp"]) if res["dev_run_tp"] is not None else [],
"设备运行情况": json.loads(res["dev_run_info"]) if res[
"dev_run_info"] is not None else [],
"运行时间段": json.loads(res["dev_run_tp"]) if res[
"dev_run_tp"] is not None else [],
"行为列表": action_list,
"行为时间": action_time_list,
}
state_dict = {
"电量与时长": json.loads(res["ele_quan_dur"]) if res["ele_quan_dur"] is not None else {},
"波动": json.loads(res["dev_wave"]) if res["dev_wave"] is not None else {},
"电量与时长": json.loads(res["ele_quan_dur"]) if res[
"ele_quan_dur"] is not None else {},
"波动": json.loads(res["dev_wave"]) if res[
"dev_wave"] is not None else {},
}
log.info(f"算法结果 result_dict={result_dict} state_dict={state_dict}")
electric_action_list = [
ElectricActionItem(
action_name=action_name, action_time=result_dict["行为时间"][index].split(" ")[1][:5],
action_name=action_name,
action_time=result_dict["行为时间"][index].split(" ")[1][:5],
)
for index, action_name in enumerate(result_dict["行为列表"])
]
......@@ -349,7 +308,8 @@ async def algorithm_result_to_front(point_id, req_date, user_id, product, detail
run_period_list=[
RunPeriodtem(
running_devices=running_devices,
time_period=[i.split(" ")[1][:5] for i in result_dict["运行时间段"][index]],
time_period=[i.split(" ")[1][:5] for i in
result_dict["运行时间段"][index]],
)
for index, running_devices in enumerate(result_dict["设备运行情况"])
],
......@@ -359,7 +319,8 @@ async def algorithm_result_to_front(point_id, req_date, user_id, product, detail
async def main():
result_dict, state_dict, p_list = await output_result("A2004000192", "A", "2020-12-02")
result_dict, state_dict, p_list = await output_result("A2004000192", "A",
"2020-12-02")
print(result_dict)
print(state_dict)
......
from pot_libs.sanic_api import summary, description
from unify_api.constants import PRODUCT
from unify_api.modules.shidianu.components.algorithm_cps import (
AlgorithmOutput,
AlgorithmInput, WcReq, WcResp, AbcResp, AbcReq,
)
from unify_api.modules.shidianu.procedures.analysis_result_service import \
wave_curve_service_new15, alarm_behavior_curve_service
wave_curve_srv, alarm_behavior_curve_service
from unify_api.modules.shidianu.procedures.output_result import algorithm_result_to_front
from unify_api.utils.time_format import srv_time
......@@ -26,7 +25,7 @@ async def post_wave_curve(req, body: WcReq) -> WcResp:
point_id = body.point_id
req_date = body.req_date
product = body.product
return await wave_curve_service_new15(point_id, req_date, product)
return await wave_curve_srv(point_id, req_date, product)
@summary("识电u-用电行为-近30天统计")
......
......@@ -9,20 +9,21 @@ import random
from functools import wraps
import pendulum
from pot_libs.utils.pendulum_wrapper import my_pendulum
from unify_api.constants import SLOTS
CST = "Asia/Shanghai"
YMD = "YYYYMMDD"
YMD_U = "YYYY-MM-DD"
YM = "YYYYMM"
YMD_Hm = "YYYY-MM-DD HH:mm"
YMD_Hms = "YYYY-MM-DD HH:mm:ss"
NO_DATA_EXPRESS = ""
def time_pick_transf(start, end, is_range=0):
"""获取intervel和slots"""
start_f = my_pendulum.from_format(start, 'YYYY-MM-DD HH:mm:ss')
end_f = my_pendulum.from_format(end, 'YYYY-MM-DD HH:mm:ss')
# start_time = pendulum.datetime(start_f.year, start_f.month, start_f.day,
# start_f.hour, start_f.minute)
# end_time = pendulum.datetime(end_f.year, end_f.month, end_f.day,
# end_f.hour, end_f.minute)
# diff = (end_time - start_time).seconds
start_f = my_pendulum.from_format(start, YMD_Hms)
end_f = my_pendulum.from_format(end, YMD_Hms)
diff = end_f.int_timestamp - start_f.int_timestamp
# 1. 计算intervel
# 1.1 区间3小时之内, 返回1min
......@@ -45,19 +46,19 @@ def time_pick_transf(start, end, is_range=0):
for i in range(slot_num):
# 区间3小时之内
if diff <= 3 * 3600:
dt = start_f.add(minutes=1 * i).format("YYYY-MM-DD HH:mm")
dt = start_f.add(minutes=1 * i).format(YMD_Hm)
dt_str = str(dt).split()[1]
if is_range:
dt_str = str(dt)
# 区间24小时之内
elif diff < 24 * 3600:
dt = start_f.add(minutes=15 * i).format("YYYY-MM-DD HH:mm")
dt = start_f.add(minutes=15 * i).format(YMD_Hm)
dt_str = str(dt).split()[1]
if is_range:
dt_str = str(dt)
# 区间48小时之内
elif 24 * 3600 <= diff <= 48 * 3600:
dt = start_f.add(minutes=15 * i).format("YYYY-MM-DD HH:mm")
dt = start_f.add(minutes=15 * i).format(YMD_Hm)
dt_str = str(dt)
# 区间在60天以内
elif 48 * 3600 < diff <= 60 * 86400:
......@@ -72,8 +73,8 @@ def time_pick_transf(start, end, is_range=0):
def time_pick_transf_new(start, end):
"""获取intervel和slots, 详细显示时间轴信息,新接口都使用这个来获取时间"""
start_f = my_pendulum.from_format(start, 'YYYY-MM-DD HH:mm:ss')
end_f = my_pendulum.from_format(end, 'YYYY-MM-DD HH:mm:ss')
start_f = my_pendulum.from_format(start, YMD_Hms)
end_f = my_pendulum.from_format(end, YMD_Hms)
diff = end_f.int_timestamp - start_f.int_timestamp
# 1. 计算intervel
# 1.1 区间48小时之内, 返回15min
......@@ -92,7 +93,7 @@ def time_pick_transf_new(start, end):
for i in range(slot_num):
# 区间48小时之内
if diff <= 48 * 3600:
dt = start_f.add(minutes=15 * i).format("YYYY-MM-DD HH:mm")
dt = start_f.add(minutes=15 * i).format(YMD_Hm)
dt_str = str(dt)
# 区间在60天以内
elif 48 * 3600 < diff <= 60 * 86400:
......@@ -107,8 +108,8 @@ def time_pick_transf_new(start, end):
def power_slots(start, end):
"""电量电费,用电统计,time=range slots计算"""
start_f = my_pendulum.from_format(start, 'YYYY-MM-DD HH:mm:ss')
end_f = my_pendulum.from_format(end, 'YYYY-MM-DD HH:mm:ss')
start_f = my_pendulum.from_format(start, YMD_Hms)
end_f = my_pendulum.from_format(end, YMD_Hms)
# 1.求出时间区间多少分钟
diff_mm = (end_f - start_f).in_minutes()
diff_hours = (end_f - start_f).in_hours()
......@@ -116,7 +117,7 @@ def power_slots(start, end):
slots = []
if diff_mm <= 24 * 60:
for i in range(24):
dt = start_f.add(hours=1 * i).format("YYYY-MM-DD HH:mm")
dt = start_f.add(hours=1 * i).format(YMD_Hm)
dt_str = str(dt)
slots.append(dt_str)
return slots, "day"
......@@ -124,7 +125,7 @@ def power_slots(start, end):
elif 24 * 60 < diff_mm <= 48 * 60:
slot_num = math.floor(diff_mm / 60)
for i in range(slot_num + 1):
dt = start_f.add(hours=1 * i).format("YYYY-MM-DD HH:mm")
dt = start_f.add(hours=1 * i).format(YMD_Hm)
dt_str = str(dt)
slots.append(dt_str)
return slots, "day"
......@@ -146,10 +147,10 @@ def power_slots(start, end):
def proxy_power_slots(start, end, date_format="MM-DD", is_duration=False):
start_f = my_pendulum.from_format(start, 'YYYY-MM-DD HH:mm:ss')
start_f = my_pendulum.from_format(start, YMD_Hms)
if is_duration:
# 计算区间
end_f = my_pendulum.from_format(end, 'YYYY-MM-DD HH:mm:ss')
end_f = my_pendulum.from_format(end, YMD_Hms)
m_number = (end_f - start_f).in_days() + 1
else:
# 只判断当前这个月有几天
......@@ -163,7 +164,7 @@ def proxy_power_slots(start, end, date_format="MM-DD", is_duration=False):
def day_of_month(start):
"""这个月有几天"""
start_f = my_pendulum.from_format(start, 'YYYY-MM-DD HH:mm:ss')
start_f = my_pendulum.from_format(start, YMD_Hms)
# 这个月有几天
m_number = start_f.days_in_month
return m_number
......@@ -171,7 +172,7 @@ def day_of_month(start):
def year_slots(start, end):
"""年计算slots"""
start_f = my_pendulum.from_format(start, 'YYYY-MM-DD HH:mm:ss')
start_f = my_pendulum.from_format(start, YMD_Hms)
slots = []
for i in range(12):
dt = start_f.add(months=i).format("YYYY-MM-DD")
......@@ -198,8 +199,8 @@ def day_slots(type='minutes'):
def range_to_type(start, end):
"""时间范围,转换为date_type"""
start_f = my_pendulum.from_format(start, 'YYYY-MM-DD HH:mm:ss')
end_f = my_pendulum.from_format(end, 'YYYY-MM-DD HH:mm:ss')
start_f = my_pendulum.from_format(start, YMD_Hms)
end_f = my_pendulum.from_format(end, YMD_Hms)
# 1.求出时间区间多少分钟
diff_mm = (end_f - start_f).in_minutes()
diff_hours = (end_f - start_f).in_hours()
......@@ -230,8 +231,8 @@ def last30_day_range_today():
"""求最近30天起止时间, 包括今天"""
now_s = pendulum.now().start_of('day')
now_e = pendulum.now().end_of('day')
start = now_s.add(days=-1 * 29).format("YYYY-MM-DD HH:mm:ss")
end = now_e.format("YYYY-MM-DD HH:mm:ss")
start = now_s.add(days=-1 * 29).format(YMD_Hms)
end = now_e.format(YMD_Hms)
return start, end
......@@ -239,8 +240,8 @@ def last_month_start_end():
"""上个月起始时间"""
now = pendulum.now()
last = now.add(months=-1)
last_start = last.start_of('month').format("YYYY-MM-DD HH:mm:ss")
last_end = last.end_of("month").format("YYYY-MM-DD HH:mm:ss")
last_start = last.start_of('month').format(YMD_Hms)
last_end = last.end_of("month").format(YMD_Hms)
return last_start, last_end
......@@ -248,8 +249,8 @@ def last30_day_range():
"""求最近30天起止时间, 不包括今天"""
now_s = pendulum.now().start_of('day')
now_e = pendulum.now().end_of('day')
start = now_s.add(days=-1 * 30).format("YYYY-MM-DD HH:mm:ss")
end = now_e.add(days=-1).format("YYYY-MM-DD HH:mm:ss")
start = now_s.add(days=-1 * 30).format(YMD_Hms)
end = now_e.add(days=-1).format(YMD_Hms)
return start, end
......@@ -257,8 +258,8 @@ def last7_day_range():
"""求最近7天起止时间, 不包括今天"""
now_s = pendulum.now().start_of('day')
now_e = pendulum.now().end_of('day')
start = now_s.add(days=-1 * 7).format("YYYY-MM-DD HH:mm:ss")
end = now_e.add(days=-1).format("YYYY-MM-DD HH:mm:ss")
start = now_s.add(days=-1 * 7).format(YMD_Hms)
end = now_e.add(days=-1).format(YMD_Hms)
return start, end
......@@ -266,8 +267,8 @@ def yesterday_range():
"""昨天起始时间"""
now_s = pendulum.now().start_of('day')
now_e = pendulum.now().end_of('day')
start = now_s.add(days=-1).format("YYYY-MM-DD HH:mm:ss")
end = now_e.add(days=-1).format("YYYY-MM-DD HH:mm:ss")
start = now_s.add(days=-1).format(YMD_Hms)
end = now_e.add(days=-1).format(YMD_Hms)
return start, end
......@@ -285,8 +286,8 @@ def last_n_day(date_str, days):
def last_15min_range():
"""求最近5分钟"""
now = pendulum.now()
start = now.add(minutes=-1 * 5).format("YYYY-MM-DD HH:mm:ss")
end = now.format("YYYY-MM-DD HH:mm:ss")
start = now.add(minutes=-1 * 5).format(YMD_Hms)
end = now.format(YMD_Hms)
return start, end
......@@ -295,21 +296,21 @@ def today_month_date():
# today_start, today_end, month_start, month_end
now = pendulum.now()
# 今天开始到结束时间
today_end = str(now.format("YYYY-MM-DD HH:mm:ss"))
today_start = str(now.start_of('day').format("YYYY-MM-DD HH:mm:ss"))
today_end = str(now.format(YMD_Hms))
today_start = str(now.start_of('day').format(YMD_Hms))
# 本月开始到结束时间
month_start = str(now.start_of('month').format("YYYY-MM-DD HH:mm:ss"))
month_end = str(now.format("YYYY-MM-DD HH:mm:ss"))
month_start = str(now.start_of('month').format(YMD_Hms))
month_end = str(now.format(YMD_Hms))
return today_start, today_end, month_start, month_end
def start_end_date():
"""获取今日、本月起始时间"""
now = pendulum.now()
today_start = str(now.start_of('day').format("YYYY-MM-DD HH:mm:ss"))
today_end = str(now.end_of('day').format("YYYY-MM-DD HH:mm:ss"))
month_start = str(now.start_of('month').format("YYYY-MM-DD HH:mm:ss"))
month_end = str(now.end_of('month').format("YYYY-MM-DD HH:mm:ss"))
today_start = str(now.start_of('day').format(YMD_Hms))
today_end = str(now.end_of('day').format(YMD_Hms))
month_start = str(now.start_of('month').format(YMD_Hms))
month_end = str(now.end_of('month').format(YMD_Hms))
return str(today_start), str(today_end), str(month_start), str(month_end)
......@@ -337,13 +338,13 @@ def srv_time():
def convert_es_str(str1: object) -> object:
"""str date转换为str es日期格式"""
es_date = my_pendulum.from_format(str1, 'YYYY-MM-DD HH:mm:ss')
es_date = my_pendulum.from_format(str1, YMD_Hms)
return str(es_date)
def end_now_str(str1: object) -> object:
"""str date转换为str es日期格式"""
end_date = my_pendulum.from_format(str1, 'YYYY-MM-DD HH:mm:ss')
end_date = my_pendulum.from_format(str1, YMD_Hms)
now_date = pendulum.now()
if end_date > now_date:
time_format = "%Y-%m-%dT%H:%M:%S+08:00"
......@@ -351,7 +352,7 @@ def end_now_str(str1: object) -> object:
return str(end_date)
def convert_to_es_str(str1, format="YYYY-MM-DD HH:mm:ss"):
def convert_to_es_str(str1, format=YMD_Hms):
"""str date转换为str es日期格式"""
es_date = my_pendulum.from_format(str1, format)
return str(es_date)
......@@ -361,8 +362,8 @@ def last_time_str(start, end, date_type, date_end=False):
"""年月日, 获取上一周期时间"""
if date_type not in ("day", "month", "year"):
return None
start_f = my_pendulum.from_format(start, 'YYYY-MM-DD HH:mm:ss')
end_f = my_pendulum.from_format(end, 'YYYY-MM-DD HH:mm:ss')
start_f = my_pendulum.from_format(start, YMD_Hms)
end_f = my_pendulum.from_format(end, YMD_Hms)
if date_type == "day":
start_last = start_f.subtract(days=1)
if date_end:
......@@ -381,8 +382,7 @@ def last_time_str(start, end, date_type, date_end=False):
end_last = start_last.end_of(unit=date_type)
else:
end_last = end_f.subtract(years=1)
return start_last.format("YYYY-MM-DD HH:mm:ss"), end_last.format(
"YYYY-MM-DD HH:mm:ss")
return start_last.format(YMD_Hms), end_last.format(YMD_Hms)
def esstr_to_dthoutstr(str1, format='%H:%M'):
......@@ -411,7 +411,7 @@ def get_datetime_str(timestamp):
'''
if not timestamp:
timestamp = time.time()
time_array = time.localtime(timestamp)
return time.strftime("%Y-%m-%d %H:%M:%S", time_array)
......@@ -499,19 +499,6 @@ def convert_number(number, divisor=10000, precision=2, convert_to_str=False):
return number
# def format_value(value, type="default", convert_to_str=True):
# if not type in DATA_FORMAT:
# type = "default"
# try:
# value = float(value)
# except Exception as e:
# return NO_DATA_EXPRESS, DATA_FORMAT[type]["unit"]
# value = convert_number(value, DATA_FORMAT[type]["precision"][0],
# DATA_FORMAT[type]["precision"][1],
# convert_to_str=convert_to_str)
# return value, DATA_FORMAT[type]["unit"]
def summary_number(number_list, summary_type="sum"):
"""
统计数值,
......@@ -726,7 +713,7 @@ def get_last_3month_start_end(date=None):
:param dt: datetime obj
:return:
"""
if not date:
dt = get_current_date_time()
else:
......@@ -788,7 +775,7 @@ def get_this_year_start_end(date=None):
:param dt: datetime obj
:return:
"""
if not date:
dt = get_current_date_time()
else:
......@@ -804,7 +791,7 @@ def get_last_year_start_end(date=None):
:param dt: datetime obj
:return:
"""
if not date:
dt = get_current_date_time()
else:
......@@ -820,7 +807,7 @@ def get_last_month_start_end(date=None):
:param dt: datetime obj
:return:
"""
if not date:
dt = get_current_date_time()
else:
......@@ -928,7 +915,7 @@ def get_current_start_n_end_dt(date_type, include_end_day=False):
start_year += 1
start_month = start_month - 12
start = datetime.datetime(start_year, start_month, 1)
elif date_type == "season":
season_number = int((current_dt.month - 1) / 3)
start = datetime.datetime(current_dt.year, season_number * 3 + 1, 1)
......@@ -1039,7 +1026,7 @@ def get_previous_slot(slots, date_type, compare_type=None):
return get_slots_between_date(
start - datetime.timedelta(days=days),
end - datetime.timedelta(days=days), date_type)
elif date_type == "hour":
hours = int((end - start).total_seconds() / 3600) + 1
return get_slots_between_date(start - datetime.timedelta(hours=hours),
......@@ -1087,8 +1074,8 @@ def get_slots_between_date(start, end, interval_type, growth_type=None):
while start <= end:
date_dts.append([start, start + datetime.timedelta(days=1)])
start += datetime.timedelta(days=1)
elif interval_type == "month":
start_months = start.year * 12 + start.month
end_months = end.year * 12 + end.month
......@@ -1200,26 +1187,8 @@ def deco_convert_date_to_dt(f):
if kwarg in kwargs and kwargs[kwarg]:
kwargs[kwarg] = convert_to_dt(kwargs[kwarg])
return f(*args, **kwargs)
return deco
# def get_start_n_end(start_date, cycle):
# """
#
# :param start_date: date string
# :param cycle: day, month or year
# :return: start dt obj and end dt obj
# """
# dt = convert_to_dt(start_date)
# if cycle == "day":
# days = 1
# elif cycle == "month":
# days = dt.timetuple().tm_mday
# elif cycle == "year":
# days = dt.timetuple().tm_yday
# datetime.timedelta
# return (dt, dt + datetime.timedelta(days=days))
return deco
# fake functions
......@@ -1416,9 +1385,9 @@ def return_page_datas(datas, page_num, page_record_num):
totals = len(datas)
start_num = (page_num - 1) * page_record_num + 1
end_num = page_num * page_record_num
if (end_num > totals):
if end_num > totals:
end_num = totals
if (start_num > totals):
if start_num > totals:
all_datas = []
else:
all_datas = datas[start_num - 1: end_num]
......@@ -1444,19 +1413,26 @@ def get_start_end_by_tz_time(time_str):
return start_str, end_str
def get_start_end_by_tz_time_new(time_str, from_format='YYYY-MM-DD',
to_format='YYYY-MM-DD HH:mm:ss'):
def get_start_end_by_tz_time_new(time_str, from_fmt=YMD_U, to_fmt=YMD_Hms):
"""
根据日期字符串获取当天起始时间
:param time_str:2022-08-03 17:53:53
:return:2022-08-03 00:00:00,2022-08-03 23:59:59
"""
date = my_pendulum.from_format(time_str, from_format)
start = date.start_of("day").format(to_format)
end = date.end_of("day").format(to_format)
date = my_pendulum.from_format(time_str, from_fmt)
start = date.start_of("day").format(to_fmt)
end = date.end_of("day").format(to_fmt)
return start, end
def get_day_start(dts, dt_fmt=None):
if dt_fmt:
dt = pendulum.from_format(str(dts), dt_fmt, tz=CST)
else:
dt = pendulum.parse(str(dts), tz=CST)
return dt.start_of("day")
def get_time_duration(start_time, end_time, is_timestamp=True,
is_need_trans=True):
"""
......@@ -1487,7 +1463,7 @@ def get_time_duration_by_str(duration_str):
days = int(duration_str / (60 * 60 * 24))
if days > 0:
return_str += "%s天" % str(days)
hours = int(duration_str % (60 * 60 * 24) / (60 * 60))
if hours > 0:
return_str += "%s时" % str(hours)
......@@ -1500,40 +1476,6 @@ def get_time_duration_by_str(duration_str):
return return_str
def time_pick_transf_new(start, end):
"""获取intervel和slots, 详细显示时间轴信息,新接口都使用这个来获取时间"""
start_f = my_pendulum.from_format(start, 'YYYY-MM-DD HH:mm:ss')
end_f = my_pendulum.from_format(end, 'YYYY-MM-DD HH:mm:ss')
diff = end_f.int_timestamp - start_f.int_timestamp
# 1. 计算intervel
# 1.1 区间48小时之内, 返回15min
if diff <= 48 * 3600:
intervel = 15 * 60
# 1.2 区间在60天以内, 返回1day
elif 48 * 3600 < diff <= 60 * 86400:
intervel = 86400
# 1.3 选择年, 返回1个月
else:
intervel = 30 * 86400
# 2. 计算slots
# 2.1 取到点的个数, 比如15min的96个点
slots = []
slot_num = round((end_f.int_timestamp - start_f.int_timestamp) / intervel)
for i in range(slot_num):
# 区间48小时之内
if diff <= 48 * 3600:
dt = start_f.add(minutes=15 * i).format("YYYY-MM-DD HH:mm")
dt_str = str(dt)
# 区间在60天以内
elif 48 * 3600 < diff <= 60 * 86400:
dt = start_f.add(days=1 * i).format("YYYY-MM-DD")
dt_str = str(dt)
else:
dt = start_f.add(months=1 * i).format("YYYY-MM")
dt_str = str(dt)
slots.append(dt_str)
return intervel, slots
def get_time_diff(start, end):
"""
获取时间段内的间隔
......@@ -1541,7 +1483,7 @@ def get_time_diff(start, end):
:param end:
:return:
"""
start_f = my_pendulum.from_format(start, 'YYYY-MM-DD HH:mm:ss')
end_f = my_pendulum.from_format(end, 'YYYY-MM-DD HH:mm:ss')
start_f = my_pendulum.from_format(start, YMD_Hms)
end_f = my_pendulum.from_format(end, YMD_Hms)
diff = end_f.int_timestamp - start_f.int_timestamp
return diff
\ No newline at end of file
return diff
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment