feat(enm): 新增瓮福数据API(采集数据查询+测点列表分页)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
zty 2026-07-21 04:20:22 -04:00
parent cee94f9f4a
commit cf2800daa0
2 changed files with 133 additions and 2 deletions

View File

@ -1,7 +1,8 @@
from django.urls import path, include from django.urls import path, include
from rest_framework.routers import DefaultRouter from rest_framework.routers import DefaultRouter
from apps.enm.views import (MpointViewSet, MpointStatViewSet, from apps.enm.views import (MpointViewSet, MpointStatViewSet,
EnStatViewSet, EnStat2ViewSet, XscriptViewSet, MpLogxAPIView) EnStatViewSet, EnStat2ViewSet, XscriptViewSet, MpLogxAPIView,
WengfuMpLogxAPIView, WengfuMpointListAPIView)
API_BASE_URL = 'api/enm/' API_BASE_URL = 'api/enm/'
HTML_BASE_URL = 'dhtml/enm/' HTML_BASE_URL = 'dhtml/enm/'
@ -16,4 +17,6 @@ router.register('xscript', XscriptViewSet, basename='xscript')
urlpatterns = [ urlpatterns = [
path(API_BASE_URL, include(router.urls)), path(API_BASE_URL, include(router.urls)),
path(f'{API_BASE_URL}mplogx/', MpLogxAPIView.as_view(), name='mplogx_list'), path(f'{API_BASE_URL}mplogx/', MpLogxAPIView.as_view(), name='mplogx_list'),
path(f'{API_BASE_URL}wengfu_mplogx/', WengfuMpLogxAPIView.as_view(), name='wengfu_mplogx'),
path(f'{API_BASE_URL}wengfu_mpoints/', WengfuMpointListAPIView.as_view(), name='wengfu_mpoints'),
] ]

View File

@ -265,6 +265,134 @@ class MpLogxAPIView(APIView):
class WengfuMpLogxAPIView(APIView):
perms_map = {"get": "*", "post": "*"}
def _get_conn(self):
from apps.utils.sql import DbConnection
return DbConnection(
host='10.0.11.52', user='postgres',
password='zcDsj@2024', database='hfnf', dbtype='pg'
)
def get(self, request, *args, **kwargs):
mpoint_ids = request.query_params.get("mpoint_ids", None)
timex_gte = request.query_params.get("timex__gte", None)
timex_lte = request.query_params.get("timex__lte", None)
page = int(request.query_params.get("page", 1))
page_size = int(request.query_params.get("page_size", 20))
conditions = []
params = []
if mpoint_ids:
ids = [x.strip() for x in mpoint_ids.split(",") if x.strip()]
placeholders = ",".join(["%s"] * len(ids))
conditions.append(f"m.mpoint_id IN ({placeholders})")
params.extend(ids)
if timex_gte:
conditions.append("m.timex >= %s")
params.append(timex_gte)
if timex_lte:
conditions.append("m.timex <= %s")
params.append(timex_lte)
where = ("WHERE " + " AND ".join(conditions)) if conditions else ""
with self._get_conn() as cur:
count_sql = f"""SELECT COUNT(*) FROM mplogx_wengfu m {where}"""
cur.execute(count_sql, params)
total = cur.fetchone()[0]
data_sql = f"""
SELECT m.mpoint_id, n.point_name AS mpoint_name, m.timex,
m.val_float AS val, m.val_bool
FROM mplogx_wengfu m
LEFT JOIN cj_mpoint_name n ON n.mpoint_id = m.mpoint_id AND n.company = '瓮福'
{where}
ORDER BY m.timex DESC
LIMIT %s OFFSET %s
"""
cur.execute(data_sql, params + [page_size, (page - 1) * page_size])
columns = [desc[0] for desc in cur.description]
results = []
for row in cur.fetchall():
d = dict(zip(columns, row))
if d.get("timex"):
d["timex"] = d["timex"].strftime("%Y-%m-%d %H:%M:%S")
results.append(d)
return Response({"count": total, "results": results})
def post(self, request, *args, **kwargs):
"""导出或图表数据(不分页)"""
mpoint_ids = request.data.get("mpoint_ids", [])
timex_gte = request.data.get("timex__gte")
timex_lte = request.data.get("timex__lte")
if not mpoint_ids or not timex_gte or not timex_lte:
raise ParseError("mpoint_ids, timex__gte, timex__lte are required")
conditions = []
params = []
placeholders = ",".join(["%s"] * len(mpoint_ids))
conditions.append(f"m.mpoint_id IN ({placeholders})")
params.extend(mpoint_ids)
conditions.append("m.timex >= %s")
params.append(timex_gte)
conditions.append("m.timex <= %s")
params.append(timex_lte)
where = "WHERE " + " AND ".join(conditions)
with self._get_conn() as cur:
sql = f"""
SELECT m.mpoint_id, n.point_name AS mpoint_name, m.timex, m.val_float AS val, m.val_bool
FROM mplogx_wengfu m
LEFT JOIN cj_mpoint_name n ON n.mpoint_id = m.mpoint_id AND n.company = '瓮福'
{where}
ORDER BY m.timex ASC
"""
cur.execute(sql, params)
columns = [desc[0] for desc in cur.description]
results = []
for row in cur.fetchall():
d = dict(zip(columns, row))
if d.get("timex"):
d["timex"] = d["timex"].strftime("%Y-%m-%d %H:%M:%S")
results.append(d)
return Response(results)
class WengfuMpointListAPIView(APIView):
"""瓮福测点列表(供下拉选择)"""
perms_map = {"get": "*"}
def get(self, request, *args, **kwargs):
keyword = request.query_params.get("keyword", "")
page = int(request.query_params.get("page", 1))
page_size = int(request.query_params.get("page_size", 50))
from apps.utils.sql import DbConnection
with DbConnection(host='10.0.11.52', user='postgres',
password='zcDsj@2024', database='hfnf', dbtype='pg') as cur:
conditions = ["company = '瓮福'"]
params = []
if keyword:
conditions.append("(mpoint_id ILIKE %s OR point_name ILIKE %s)")
params.extend([f"%{keyword}%", f"%{keyword}%"])
where = "WHERE " + " AND ".join(conditions)
cur.execute(f"SELECT COUNT(*) FROM cj_mpoint_name {where}", params)
total = cur.fetchone()[0]
cur.execute(f"""
SELECT mpoint_id, point_name FROM cj_mpoint_name
{where} ORDER BY mpoint_id LIMIT %s OFFSET %s
""", params + [page_size, (page - 1) * page_size])
columns = [desc[0] for desc in cur.description]
results = [dict(zip(columns, row)) for row in cur.fetchall()]
return Response({"count": total, "results": results})
class MpLogxViewSet(CustomListModelMixin, CustomGenericViewSet): class MpLogxViewSet(CustomListModelMixin, CustomGenericViewSet):
""" """
list: 测点采集数据 list: 测点采集数据