Skip to content

Commit

Permalink
style(variant): improve logs (#2179)
Browse files Browse the repository at this point in the history
Change the display of number of days in logs related to snapshot
clearing tasks.
  • Loading branch information
maugde authored Oct 10, 2024
1 parent dc0130d commit 4374350
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
23 changes: 11 additions & 12 deletions antarest/study/storage/variantstudy/variant_study_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from pathlib import Path
from uuid import uuid4

import humanize
from fastapi import HTTPException
from filelock import FileLock

Expand Down Expand Up @@ -1056,13 +1057,13 @@ def initialize_additional_data(self, variant_study: VariantStudy) -> bool:
)
return False

def clear_all_snapshots(self, retention_hours: timedelta, params: t.Optional[RequestParameters] = None) -> str:
def clear_all_snapshots(self, retention_time: timedelta, params: t.Optional[RequestParameters] = None) -> str:
"""
Admin command that clear all variant snapshots older than `retention_hours` (in hours).
Only available for admin users.
Args:
retention_hours: number of retention hours
retention_time: number of retention hours
params: request parameters used to identify the user status
Returns: None
Expand All @@ -1072,11 +1073,9 @@ def clear_all_snapshots(self, retention_hours: timedelta, params: t.Optional[Req
if params is None or (params.user and not params.user.is_site_admin() and not params.user.is_admin_token()):
raise UserHasNotPermissionError()

task_name = f"Cleaning all snapshot updated or accessed at least {retention_hours} hours ago."
task_name = f"Cleaning all snapshot updated or accessed at least {humanize.precisedelta(retention_time)} ago."

snapshot_clearing_task_instance = SnapshotCleanerTask(
variant_study_service=self, retention_hours=retention_hours
)
snapshot_clearing_task_instance = SnapshotCleanerTask(variant_study_service=self, retention_time=retention_time)

return self.task_service.add_task(
snapshot_clearing_task_instance,
Expand All @@ -1092,10 +1091,10 @@ class SnapshotCleanerTask:
def __init__(
self,
variant_study_service: VariantStudyService,
retention_hours: timedelta,
retention_time: timedelta,
) -> None:
self._variant_study_service = variant_study_service
self._retention_hours = retention_hours
self._retention_time = retention_time

def _clear_all_snapshots(self) -> None:
with db():
Expand All @@ -1106,15 +1105,15 @@ def _clear_all_snapshots(self) -> None:
)
)
for variant in variant_list:
if variant.updated_at and variant.updated_at < datetime.utcnow() - self._retention_hours:
if variant.last_access and variant.last_access < datetime.utcnow() - self._retention_hours:
if variant.updated_at and variant.updated_at < datetime.utcnow() - self._retention_time:
if variant.last_access and variant.last_access < datetime.utcnow() - self._retention_time:
self._variant_study_service.clear_snapshot(variant)

def run_task(self, notifier: TaskUpdateNotifier) -> TaskResult:
msg = f"Start cleaning all snapshots updated or accessed {self._retention_hours} hours ago."
msg = f"Start cleaning all snapshots updated or accessed {humanize.precisedelta(self._retention_time)} ago."
notifier(msg)
self._clear_all_snapshots()
msg = f"All selected snapshots were successfully cleared."
msg = "All selected snapshots were successfully cleared."
notifier(msg)
return TaskResult(success=True, message=msg)

Expand Down
7 changes: 4 additions & 3 deletions antarest/study/web/variant_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import logging
from typing import List, Optional, Union

import humanize
from fastapi import APIRouter, Body, Depends

from antarest.core.config import Config
Expand Down Expand Up @@ -431,15 +432,15 @@ def clear_variant_snapshots(
current_user: JWTUser = Depends(auth.get_current_user),
) -> str:
"""
Endpoint that clear `limit` hours old and older variant snapshots.
Endpoint that clear snapshots of variant which were updated or accessed `hours` hours ago.
Args: limit (int, optional): Number of hours to clear. Defaults to 24.
Args: hours (int, optional): Number of hours to clear. Defaults to 24.
Returns: ID of the task running the snapshot clearing.
"""
retention_hours = datetime.timedelta(hours=hours)
logger.info(
f"Delete all variant snapshots older than {retention_hours} hours.",
f"Delete all variant snapshots older than {humanize.precisedelta(retention_hours)}.",
extra={"user": current_user.id},
)
params = RequestParameters(user=current_user)
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ click~=8.0.3
contextvars~=2.4
filelock~=3.4.2
gunicorn~=20.1.0
humanize~=4.10.0; python_version <= '3.8'
humanize~=4.11.0; python_version > '3.8'
jsonref~=0.2
PyJWT~=2.9.0
MarkupSafe~=2.0.1
Expand Down

0 comments on commit 4374350

Please sign in to comment.