Logging nicer timestamp for logging import elapsed time.

This commit is contained in:
Roland 2022-03-10 13:17:12 -07:00
parent de5ec99954
commit 6a22090e54
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
3 changed files with 55 additions and 5 deletions

View file

@ -7,8 +7,8 @@ from typing import List
from sqlalchemy.orm import Session
from modules.map.models import MapFile
from wrolpi.common import get_media_directory, walk, logger
from wrolpi.dates import now
from wrolpi.common import get_media_directory, walk, logger, wrol_mode_check
from wrolpi.dates import now, timedelta_to_timestamp
from wrolpi.db import optional_session, get_db_session
from wrolpi.vars import PYTEST, PROJECT_DIR
@ -112,7 +112,8 @@ async def import_pbf(pbf: Path):
proc = await asyncio.create_subprocess_shell(cmd, stderr=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE)
stdout, stderr = await proc.communicate()
logger.debug(f'Import of {pbf} took {now() - start}')
elapsed = now() - start
logger.info(f'Import of {pbf} took {timedelta_to_timestamp(elapsed)}')
if proc.returncode != 0:
for line in stdout.decode().splitlines():
logger.info(line)
@ -136,6 +137,7 @@ def get_pbf_import_status(session: Session = None):
MOD_TILE_CACHE_DIR = Path('/var/lib/mod_tile/ajt')
@wrol_mode_check
def clear_mod_tile():
"""Remove all cached map tile files"""
if PYTEST:

View file

@ -1,5 +1,5 @@
import logging
from datetime import datetime, date, timezone
from datetime import datetime, date, timezone, timedelta
from enum import Enum
import pytz
@ -97,6 +97,37 @@ def from_timestamp(timestamp: float) -> datetime:
return local_timezone(datetime.fromtimestamp(timestamp))
def seconds_to_timestamp(seconds: int) -> str:
"""Convert an integer into a timestamp string."""
weeks = seconds // Seconds.week
seconds = seconds % Seconds.week
days = seconds // Seconds.day
seconds = seconds % Seconds.day
hours = seconds // Seconds.hour
seconds = seconds % Seconds.hour
minutes = seconds // Seconds.minute
seconds = seconds % Seconds.minute
timestamp = f'{hours:02}:{minutes:02}:{seconds:02}'
if weeks:
timestamp = f'{weeks}w {days}d {timestamp}'
elif days:
timestamp = f'{days}d {timestamp}'
return timestamp
def timedelta_to_timestamp(delta: timedelta) -> str:
"""Convert a datetime.timestamp into a timestamp string.
>>> timedelta_to_timestamp(timedelta(seconds=60))
'00:01:00'
>>> timedelta_to_timestamp(timedelta(weeks=1, days=1, seconds=7))
'1w 1d 00:00:07'
>>> timedelta_to_timestamp(timedelta(days=10))
'1w 3d 00:00:00'
"""
return seconds_to_timestamp(int(delta.total_seconds()))
def recursive_replace_tz(obj, tz=pytz.utc):
"""Recursively replace the timezone of any datetimes."""
from wrolpi.common import recursive_map

View file

@ -1,9 +1,10 @@
from datetime import datetime, timedelta
import pytest
from sqlalchemy import Column, Integer
from wrolpi.common import Base
from wrolpi.dates import TZDateTime, local_timezone, strftime_ms
from wrolpi.dates import TZDateTime, local_timezone, strftime_ms, timedelta_to_timestamp
from wrolpi.db import get_db_session, get_db_curs
from wrolpi.test.common import TestAPI, wrap_test_db, skip_circleci
@ -60,3 +61,19 @@ class TestDates(TestAPI):
tt = session.query(TestTable).one()
# Datetime is incremented by an hour.
self.assertEqual(tt.dt, local_timezone(datetime(2021, 10, 5, 17, 20, 10, 346823)))
@pytest.mark.parametrize('td,expected', [
(timedelta(seconds=0), '00:00:00'),
(timedelta(seconds=1), '00:00:01'),
(timedelta(seconds=60), '00:01:00'),
(timedelta(seconds=61), '00:01:01'),
(timedelta(seconds=65), '00:01:05'),
(timedelta(days=1), '1d 00:00:00'),
(timedelta(seconds=86461), '1d 00:01:01'),
(timedelta(weeks=1, days=1, seconds=7), '1w 1d 00:00:07'),
(timedelta(weeks=16, days=3, hours=17, minutes=46, seconds=39), '16w 3d 17:46:39'),
(timedelta(days=10), '1w 3d 00:00:00'),
])
def test_timedelta_to_timestamp(td, expected):
assert timedelta_to_timestamp(td) == expected