Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: ♻️ adding UTC timezone to the project #3912

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a390713
adding UTC timezone to the project
matusdrobuliak66 Feb 23, 2023
422dd6c
Merge branch 'master' into maintenance/propagating-UTC-timezone-every…
matusdrobuliak66 Feb 23, 2023
be56e2c
correction of services/api-server datetime UTC timezone
matusdrobuliak66 Feb 23, 2023
87b8241
Merge branch 'maintenance/propagating-UTC-timezone-everywhere' of git…
matusdrobuliak66 Feb 23, 2023
05562ab
correction of datetime UTC timezone
matusdrobuliak66 Feb 23, 2023
f8413fc
correction of datetime UTC timezone
matusdrobuliak66 Feb 23, 2023
588f9c3
correction of datetime UTC timezone
matusdrobuliak66 Feb 23, 2023
f4cf1eb
correction of datetime UTC timezone
matusdrobuliak66 Feb 23, 2023
0b5ebb5
Merge branch 'master' into maintenance/propagating-UTC-timezone-every…
matusdrobuliak66 Feb 23, 2023
47c2fd3
correction of datetime UTC timezone
matusdrobuliak66 Feb 23, 2023
a0c28cc
Merge branch 'maintenance/propagating-UTC-timezone-everywhere' of git…
matusdrobuliak66 Feb 23, 2023
cd85bc3
Merge branch 'master' into maintenance/propagating-UTC-timezone-every…
matusdrobuliak66 Feb 23, 2023
7024146
Merge branch 'master' into maintenance/propagating-UTC-timezone-every…
matusdrobuliak66 Feb 23, 2023
9035e95
making working with timezone.utc consistent
matusdrobuliak66 Feb 24, 2023
9bce164
Merge branch 'master' into maintenance/propagating-UTC-timezone-every…
matusdrobuliak66 Feb 24, 2023
fddd137
correction of failing autoscaling unit tests
matusdrobuliak66 Feb 24, 2023
c4c05e6
Revert "correction of failing autoscaling unit tests"
matusdrobuliak66 Feb 24, 2023
a4032a8
Revert "Merge branch 'master' into maintenance/propagating-UTC-timezo…
matusdrobuliak66 Feb 24, 2023
ae251e6
Revert "making working with timezone.utc consistent"
matusdrobuliak66 Feb 24, 2023
77c0f6c
Merge remote-tracking branch 'upstream/master'
matusdrobuliak66 Feb 24, 2023
1da531d
Merge branch 'master' into maintenance/propagating-UTC-timezone-every…
matusdrobuliak66 Feb 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"""
import json
import warnings
from typing import Dict, Iterable, Optional, Set, Tuple, Type
from typing import Iterable, Optional

from pydantic import BaseModel, create_model, validator
from pydantic.fields import ModelField, Undefined
Expand All @@ -38,7 +38,7 @@
)


def collect_fields_attrs(model_cls: Type[BaseModel]) -> Dict[str, Dict[str, str]]:
def collect_fields_attrs(model_cls: type[BaseModel]) -> dict[str, dict[str, str]]:
"""

>>> class MyModel(BaseModel):
Expand Down Expand Up @@ -100,33 +100,31 @@ def _stringify(obj):

def _eval_selection(
model_fields: Iterable[ModelField],
include: Optional[Set[str]],
exclude: Optional[Set[str]],
include: Optional[set[str]],
exclude: Optional[set[str]],
exclude_optionals: bool,
) -> Set[str]:
) -> set[str]:
# TODO: use dict for deep include/exclude! SEE https://pydantic-docs.helpmanual.io/usage/exporting_models/

if include is None:
include = set(f.name for f in model_fields)
include = {f.name for f in model_fields}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this done automatically or you had to change manually?

if exclude is None:
exclude = set()
if exclude_optionals:
exclude = exclude.union(
set(f.name for f in model_fields if f.required == False)
)
exclude = exclude.union({f.name for f in model_fields if f.required == False})

selection = include - exclude
return selection


def _extract_field_definitions(
model_cls: Type[BaseModel],
model_cls: type[BaseModel],
*,
include: Optional[Set[str]],
exclude: Optional[Set[str]],
include: Optional[set[str]],
exclude: Optional[set[str]],
exclude_optionals: bool,
set_all_optional: bool,
) -> Dict[str, Tuple]:
) -> dict[str, tuple]:
"""
Returns field_definitions: fields of the model in the format
`<name>=(<type>, <default default>)` or `<name>=<default value>`,
Expand Down Expand Up @@ -160,16 +158,16 @@ def _extract_field_definitions(


def copy_model(
reference_cls: Type[BaseModel],
reference_cls: type[BaseModel],
*,
name: str = None,
include: Optional[Set[str]] = None,
exclude: Optional[Set[str]] = None,
include: Optional[set[str]] = None,
exclude: Optional[set[str]] = None,
exclude_optionals: bool = False,
as_update_model: bool = False,
skip_validators: bool = False,
__config__: Type[BaseConfig] = None,
) -> Type[BaseModel]:
__config__: type[BaseConfig] = None,
) -> type[BaseModel]:
"""
Creates a clone of `reference_cls` with a different name and a subset of fields

Expand All @@ -190,7 +188,7 @@ def copy_model(

# VALIDATORS

validators_funs: Dict[str, classmethod] = {}
validators_funs: dict[str, classmethod] = {}
# A dict of method names and @validator class methods
# SEE example in https://pydantic-docs.helpmanual.io/usage/models/#dynamic-model-creation
if not skip_validators and reference_cls != BaseModel:
Expand Down
9 changes: 6 additions & 3 deletions packages/models-library/tests/test_basic_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import keyword
import re
from datetime import datetime
from datetime import datetime, timezone
from typing import Any, Optional, Pattern, Sequence, Union

import pytest
Expand Down Expand Up @@ -172,7 +172,7 @@ class webserver_timedate_utils:

@classmethod
def now(cls) -> datetime:
return datetime.utcnow()
return datetime.now(timezone.utc).replace(tzinfo=None)

@classmethod
def format_datetime(cls, snapshot: datetime) -> str:
Expand All @@ -195,7 +195,10 @@ def to_datetime(cls, snapshot: str) -> datetime:
("2020-12-30T23:15:00.345Z", ("12", "30", "23", ":00", "00", ".345")),
("2020-12-30 23:15:00", INVALID),
(datetime.now().isoformat(), INVALID), # as '2020-11-29T23:09:21.859469'
(datetime.utcnow().isoformat(), INVALID), # as '2020-11-29T22:09:21.859469'
(
datetime.now(timezone.utc).replace(tzinfo=None).isoformat(),
INVALID,
), # as '2020-11-29T22:09:21.859469'
(webserver_timedate_utils.now_str(), VALID),
(
webserver_timedate_utils.format_datetime(
Expand Down
4 changes: 2 additions & 2 deletions packages/postgres-database/tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# pylint: disable=unused-argument
# pylint: disable=unused-variable

from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import Optional

import pytest
Expand Down Expand Up @@ -77,7 +77,7 @@ async def test_trial_accounts(pg_engine: Engine):
async with pg_engine.acquire() as conn:

# creates trial user
client_now = datetime.utcnow()
client_now = datetime.now(timezone.utc).replace(tzinfo=None)
user_id: Optional[int] = await conn.scalar(
users.insert()
.values(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import itertools
import json
import random
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import Any, Callable, Final
from uuid import uuid4

Expand Down Expand Up @@ -118,7 +118,7 @@ def fake_task_factory(first_internal_id=1) -> Callable:

def fake_task(**overrides) -> dict[str, Any]:

t0 = datetime.utcnow()
t0 = datetime.now(timezone.utc).replace(tzinfo=None)
data = dict(
project_id=uuid4(),
node_id=uuid4(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import subprocess
from datetime import datetime
from datetime import datetime, timezone
from pathlib import Path
from typing import Optional

Expand Down Expand Up @@ -107,8 +107,8 @@ def create_docker_compose_image_spec(
"No explicit config for OCI/label-schema found (optional), skipping OCI annotations."
)
# add required labels
extra_labels[f"{LS_LABEL_PREFIX}.build-date"] = datetime.utcnow().strftime(
"%Y-%m-%dT%H:%M:%SZ"
extra_labels[f"{LS_LABEL_PREFIX}.build-date"] = (
datetime.now(timezone.utc).replace(tzinfo=None).strftime("%Y-%m-%dT%H:%M:%SZ")
)
extra_labels[f"{LS_LABEL_PREFIX}.schema-version"] = "1.0"

Expand Down
6 changes: 3 additions & 3 deletions packages/service-library/src/servicelib/exception_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from datetime import datetime
from datetime import datetime, timezone
from typing import Optional

from pydantic import BaseModel, Field, NonNegativeFloat, PrivateAttr
Expand Down Expand Up @@ -49,11 +49,11 @@ def try_to_raise(self, exception: BaseException) -> None:

# first time the exception was detected
if self._first_exception_skip is None:
self._first_exception_skip = datetime.utcnow()
self._first_exception_skip = datetime.now(timezone.utc).replace(tzinfo=None)

# raise if subsequent exception is outside of delay window
elif (
datetime.utcnow() - self._first_exception_skip
datetime.now(timezone.utc).replace(tzinfo=None) - self._first_exception_skip
).total_seconds() > self.delay_for:
raise exception

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import urllib.parse
from asyncio import Task
from datetime import datetime
from datetime import datetime, timezone
from typing import Any, Awaitable, Callable, Coroutine, Optional

from pydantic import (
Expand Down Expand Up @@ -76,7 +76,9 @@ class TrackedTask(BaseModel):
description="if True then the task will not be auto-cancelled if no one enquires of its status",
)

started: datetime = Field(default_factory=datetime.utcnow)
started: datetime = Field(
default_factory=lambda: datetime.now(timezone.utc).replace(tzinfo=None)
)
last_status_check: Optional[datetime] = Field(
default=None,
description=(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import urllib.parse
from collections import deque
from contextlib import suppress
from datetime import datetime
from datetime import datetime, timezone
from typing import Any, Optional, Protocol
from uuid import uuid4

Expand Down Expand Up @@ -96,7 +96,7 @@ async def _stale_tasks_monitor_worker(self) -> None:
# will not be the case.

while await asyncio.sleep(self.stale_task_check_interval_s, result=True):
utc_now = datetime.utcnow()
utc_now = datetime.now(timezone.utc).replace(tzinfo=None)

tasks_to_remove: list[TaskId] = []
for tasks in self._tasks_groups.values():
Expand Down Expand Up @@ -200,7 +200,7 @@ def get_task_status(
raises TaskNotFoundError if the task cannot be found
"""
tracked_task: TrackedTask = self._get_tracked_task(task_id, with_task_context)
tracked_task.last_status_check = datetime.utcnow()
tracked_task.last_status_check = datetime.now(timezone.utc).replace(tzinfo=None)

task = tracked_task.task
done = task.done()
Expand Down
7 changes: 5 additions & 2 deletions packages/service-library/tests/fastapi/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# pylint: disable=unused-variable

import socket
from datetime import datetime
from datetime import datetime, timezone
from typing import AsyncIterable, Callable, cast

import pytest
Expand All @@ -23,7 +23,10 @@ def app() -> FastAPI:

@api_router.get("/")
def _get_root():
return {"name": __name__, "timestamp": datetime.utcnow().isoformat()}
return {
"name": __name__,
"timestamp": datetime.now(timezone.utc).replace(tzinfo=None).isoformat(),
}

@api_router.get("/data")
def _get_data(x: PositiveFloat, y: int = Query(..., gt=3, lt=4)):
Expand Down
10 changes: 7 additions & 3 deletions scripts/demo/create_portal_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import logging
import sys
from contextlib import contextmanager
from datetime import datetime
from datetime import datetime, timezone
from pathlib import Path
from string import ascii_uppercase
from typing import Optional
Expand Down Expand Up @@ -89,7 +89,7 @@ def main(mock_codes, *, trial_account_days: Optional[int] = None, uid: int = 1):
with _open(file_path) as fh:
print(
"<!-- Generated by {} on {} -->".format(
current_path.name, datetime.utcnow()
current_path.name, datetime.now(timezone.utc).replace(tzinfo=None)
),
file=fh,
)
Expand Down Expand Up @@ -133,7 +133,11 @@ def main(mock_codes, *, trial_account_days: Optional[int] = None, uid: int = 1):
)
print('""issuer"" : ""[email protected]"" ,', file=fh)
print(f'""trial_account_days"" : ""{trial_account_days}""', file=fh)
print('}",%s' % datetime.now().isoformat(sep=" "), file=fh)
print(
'}",%s'
% datetime.now(timezone.utc).replace(tzinfo=None).isoformat(sep=" "),
file=fh,
)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import io
import logging
from collections import deque
from datetime import datetime
from datetime import datetime, timezone
from textwrap import dedent
from typing import IO, Optional
from uuid import UUID
Expand Down Expand Up @@ -103,7 +103,9 @@ async def upload_file(
)
# assign file_id.
file_meta: File = await File.create_from_uploaded(
file, file_size=file_size, created_at=datetime.utcnow().isoformat()
file,
file_size=file_size,
created_at=datetime.now(timezone.utc).replace(tzinfo=None).isoformat(),
)
logger.debug(
"Assigned id: %s of %s bytes (content-length), real size %s bytes",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
from datetime import datetime
from typing import Callable, Tuple
from datetime import datetime, timezone
from typing import Callable

from fastapi import APIRouter, Depends
from fastapi.responses import PlainTextResponse
Expand All @@ -19,7 +19,7 @@

@router.get("/", include_in_schema=False, response_class=PlainTextResponse)
async def check_service_health():
return f"{__name__}@{datetime.utcnow().isoformat()}"
return f"{__name__}@{datetime.now(timezone.utc).replace(tzinfo=None).isoformat()}"


@router.get("/state", include_in_schema=False)
Expand All @@ -31,7 +31,7 @@ async def get_service_state(
url_for: Callable = Depends(get_reverse_url_mapper),
):
apis = (catalog_client, director2_api, storage_client, webserver_client)
heaths: Tuple[bool] = await asyncio.gather(*[api.is_responsive() for api in apis])
heaths: tuple[bool] = await asyncio.gather(*[api.is_responsive() for api in apis])

current_status = AppStatusCheck(
app_name=PROJECT_NAME,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import hashlib
from datetime import datetime
from datetime import datetime, timezone
from enum import Enum
from typing import Optional, Type, Union
from typing import Optional, Union
from uuid import UUID, uuid4

from pydantic import BaseModel, Field, HttpUrl, conint, validator
Expand Down Expand Up @@ -174,7 +174,7 @@ def create_now(
id=global_uuid,
runner_name=parent_name,
inputs_checksum=inputs_checksum,
created_at=datetime.utcnow(),
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
url=None,
runner_url=None,
outputs_url=None,
Expand Down Expand Up @@ -215,7 +215,7 @@ class TaskStates(str, Enum):
ABORTED = "ABORTED"


PercentageInt: Type[int] = conint(ge=0, le=100)
PercentageInt: type[int] = conint(ge=0, le=100)


class JobStatus(BaseModel):
Expand Down Expand Up @@ -254,5 +254,5 @@ class Config(BaseConfig):
}

def take_snapshot(self, event: str = "submitted"):
setattr(self, f"{event}_at", datetime.utcnow())
setattr(self, f"{event}_at", datetime.now(timezone.utc).replace(tzinfo=None))
return getattr(self, f"{event}_at")
Loading