Skip to content

Commit

Permalink
Add notifications section to night log email and plain-text version
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Nov 7, 2024
1 parent d82c7b7 commit f1d5407
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### 🚀 New

* Add the `/logs/notifications/{mjd}` route to retrieve the Overwatcher notifications for a given MJD.
* Add notifications section to night log email and plain-text version.


## 0.1.11 - 2024-11-06
Expand Down
14 changes: 11 additions & 3 deletions src/lvmapi/data/night_log_email_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,21 @@ <h1 class="header-text">
</div>
<hr />
{% if exposure_table %}
<pre>
{{- exposure_table }}
</pre>
<pre>{{- exposure_table }}</pre>
{% else %}
<div>No exposures.</div>
{% endif %}

<div class="section">
Notifications
</div>
<hr />
{% if notifications %}
<pre>{{- notifications }}</pre>
{% else %}
<div>No data.</div>
{% endif %}

<div class="section">
Software versions
</div>
Expand Down
7 changes: 7 additions & 0 deletions src/lvmapi/routers/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class NightLogData(BaseModel):
str | None,
Field(description="The exposure table for the night log"),
] = None
notifications: Annotated[
list[Notification],
Field(description="The list of notifications for the night log"),
] = []


class NightLogPostComment(BaseModel):
Expand Down Expand Up @@ -253,10 +257,13 @@ async def route_get_night_logs_mjd(

exposure_table_ascii = await get_exposure_table_ascii(mjd)

notifications = await route_get_notifications(mjd)

return NightLogData(
**data,
comments=comments,
exposure_table=exposure_table_ascii,
notifications=notifications,
)


Expand Down
43 changes: 40 additions & 3 deletions src/lvmapi/tools/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,30 @@ async def get_night_log_data(sjd: int | None = None):
return result


def notifications_to_list(notifications: list[dict[str, Any]]) -> list[str]:
"""Converts a list of notifications to a list of strings."""

if len(notifications) == 0:
return ["No notifications"]

notification_list: list[str] = []
for notification in notifications:
date = notification["date"].strftime("%Y-%m-%d %H:%M:%S")
level = notification["level"].upper()
message = notification["message"]
payload = ", ".join(
[f"{key}: {value}" for key, value in notification["payload"].items()]
)

notification_str = f"{date} - {level} - {message}"
if payload:
notification_str += f" {{ {payload} }}"

notification_list.append(notification_str)

return notification_list


async def get_plaintext_night_log(sjd: int | None = None):
"""Returns the night log as a plaintext string."""

Expand Down Expand Up @@ -547,6 +571,11 @@ async def get_plaintext_night_log(sjd: int | None = None):
Versions
--------
{versions}
Notifications
-------------
{notifications}
"""

date = Time(sjd - 1, format="mjd").datetime.strftime("%A, %B %-d, %Y")
Expand Down Expand Up @@ -578,6 +607,8 @@ async def get_plaintext_night_log(sjd: int | None = None):
versions = await get_actor_versions()
versions_l = [f"{actor}: {version or '?'}" for actor, version in versions.items()]

notifications = await get_notifications(sjd)

return nigh_log.format(
date=date,
sjd=sjd,
Expand All @@ -587,6 +618,7 @@ async def get_plaintext_night_log(sjd: int | None = None):
other="\n".join(other) or "No comments",
versions="\n".join(versions_l),
exposure_data=exposure_table or "No exposures found",
notifications="\n".join(notifications_to_list(notifications)),
)


Expand Down Expand Up @@ -637,6 +669,7 @@ async def email_night_log(
lvmweb_url = config["night_logs.lvmweb_url"] + str(sjd)

versions = await get_actor_versions()
notifications = await get_notifications(sjd)

html_message = html_template.render(
sjd=sjd,
Expand All @@ -646,8 +679,9 @@ async def email_night_log(
weather=data["comments"]["weather"],
issues=data["comments"]["issues"],
other=data["comments"]["other"],
exposure_table=exposure_table,
exposure_table=exposure_table.strip() if exposure_table else None,
software_versions=versions,
notifications=("\n".join(notifications_to_list(notifications))).strip(),
)

recipients = config["night_logs.email_recipients"]
Expand Down Expand Up @@ -727,11 +761,14 @@ async def get_notifications(sjd: int | None = None):
uri = config["database.uri"]
table = Identifier(*config["database.tables.notification"].split("."))

query = SQL("SELECT * FROM {table} WHERE mjd = %s ORDER BY date ASC")
query = SQL("""
SELECT * FROM {table}
WHERE mjd = %s AND message != %s ORDER BY date ASC
""")

async with await psycopg.AsyncConnection.connect(uri) as aconn:
async with aconn.cursor(row_factory=dict_row) as acursor:
await acursor.execute(query.format(table=table), (sjd,))
await acursor.execute(query.format(table=table), (sjd, "I am alive!"))
notifications = await acursor.fetchall()

return notifications

0 comments on commit f1d5407

Please sign in to comment.