-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
79 lines (60 loc) · 2.32 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import calendar
import datetime
import json
import logging
import boto3
from dateutil import parser
from neo4j import GraphDatabase
import util.queries as q
logger = logging.getLogger()
logger.setLevel(logging.INFO)
ssmc = boto3.client('ssm')
def get_ssm_param(key):
resp = ssmc.get_parameter(
Name=key,
WithDecryption=True
)
return resp['Parameter']['Value']
host_port = get_ssm_param('com.neo4j.graphacademy.dbhostport')
user = get_ssm_param('com.neo4j.graphacademy.dbuser')
password = get_ssm_param('com.neo4j.graphacademy.dbpassword')
db_driver = GraphDatabase.driver(f"neo4j://{host_port}", auth=(user, password))
def workdays(d, end, excluded=(6, 7)):
days = []
while d.date() <= end.date():
if d.isoweekday() not in excluded:
days.append(d)
d += datetime.timedelta(days=1)
return days
def all_ninjas(event, context):
print(event)
print("fyneh")
qs = event.get("multiValueQueryStringParameters")
if qs and qs.get("date"):
now = parser.parse(qs["date"][0]).replace(day=1)
else:
now = datetime.datetime.now().replace(day=1)
start = now - datetime.timedelta(days=(now.isoweekday() + 1) % 7)
end = now.replace(day=calendar.monthrange(now.year, now.month)[1])
end = end - datetime.timedelta(days=(end.isoweekday() + 1) % 7)
logger.info(f"Retrieving Ninja activities for {now}. Start: {start}, End: {end}")
weeks = [{"start": day, "end": day + datetime.timedelta(days=6)} for day in
workdays(start, end, [2, 3, 4, 5, 6, 7])]
with db_driver.session() as session:
params = {"year": now.year, "month": now.month}
print("params", params)
result = session.run(q.ninjas_api_discourse_query, params)
discourse_rows = [row.data() for row in result]
params = {"now": now.strftime("%Y-%m")}
result = session.run(q.ninjas_api_so_query, params)
so_rows = [row.data() for row in result]
return {"statusCode": 200, "body": json.dumps({
"discourse": discourse_rows,
"so": so_rows,
"weeks": [{"start": week["start"].strftime("%Y-%m-%d"),
"end": week["end"].strftime("%Y-%m-%d")}
for week in weeks]
}), "headers": {
"Content-Type": "application/json",
'Access-Control-Allow-Origin': '*'
}}