Update auto_cache
This commit is contained in:
113
auto_cache.py
113
auto_cache.py
@@ -5,96 +5,21 @@ import shutil
|
|||||||
import redis
|
import redis
|
||||||
import urllib.request
|
import urllib.request
|
||||||
|
|
||||||
def queryallrecordsbydate(date: str):
|
def queryallrecordsbydate(querydate: str, redis_client: redis.Redis):
|
||||||
|
cache_key = f"queryallrecordsbydate_{querydate}"
|
||||||
|
data = redis_client.get(cache_key)
|
||||||
|
|
||||||
print(f'queryallrecordsbydate: {date}')
|
if not data:
|
||||||
|
nodes_links: tuple = influxdb_api.query_all_records_by_date(query_date=querydate)
|
||||||
|
|
||||||
try:
|
def queryallscadarecordsbydate(querydate: str, redis_client: redis.Redis):
|
||||||
response = urllib.request.urlopen(f'http://localhost/queryallrecordsbydate/?querydate={date}')
|
cache_key = f"queryallscadarecordsbydate_{querydate}"
|
||||||
html = response.read().decode('utf-8')
|
data = redis_client.get(cache_key)
|
||||||
#print(html)
|
|
||||||
except urllib.error.URLError as e:
|
|
||||||
print("Error")
|
|
||||||
|
|
||||||
|
if not data:
|
||||||
|
result_dict = influxdb_api.query_all_SCADA_records_by_date(query_date=querydate)
|
||||||
|
|
||||||
def queryallrecordsbydatewithtype(date: str):
|
def auto_cache_data(redis_client: redis.Redis):
|
||||||
|
|
||||||
print(f'queryallrecordsbydatewithtype: {date}')
|
|
||||||
|
|
||||||
try:
|
|
||||||
response = urllib.request.urlopen(f'http://localhost/queryallrecordsbydatewithtype/?querydate={date}&querytype=node')
|
|
||||||
html = response.read().decode('utf-8')
|
|
||||||
#print(html)
|
|
||||||
|
|
||||||
response = urllib.request.urlopen(f'http://localhost/queryallrecordsbydatewithtype/?querydate={date}&querytype=link')
|
|
||||||
html = response.read().decode('utf-8')
|
|
||||||
#print(html)
|
|
||||||
except urllib.error.URLError as e:
|
|
||||||
print("Error")
|
|
||||||
|
|
||||||
def queryallrecordsbydateproperty(date: str):
|
|
||||||
|
|
||||||
print(f'queryallrecordsbydateproperty: {date}')
|
|
||||||
|
|
||||||
try:
|
|
||||||
|
|
||||||
node_properties = ['head', 'pressure', 'actualdemand', 'quality']
|
|
||||||
for property in node_properties:
|
|
||||||
response = urllib.request.urlopen(f'http://localhost/queryallrecordsbydateproperty/?querydate={date}&querytype=node&property={property}')
|
|
||||||
html = response.read().decode('utf-8')
|
|
||||||
#print(html)
|
|
||||||
|
|
||||||
link_properties = ['flow', 'velocity', 'headloss', 'quality']
|
|
||||||
for property in link_properties:
|
|
||||||
response = urllib.request.urlopen(f'http://localhost/queryallrecordsbydateproperty/?querydate={date}&querytype=link&property={property}')
|
|
||||||
html = response.read().decode('utf-8')
|
|
||||||
#print(html)
|
|
||||||
|
|
||||||
except urllib.error.URLError as e:
|
|
||||||
print("Error")
|
|
||||||
|
|
||||||
def queryallscadarecordsbydate(date: str):
|
|
||||||
|
|
||||||
print(f'queryallscadarecordsbydate: {date}')
|
|
||||||
|
|
||||||
try:
|
|
||||||
response = urllib.request.urlopen(f'http://localhost/queryallscadarecordsbydate/?querydate={date}')
|
|
||||||
html = response.read().decode('utf-8')
|
|
||||||
#print(html)
|
|
||||||
except urllib.error.URLError as e:
|
|
||||||
print("Error")
|
|
||||||
|
|
||||||
def queryallschemeallrecords(date: str):
|
|
||||||
|
|
||||||
print(f'queryallschemeallrecords: {date}')
|
|
||||||
|
|
||||||
try:
|
|
||||||
response = urllib.request.urlopen(f'http://localhost/queryallschemeallrecords/?schemetype=burst_Analysis&schemename=testSchema&querydate={date}')
|
|
||||||
html = response.read().decode('utf-8')
|
|
||||||
#print(html)
|
|
||||||
except urllib.error.URLError as e:
|
|
||||||
print("Error")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def auto_cache_data():
|
|
||||||
"""
|
|
||||||
redis_client = redis.Redis(host='localhost', port=6379, db=0)
|
|
||||||
|
|
||||||
items = ['queryallrecordsbydatewithtype_[DATE]_link']
|
|
||||||
|
|
||||||
today = datetime.date.today()
|
|
||||||
print(today)
|
|
||||||
for item in items:
|
|
||||||
prev_day = today - datetime.timedelta(days=1)
|
|
||||||
str_prev_day = prev_day.strftime('%Y-%m-%d')
|
|
||||||
print(str_prev_day)
|
|
||||||
key = item.replace('[DATE]', str_prev_day)
|
|
||||||
|
|
||||||
if not redis_client.exists(key):
|
|
||||||
query_all_records_by_date_with_type_link(str_prev_day)
|
|
||||||
"""
|
|
||||||
|
|
||||||
# auto cache data for the last 3 days
|
# auto cache data for the last 3 days
|
||||||
today = datetime.date.today()
|
today = datetime.date.today()
|
||||||
for i in range(1, 4):
|
for i in range(1, 4):
|
||||||
@@ -102,17 +27,19 @@ def auto_cache_data():
|
|||||||
str_prev_day = prev_day.strftime('%Y-%m-%d')
|
str_prev_day = prev_day.strftime('%Y-%m-%d')
|
||||||
print(str_prev_day)
|
print(str_prev_day)
|
||||||
|
|
||||||
queryallrecordsbydate(str_prev_day)
|
queryallrecordsbydate(str_prev_day, redis_client)
|
||||||
queryallrecordsbydatewithtype(str_prev_day)
|
queryallscadarecordsbydate(str_prev_day, redis_client)
|
||||||
queryallrecordsbydateproperty(str_prev_day)
|
|
||||||
queryallscadarecordsbydate(str_prev_day)
|
|
||||||
queryallschemeallrecords(str_prev_day)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
auto_cache_data()
|
|
||||||
|
# 初始化 Redis 连接
|
||||||
|
# 用redis 限制并发访u
|
||||||
|
redis_client = redis.Redis(host="localhost", port=6379, db=0)
|
||||||
|
|
||||||
|
auto_cache_data(redis_client)
|
||||||
|
|
||||||
# auto run in the midnight
|
# auto run in the midnight
|
||||||
schedule.every().day.at("03:00").do(auto_cache_data)
|
schedule.every().day.at("03:00").do(auto_cache_data, redis_client)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
schedule.run_pending()
|
schedule.run_pending()
|
||||||
|
|||||||
Reference in New Issue
Block a user