-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add OverheadHelper * Decorate stage coroutines to record overhead * Handle timestamp to UTC datetime in 3.10 * Move import of UTC after version check * Update codecov config * Add tests for OverheadHelper
- Loading branch information
Showing
10 changed files
with
294 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# @Author: José Sánchez-Gallego ([email protected]) | ||
# @Date: 2023-12-04 | ||
# @Filename: overhead.py | ||
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause) | ||
|
||
from __future__ import annotations | ||
|
||
import asyncio | ||
import sys | ||
import time | ||
from dataclasses import dataclass | ||
from datetime import datetime | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
|
||
if TYPE_CHECKING: | ||
from hal.macros.macro import Macro | ||
|
||
|
||
__all__ = ["OverheadHelper"] | ||
|
||
|
||
@dataclass | ||
class OverheadHelper: | ||
"""Collects and records stage overheads.""" | ||
|
||
macro: Macro | ||
stage: str | ||
|
||
def __post_init__(self) -> None: | ||
self.elapsed: float | None = None | ||
|
||
self.start_time: float | None = None | ||
self.end_time: float | None = None | ||
|
||
self.success: bool = False | ||
|
||
async def __aenter__(self): | ||
"""Starts the timer.""" | ||
|
||
self.elapsed = 0 | ||
self.start_time = time.time() | ||
|
||
return self | ||
|
||
async def __aexit__(self, exc_type, exc_value, traceback): | ||
"""Stops the timer and records the overhead.""" | ||
|
||
if self.start_time is None: | ||
raise ValueError("Timer not started") | ||
|
||
self.end_time = time.time() | ||
self.elapsed = round(time.time() - self.start_time, 2) | ||
|
||
if exc_type is not None: | ||
self.success = False | ||
else: | ||
self.success = True | ||
|
||
await self.emit_keywords() | ||
asyncio.get_running_loop().call_soon(self.update_database) | ||
|
||
# __aexit__ will re-raise the exception if the return value is None/False. | ||
return self.success | ||
|
||
async def emit_keywords(self): | ||
"""Emits the overhead as a keyword.""" | ||
|
||
command = self.macro.command | ||
stage_full = f"{self.macro.name}.{self.stage}" | ||
|
||
if self.elapsed is None: | ||
command.warning(f"Overhead was not recorded for stage {stage_full}.") | ||
return | ||
|
||
command.info(stage_duration=[self.macro.name, self.stage, self.elapsed]) | ||
|
||
def _get_datetime(self, timestamp: float | None): | ||
"""Converts a timestamp to a datetime object.""" | ||
|
||
if timestamp is None: | ||
return None | ||
|
||
if sys.version_info >= (3, 11): | ||
from datetime import UTC | ||
|
||
return datetime.fromtimestamp(timestamp, tz=UTC) | ||
|
||
return datetime.utcfromtimestamp(timestamp) | ||
|
||
def update_database(self): | ||
"""Updates the database with the overhead.""" | ||
|
||
from sdssdb.peewee.sdss5db.opsdb import Overhead, database | ||
|
||
command = self.macro.command | ||
|
||
if not database.connected: | ||
command.warning("Failed connecting to DB. Overhead cannot be recorded.") | ||
return | ||
|
||
with database.atomic(): | ||
try: | ||
actor = self.macro.actor | ||
configuration = actor.helpers.jaeger.configuration | ||
cid = configuration.configuration_id if configuration else None | ||
|
||
start_time_dt = self._get_datetime(self.start_time) | ||
end_time_dt = self._get_datetime(self.end_time) | ||
|
||
Overhead.insert( | ||
configuration_id=cid, | ||
macro=self.macro.name, | ||
stage=self.stage, | ||
start_time=start_time_dt, | ||
end_time=end_time_dt, | ||
elapsed=self.elapsed, | ||
success=self.success, | ||
).execute() | ||
|
||
except Exception as err: | ||
command.warning(f"Failed creating overhead record: {err}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.