44import io
55import json
66import ast
7- import datetime
87from http import HTTPStatus
98import minio
109import valkey
4443
4544valkey_client = valkey .Valkey (host = config ["VKURL" ], port = config ["VKPORT" ], db = 0 )
4645
46+ SENSEBOX_FAIL_COUNT = 0
47+
4748
4849@app .route (rule = "/metrics" )
4950def metrics ():
@@ -56,21 +57,11 @@ def print_version():
5657 return jsonify ({"version" : __version__ }), 200
5758
5859
59- @app .route (rule = "/temperature" )
60- def temperature ():
61- v_value = valkey_client .get (name = "avg-temp" )
62- if v_value is not None :
63- v_value = json .loads (
64- s = json .dumps (
65- obj = ast .literal_eval (node_or_string = v_value .decode (encoding = "UTF-8" ))
66- )
67- )
68- if v_value .get ("status" ) == "Internal Error" :
69- return jsonify (v_value ), HTTPStatus .INTERNAL_SERVER_ERROR
70- else :
71- return jsonify (v_value ), HTTPStatus .OK
60+ def query_main_and_store ():
7261 api = OpenSenseMap (base_url = BASE_URL )
73- data , return_code = api .get_avg_temperature (sense_boxes = senseBoxes )
62+ data , return_code , fail_count = api .get_avg_temperature (sense_boxes = senseBoxes )
63+ global SENSEBOX_FAIL_COUNT
64+ SENSEBOX_FAIL_COUNT = fail_count
7465
7566 data_as_bytes = str (object = data ).encode (encoding = "UTF-8" )
7667 data_to_minio_stream = io .BytesIO (initial_bytes = data_as_bytes )
@@ -82,26 +73,54 @@ def temperature():
8273 )
8374
8475 valkey_client .set (name = "avg-temp" , value = str (object = data ), ex = 3000 )
76+ return jsonify (data ), return_code
77+
78+
79+ @app .route (rule = "/temperature" )
80+ def temperature ():
81+ v_value = valkey_client .get (name = "avg-temp" )
82+ if v_value is not None :
83+ v_value = json .loads (
84+ s = json .dumps (
85+ obj = ast .literal_eval (node_or_string = v_value .decode (encoding = "UTF-8" ))
86+ )
87+ )
88+ if v_value .get ("status" ) == "Internal Error" :
89+ return jsonify (v_value ), HTTPStatus .INTERNAL_SERVER_ERROR
90+ return jsonify (v_value ), HTTPStatus .OK
8591
8692 total_temp_requests .inc ()
87- return jsonify ( data ), return_code
93+ return query_main_and_store ()
8894
8995
9096@app .route (rule = "/store" )
9197def store ():
98+ query_main_and_store ()
9299 total_store_requests .inc ()
93100 return temperature ()
94101
95102
96103@app .route (rule = "/readyz" )
97104def readyz ():
98- # prometheus
99- pass
105+ most_sense_boxes_working = SENSEBOX_FAIL_COUNT < (len (senseBoxes ) // 2 ) + 1
106+ v_value = valkey_client .get (name = "avg-temp" )
107+ v_ttl = valkey_client .ttl (name = "avg-temp" )
108+
109+ if most_sense_boxes_working and v_value is not None and v_ttl > 1 :
110+ return str (SENSEBOX_FAIL_COUNT ), HTTPStatus .OK
111+ return str (SENSEBOX_FAIL_COUNT ), HTTPStatus .INTERNAL_SERVER_ERROR
112+
113+
114+ # Hacky way to populate cache, s3 and senseBox_fail_count
115+ @app .before_request
116+ def first_temp_query ():
117+ app .before_request_funcs [None ].remove (first_temp_query )
118+ temperature ()
100119
101120
102121if __name__ == "__main__" :
103- bucket_found = minio_client .bucket_exists (bucket_name = bucket_name )
104- if not bucket_found :
122+ BUCKET_FOUND = minio_client .bucket_exists (bucket_name = bucket_name )
123+ if not BUCKET_FOUND :
105124 minio_client .make_bucket (bucket_name = bucket_name )
106125
107126 from waitress import serve
0 commit comments