This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
75 lines (59 loc) · 2.1 KB
/
main.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
#!/usr/bin/env python3
import asyncio
import os
from datetime import datetime, timedelta
from pngme.api import AsyncClient
async def get_count_loan_repaid_events(
api_client: AsyncClient,
user_uuid: str,
utc_starttime: datetime,
utc_endtime: datetime,
) -> int:
"""
Count events labeled with LoanRepaid across all institutions
Args:
api_client: Pngme Async API client
user_uuid: the Pngme user_uuid for the mobile phone user
utc_starttime: the UTC time to start the time window
utc_endtime: the UTC time to end the time window
Returns:
count of LoanRepaid events within the given time window
"""
# STEP 1: fetch list of institutions belonging to the user
institutions = await api_client.institutions.get(user_uuid=user_uuid)
# STEP 2: fetch alert records for all institutions with LoanRepaid events
alerts_coroutines = []
for inst in institutions:
alerts_coroutines.append(
api_client.alerts.get(
user_uuid=user_uuid,
institution_id=inst["institution_id"],
utc_starttime=utc_starttime,
utc_endtime=utc_endtime,
labels=["LoanRepaid"],
)
)
alerts_by_institution = await asyncio.gather(*alerts_coroutines)
# STEP 3: flatten alerts into a single list
all_alerts = []
for alerts_list in alerts_by_institution:
for alert in alerts_list:
all_alerts.append(alert)
# STEP 4: count number of alerts
return len(all_alerts)
if __name__ == "__main__":
# Mercy Otieno, mercy@pngme.demo.com, 254123456789
user_uuid = "958a5ae8-f3a3-41d5-ae48-177fdc19e3f4"
token = os.environ["PNGME_TOKEN"]
client = AsyncClient(token)
utc_endtime = datetime(2021, 10, 1)
utc_starttime = utc_endtime - timedelta(days=30)
async def main():
count_loan_repaid_events = await get_count_loan_repaid_events(
client,
user_uuid,
utc_starttime,
utc_endtime,
)
print(count_loan_repaid_events)
asyncio.run(main())