Skip to content

Commit bf0bf4e

Browse files
committed
fixed missing positional arguments in logging statement, added support for reusing the same session when storing all animals and events, restored various logging statements in ingest_sources_from_api.py
1 parent 7c63494 commit bf0bf4e

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
from api.API_ingest import shelterluv_people, salesforce_contacts
1+
from api.API_ingest import shelterluv_people, salesforce_contacts, sl_animal_events
22
import structlog
33
logger = structlog.get_logger()
44

55
def start(session):
66
logger.debug("Start Fetching raw data from different API sources")
7-
#Run each source to store the output in dropbox and in the container as a CSV
8-
shelterluv_people.store_shelterluv_people_all(session)
7+
logger.debug(" Fetching Salesforce contacts")
98
salesforce_contacts.store_contacts_all(session)
10-
logger.debug("Finish Fetching raw data from different API sources")
11-
slp_count = shelterluv_api_handler.store_shelterluv_people_all(conn)
12-
logger.debug(" Finished fetching Shelterluv people - %d records" , slp_count)
13-
9+
logger.debug(" Finished fetching Salesforce contacts")
10+
logger.debug(" Fetching Shelterluv people")
11+
shelterluv_people.store_shelterluv_people_all(session)
12+
logger.debug(" Finished fetching Shelterluv people")
1413
logger.debug(" Fetching Shelterluv events")
15-
#Run each source to store the output in dropbox and in the container as a CSV
16-
sle_count = sl_animal_events.slae_test()
14+
sle_count = sl_animal_events.store_all_animals_and_events(session)
1715
logger.debug(" Finished fetching Shelterluv events - %d records" , sle_count)
1816

1917
logger.debug("Finished fetching raw data from different API sources")
2018

21-
22-
#TODO: Return object with count for each data source?

src/server/api/API_ingest/shelterluv_db.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
from api.file_uploader import validate_and_arrange_upload
1515
from sqlalchemy.orm import Session, sessionmaker
1616

17+
import structlog
18+
logger = structlog.get_logger()
19+
1720

1821
def insert_animals(animal_list):
1922
"""Insert animal records into shelterluv_animals table and return row count. """
@@ -65,31 +68,28 @@ def truncate_animals():
6568
return 0
6669

6770

68-
def truncate_events():
71+
def truncate_events(session):
6972
"""Truncate the shelterluv_events table"""
7073

71-
Session = sessionmaker(engine)
72-
session = Session()
7374
metadata = MetaData()
7475
sla = Table("sl_animal_events", metadata, autoload=True, autoload_with=engine)
7576

7677
truncate = "TRUNCATE table sl_animal_events;"
7778
result = session.execute(truncate)
7879

79-
session.commit() # Commit all inserted rows
80-
session.close()
81-
8280
return 0
8381

84-
8582
def insert_events(event_list):
83+
Session = sessionmaker(engine)
84+
session = Session()
85+
insert_events(session,event_list)
86+
87+
def insert_events(session, event_list):
8688
"""Insert event records into sl_animal_events table and return row count. """
8789

8890
# Always a clean insert
89-
truncate_events()
91+
truncate_events(session)
9092

91-
Session = sessionmaker(engine)
92-
session = Session()
9393
metadata = MetaData()
9494
sla = Table("sl_animal_events", metadata, autoload=True, autoload_with=engine)
9595

@@ -141,9 +141,7 @@ def insert_events(event_list):
141141

142142
# TODO: Wrap with try/catch
143143
ret = session.execute(sla.insert(ins_list))
144-
145-
session.commit() # Commit all inserted rows
146-
session.close()
144+
logger.debug("finished inserting events")
147145

148146
return ret.rowcount
149147

src/server/api/API_ingest/sl_animal_events.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import requests
88

99
from api.API_ingest import shelterluv_db
10-
from server.api.API_ingest.shelterluv_db import insert_animals
1110

1211
# There are a number of different record types. These are the ones we care about.
1312
keep_record_types = [
@@ -143,7 +142,7 @@ def get_events_bulk():
143142
more_records = decoded["has_more"] # if so, we'll make another pass
144143
offset += limit
145144
if offset % 1000 == 0:
146-
logger.debug("Reading offset ", str(offset))
145+
logger.debug("Reading offset %s", str(offset))
147146
if TEST_MODE and offset > 1000:
148147
more_records=False # Break out early
149148

@@ -155,17 +154,30 @@ def get_events_bulk():
155154

156155
def slae_test():
157156
total_count = get_event_count()
158-
logger.debug("Total events:", total_count)
157+
logger.debug("Total events: %d", total_count)
159158

160159
b = get_events_bulk()
161-
logger.debug("Strored records:", len(b))
160+
logger.debug("Stored records: %d", len(b))
162161

163162
# f = filter_events(b)
164163
# print(f)
165164

166165
count = shelterluv_db.insert_events(b)
167166
return count
168167

168+
def store_all_animals_and_events(session):
169+
total_count = get_event_count()
170+
logger.debug("Total events: %d", total_count)
171+
172+
b = get_events_bulk()
173+
logger.debug("Stored records: %d", len(b))
174+
175+
# f = filter_events(b)
176+
# print(f)
177+
178+
count = shelterluv_db.insert_events(session, b)
179+
return count
180+
169181

170182
# Query to get last adopt/foster event:
171183

0 commit comments

Comments
 (0)