48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
import schedule
|
|
import time
|
|
import datetime
|
|
import shutil
|
|
import redis
|
|
import urllib.request
|
|
|
|
def queryallrecordsbydate(querydate: str, redis_client: redis.Redis):
|
|
cache_key = f"queryallrecordsbydate_{querydate}"
|
|
data = redis_client.get(cache_key)
|
|
|
|
if not data:
|
|
nodes_links: tuple = influxdb_api.query_all_records_by_date(query_date=querydate)
|
|
|
|
def queryallscadarecordsbydate(querydate: str, redis_client: redis.Redis):
|
|
cache_key = f"queryallscadarecordsbydate_{querydate}"
|
|
data = redis_client.get(cache_key)
|
|
|
|
if not data:
|
|
result_dict = influxdb_api.query_all_SCADA_records_by_date(query_date=querydate)
|
|
|
|
def auto_cache_data(redis_client: redis.Redis):
|
|
# auto cache data for the last 3 days
|
|
today = datetime.date.today()
|
|
for i in range(1, 4):
|
|
prev_day = today - datetime.timedelta(days=i)
|
|
str_prev_day = prev_day.strftime('%Y-%m-%d')
|
|
print(str_prev_day)
|
|
|
|
queryallrecordsbydate(str_prev_day, redis_client)
|
|
queryallscadarecordsbydate(str_prev_day, redis_client)
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# 初始化 Redis 连接
|
|
# 用redis 限制并发访u
|
|
redis_client = redis.Redis(host="localhost", port=6379, db=0)
|
|
|
|
auto_cache_data(redis_client)
|
|
|
|
# auto run in the midnight
|
|
schedule.every().day.at("03:00").do(auto_cache_data, redis_client)
|
|
|
|
while True:
|
|
schedule.run_pending()
|
|
time.sleep(1)
|
|
|