Commit 2864cf8a authored by ZZH's avatar ZZH

fix homepage2023-6-14

parent 7413f4dc
import pendulum
from datetime import timedelta
import math
from pot_libs.es_util.es_utils import EsUtil from pot_libs.es_util.es_utils import EsUtil
from pot_libs.logger import log from pot_libs.logger import log
from unify_api.constants import POINT_1MIN_EVENT from unify_api.constants import POINT_1MIN_EVENT
from unify_api.utils.time_format import convert_es_str from unify_api.utils.time_format import convert_es_str, CST
from pot_libs.mysql_util.mysql_util import MysqlUtil from pot_libs.mysql_util.mysql_util import MysqlUtil
index = POINT_1MIN_EVENT index = POINT_1MIN_EVENT
...@@ -112,7 +115,47 @@ async def get_md_space(inline_ids, last_month_str): ...@@ -112,7 +115,47 @@ async def get_md_space(inline_ids, last_month_str):
async def get_tc_runtime(inline_ids): async def get_tc_runtime(inline_ids):
sql = "SELECT inlid, name, tc_runtime FROM `inline` where inlid in %s;" sql = "SELECT inlid, name, tc_runtime FROM `inline` where inlid in %s;"
async with MysqlUtil() as conn: async with MysqlUtil() as conn:
tc_runtimes = await conn.fetchall(sql, args=(inline_ids, )) tc_runtimes = await conn.fetchall(sql, args=(inline_ids,))
return tc_runtimes return tc_runtimes
async def compy_real_pf(cid):
sql = "select pid, inlid from point where cid=%s and add_to_company=1"
async with MysqlUtil() as conn:
pids = [r["pid"] for r in await conn.fetchall(sql, (cid,))]
if not pids:
return ""
dt = pendulum.now(tz=CST)
tstamp = dt.int_timestamp // (15 * 60) * (15 * 60)
dt = pendulum.from_timestamp(tstamp, tz=CST)
end_dt = (dt - timedelta(minutes=15)).strftime("%Y-%m-%d %H:%M:%S")
str_dt = dt.strftime("%Y-%m-%d %H:%M:%S")
sql = f"SELECT pid, create_time, pttl_mean, qttl_mean FROM " \
f"`point_15min_electric` where create_time in " \
f"('{str_dt}', '{end_dt}') and pid in %s;"
total_pttl, total_qttl = 0, 0
for r in await conn.fetchall(sql, (tuple(pids),)):
total_pttl += r["pttl_mean"]
total_qttl += r["qttl_mean"]
std_cos = math.sqrt(total_pttl * total_pttl + total_qttl * total_qttl)
return round(total_pttl / std_cos, 2) if std_cos else ""
async def compy_lst_month_pf(cid):
sql = "SELECT inlid FROM inline WHERE cid=%s;"
async with MysqlUtil() as conn:
inlids = [r["inlid"] for r in await conn.fetchall(sql, (cid,))]
if not inlids:
return ""
now_dt = pendulum.now(tz=CST)
cal_month = now_dt.subtract(months=1).format("YYYY-MM-DD")
sql = "SELECT cos FROM algo_power_factor_result " \
"WHERE inlid in %s and month=%s"
cos_lst = [r["cos"] for r in
await conn.fetchall(sql, (inlids, cal_month))]
return min(cos_lst) if cos_lst else ""
import json import json
import time import time
from datetime import datetime, timedelta from datetime import datetime, timedelta
from math import sqrt
import pendulum import pendulum
from pot_libs.settings import SETTING from pot_libs.settings import SETTING
...@@ -28,7 +27,7 @@ from unify_api.modules.home_page.components.count_info_cps import ( ...@@ -28,7 +27,7 @@ from unify_api.modules.home_page.components.count_info_cps import (
from unify_api.utils.time_format import last30_day_range from unify_api.utils.time_format import last30_day_range
from unify_api.modules.home_page.dao.count_info_dao import ( from unify_api.modules.home_page.dao.count_info_dao import (
get_inline_by_cid, get_power_factor_kpi, get_pcvf_kpi, get_economic_kpi, get_inline_by_cid, get_power_factor_kpi, get_pcvf_kpi, get_economic_kpi,
get_md_space, get_tc_runtime get_md_space, get_tc_runtime, compy_real_pf, compy_lst_month_pf
) )
from unify_api.modules.electric_optimization.dao.power_index import ( from unify_api.modules.electric_optimization.dao.power_index import (
price_policy_by_cid price_policy_by_cid
...@@ -330,63 +329,11 @@ async def power_charge_price(cid): ...@@ -330,63 +329,11 @@ async def power_charge_price(cid):
async def cal_power_factor(cid): async def cal_power_factor(cid):
"""首页获取实时功率因数, 上月功率因数""" """首页获取实时功率因数, 上月功率因数"""
point_sql = "select pid,inlid from point where cid=%s and add_to_company=1" real_cos = await compy_real_pf(cid)
async with MysqlUtil() as conn: lst_month_cos = await compy_lst_month_pf(cid)
points = await conn.fetchall(point_sql, args=(cid,)) if lst_month_cos:
point_ids = [i["pid"] for i in points] lst_month_cos = round(lst_month_cos, 2)
now = datetime.now() return real_cos, lst_month_cos
if now.month == 1:
last_month_dt = datetime(year=now.year - 1, month=12, day=1)
else:
last_month_dt = datetime(year=now.year, month=now.month - 1, day=1)
# 首页功率因数取所有进线中最小的
inline_sql = "SELECT inlid, `name` FROM inline WHERE cid=%s"
async with MysqlUtil() as conn:
inlines = await conn.fetchall(inline_sql, args=(cid,))
inline_ids = [inline["inlid"] for inline in inlines]
power_factor_results = []
sql = "SELECT inlid, save_charge pf_cost, `kpi_x`, `save_charge` " \
"FROM algo_power_factor_result WHERE inlid in %s and month=%s"
if inline_ids:
power_factor_results = await conn.fetchall(sql, args=(
inline_ids, last_month_dt))
pf_kpi_x_list = [
i["kpi_x"] for i in power_factor_results if
type(i["kpi_x"]) in [int, float]
]
last_month_cos = min(pf_kpi_x_list) if len(pf_kpi_x_list) else ""
dt = pendulum.now(tz="Asia/Shanghai")
tstamp = dt.int_timestamp // (15 * 60) * (15 * 60)
dt = pendulum.from_timestamp(tstamp, tz="Asia/Shanghai")
end_dt = (dt - timedelta(minutes=15)).strftime("%Y-%m-%d %H:%M:%S")
str_dt = dt.strftime("%Y-%m-%d %H:%M:%S")
electric_sql = f"SELECT pid,create_time,pttl_mean,qttl_mean FROM " \
f"`point_15min_electric` where create_time in " \
f"('{str_dt}', '{end_dt}') and pid in %s"
results = await conn.fetchall(electric_sql, args=(tuple(point_ids),))
point_map = {}
for res in results:
point_map.setdefault(res["pid"], []).append(
{
"quarter_time": str(res["create_time"]),
"pttl_mean": res["pttl_mean"],
"qttl_mean": res["qttl_mean"],
}
)
total_pttl, total_qttl = 0, 0
for point_id, records in point_map.items():
total_pttl += records[0]["pttl_mean"]
total_qttl += records[0]["qttl_mean"]
# 计算实时功率的公式
cos_ttl = ""
l = sqrt(total_pttl * total_pttl + total_qttl * total_qttl)
if l:
cos_ttl = round(total_pttl / l, 2)
if type(last_month_cos) in [int, float]:
last_month_cos = round(last_month_cos, 2)
return cos_ttl, last_month_cos
async def optimization_count_info(company_id: int): async def optimization_count_info(company_id: int):
......
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