|
1 | | -from api.api import common_api |
2 | | -from config import engine |
3 | | -from flask import jsonify, current_app |
4 | | -from sqlalchemy.sql import text |
5 | | -import requests |
6 | | -import time |
7 | | -from datetime import datetime |
8 | | - |
9 | | -from sqlalchemy.dialects.postgresql import insert |
10 | 1 | from sqlalchemy import Table, MetaData |
11 | | -from pipeline import flow_script |
| 2 | +from sqlalchemy.orm import sessionmaker |
| 3 | + |
12 | 4 | from config import engine |
13 | | -from flask import request, redirect, jsonify, current_app |
14 | | -from api.file_uploader import validate_and_arrange_upload |
15 | | -from sqlalchemy.orm import Session, sessionmaker |
16 | 5 |
|
17 | 6 | import structlog |
18 | 7 | logger = structlog.get_logger() |
@@ -68,80 +57,84 @@ def truncate_animals(): |
68 | 57 | return 0 |
69 | 58 |
|
70 | 59 |
|
71 | | -def truncate_events(session): |
| 60 | +def truncate_events(): |
72 | 61 | """Truncate the shelterluv_events table""" |
73 | 62 |
|
74 | | - metadata = MetaData() |
75 | | - sla = Table("sl_animal_events", metadata, autoload=True, autoload_with=engine) |
| 63 | + Session = sessionmaker(engine) |
| 64 | + with Session() as session: |
| 65 | + metadata = MetaData() |
| 66 | + sla = Table("sl_animal_events", metadata, autoload=True, autoload_with=engine) |
76 | 67 |
|
77 | | - truncate = "TRUNCATE table sl_animal_events;" |
78 | | - result = session.execute(truncate) |
| 68 | + truncate = "TRUNCATE table sl_animal_events;" |
| 69 | + result = session.execute(truncate) |
| 70 | + session.commit() |
79 | 71 |
|
80 | 72 | return 0 |
81 | 73 |
|
82 | 74 | 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): |
88 | 75 | """Insert event records into sl_animal_events table and return row count. """ |
89 | 76 |
|
90 | | - # Always a clean insert |
91 | | - truncate_events(session) |
92 | | - |
93 | | - metadata = MetaData() |
94 | | - sla = Table("sl_animal_events", metadata, autoload=True, autoload_with=engine) |
95 | | - |
96 | | - # TODO: Pull from DB - inserted in db_setup/base_users.py/populate_sl_event_types() |
97 | | - event_map = { |
98 | | - "Outcome.Adoption": 1, |
99 | | - "Outcome.Foster": 2, |
100 | | - "Outcome.ReturnToOwner": 3, |
101 | | - "Intake.AdoptionReturn": 4, |
102 | | - "Intake.FosterReturn":5 |
103 | | - } |
104 | | - |
105 | | - # """ INSERT INTO "sl_event_types" ("id","event_name") VALUES |
106 | | - # ( 1,'Outcome.Adoption' ), |
107 | | - # ( 2,'Outcome.Foster' ), |
108 | | - # ( 3,'Outcome.ReturnToOwner' ), |
109 | | - # ( 4,'Intake.AdoptionReturn' ), |
110 | | - # ( 5,'Intake.FosterReturn' ) """ |
111 | | - |
112 | | - |
113 | | - |
114 | | - |
115 | | - # Event record: [ AssociatedRecords[Type = Person]["Id"]', |
116 | | - # AssociatedRecords[Type = Animal]["Id"]', |
117 | | - # "Type", |
118 | | - # "Time" |
119 | | - # ] |
120 | | - # |
121 | | - # In db: ['id', |
122 | | - # 'person_id', |
123 | | - # 'animal_id', |
124 | | - # 'event_type', |
125 | | - # 'time'] |
| 77 | + offset = 0 |
| 78 | + has_more = True |
126 | 79 |
|
127 | | - ins_list = [] # Create a list of per-row dicts |
128 | | - for rec in event_list: |
129 | | - ins_list.append( |
130 | | - { |
131 | | - "person_id": next( |
132 | | - filter(lambda x: x["Type"] == "Person", rec["AssociatedRecords"]) |
133 | | - )["Id"], |
134 | | - "animal_id": next( |
135 | | - filter(lambda x: x["Type"] == "Animal", rec["AssociatedRecords"]) |
136 | | - )["Id"], |
137 | | - "event_type": event_map[rec["Type"]], |
138 | | - "time": rec["Time"], |
139 | | - } |
140 | | - ) |
| 80 | + # Always a clean insert |
| 81 | + truncate_events() |
141 | 82 |
|
142 | | - # TODO: Wrap with try/catch |
143 | | - ret = session.execute(sla.insert(ins_list)) |
144 | | - logger.debug("finished inserting events") |
| 83 | + Session = sessionmaker(engine) |
| 84 | + with Session() as session: |
| 85 | + metadata = MetaData() |
| 86 | + sla = Table("sl_animal_events", metadata, autoload=True, autoload_with=engine) |
| 87 | + |
| 88 | + # TODO: Pull from DB - inserted in db_setup/base_users.py/populate_sl_event_types() |
| 89 | + event_map = { |
| 90 | + "Outcome.Adoption": 1, |
| 91 | + "Outcome.Foster": 2, |
| 92 | + "Outcome.ReturnToOwner": 3, |
| 93 | + "Intake.AdoptionReturn": 4, |
| 94 | + "Intake.FosterReturn":5 |
| 95 | + } |
| 96 | + |
| 97 | + # """ INSERT INTO "sl_event_types" ("id","event_name") VALUES |
| 98 | + # ( 1,'Outcome.Adoption' ), |
| 99 | + # ( 2,'Outcome.Foster' ), |
| 100 | + # ( 3,'Outcome.ReturnToOwner' ), |
| 101 | + # ( 4,'Intake.AdoptionReturn' ), |
| 102 | + # ( 5,'Intake.FosterReturn' ) """ |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + # Event record: [ AssociatedRecords[Type = Person]["Id"]', |
| 108 | + # AssociatedRecords[Type = Animal]["Id"]', |
| 109 | + # "Type", |
| 110 | + # "Time" |
| 111 | + # ] |
| 112 | + # |
| 113 | + # In db: ['id', |
| 114 | + # 'person_id', |
| 115 | + # 'animal_id', |
| 116 | + # 'event_type', |
| 117 | + # 'time'] |
| 118 | + |
| 119 | + ins_list = [] # Create a list of per-row dicts |
| 120 | + for rec in event_list: |
| 121 | + ins_list.append( |
| 122 | + { |
| 123 | + "person_id": next( |
| 124 | + filter(lambda x: x["Type"] == "Person", rec["AssociatedRecords"]) |
| 125 | + )["Id"], |
| 126 | + "animal_id": next( |
| 127 | + filter(lambda x: x["Type"] == "Animal", rec["AssociatedRecords"]) |
| 128 | + )["Id"], |
| 129 | + "event_type": event_map[rec["Type"]], |
| 130 | + "time": rec["Time"], |
| 131 | + } |
| 132 | + ) |
| 133 | + |
| 134 | + # TODO: Wrap with try/catch |
| 135 | + ret = session.execute(sla.insert(ins_list)) |
| 136 | + session.commit() |
| 137 | + logger.debug("finished inserting events") |
145 | 138 |
|
146 | 139 | return ret.rowcount |
147 | 140 |
|
0 commit comments