Refactor timezone handling to use LOCAL_TIMEZONE consistently and log ingestion timestamps
This commit is contained in:
1 parent
2b9f674761
commit
b4c923ac4e
1 file changed
+10
-2
+10
-2
@@ -29,7 +29,7 @@ DISCORD_WEBHOOK_URL = os.getenv("DISCORD_WEBHOOK_URL", "")
|
||||
MAX_VISITS_PER_REQUEST = int(os.getenv("MAX_VISITS_PER_REQUEST", "100"))
|
||||
PAGE_SIZE_CHOICES = (25, 50, 100, 250)
|
||||
DEFAULT_PAGE_SIZE = 50
|
||||
VIEWER_TIMEZONE = ZoneInfo("Europe/Ljubljana")
|
||||
LOCAL_TIMEZONE = ZoneInfo("Europe/Ljubljana")
|
||||
|
||||
class Visit(BaseModel):
|
||||
url: HttpUrl
|
||||
@@ -132,6 +132,14 @@ def healthz():
|
||||
async def receive_visits(batch: VisitBatch, authorization: Annotated[str | None, Header()] = None):
|
||||
require_token(authorization)
|
||||
stored = save_batch(batch)
|
||||
ingestion_timestamp = datetime.now(timezone.utc).astimezone(LOCAL_TIMEZONE).isoformat(timespec="seconds")
|
||||
logger.info(
|
||||
"visit_batch_ingested ingestion_timestamp=%s timezone=Europe/Ljubljana device_id=%s accepted=%d stored=%d",
|
||||
ingestion_timestamp,
|
||||
batch.device_id,
|
||||
len(batch.visits),
|
||||
stored,
|
||||
)
|
||||
asyncio.create_task(post_discord(batch))
|
||||
return {"accepted": len(batch.visits), "stored": stored}
|
||||
|
||||
@@ -149,7 +157,7 @@ def viewer_timestamp(value: str) -> str:
|
||||
timestamp = datetime.fromisoformat(normalized)
|
||||
if timestamp.tzinfo is None:
|
||||
timestamp = timestamp.replace(tzinfo=timezone.utc)
|
||||
return timestamp.astimezone(VIEWER_TIMEZONE).strftime("%m. %d. %H:%M")
|
||||
return timestamp.astimezone(LOCAL_TIMEZONE).strftime("%m. %d. %H:%M")
|
||||
except (AttributeError, TypeError, ValueError):
|
||||
return str(value)
|
||||
|
||||
|
||||
Reference in new issue
Block a user