utils.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import time
  2. from typing import TYPE_CHECKING, cast
  3. if TYPE_CHECKING:
  4. from typing import Any, Tuple
  5. from sentry_sdk._types import MonitorConfigScheduleUnit
  6. def _now_seconds_since_epoch():
  7. # type: () -> float
  8. # We cannot use `time.perf_counter()` when dealing with the duration
  9. # of a Celery task, because the start of a Celery task and
  10. # the end are recorded in different processes.
  11. # Start happens in the Celery Beat process,
  12. # the end in a Celery Worker process.
  13. return time.time()
  14. def _get_humanized_interval(seconds):
  15. # type: (float) -> Tuple[int, MonitorConfigScheduleUnit]
  16. TIME_UNITS = ( # noqa: N806
  17. ("day", 60 * 60 * 24.0),
  18. ("hour", 60 * 60.0),
  19. ("minute", 60.0),
  20. )
  21. seconds = float(seconds)
  22. for unit, divider in TIME_UNITS:
  23. if seconds >= divider:
  24. interval = int(seconds / divider)
  25. return (interval, cast("MonitorConfigScheduleUnit", unit))
  26. return (int(seconds), "second")
  27. class NoOpMgr:
  28. def __enter__(self):
  29. # type: () -> None
  30. return None
  31. def __exit__(self, exc_type, exc_value, traceback):
  32. # type: (Any, Any, Any) -> None
  33. return None