Skip to content

Commit 86e6d9b

Browse files
committed
last-update logging #538
1 parent 6cab519 commit 86e6d9b

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

src/server/api/API_ingest/ingest_sources_from_api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from api.API_ingest import shelterluv_people, salesforce_contacts, sl_animal_events
22
import structlog
3+
4+
from pipeline.log_db import log_shelterluv_update
35
logger = structlog.get_logger()
46

57
def start():
@@ -16,6 +18,7 @@ def start():
1618
logger.debug(" Fetching Shelterluv events")
1719
sle_count = sl_animal_events.store_all_animals_and_events()
1820
logger.debug(" Finished fetching Shelterluv events - %d records" , sle_count)
21+
log_shelterluv_update()
1922

2023
logger.debug("Finished fetching raw data from different API sources")
2124

src/server/api/admin_api.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,26 @@ def start_job():
231231
return job_id
232232

233233

234+
@admin_api.route("/api/get_last_runs", methods=["GET"])
235+
#@jwt_ops.admin_required
236+
def get_run_logs():
237+
""" Get the timestamps of the last update runs"""
238+
239+
with engine.connect() as connection:
240+
q = text("""select keycol,valcol from kv_unique where keycol like '%_update'; """)
241+
result = connection.execute(q)
242+
243+
if result.rowcount > 0:
244+
rows = result.fetchall()
245+
246+
row_list = []
247+
248+
for row in rows:
249+
row_dict = row._mapping
250+
row_list.append({row_dict['keycol'] : row_dict['valcol']})
251+
252+
return jsonify(row_list)
253+
234254

235255
def insert_rfm_scores(score_list):
236256
"""Take a list of (matching_id, score) and insert into the

src/server/api/file_uploader.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
from donations_importer import validate_import_sfd
33
from flask import current_app
44
from models import ManualMatches, SalesForceContacts, ShelterluvPeople, Volgistics
5+
from pipeline.log_db import log_volgistics_update
56
from volgistics_importer import open_volgistics, validate_import_vs, volgistics_people_import
67
from werkzeug.utils import secure_filename
7-
88
import structlog
99
logger = structlog.get_logger()
1010

@@ -35,6 +35,7 @@ def determine_upload_type(file, file_extension, conn):
3535
validate_import_vs(workbook)
3636
volgistics_people_import(workbook)
3737
workbook.close()
38+
log_volgistics_update()
3839
return
3940

4041
logger.error("Don't know how to process file: %s", file.filename)

src/server/pipeline/log_db.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
from sqlalchemy.sql import text
44
from flask import current_app
5+
import time
56

67
from sqlalchemy.dialects.postgresql import insert
78
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, exc, select
@@ -13,6 +14,8 @@
1314
metadata = MetaData()
1415

1516
ex_stat = Table("execution_status", metadata, autoload=True, autoload_with=engine)
17+
kvt = Table("kv_unique", metadata, autoload=True, autoload_with=engine)
18+
1619

1720
# Alembic version bfb1262d3195
1821

@@ -52,3 +55,70 @@ def log_exec_status(job_id: str, exec_stage: str, exec_status: str, job_details:
5255
logger.error(e)
5356

5457

58+
def log_volgistics_update():
59+
"""Log Volgistics data update"""
60+
61+
timestamp = datetime.now().ctime()
62+
63+
with engine.connect() as connection:
64+
ins_stmt = insert(kvt).values(
65+
keycol = 'last_volgistics_update',
66+
valcol = timestamp,
67+
)
68+
# If key already present in DB, do update instead
69+
upsert = ins_stmt.on_conflict_do_update(
70+
constraint='kv_unique_keycol_key',
71+
set_=dict(valcol=timestamp)
72+
)
73+
74+
try:
75+
connection.execute(upsert)
76+
except Exception as e:
77+
logger.error("Insert/Update failed on Volgistics stats")
78+
logger.error(e)
79+
80+
81+
def log_shelterluv_update():
82+
"""Log Shelterluv data update"""
83+
84+
timestamp = datetime.now().ctime()
85+
86+
with engine.connect() as connection:
87+
ins_stmt = insert(kvt).values(
88+
keycol = 'last_shelterluv_update',
89+
valcol = timestamp,
90+
)
91+
# If key already present in DB, do update instead
92+
upsert = ins_stmt.on_conflict_do_update(
93+
constraint='kv_unique_keycol_key',
94+
set_=dict(valcol=timestamp)
95+
)
96+
97+
try:
98+
connection.execute(upsert)
99+
except Exception as e:
100+
logger.error("Insert/Update failed on Shelterluv stats")
101+
logger.error(e)
102+
103+
104+
def log_salesforce_update():
105+
"""Log SalesForce data update"""
106+
107+
timestamp = datetime.now().ctime()
108+
109+
with engine.connect() as connection:
110+
ins_stmt = insert(kvt).values(
111+
keycol = 'last_salesforce_update',
112+
valcol = timestamp,
113+
)
114+
# If key already present in DB, do update instead
115+
upsert = ins_stmt.on_conflict_do_update(
116+
constraint='kv_unique_keycol_key',
117+
set_=dict(valcol=timestamp)
118+
)
119+
120+
try:
121+
connection.execute(upsert)
122+
except Exception as e:
123+
logger.error("Insert/Update failed on SalseForce stats")
124+
logger.error(e)

0 commit comments

Comments
 (0)