rain_condition.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from fastapi import APIRouter, Request, Depends, Query, HTTPException, status
  4. from common.security import valid_access_token
  5. from sqlalchemy.orm import Session
  6. from sqlalchemy.sql import func
  7. from common.auth_user import *
  8. from sqlalchemy import text
  9. from pydantic import BaseModel
  10. from common.BigDataCenterAPI import *
  11. from database import get_db
  12. from typing import List
  13. from utils import *
  14. from utils.risk import *
  15. import json
  16. import traceback
  17. router = APIRouter()
  18. @router.post('/get_township_rain')
  19. async def mine(request: Request,db: Session = Depends(get_db)):
  20. try:
  21. body = await request.json()
  22. rn = body.get('rn')
  23. result = get_rain_township(db,rn)
  24. #print(result)
  25. return {
  26. "code": 200,
  27. "msg": "成功",
  28. "data":
  29. result
  30. }
  31. except Exception as e:
  32. db.rollback()
  33. traceback.print_exc()
  34. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  35. @router.post('/township_rain_warrning')
  36. async def mine(request: Request,body = Depends(remove_xss_json),db: Session = Depends(get_db)):
  37. try:
  38. body = await request.json()
  39. rn = body.get('rn')
  40. one_hour = get_rain_township(db,1)
  41. three_hour = get_rain_township(db,3)
  42. max_rain = get_max_rain_township(db)
  43. # print(one_hour)
  44. # print(three_hour)
  45. one_hour_rain = 6
  46. one_hour_more_50 = 0
  47. three_hour_more_50 = 0
  48. max_time = get_rain_max_time(db)
  49. result = ''
  50. raining_township = 0
  51. for i in range(len(one_hour)):
  52. if one_hour[i]["value"] == 0:
  53. one_hour_rain-=1
  54. elif one_hour[i]["value"] != 0:
  55. raining_township+=1
  56. elif one_hour[i]['name'] in ['暴雨(50-100)','大暴雨(100-200)','特大暴雨(>200)']:
  57. one_hour_more_50+=one_hour[i]["value"]
  58. elif three_hour[i]['name'] in ['大暴雨(100-200)','特大暴雨(>200)']:
  59. three_hour_more_50+=one_hour[i]["value"]
  60. max_rain_township = max_rain[0]["township"]
  61. max_rain_value = max_rain[0]["rn"]
  62. # print(one_hour_rain)
  63. # print(one_hour_more_50)
  64. # print(three_hour_more_50)
  65. # print(max_rain_township)
  66. # print(max_rain_value)
  67. if one_hour_rain==0:
  68. result = '''{}至当前,当前无地区下雨 。
  69. 当前1小时降雨量大于50毫米的镇街 {}个;
  70. 当前3小时降雨量大于100毫米的镇街 {}个;
  71. '''.format(max_time,one_hour_more_50,three_hour_more_50)
  72. else:
  73. result = '''{}至当前,全市有 {}个镇街发生降雨, 最大降雨出现在 {},累计雨量为{}毫米 。
  74. 当前1小时降雨量大于50毫米的镇街 {}个;
  75. 当前3小时降雨量大于100毫米的镇街 {}个;
  76. '''.format(max_time,one_hour_rain,max_rain_township,max_rain_value,one_hour_more_50,three_hour_more_50)
  77. #print(result)
  78. return {
  79. "code": 200,
  80. "msg": "成功",
  81. "data":
  82. {"result":result}
  83. }
  84. except Exception as e:
  85. db.rollback()
  86. traceback.print_exc()
  87. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")