Skip to content

Commit

Permalink
Email plots and generally improve email template
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Sep 17, 2024
1 parent 43377dd commit 9269154
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 125 deletions.
35 changes: 26 additions & 9 deletions src/lvmcryo/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ async def ln2(

from sdsstools.logger import CustomJsonFormatter, get_logger

from lvmcryo.handlers.ln2 import LN2Handler
from lvmcryo.handlers.ln2 import LN2Handler, get_now
from lvmcryo.notifier import Notifier
from lvmcryo.runner import ln2_runner
from lvmcryo.tools import LockExistsError, ensure_lock
Expand All @@ -386,6 +386,7 @@ async def ln2(
json_path: pathlib.Path | None = None
json_handler: FileHandler | None = None
log_data: list[dict] | None = None
images: dict[str, pathlib.Path | None] = {}

if action == Actions.abort:
return await _close_valves_helper()
Expand Down Expand Up @@ -505,8 +506,10 @@ async def ln2(

if notify:
log.warning("Sending failure notifications.")
await notifier.notify_failure(
f"LN2 {action.value} failed because a lockfile was already present."
await notifier.notify_after_fill(
False,
error_message=f"LN2 {action.value} failed because a lockfile "
"was already present.",
)

# Do not do anything special for this error, just exit.
Expand Down Expand Up @@ -534,6 +537,7 @@ async def ln2(
log.info(f"LN2 {action.value} completed successfully.")

finally:
handler.event_times.end_time = get_now()
await handler.clear()

if not skip_finally:
Expand All @@ -546,7 +550,7 @@ async def ln2(
valve: valve_model.model_dump()
for valve, valve_model in config.valve_info.items()
}
record_pk = await post_fill_tasks(
record_pk, plot_paths = await post_fill_tasks(
handler,
write_data=config.write_data,
data_path=config.data_path,
Expand All @@ -564,13 +568,24 @@ async def ln2(
},
)

images = {
"pressure": plot_paths.get("pressure_png", None),
"temps": plot_paths.get("temps_png", None),
"thermistors": plot_paths.get("thermistors_png", None),
}

if record_pk:
log.debug(f"Record {record_pk} created in the database.")

if notify:
if error:
log.warning("Sending failure notifications.")
await notifier.notify_failure(error, handler)
await notifier.notify_after_fill(
False,
error_message=error,
handler=handler,
images=images,
)

elif config.email_level == NotificationLevel.info:
# The handler has already emitted a notification to
Expand All @@ -579,10 +594,12 @@ async def ln2(
# TODO: include log and more data here.
# For now it's just plain text.

log.debug("Sending notification email.")
notifier.send_email(
message="The LN2 fill completed successfully.",
subject="SUCCESS: LVM LN2 fill",
log.info("Sending notification email.")
await notifier.notify_after_fill(
True,
handler=handler,
images=images,
post_to_slack=False, # Already done.
)

if error:
Expand Down
31 changes: 17 additions & 14 deletions src/lvmcryo/handlers/ln2.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,17 @@ def convert_datetime_to_iso_8601_with_z_suffix(dt: datetime.datetime) -> str:
return dt.strftime("%Y-%m-%dT%H:%M:%SZ")


def get_now():
"""Returns a UTC datetime for now."""

return datetime.datetime.now(datetime.UTC)


class EventDict(BaseModel):
"""Dictionary of events."""

start_time: datetime.datetime = field(default_factory=get_now)
end_time: datetime.datetime | None = None
purge_start: datetime.datetime | None = None
purge_complete: datetime.datetime | None = None
fill_start: datetime.datetime | None = None
Expand Down Expand Up @@ -248,11 +256,6 @@ async def check(

return True

def _get_now(self):
"""Returns a UTC datetime for now."""

return datetime.datetime.now(datetime.UTC)

async def purge(
self,
purge_valve: str | None = None,
Expand Down Expand Up @@ -288,7 +291,7 @@ async def purge(

valve_handler = self.valve_handlers[purge_valve]

self.event_times.purge_start = self._get_now()
self.event_times.purge_start = get_now()

self.log.info(
f"Beginning purge using valve {valve_handler.valve!r} with "
Expand All @@ -315,7 +318,7 @@ async def purge(
raise

finally:
self.event_times.purge_complete = self._get_now()
self.event_times.purge_complete = get_now()
if prompt:
sshkeyboard.stop_listening()
await asyncio.sleep(1)
Expand Down Expand Up @@ -371,7 +374,7 @@ async def fill(
)
)

self.event_times.fill_start = self._get_now()
self.event_times.fill_start = get_now()

self.log.info(
f"Beginning fill on cameras {cameras!r} with "
Expand All @@ -394,7 +397,7 @@ async def fill(
raise

finally:
self.event_times.fill_complete = self._get_now()
self.event_times.fill_complete = get_now()
if prompt:
sshkeyboard.stop_listening()
await asyncio.sleep(1)
Expand Down Expand Up @@ -425,13 +428,13 @@ async def monitor_keys(key: str):
Panel(
Align(
render(
'Press [green]"x"[/] to abort or [green]"enter"[/] '
f"to finish the {action}."
f'Press [green]"enter"[/] to finish the {action} '
'or [green]"x"[/] to abort.'
),
"center",
),
box=box.HEAVY,
border_style="red",
border_style="green",
)
)

Expand Down Expand Up @@ -496,12 +499,12 @@ def fail(self):
"""Sets the fail flag and event time."""

self.failed = True
self.event_times.failed = self._get_now()
self.event_times.failed = get_now()

def abort(self):
"""Aborts the fill."""

self.aborted = True
self.event_times.aborted = self._get_now()
self.event_times.aborted = get_now()

self.fail()
Loading

0 comments on commit 9269154

Please sign in to comment.