/
SlackSummaryPublisher.py
executable file
·349 lines (295 loc) · 11.7 KB
/
SlackSummaryPublisher.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: ampel/contrib/hu/t3/SlackSummaryPublisher.py
# License: BSD-3-Clause
# Author: robert stein
# Date: 11.03.2018
# Last Modified Date: 14.11.2018
# Last Modified By: valery brinnel <firstname.lastname@gmail.com>
import datetime
import io
from collections.abc import Generator, Iterable, Mapping
from typing import Any, cast
import numpy as np
import pandas as pd
import requests
from slack_sdk import WebClient
from slack_sdk.errors import SlackClientError
from slack_sdk.web import SlackResponse
from ampel.abstract.AbsT3ReviewUnit import AbsT3ReviewUnit
from ampel.log.utils import log_exception
from ampel.secret.NamedSecret import NamedSecret
from ampel.struct.T3Store import T3Store
from ampel.types import T3Send
from ampel.view.TransientView import TransientView
from ampel.ztf.util.ZTFIdMapper import to_ztf_id
class SlackSummaryPublisher(AbsT3ReviewUnit):
""""""
dry_run: bool = False
quiet: bool = False
slack_channel: str
slack_token: NamedSecret[str]
excitement: dict[str, int] = {"Low": 50, "Mid": 200, "High": 400}
full_photometry: bool = False
#: Fields to extract from each detection (if full_photometry enabled)
cols: list[str] = [
"ra",
"dec",
"magpsf",
"sgscore1",
"rb",
"distnr",
"distpsnr1",
"isdiffpos",
]
def process(
self, gen: Generator[TransientView, T3Send, None], t3s: T3Store
) -> None:
""""""
channels: set[str] = set()
frames, photometry = self.combine_transients(gen, channels)
if len(frames) == 0 and self.quiet:
return
date = str(datetime.datetime.now(tz=datetime.timezone.utc).date())
sc = WebClient(self.slack_token.get())
m = calculate_excitement(len(frames), date=date, thresholds=self.excitement)
if self.dry_run:
self.logger.info(m)
else:
# we know this is a sync client; work around lazy type annotations
api = cast(
SlackResponse,
sc.api_call(
"chat.postMessage",
json={
"channel": self.slack_channel,
"text": m,
"username": "AMPEL-live",
"as_user": False,
},
),
)
if not api["ok"]:
raise SlackClientError(api["error"])
if len(frames) > 0:
df = pd.DataFrame.from_records(frames)
# Set fill value for channel columns to False
for channel in channels:
df[channel].fillna(False, inplace=True)
# Set fill value for all other columns to MISSING
for field in set(df.columns.values).difference(channels):
df[field].fillna("MISSING", inplace=True)
# Move channel info at end
df = df.reindex(
copy=False,
columns=[c for c in df.columns if c not in channels] + list(channels),
)
filename = "Summary_%s.csv" % date
buffer = io.StringIO(filename)
df.to_csv(buffer)
param = {
"token": self.slack_token.get(),
"channels": self.slack_channel,
"title": "Summary: " + date,
"username": "AMPEL-live",
"as_user": "false",
"filename": filename,
}
if self.dry_run:
# log only first two lines
csv = buffer.getvalue()
idx = 0
for _ in range(2):
idx = csv.find("\n", idx) + 1
self.logger.info(
{
"files": {"file": csv[:idx] + "..."},
"token": param["token"][:8] + "...",
**{k: v for (k, v) in param.items() if k != "token"},
}
)
else:
r = requests.post(
"https://slack.com/api/files.upload",
params=param,
files={"file": buffer.getvalue()},
)
r.raise_for_status()
if not r.json()["ok"]:
raise SlackClientError(r.json()["error"])
self.logger.info(r.text)
if self.full_photometry:
photometry_df = pd.concat(photometry, sort=False)
# Set fill value for channel columns to False
for channel in channels:
photometry_df[channel].fillna(False, inplace=True)
# Set fill value for all other columns to MISSING
for field in set(photometry_df.columns.values).difference(channels):
photometry_df[field].fillna("MISSING", inplace=True)
# Move channel info at end
photometry_df = photometry_df.reindex(
copy=False,
columns=[c for c in df.columns if c not in channels]
+ list(channels),
)
filename = "Photometry_%s.csv" % date
buffer = io.StringIO(filename)
photometry_df.to_csv(buffer)
param = {
"token": self.slack_token.get(),
"channels": self.slack_channel,
"title": "Full Photometry: " + date,
"username": "AMPEL-live",
"as_user": "false",
"filename": filename,
}
if self.dry_run:
csv = buffer.getvalue()
idx = 0
for _ in range(2):
idx = csv.find("\n", idx) + 1
self.logger.info({"files": {"file": csv[:idx] + "..."}, **param})
else:
r = requests.post(
"https://slack.com/api/files.upload",
params=param,
files={"file": buffer.getvalue()},
)
r.raise_for_status()
if not r.json()["ok"]:
raise SlackClientError(r.json()["error"])
self.logger.info(r.text)
def combine_transients(
self,
transients: Iterable[TransientView],
channels: set[str],
) -> tuple[list[pd.DataFrame], list[pd.DataFrame]]:
""""""
frames = []
photometry = []
for transient in transients:
mycols = set(self.cols)
frame = {
"tranId": transient.id,
"ztf_name": to_ztf_id(transient.id),
}
if summary := transient.get_t2_body(unit="T2LightCurveSummary"):
frame.update(summary) # type: ignore[arg-type]
# include other T2 results, flattened
for t2record in transient.t2 or []:
if (
t2record.unit == "T2LightCurveSummary"
or not (output := t2record.get_payload())
or not isinstance(output, dict)
):
continue
# Flatten T2 output dictionary
# If this is not a dictionry it will be through
res_flat = flat_dict(output, prefix="T2-")
# Add these to the dataframe (could we just join the dictionaries?)
for key, value in res_flat.items():
try:
frame[key] = str(value)
except ValueError as ve: # noqa: PERF203
log_exception(self.logger, ve)
assert transient.stock
frame.update(
{str(channel): True for channel in (transient.stock["channel"] or [])}
)
channels.update(str(c) for c in transient.stock["channel"] or [])
frames.append(frame)
if self.full_photometry:
if transient.t0 is None:
raise ValueError(
"Full photometry requested, but no T0 records loaded"
)
photometry.append(
pd.DataFrame(
[
{
"ztf_name": frame["ztf_name"],
**{k: v for k, v in pp["body"].items() if k in mycols},
**{
str(channel): True
for channel in (transient.stock["channel"] or [])
},
}
for pp in transient.t0
if (not pp.get("tag") or "SUPERSEDED" not in pp["tag"])
]
)
)
return frames, photometry
def calculate_excitement(n_transients, date, thresholds, n_alerts=np.nan):
""""""
message = "UPDATE! Alert summary for " + date + ". "
if n_alerts == 0:
message += "\n DISASTER! No alerts were ingested last night. :sob: "
message += "Perhaps ZTF had technical problems? :telescope: \n"
message += (
"It might be worth checking the ZTF-General Slack channel "
"for detector issues."
)
return message
if not np.isnan(n_alerts):
message += " In total, " + str(n_alerts) + " alerts were ingested. \n"
else:
message += "\n"
if n_transients == 0:
message += (
"TRAGEDY! Sadly, it seems that no transients passed the "
"filters last night"
)
return message
if n_transients == 1:
message += (
"LONE WOLF! It seems that there was just one transient"
" which passed the filters. Guess it's better than "
"nothing... :shrug: \n ~The results are summarised "
"below.~ That one result is shown below."
)
return message
if n_transients < thresholds["Low"]:
message += (
"MEH! We only found " + str(n_transients) + " "
"transients last night. :unamused: That's "
"disappointing. Hopefully we get more tomorrow!"
)
elif n_transients < thresholds["Mid"]:
message += (
"NOT BAD! We found " + str(n_transients) + " "
"transients last night. :slightly_smiling_face: "
"Let's keep up the good work!"
)
elif n_transients < thresholds["High"]:
message += (
"IMPRESSIVE! We found "
+ str(n_transients)
+ " transients last night. :grin: That's exciting! Good "
"luck to anyone trying to check all of those by hand..."
)
else:
message += (
"WOW!!! We found "
+ str(n_transients)
+ " transients last night. :tada: That's loads! Now we "
"just need to figure out what to do with all of them..."
)
message += "\n The results are summarised below. "
return message
def flat_dict(d: Mapping[Any, Any], prefix: str = "") -> dict[str, Any]:
"""
Loop through dictionary d
Append any key, val pairs to the return list ret
Add the prefix to any key param
Recurse if encountered value is a nested dictionary.
"""
if not isinstance(d, Mapping):
return d
ret: dict[str, Any] = {}
for key, val in d.items():
if isinstance(val, Mapping):
ret = {**ret, **flat_dict(val, prefix=prefix + str(key) + "_")}
else:
ret[prefix + str(key)] = val
return ret