Skip to content

Commit 156d054

Browse files
committed
Merge branch '494-logging' of github.com:CodeForPhilly/paws-data-pipeline into 494-logging
2 parents 67045f2 + 17c13d6 commit 156d054

File tree

7 files changed

+106
-18
lines changed

7 files changed

+106
-18
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ src/server/api/__pycache__
1515
/.idea
1616
start_env.sh
1717
*.DS_Store
18-
1918
/src/server/venv/
2019
/src/local_files/
2120
/src/server/secrets_dict.py

src/server/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ RUN chmod 666 /usr/lib/uwsgi/plugins/python38_plugin.so
2323

2424
COPY . .
2525

26-
RUN mkdir /app/static \
26+
RUN mkdir -p /app/static \
2727
/app/static/raw_data \
2828
/app/static/logs \
2929
/app/static/zipped
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
from sqlalchemy.orm import sessionmaker
3+
from simple_salesforce import Salesforce
4+
from config import engine
5+
6+
import structlog
7+
logger = structlog.get_logger()
8+
9+
10+
def get_updated_contact_data():
11+
Session = sessionmaker(engine)
12+
13+
qry = """ -- Collect latest foster/volunteer dates
14+
with ev_dates as
15+
(select
16+
person_id,
17+
max(case when event_type=1 then time else null end) adopt,
18+
max(case when event_type=2 then time else null end) foster_out,
19+
-- max(case when event_type=3 then time else null end) rto,
20+
max(case when event_type=5 then time else null end) foster_return
21+
22+
from
23+
sl_animal_events sla
24+
left join sl_event_types sle on sle.id = sla.event_type
25+
26+
where sle.id in (1,2,5)
27+
group by person_id
28+
order by person_id
29+
)
30+
31+
32+
select json_agg (upd) as "cd" from (
33+
select
34+
slsf.source_id as "contactId" , -- long salesforce string
35+
slp.id as "personId" , -- short PAWS-local shelterluv id
36+
37+
case
38+
when
39+
(extract(epoch from now())::bigint - foster_out < 365*86400) -- foster out in last year
40+
or (extract(epoch from now())::bigint - foster_return < 365*86400) -- foster return
41+
then 'Active'
42+
else 'Inactive'
43+
end as "updatedFosterStatus" ,
44+
45+
(to_timestamp(foster_out ) at time zone 'America/New_York')::date as "updatedFosterStartDate",
46+
(to_timestamp(foster_return ) at time zone 'America/New_York')::date as "updatedFosterEndDate",
47+
48+
min(vs.from_date) as "updatedFirstVolunteerDate",
49+
max(vs.from_date) as "updatedLastVolunteerDate",
50+
vc.source_id as "volgisticsId"
51+
52+
53+
from
54+
ev_dates
55+
left join pdp_contacts slc on slc.source_id = person_id::text and slc.source_type = 'shelterluvpeople'
56+
left join pdp_contacts slsf on slsf.matching_id = slc.matching_id and slsf.source_type = 'salesforcecontacts'
57+
left join shelterluvpeople slp on slp.internal_id = person_id::text
58+
left join pdp_contacts vc on vc.matching_id = slc.matching_id and vc.source_type = 'volgistics'
59+
left join volgisticsshifts vs on vs.volg_id::text = vc.source_id
60+
61+
where
62+
slsf.source_id is not null
63+
64+
group by
65+
slsf.source_id,
66+
slp.id,
67+
vc.source_id,
68+
foster_out ,
69+
foster_return
70+
71+
) upd ;
72+
73+
74+
"""
75+
76+
with Session() as session:
77+
result = session.execute(qry)
78+
sfdata = result.fetchone()[0]
79+
logger.debug("Query for Salesforce update returned %d records", len(sfdata))
80+
return sfdata

src/server/api/internal_api.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import structlog
44
from flask import jsonify
55

6-
from api.API_ingest import ingest_sources_from_api
6+
from api.API_ingest import ingest_sources_from_api, salesforce_contacts
77
from api.api import internal_api
88
from rfm_funcs.create_scores import create_scores
9+
from api.API_ingest import updated_data
910

1011
logger = structlog.get_logger()
1112

@@ -42,3 +43,11 @@ def hit_create_scores():
4243
tuple_count = create_scores()
4344
logger.info("create_scores() processed %s scores", str(tuple_count) )
4445
return jsonify(200)
46+
47+
48+
@internal_api.route("/api/internal/get_updated_data", methods=["GET"])
49+
def get_contact_data():
50+
logger.debug("Calling get_updated_contact_data()")
51+
contact_json = updated_data.get_updated_contact_data()
52+
logger.debug("Returning %d contact records", len(contact_json) )
53+
return jsonify(contact_json), 200

src/server/bin/startServer.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,4 @@ echo "SLEEPING.. WAITING FOR DB"; sleep 5; echo "WAKING"; alembic upgrade head;
1111

1212
# --no-reload prevents Flask restart, which usually happens in middle of create_base_users()
1313
#TODO: SECURITY - ensure we are not running in debug mode in production
14-
UWSGI_LOG_CONFIG="bin/uwsgi_log_config.json"
15-
# Remove newlines and extra whitespace from log formatting
16-
LOG_FORMAT=$(cat $UWSGI_LOG_CONFIG | tr -d \\n | tr -s ' ')
17-
uwsgi --logformat "$LOG_FORMAT" --http-socket :5000 --plugin python38 --module wsgi:app --chdir /app --pythonpath . --processes 2 --threads 4 --master
14+
uwsgi bin/uwsgi.ini

src/server/bin/uwsgi.ini

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[uwsgi]
2+
http-socket = :5000
3+
plugin = python38
4+
module = wsgi:app
5+
chdir = /app
6+
pythonpath = .
7+
processes = 2
8+
threads = 4
9+
log-4xx = true
10+
log-5xx = true
11+
disable-logging = true
12+
logdate = %%Y-%%m-%%d-T%%H:%%M:%%SZ
13+
logformat = {"timestamp": "%(ftime)", "address": "%(addr)", "method": "%(method)", "protocol": "%(proto)", "resp_size": "%(size)", "request_body_size": "%(cl)", "response_status": "%(status)", "response_time": "%(secs)", "uri": "%(uri)"}
14+
logformat-strftime = true

src/server/bin/uwsgi_log_config.json

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)