-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_app.py
More file actions
263 lines (195 loc) · 9.54 KB
/
function_app.py
File metadata and controls
263 lines (195 loc) · 9.54 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import logging
from asyncio import gather
from datetime import date, datetime, timedelta
from typing import Tuple
from zoneinfo import ZoneInfo
import azure.functions as func
import pandas
import yarl
from azure.identity.aio import DefaultAzureCredential
from azure.keyvault.secrets.aio import SecretClient
from choirgenius import ChoirGenius, EventType
from graph_email import send_email
from member_nags import generate_member_nags
from monday import get_monday_auditions, get_monday_roster
from attendance_report import generate_attendance_report
from consistency_report import generate_consistency_report
from projected_report import generate_projected_attendance_report
from report_utils import Email
AZURE_VAULT_URL = "https://cantorivault.vault.azure.net/"
MONDAY_SECRET_NAME = "monday-api-key"
CHOIRGENIUS_SECRET_USER = "choirgenius-user"
CHOIRGENIUS_SECRET_PASSWORD = "choirgenius-password"
CANTORI_CHOIRGENIUS_COM = yarl.URL("https://cantori.choirgenius.com/")
ERROR_EMAILS = ("richard.berg@cantorinewyork.com",)
app = func.FunctionApp()
class CantoriError(RuntimeError):
pass
@app.route(route="timezone")
async def debug_time_zone(req: func.HttpRequest) -> func.HttpResponse:
msg = f"""
Current UTC time: {datetime.now(ZoneInfo("UTC"))}
Current NYC time: {datetime.now(ZoneInfo("America/New_York"))}
Current local time: {datetime.now()}
"""
return func.HttpResponse(msg)
# 10PM daily, Eastern Time, from September to May
# Linux consumption apps don't support TZ, so 2AM Friday UTC is the best
# approximation we've got. Sometimes it'll be 10pm in NYC, sometimes 9pm.
@app.schedule(schedule="0 0 2 * 9-12,1-5 *", arg_name="myTimer")
async def trigger_attendance_report(myTimer: func.TimerRequest) -> None:
if myTimer.past_due:
logging.warning("The timer was past due!")
return
await send_attendance_report()
@app.route(route="attendance_report", methods=[func.HttpMethod.POST])
async def post_attendance_report(req: func.HttpRequest):
await send_attendance_report(force=True)
async def send_attendance_report(force: bool = False):
try:
roster = await get_roster()
current_nyc_time = datetime.now(ZoneInfo("America/New_York"))
current_nyc_date = current_nyc_time.date()
cycle_from, cycle_to = _determine_concert_cycle(roster, current_nyc_date)
# exclude today's rehearsal stats, until 7PM New York time
if current_nyc_time.hour <= 19:
rehearsals_to = current_nyc_date - timedelta(days=1)
else:
rehearsals_to = current_nyc_date
async with await _get_choirgenius() as cg:
actual_attendance, projected_attendance = await gather(
cg.get_rehearsal_attendance(cycle_from, rehearsals_to),
cg.get_projected_attendance(cycle_from, cycle_to, EventType.REHEARSAL),
)
email, worth_sending = generate_attendance_report(
actual_attendance, projected_attendance, roster, current_nyc_date, cycle_from, cycle_to
)
if worth_sending or force:
await send_email(email)
except CantoriError as e:
await send_email(Email("Error generating report", str(e), ERROR_EMAILS))
# 2PM Eastern daily, from September to May
@app.schedule(schedule="0 0 18 * 9-12,1-5 *", arg_name="myTimer")
async def trigger_projected_attendance_report(myTimer: func.TimerRequest) -> None:
if myTimer.past_due:
logging.warning("The timer was past due!")
return
await send_projected_attendance_report()
@app.route(route="projected_attendance_report", methods=[func.HttpMethod.POST])
async def post_projected_attendance_report(req: func.HttpRequest):
await send_projected_attendance_report(force=True)
async def send_projected_attendance_report(force: bool = False):
try:
roster = await get_roster()
current_nyc_time = datetime.now(ZoneInfo("America/New_York"))
current_nyc_date = current_nyc_time.date()
cycle_from, cycle_to = _determine_concert_cycle(roster, current_nyc_date)
async with await _get_choirgenius() as cg:
projected_attendance = await cg.get_projected_attendance(
cycle_from, cycle_to, EventType.REHEARSAL
)
email, worth_sending = generate_projected_attendance_report(
projected_attendance, roster, current_nyc_date, cycle_to
)
if worth_sending or force:
await send_email(email)
except CantoriError as e:
await send_email(Email("Error generating report", str(e), ERROR_EMAILS))
# 10PM every night
@app.schedule(schedule="0 0 2 * * *", arg_name="myTimer")
async def trigger_consistency_report(myTimer: func.TimerRequest) -> None:
if myTimer.past_due:
logging.warning("The timer was past due!")
return
await send_consistency_report()
@app.route(route="consistency_report", methods=[func.HttpMethod.POST])
async def post_consistency_report(req: func.HttpRequest):
await send_consistency_report(force=True)
async def send_consistency_report(force: bool = False):
try:
current_nyc_time = datetime.now(ZoneInfo("America/New_York"))
current_nyc_date = current_nyc_time.date()
season = _determine_season(date.today())
async with await _get_choirgenius() as cg:
roster, candidates, cg_active = await gather(
get_roster(), get_audition_candidates(), cg.get_active()
)
cycle_from, cycle_to = _determine_concert_cycle(roster, current_nyc_date)
projected_concert_attendance = await cg.get_projected_attendance(
cycle_from, cycle_to, EventType.CONCERT
)
email, worth_sending = generate_consistency_report(
roster, candidates, cg_active, projected_concert_attendance, season, cycle_to, current_nyc_time
)
if worth_sending or force:
await send_email(email)
except CantoriError as e:
await send_email(Email("Error generating report", str(e), ERROR_EMAILS))
# 7:30AM Tuesdays, September to May
@app.schedule(schedule="0 30 13 * 9-12,1-5 Tue", arg_name="myTimer")
async def trigger_member_nags(myTimer: func.TimerRequest) -> None:
if myTimer.past_due:
logging.warning("The timer was past due!")
return
await send_member_nags()
@app.route(route="member_nags", methods=[func.HttpMethod.POST])
async def post_member_nags(req: func.HttpRequest):
await send_member_nags(force=True)
async def send_member_nags(force: bool = False):
try:
roster = await get_roster()
current_nyc_time = datetime.now(ZoneInfo("America/New_York"))
current_nyc_date = current_nyc_time.date()
cycle_from, cycle_to = _determine_concert_cycle(roster, current_nyc_date)
async with await _get_choirgenius() as cg:
projected_attendance = await cg.get_projected_attendance(
current_nyc_date, cycle_to, EventType.REHEARSAL
)
emails = generate_member_nags(projected_attendance, roster, cycle_to)
for email in emails:
await send_email(email)
except CantoriError as e:
await send_email(Email("Error generating report", str(e), ERROR_EMAILS))
def _determine_season(today: date) -> str:
if today.month >= 7:
return f"{today.year}-{str(today.year + 1)[-2:]}"
else:
return f"{today.year - 1}-{str(today.year)[-2:]}"
def _determine_concert_cycle(roster: pandas.DataFrame, today: date) -> Tuple[date, date]:
concert_dates = sorted(d for d in roster.columns if isinstance(d, date))
if not concert_dates:
raise CantoriError("Monday.com roster has no concert cycles defined")
cycle_from = date(concert_dates[0].year, 7, 1)
for cycle_to in concert_dates:
if cycle_from <= today <= cycle_to:
return cycle_from, cycle_to
else:
cycle_from = cycle_to + timedelta(days=1)
raise CantoriError("Today isn't part of the season (as defined by Monday.com roster)")
async def get_roster() -> pandas.DataFrame:
monday_api_key = await _get_monday_key()
roster = await get_monday_roster(monday_api_key)
logging.info(f"Got {len(roster)} roster singers from Monday.com")
return roster
async def get_audition_candidates() -> pandas.DataFrame:
monday_api_key = await _get_monday_key()
candidates = await get_monday_auditions(monday_api_key)
logging.info(f"Got {len(candidates)} audition candidates from Monday.com")
return candidates
async def _get_monday_key() -> str:
async with DefaultAzureCredential() as credential:
async with SecretClient(vault_url=AZURE_VAULT_URL, credential=credential) as secret_client:
assert isinstance(secret_client, SecretClient) # base class not annotated with Self
monday_api_key = await secret_client.get_secret(MONDAY_SECRET_NAME)
if not monday_api_key.value:
raise RuntimeError("Monday API key not found in Vault")
return monday_api_key.value
async def _get_choirgenius() -> ChoirGenius:
async with DefaultAzureCredential() as credential:
async with SecretClient(vault_url=AZURE_VAULT_URL, credential=credential) as secret_client:
assert isinstance(secret_client, SecretClient)
user = await secret_client.get_secret(CHOIRGENIUS_SECRET_USER)
password = await secret_client.get_secret(CHOIRGENIUS_SECRET_PASSWORD)
if not (user.value and password.value):
raise RuntimeError("ChoirGenius credentials not found in Vault")
return ChoirGenius(CANTORI_CHOIRGENIUS_COM, user.value, password.value)