Bladeren bron

雨情分析详情接口

baoyubo 9 maanden geleden
bovenliggende
commit
f40334d955
3 gewijzigde bestanden met toevoegingen van 63 en 4 verwijderingen
  1. 3 3
      jobs/rainfall_conditions_job.py
  2. 4 1
      routers/api/__init__.py
  3. 56 0
      routers/api/rainfall/__init__.py

+ 3 - 3
jobs/rainfall_conditions_job.py

@@ -13,11 +13,11 @@ from config import settings
 
 
 
-def get_stcd_data(stcd=''):
+def get_stcd_data(stcd='',hours=24):
     # 获取当前时间
     now = datetime.now()
     # 计算 24 小时前的时间
-    twenty_four_hours_ago = now - timedelta(hours=24)
+    twenty_four_hours_ago = now - timedelta(hours=hours)
     formatted_now = now.strftime('%Y-%m-%d %H')
     formatted_twenty_four_hours_ago = twenty_four_hours_ago.strftime('%Y-%m-%d %H')
     url = 'https://19.15.75.180:8581/GatewayMsg/http/api/proxy/invoke'
@@ -73,7 +73,7 @@ def put_data(db=get_db_local()):
     rainfull = get_stcd_data()
     if len(rainfull)>0:
         now = datetime.now()
-        twenty_four_hours_ago = now - timedelta(hours=24)
+        twenty_four_hours_ago = now - timedelta(hours=25)
         db.query(GovdataRainDataInfo).filter(GovdataRainDataInfo.create_time <twenty_four_hours_ago ).delete()
         db.commit()
         for info in rainfull:

+ 4 - 1
routers/api/__init__.py

@@ -17,6 +17,7 @@ from . import eventManagement
 from . import spatialAnalysis
 from . import taskRegistration
 from . import pattern
+from . import rainfall
 
 router = APIRouter()
 
@@ -35,4 +36,6 @@ router.include_router(emergencyPlans.router, prefix="/emergency_plan")
 
 router.include_router(eventManagement.router, prefix="/event_management", tags=["事件管理"])
 router.include_router(spatialAnalysis.router, prefix="/spatial_analysis", tags=["空间分析"])
-router.include_router(pattern.router, prefix="/pattern", tags=["实施测绘"])
+router.include_router(pattern.router, prefix="/pattern", tags=["实时测绘"])
+
+router.include_router(rainfall.router, prefix="/rainfall", tags=["雨情分析"])

+ 56 - 0
routers/api/rainfall/__init__.py

@@ -0,0 +1,56 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+from fastapi import APIRouter, Request, Depends, Query, HTTPException, status
+from common.security import valid_access_token
+from sqlalchemy.orm import Session
+from sqlalchemy.sql import func
+from common.auth_user import *
+from sqlalchemy import  text
+from pydantic import BaseModel
+from common.BigDataCenterAPI import *
+from database import get_db
+from typing import List
+from models import *
+from utils import *
+from utils.spatial import *
+import json
+import traceback
+from jobs.rainfall_conditions_job import get_stcd_data
+from datetime import datetime,timedelta
+
+
+router = APIRouter()
+
+@router.get("/info/{code}")
+async def get_pattern_info(
+    code: str,
+    db: Session = Depends(get_db)
+):
+    rainfulldata = get_stcd_data(code,12)
+    update_time_list = []
+    rainfall_history = []
+    rainfall_future = []
+    cumulative_rainfall = []
+    for i in rainfulldata[::-1]:
+        area_name = i['F3070220000034_000018002']
+        update_time_list.append( datetime.strptime(i['F3070220000034_000018006'], "%Y-%m-%d %H:%M:%S"))
+        create_time = datetime.strptime(i['F3070220000034_000018004'], "%Y-%m-%d %H:%M:%S")
+        hour = create_time.strftime("%H")
+        value = i['F3070220000034_000018005']
+        rainfall_history.append({"hour":hour,"value":value})
+    update_time_max = max(update_time_list).strftime("%Y-%m-%d %H:%M:%S")
+    for t in range(1,13):
+        future_time = create_time+ timedelta(hours=t)
+        hour = future_time.strftime("%H")
+        value = 0
+        rainfall_future.append({"hour":hour,"value":value})
+
+    rainfall = 0
+    for cumulative in rainfall_history+rainfall_future:
+        rainfall += cumulative['value']
+        cumulative_rainfall.append({"hour":cumulative['hour'],"value":rainfall})
+
+    return {
+        "code": 200, "msg": "获取成功", "data":{"areaName":area_name,"updateTime":update_time_max,"rainfallHistory":rainfall_history,"rainfallFuture":rainfall_future,"cumulativeRainfall":cumulative_rainfall}
+    }