rainfall_util.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 fastapi.responses import JSONResponse
  6. from sqlalchemy.orm import Session
  7. from sqlalchemy import and_, or_,text
  8. from sqlalchemy.sql import func
  9. from sqlalchemy.future import select
  10. from common.auth_user import *
  11. from pydantic import BaseModel
  12. from database import get_db
  13. from typing import List
  14. from models import *
  15. from utils import *
  16. from utils.ry_system_util import *
  17. from utils.video_util import *
  18. from collections import defaultdict
  19. import traceback
  20. from concurrent.futures import ThreadPoolExecutor, as_completed
  21. from jobs.rainfall_conditions_job import get_stcd_data
  22. from multiprocessing import Pool, cpu_count
  23. import json
  24. import time
  25. import math
  26. def get_real_code(db,longitude,latitude):
  27. sql = text("""
  28. SELECT area_name, `code`, longitude, latitude, distance FROM (
  29. SELECT
  30. area_name,
  31. `code`,
  32. longitude,
  33. latitude,
  34. ST_Distance_Sphere(
  35. ST_GeomFromText(CONCAT('POINT(', longitude, ' ', latitude, ')')),
  36. ST_GeomFromText(:point)
  37. ) AS distance
  38. FROM govdata_real_time_address
  39. WHERE longitude IS NOT NULL AND latitude IS NOT NULL
  40. ORDER BY distance ASC
  41. LIMIT 1
  42. ) T
  43. """).bindparams(point=f"POINT({longitude} {latitude})")
  44. # 执行查询
  45. result = db.execute(sql).fetchone()
  46. # 处理结果
  47. if result:
  48. return dict(result)['code']
  49. else:
  50. return None
  51. # def get_rainfall(
  52. # code: str,num:int,
  53. # db: Session = Depends(get_db)
  54. # ):
  55. # value=0
  56. # rainfulldata = get_stcd_data(code,num+1)
  57. # for i in rainfulldata:
  58. # value += i['F3070220000034_000018005']
  59. #
  60. # return value
  61. def get_rainfall(
  62. code: str,num:int=24,
  63. db: Session = Depends(get_db)
  64. ):
  65. value=0
  66. now = datetime.now()
  67. twenty_four_hours_ago = now - timedelta(hours=num+1)
  68. formatted_now = f"{now.strftime('%Y-%m-%d %H')}:00:00"
  69. formatted_twenty_four_hours_ago = f"{twenty_four_hours_ago.strftime('%Y-%m-%d %H')}:00:00"
  70. sql = text("""SELECT SUM(rainfall) as rainfall FROM sharedb.`govdata_rain_data_info` where `code`=:code and create_time BETWEEN :formatted_twenty_four_hours_ago and :formatted_now""").bindparams(code=code,formatted_twenty_four_hours_ago=formatted_twenty_four_hours_ago,formatted_now=formatted_now)
  71. # 执行查询
  72. # print(sql,formatted_now,formatted_twenty_four_hours_ago)
  73. result = db.execute(sql).fetchone()
  74. # 处理结果
  75. if result:
  76. return dict(result)['rainfall']
  77. else:
  78. return 0
  79. # rainfulldata = get_stcd_data(code,num+1)
  80. # for i in rainfulldata:
  81. # value += i['F3070220000034_000018005']
  82. #
  83. # return value
  84. def get_weather_warning(area_name : str,db: Session = Depends(get_db)):
  85. area_name= area_name.replace('区','').replace('市','').replace('技术开发区','').replace('新区','')
  86. sql = text(
  87. """SELECT CONCAT(`type`,`state`) as `weather_warning_type`,levelnum as `weather_warninglevel` FROM sharedb.weather_latest_warnings where county = :county or county='茂名' ORDER BY `levelnum` desc LIMIT 1""").bindparams(
  88. county=area_name)
  89. # 执行查询
  90. # print(sql,formatted_now,formatted_twenty_four_hours_ago)
  91. result = db.execute(sql).fetchone()
  92. # 处理结果
  93. if result:
  94. return dict(result)['weather_warning_type'],dict(result)['weather_warninglevel']
  95. else:
  96. return '-','-'