-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.py
282 lines (224 loc) · 8.05 KB
/
email.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import logging
import pathlib
import urllib
from jinja2 import Template
from airflow.configuration import conf
from airflow.decorators import task
from airflow.models import Variable
from libsys_airflow.plugins.shared.utils import send_email_with_server_name
from libsys_airflow.plugins.shared.utils import is_production
logger = logging.getLogger(__name__)
def _oclc_identifiers(multiple_codes: list, folio_url: str):
template = Template(
"""<h2>Multiple OCLC Identifiers</h2>
<p>These Instances contain multiple OCLC identifiers and need
manual remediation to be uploaded to OCLC</p>
<ul>
{% for row in multiple_codes %}
<li>
<a href="{{folio_url}}/inventory/viewsource/{{row[0]}}">MARC view of Instance {{row[0]}}</a>
with OCLC Identifiers {% for code in row[2] %}{{ code }}{% if not loop.list %}, {% endif %}{% endfor %}.
</li>
{% endfor %}
</ul>
"""
)
return template.render(folio_url=folio_url, multiple_codes=multiple_codes)
def _cohort_emails():
return {
"business": Variable.get("OCLC_EMAIL_BUS"),
"hoover": Variable.get("OCLC_EMAIL_HOOVER"),
"lane": Variable.get("OCLC_EMAIL_LANE"),
"law": Variable.get("OCLC_EMAIL_LAW"),
"sul": Variable.get("OCLC_EMAIL_SUL"),
}
def _match_oclc_library(**kwargs):
library: str = kwargs["library"]
to_emails: list = kwargs["to_emails"]
subject_line: str = kwargs["subject_line"]
cohort_emails: dict = kwargs["cohort_emails"]
match library:
case "CASUM":
to_emails.insert(0, cohort_emails.get("lane"))
subject_line += " Lane"
case "HIN":
to_emails.insert(0, cohort_emails.get("hoover"))
subject_line += " Hoover"
case "RCJ":
to_emails.insert(0, cohort_emails.get("law"))
subject_line += " Law"
case "S7Z":
to_emails.insert(0, cohort_emails.get("business"))
subject_line += " Business"
case "STF":
to_emails.insert(0, cohort_emails.get("sul"))
subject_line += " SUL"
return to_emails, subject_line
def _oclc_report_html(report: str, library: str):
report_path = pathlib.Path(report)
report_type = report_path.parent.name
airflow_url = conf.get('webserver', 'base_url') # type: ignore
if not airflow_url.endswith("/"):
airflow_url = f"{airflow_url}/"
report_url = f"{airflow_url}data_export_oclc_reports/{library}/{report_type}/{report_path.name}"
return f"""{report_type} link: <a href="{report_url}">{report_path.name}</a>"""
def dag_run_url(dag_run) -> str:
airflow_url = conf.get('webserver', 'base_url')
if not airflow_url.endswith("/"):
airflow_url = f"{airflow_url}/"
params = urllib.parse.urlencode({"dag_run_id": dag_run.run_id})
return f"{airflow_url}dags/{dag_run.id}/grid?{params}"
def generate_holdings_errors_emails(error_reports: dict):
"""
Generates emails for holdings set errors for cohort libraries
"""
devs_email = Variable.get("EMAIL_DEVS")
cohort_emails = _cohort_emails()
for library, report in error_reports.items():
to_emails = [
devs_email,
]
report_path = pathlib.Path(report)
report_type = report_path.parent.name
match report_type:
case "set_holdings_match":
subject_line = "OCLC: Set holdings match error for"
case "unset_holdings":
subject_line = "OCLC: Unset holdings error for"
case _:
subject_line = "OCLC: Set holdings error for"
to_emails, subject_line = _match_oclc_library(
library=library,
to_emails=to_emails,
cohort_emails=cohort_emails,
subject_line=subject_line,
)
if not is_production():
to_emails.pop(0) # Should only send report to libsys devs
html_content = _oclc_report_html(report, library)
send_email_with_server_name(
to=to_emails, subject=subject_line, html_content=html_content
)
def generate_oclc_new_marc_errors_email(error_reports: dict):
"""
Generates emails for each library for OCLC MARC errors for new-to-OCLC
records
"""
devs_email = Variable.get("EMAIL_DEVS")
cohort_emails = _cohort_emails()
airflow_url = conf.get('webserver', 'base_url') # type: ignore
if not airflow_url.endswith("/"):
airflow_url = f"{airflow_url}/"
subject_line = "OCLC: MARC Errors for New Record"
for library, report in error_reports.items():
to_emails = [
devs_email,
]
_match_oclc_library(
library=library,
to_emails=to_emails,
cohort_emails=cohort_emails,
subject_line=subject_line,
)
if not is_production():
to_emails.pop(0) # Should only send report to libsys devs
html_content = _oclc_report_html(report, library)
send_email_with_server_name(
to=to_emails, subject=subject_line, html_content=html_content
)
def generate_multiple_oclc_identifiers_email(multiple_codes: list):
"""
Generates an email for review by staff when multiple OCLC numbers
exist for a record
"""
if len(multiple_codes) < 1:
logger.info("No multiple OCLC Identifiers")
return
logger.info(
f"Generating Email of Multiple OCLC Identifiers for {len(multiple_codes)}"
)
folio_url = Variable.get("FOLIO_URL")
devs_email = Variable.get("EMAIL_DEVS")
cohort_emails = _cohort_emails()
html_content = _oclc_identifiers(multiple_codes, folio_url)
if is_production():
send_email_with_server_name(
to=[
devs_email,
cohort_emails["business"],
cohort_emails["hoover"],
cohort_emails["lane"],
cohort_emails["law"],
cohort_emails["sul"],
],
subject="Review Instances with Multiple OCLC Indentifiers",
html_content=html_content,
)
else:
folio_url = folio_url.replace("https://", "").replace(".stanford.edu", "")
send_email_with_server_name(
to=[
devs_email,
],
subject=f"{folio_url} - Review Instances with Multiple OCLC Indentifiers",
html_content=html_content,
)
def _failed_transmission_email_body(
files: list, vendor: str, dag_id: str, dag_run_id: str, dag_run_url: str
):
template = Template(
"""
{% if vendor|length > 0 %}
<h2>Failed to Transmit Files for {{ dag_id }} {{ vendor }}</h2>
{% else %}
<h2>Failed to Transmit Files for {{ dag_id }}</h2>
{% endif %}
<p><a href="{{ dag_run_url }}">{{ dag_run_id }}</a>
<p>These files failed to transmit</p>
<ol>
{% for row in files %}
<li>
{{ row }}
</li>
{% endfor %}
</ol>
"""
)
return template.render(
files=files,
vendor=vendor,
dag_id=dag_id,
dag_run_id=dag_run_id,
dag_run_url=dag_run_url,
)
@task
def failed_transmission_email(files: list, **kwargs):
"""
Generates an email listing files that failed to transmit
Sends to libsys devs to troubleshoot
"""
dag_run = kwargs["dag_run"]
dag_id = dag_run.id
dag_run_id = dag_run.run_id
run_url = dag_run_url(dag_run)
params = kwargs.get("params", {})
full_dump_vendor = params.get("vendor", {})
if len(files) == 0:
logger.info("No failed files to send in email")
return
logger.info("Generating email of failed to transmit files")
devs_to_email_addr = Variable.get("EMAIL_DEVS")
html_content = _failed_transmission_email_body(
files,
full_dump_vendor,
dag_id,
dag_run_id,
run_url,
)
send_email_with_server_name(
to=[
devs_to_email_addr,
],
subject=f"Failed File Transmission for {dag_id} {dag_run_id}",
html_content=html_content,
)