24
24
def __allowed_file (filename ):
25
25
return "." in filename and filename .rsplit ("." , 1 )[1 ].lower () in ALLOWED_EXTENSIONS
26
26
27
-
28
-
29
27
kvt = Table ("kv_unique" , metadata , autoload = True , autoload_with = engine )
30
28
31
29
@@ -70,27 +68,31 @@ def execute():
70
68
statistics = get_statistics ()
71
69
72
70
last_execution_details = {"executionTime" : current_time , "stats" : statistics }
73
-
74
71
last_ex_json = (json .dumps (last_execution_details ))
75
72
73
+ # Write Last Execution stats to DB
76
74
with engine .connect () as connection :
77
75
ins_stmt = insert (kvt ).values (
78
76
keycol = 'last_execution_time' ,
79
77
valcol = last_ex_json ,
80
78
)
81
-
79
+ # If key already present in DB, do update instead
82
80
upsert = ins_stmt .on_conflict_do_update (
83
81
constraint = 'kv_unique_keycol_key' ,
84
82
set_ = dict (valcol = last_ex_json )
85
- )
86
-
87
- connection .execute (upsert )
83
+ )
88
84
85
+ try :
86
+ connection .execute (upsert )
87
+ except Exception as e :
88
+ current_app .logger .error ("Insert/Update failed on Last Execution stats" )
89
+ current_app .logger .exception (e )
89
90
90
91
return jsonify (success = True )
91
92
92
93
93
94
def get_statistics ():
95
+ """ Write Last Execution stats to DB. """
94
96
with engine .connect () as connection :
95
97
query_matches = text ("SELECT count(*) FROM (SELECT distinct matching_id from pdp_contacts) as a;" )
96
98
query_total_count = text ("SELECT count(*) FROM pdp_contacts;" )
@@ -108,32 +110,20 @@ def get_statistics():
108
110
109
111
@admin_api .route ("/api/statistics" , methods = ["GET" ])
110
112
def list_statistics ():
111
- try :
113
+ """ Pull Last Execution stats from DB. """
112
114
115
+ last_execution_details = '{}' # Empty but valid JSON
113
116
117
+ try :
114
118
with engine .connect () as connection :
115
119
s = text ("select valcol from kv_unique where keycol = 'last_execution_time';" )
116
120
result = connection .execute (s )
117
121
last_execution_details = result .fetchone ()[0 ]
118
122
119
123
120
- # last_execution_file = open(LOGS_PATH + "last_execution.json", "r")
121
- # last_execution_details = json.loads(last_execution_file.read())
122
- # last_execution_file.close()
123
-
124
- except (FileNotFoundError ):
125
- current_app .logger .error ("last_execution.json file was missing" )
126
- return abort (500 )
127
-
128
- except (json .JSONDecodeError ):
129
- current_app .logger .error (
130
- "last_execution.json could not be decoded - possible corruption"
131
- )
132
- return abort (500 )
133
-
134
124
except Exception as e :
135
- current_app .logger .error ("Failure reading last_execution.json: " , e )
136
- return abort (500 )
125
+ current_app .logger .error ("Failure reading Last Execution stats from DB" )
126
+ # return abort(500) # Weird but not worth a 500
137
127
138
128
return last_execution_details
139
129
0 commit comments