From c0574408e2584e5d6c169d5a3856a12cdc574336 Mon Sep 17 00:00:00 2001 From: Michael Woolnough Date: Mon, 21 Oct 2024 10:13:51 +0100 Subject: [PATCH] Record and recall environment creation time. --- schema.graphql | 1 + softpack_core/artifacts.py | 1 + softpack_core/schemas/environment.py | 7 ++++++- tests/integration/test_environment.py | 6 ++++++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/schema.graphql b/schema.graphql index 81d504b..471fba6 100644 --- a/schema.graphql +++ b/schema.graphql @@ -39,6 +39,7 @@ type Environment { state: State tags: [String!]! hidden: Boolean! + created: Int! cachedEnvs: [Environment!]! interpreters: Interpreters! requested: DateTime diff --git a/softpack_core/artifacts.py b/softpack_core/artifacts.py index f7263c1..39bd32f 100644 --- a/softpack_core/artifacts.py +++ b/softpack_core/artifacts.py @@ -177,6 +177,7 @@ def spec(self) -> Box: info["tags"] = getattr(metadata, "tags", []) info["hidden"] = getattr(metadata, "hidden", False) info["force_hidden"] = getattr(metadata, "force_hidden", False) + info["created"] = getattr(metadata, "created", 0) info["interpreters"] = Interpreters() diff --git a/softpack_core/schemas/environment.py b/softpack_core/schemas/environment.py index 72ff976..43be38a 100644 --- a/softpack_core/schemas/environment.py +++ b/softpack_core/schemas/environment.py @@ -10,6 +10,7 @@ import statistics from dataclasses import dataclass, field from pathlib import Path +from time import time from traceback import format_exception_only from typing import List, Optional, Tuple, Union, cast @@ -333,6 +334,7 @@ class Environment: state: Optional[State] tags: list[str] hidden: bool + created: int cachedEnvs: list["Environment"] = field(default_factory=list) interpreters: Interpreters = field(default_factory=Interpreters) @@ -417,6 +419,7 @@ def from_artifact(cls, obj: Artifacts.Object) -> Optional["Environment"]: type=spec.get("type", ""), tags=spec.tags, hidden=spec.hidden, + created=spec.created, interpreters=spec.get("interpreters", Interpreters()), ) except KeyError: @@ -553,7 +556,9 @@ def create_new_env( ) definitionData = yaml.dump(softpack_definition) - meta = dict(tags=sorted(set(env.tags or []))) + meta = dict( + tags=sorted(set(env.tags or [])), created=round(time()) + ) metaData = yaml.dump(meta) tree_oid = artifacts.create_files( diff --git a/tests/integration/test_environment.py b/tests/integration/test_environment.py index fad24cf..5181881 100644 --- a/tests/integration/test_environment.py +++ b/tests/integration/test_environment.py @@ -7,6 +7,7 @@ import datetime import io import json +import time from pathlib import Path from typing import Optional @@ -78,6 +79,7 @@ def test_create(httpx_post, testable_env_input: EnvironmentInput) -> None: meta_yml = file_in_remote(dir / artifacts.meta_file) expected_meta_yml = {"tags": []} actual_meta_yml = yaml.safe_load(meta_yml.data.decode()) + del actual_meta_yml["created"] assert actual_meta_yml == expected_meta_yml dir = Path( @@ -98,6 +100,7 @@ def test_create(httpx_post, testable_env_input: EnvironmentInput) -> None: meta_yml = file_in_remote(dir / artifacts.meta_file) expected_meta_yml = {"tags": ["bar", "foo"]} actual_meta_yml = yaml.safe_load(meta_yml.data.decode()) + del actual_meta_yml["created"] assert actual_meta_yml == expected_meta_yml result = Environment.create(testable_env_input) @@ -315,7 +318,9 @@ def test_iter_no_statuses(testable_env_input): @pytest.mark.asyncio async def test_states(httpx_post, testable_env_input, mocker): orig_name = testable_env_input.name + startTime = time.time() - 1 result = Environment.create(testable_env_input) + endTime = time.time() + 1 testable_env_input.name = orig_name assert isinstance(result, CreateEnvironmentSuccess) httpx_post.assert_called_once() @@ -349,6 +354,7 @@ async def test_states(httpx_post, testable_env_input, mocker): assert any(p.version == "v1.1" for p in env.packages) assert env.type == Artifacts.built_by_softpack assert env.state == State.queued + assert env.created >= startTime and env.created <= endTime upload = UploadFile( filename=Artifacts.builder_out, file=io.BytesIO(b"some output")