Skip to content

Commit b496176

Browse files
committed
Merge branch 'master' of https://github.com/CodeForPhilly/paws-data-pipeline into issue-166-360Page
2 parents f15de91 + ad5f82a commit b496176

File tree

11 files changed

+14253
-98
lines changed

11 files changed

+14253
-98
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ start_env.sh
1818

1919
/src/server/venv/
2020
/src/local_files/
21+
/src/server/secrets.py

src/client/.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@
2020

2121
npm-debug.log*
2222
yarn-debug.log*
23-
yarn-error.log*
24-
package-lock.json
23+
yarn-error.log*

src/client/package-lock.json

Lines changed: 14174 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client/src/pages/Admin.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,9 @@ class Admin extends Component {
175175
: _.isEmpty(this.state.statistics) !== true &&
176176
<TableContainer component={Paper} className="statisticsData">
177177
<Table aria-label="simple table" className={classes.table}>
178-
<TableHead>
179-
<TableRow>
180-
<TableCell>Sources Matched</TableCell>
181-
<TableCell align="left">Number of Matches</TableCell>
182-
</TableRow>
183-
</TableHead>
184178
<TableBody>
185-
{this.state.statistics.map((row) => (
186-
<TableRow key={row[0]}>
179+
{this.state.statistics.map((row, index) => (
180+
<TableRow key={index}>
187181
<TableCell align="left" component="th" scope="row">
188182
{row[0]}
189183
</TableCell>

src/server/api/admin_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ def get_statistics():
105105

106106
# Need to iterate over the results proxy
107107
results = {
108-
"matches count": [dict(row) for row in matches_count_query_result][0]["count"],
109-
"total contacts count": [dict(row) for row in total_count_query_result][0]["count"]
108+
"Distinct Matching Groups Count": [dict(row) for row in matches_count_query_result][0]["count"],
109+
"Total Contacts Count": [dict(row) for row in total_count_query_result][0]["count"]
110110
}
111111

112112
return results

src/server/api/common_api.py

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
from config import engine
33
from flask import jsonify
44
from sqlalchemy.sql import text
5+
import requests
6+
import json
7+
from secrets import SHELTERLUV_SECRET_TOKEN
58

69

710
@common_api.route('/api/contacts/<search_text>', methods=['GET'])
@@ -37,24 +40,45 @@ def get_360(matching_id):
3740
query_result = connection.execute(query, matching_id=matching_id)
3841

3942
result["contact_details"] = [dict(row) for row in query_result]
40-
result["shifts"] = []
41-
result["donations"] = []
42-
result["adoptions"] = []
4343

44-
# todo: complete retrieving details for response
45-
for row in query_result:
44+
for row in result["contact_details"]:
45+
if row["source_type"] == "salesforcecontacts":
46+
donations_query = text("select * from salesforcedonations where contact_id like :salesforcecontacts_id")
47+
salesforce_contacts_query_result = connection.execute(donations_query,
48+
salesforcecontacts_id=row["source_id"] + "%")
49+
salesforce_donations_results = [dict(row) for row in salesforce_contacts_query_result]
50+
result['donations'] = salesforce_donations_results
51+
4652
if row["source_type"] == "volgistics":
47-
query = text("select * from volgisticsshifts where number = :volgistics_id")
48-
query_result = connection.execute(query, volgistics_id=row["source_id"])
49-
result["shifts"] += [dict(row) for row in query_result]
50-
51-
'''
52-
query = text("select * from salesforcedonations where contact_id = :salesforcecontacts_id")
53-
query_result = connection.execute(query, salesforcecontacts_id=salesforcecontacts_id)
54-
salesforcedonations_results = [dict(row) for row in query_result]
55-
56-
if salesforcedonations_results:
57-
result['salesforcedonations'] = salesforcedonations_results
58-
'''
53+
shifts_query = text("select * from volgisticsshifts where number = :volgistics_id")
54+
volgistics_shifts_query_result = connection.execute(shifts_query, volgistics_id=row["source_id"])
55+
volgisticsshifts_results = [dict(row) for row in volgistics_shifts_query_result]
56+
result['shifts'] = volgisticsshifts_results
57+
58+
if row["source_type"] == "shelterluvpeople":
59+
adoptions = []
60+
person = requests.get("http://shelterluv.com/api/v1/people/{}".format(row["source_id"]),
61+
headers={"x-api-key": SHELTERLUV_SECRET_TOKEN})
62+
person_json = person.json()
63+
animal_ids = person_json["Animal_ids"]
64+
65+
for animal_id in animal_ids:
66+
animal_events = requests.get("http://shelterluv.com/api/v1/animals/{}/events".format(animal_id),
67+
headers={"x-api-key": SHELTERLUV_SECRET_TOKEN})
68+
animal_events_json = animal_events.json()
69+
70+
for event in animal_events_json["events"]:
71+
for adoption in event["AssociatedRecords"]:
72+
if adoption["Type"] == "Person" and adoption["Id"] == row["source_id"]:
73+
del event["AssociatedRecords"]
74+
animal_details = requests.get(
75+
"http://shelterluv.com/api/v1/animals/{}".format(animal_id),
76+
headers={"x-api-key": SHELTERLUV_SECRET_TOKEN})
77+
78+
animal_details_json = animal_details.json()
79+
event["animal_details"] = animal_details_json
80+
adoptions.append(event)
81+
82+
result['adoptions'] = adoptions
5983

6084
return jsonify({'result': result})

src/server/datasource_manager.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,18 @@ def volgistics_address(index, street):
9494
"state": "mailing_state_province",
9595
"zip": "mailing_zip_postal_code",
9696
"others": {
97-
"additional_sources": [{
98-
"salesforcedonations": {
99-
'should_drop_first_column': True
100-
}
101-
}
102-
],
10397
"should_drop_first_column": True
10498
}
10599

106100
},
101+
"salesforcedonations": {
102+
"parent": "salesforcecontacts",
103+
"others": {
104+
"should_drop_first_column": True
105+
}
106+
},
107107
"shelterluvpeople": {
108-
"source_id": "id",
108+
"source_id": "internal-id",
109109
"first_name": "firstname",
110110
"last_name": "lastname",
111111
"email": "email",
@@ -131,19 +131,13 @@ def volgistics_address(index, street):
131131
"state": "state",
132132
"zip": "zip",
133133
"others": {
134-
"additional_sources": [{
135-
"volgisticsshifts": {
136-
'should_drop_first_column': True
137-
}
138-
}
139-
],
140134
"should_drop_first_column": True
141135
}
142-
136+
},
137+
"volgisticsshifts": {
138+
"parent": "volgistics",
139+
"others": {
140+
"should_drop_first_column": True
141+
}
143142
}
144143
}
145-
146-
147-
148-
149-

src/server/models.py

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import datetime
22

3-
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey
3+
from sqlalchemy import Column, Integer, String, DateTime
44
from sqlalchemy.dialects.postgresql import JSONB
55
from sqlalchemy.ext.declarative import declarative_base
66

@@ -89,42 +89,4 @@ class Volgistics(Base):
8989
json = Column(JSONB)
9090

9191

92-
class SalesForceDonations(Base):
93-
__tablename__ = "salesforcedonations"
9492

95-
_id = Column(Integer, primary_key=True)
96-
recurring_donor = Column(String)
97-
opportunity_owner = Column(String)
98-
account_id = Column(String)
99-
account_name = Column(String)
100-
opportunity_id = Column(String)
101-
opportunity_name = Column(String)
102-
stage = Column(String)
103-
fiscal_period = Column(String)
104-
amount = Column(String)
105-
probability = Column(String)
106-
age = Column(String)
107-
close_date = Column(String)
108-
created_date = Column(String)
109-
next_step = Column(String)
110-
lead_source = Column(String)
111-
type = Column(String)
112-
source = Column(String)
113-
contact_id = Column(String)
114-
primary_campaign_source = Column(String)
115-
116-
117-
class Volgistics_Shifts(Base):
118-
__tablename__ = 'volgisticsshifts'
119-
120-
_id = Column(Integer, primary_key=True)
121-
number = Column(String)
122-
site = Column(String)
123-
place = Column(String)
124-
assignment = Column(String)
125-
role = Column(String)
126-
from_date = Column('from', DateTime)
127-
to = Column(DateTime)
128-
spare_date = Column(String)
129-
spare_chechbox = Column(String)
130-
coordinator = Column(String)

src/server/pipeline/clean_and_load_data.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from config import CURRENT_SOURCE_FILES_PATH
1010

1111

12-
def start(pdp_contacts_df, file_path_list):
12+
def start(connection, pdp_contacts_df, file_path_list):
1313
result = pd.DataFrame(columns=pdp_contacts_df.columns)
1414

1515
for uploaded_file in file_path_list:
@@ -26,12 +26,16 @@ def start(pdp_contacts_df, file_path_list):
2626
normalization_without_others = copy.deepcopy(SOURCE_NORMALIZATION_MAPPING[table_name])
2727
normalization_without_others.pop("others") # copy avoids modifying the imported mapping
2828

29-
source_df = create_normalized_df(df, normalization_without_others, table_name)
29+
if "parent" not in normalization_without_others:
30+
source_df = create_normalized_df(df, normalization_without_others, table_name)
31+
32+
if result.empty:
33+
result = source_df
34+
else:
35+
result = pd.concat([result, source_df])
3036

31-
if result.empty:
32-
result = source_df
3337
else:
34-
result = pd.concat([result, source_df])
38+
df.to_sql(table_name, connection, index=False, if_exists='append')
3539

3640
current_app.logger.info(' - Finish load_paws_data on: ' + uploaded_file)
3741

src/server/pipeline/flow_script.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def start_flow():
2323
# Clean the input data and normalize
2424
# input - existing files in path
2525
# output - normalized object of all entries
26-
normalized_data = clean_and_load_data.start(pdp_contacts_df, file_path_list)
26+
normalized_data = clean_and_load_data.start(connection, pdp_contacts_df, file_path_list)
2727

2828
# Standardize column data types
2929
# If additional inconsistencies are encountered, may need to enforce the schema of

0 commit comments

Comments
 (0)