Skip to content

Commit

Permalink
Save report.json after 10 second intervals. (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
keyurva authored Nov 28, 2023
1 parent 1224a9a commit 71906d0
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion simple/stats/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
from enum import Enum
from functools import wraps
import json
import time

from util.filehandler import FileHandler

# Minimum interval before a report should be saved to disk or cloud.
# This keeps it from reporting too frequently and running into GCS rate limit issues.
_REPORT_SAVE_INTERVAL_SECONDS = 10.0


class Status(Enum):
NOT_STARTED = auto()
Expand All @@ -28,6 +33,10 @@ class Status(Enum):
FAILURE = auto()


def _is_done_status(status: Status) -> bool:
return status == Status.SUCCESS or status == Status.FAILURE


class ImportReporter:
"""Generates a report on every reported change.
Expand All @@ -38,6 +47,7 @@ def __init__(self, report_fh: FileHandler) -> None:
self.status = Status.NOT_STARTED
self.start_time = None
self.last_update = datetime.now()
self.last_reported: float | None = None
self.report_fh = report_fh
self.data = {}
self.import_files: dict[str, FileImportReporter] = {}
Expand Down Expand Up @@ -113,7 +123,12 @@ def _maybe_report(field: str, func=None):

def save(self) -> None:
self.last_update = datetime.now()
self.report_fh.write_string(json.dumps(self.json(), indent=2))
should_report = not self.last_reported or time.time(
) - self.last_reported >= _REPORT_SAVE_INTERVAL_SECONDS or _is_done_status(
self.status)
if should_report:
self.last_reported = time.time()
self.report_fh.write_string(json.dumps(self.json(), indent=2))


class FileImportReporter:
Expand Down

0 comments on commit 71906d0

Please sign in to comment.