4
4
from datetime import datetime
5
5
import json
6
6
from sqlalchemy .sql import text
7
+ from sqlalchemy import Table , Column , Integer , String , MetaData , ForeignKey , exc , select
7
8
from pipeline import flow_script
8
9
from config import engine
9
10
from flask import request , redirect , jsonify , current_app , abort
16
17
17
18
ALLOWED_EXTENSIONS = {"csv" , "xlsx" }
18
19
20
+ metadata = MetaData ()
19
21
20
22
def __allowed_file (filename ):
21
23
return "." in filename and filename .rsplit ("." , 1 )[1 ].lower () in ALLOWED_EXTENSIONS
22
24
23
25
26
+
27
+ kvt = Table ("kv_unique" , metadata , autoload = True , autoload_with = engine )
28
+
29
+
30
+
24
31
# file upload tutorial
25
32
@admin_api .route ("/api/file" , methods = ["POST" ])
26
33
def uploadCSV ():
@@ -62,9 +69,16 @@ def execute():
62
69
63
70
last_execution_details = {"executionTime" : current_time , "stats" : statistics }
64
71
65
- last_execution_file = open (LOGS_PATH + "last_execution.json" , "w" )
66
- last_execution_file .write (json .dumps (last_execution_details ))
67
- last_execution_file .close ()
72
+ last_ex_json = (json .dumps (last_execution_details ))
73
+
74
+ with engine .connect () as connection :
75
+ ins_stmt = kvt .insert ().values (
76
+ keycol = 'last_execution_time' ,
77
+ valcol = last_ex_json ,
78
+ )
79
+
80
+ connection .execute (ins_stmt )
81
+
68
82
69
83
return jsonify (success = True )
70
84
@@ -88,9 +102,17 @@ def get_statistics():
88
102
@admin_api .route ("/api/statistics" , methods = ["GET" ])
89
103
def list_statistics ():
90
104
try :
91
- last_execution_file = open (LOGS_PATH + "last_execution.json" , "r" )
92
- last_execution_details = json .loads (last_execution_file .read ())
93
- last_execution_file .close ()
105
+
106
+
107
+ with engine .connect () as connection :
108
+ s = text ("select valcol from kv_unique where keycol = 'last_execution_time';" )
109
+ result = connection .execute (s )
110
+ last_execution_details = result .fetchone ()[0 ]
111
+
112
+
113
+ # last_execution_file = open(LOGS_PATH + "last_execution.json", "r")
114
+ # last_execution_details = json.loads(last_execution_file.read())
115
+ # last_execution_file.close()
94
116
95
117
except (FileNotFoundError ):
96
118
current_app .logger .error ("last_execution.json file was missing" )
@@ -106,7 +128,7 @@ def list_statistics():
106
128
current_app .logger .error ("Failure reading last_execution.json: " , e )
107
129
return abort (500 )
108
130
109
- return jsonify ( last_execution_details )
131
+ return last_execution_details
110
132
111
133
112
134
"""
0 commit comments