Skip to content

Commit 3bec35c

Browse files
committed
Quick & dirty write/read table (no UPSERT yet)
1 parent 5e1a43f commit 3bec35c

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/server/api/admin_api.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from datetime import datetime
55
import json
66
from sqlalchemy.sql import text
7+
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, exc, select
78
from pipeline import flow_script
89
from config import engine
910
from flask import request, redirect, jsonify, current_app, abort
@@ -16,11 +17,17 @@
1617

1718
ALLOWED_EXTENSIONS = {"csv", "xlsx"}
1819

20+
metadata = MetaData()
1921

2022
def __allowed_file(filename):
2123
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
2224

2325

26+
27+
kvt = Table("kv_unique", metadata, autoload=True, autoload_with=engine)
28+
29+
30+
2431
# file upload tutorial
2532
@admin_api.route("/api/file", methods=["POST"])
2633
def uploadCSV():
@@ -62,9 +69,16 @@ def execute():
6269

6370
last_execution_details = {"executionTime": current_time, "stats": statistics}
6471

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+
6882

6983
return jsonify(success=True)
7084

@@ -88,9 +102,17 @@ def get_statistics():
88102
@admin_api.route("/api/statistics", methods=["GET"])
89103
def list_statistics():
90104
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()
94116

95117
except (FileNotFoundError):
96118
current_app.logger.error("last_execution.json file was missing")
@@ -106,7 +128,7 @@ def list_statistics():
106128
current_app.logger.error("Failure reading last_execution.json: ", e)
107129
return abort(500)
108130

109-
return jsonify(last_execution_details)
131+
return last_execution_details
110132

111133

112134
"""

0 commit comments

Comments
 (0)