From 2cfcc429e7f97a03eee18c27f610ae6566138b1e Mon Sep 17 00:00:00 2001 From: Michael Woolnough <130465766+mjkw31@users.noreply.github.com> Date: Tue, 10 Sep 2024 14:48:41 +0100 Subject: [PATCH] Cache expensive data collection. (#57) --- softpack_core/artifacts.py | 2 ++ softpack_core/schemas/environment.py | 10 ++++++++-- softpack_core/schemas/package_collection.py | 9 ++++++--- softpack_core/spack.py | 3 +++ 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/softpack_core/artifacts.py b/softpack_core/artifacts.py index 96fae0d..6f9681e 100644 --- a/softpack_core/artifacts.py +++ b/softpack_core/artifacts.py @@ -81,6 +81,7 @@ class Artifacts: users_folder_name = "users" groups_folder_name = "groups" credentials_callback = None + updated = True @dataclass class Object: @@ -408,6 +409,7 @@ def commit_and_push( ) remote = self.repo.remotes[0] remote.push([self.repo.head.name], callbacks=self.credentials_callback) + self.updated = True return oid def build_tree( diff --git a/softpack_core/schemas/environment.py b/softpack_core/schemas/environment.py index c25697e..f036d84 100644 --- a/softpack_core/schemas/environment.py +++ b/softpack_core/schemas/environment.py @@ -8,7 +8,7 @@ import io import re import statistics -from dataclasses import dataclass +from dataclasses import dataclass, field from pathlib import Path from traceback import format_exception_only from typing import List, Optional, Tuple, Union, cast @@ -322,6 +322,7 @@ class Environment: state: Optional[State] tags: list[str] hidden: bool + cachedEnvs: list["Environment"] = field(default_factory=list) requested: Optional[datetime.datetime] = None build_start: Optional[datetime.datetime] = None @@ -335,6 +336,9 @@ def iter(cls) -> list["Environment"]: Returns: Iterable[Environment]: An iterator of Environment objects. """ + if not artifacts.updated: + return cls.cachedEnvs + statuses = BuildStatus.get_all() if isinstance(statuses, BuilderError): statuses = [] @@ -366,6 +370,9 @@ def iter(cls) -> list["Environment"]: env.build_start = status.build_start env.build_done = status.build_done + cls.cachedEnvs = environment_objects + artifacts.updated = False + return environment_objects @classmethod @@ -640,7 +647,6 @@ def store_metadata(cls, environment_path: Path, metadata: Box) -> None: metadata.to_yaml(), overwrite=True, ) - artifacts.commit_and_push(tree_oid, "update metadata") @classmethod diff --git a/softpack_core/schemas/package_collection.py b/softpack_core/schemas/package_collection.py index 6b0a3f2..2338c75 100644 --- a/softpack_core/schemas/package_collection.py +++ b/softpack_core/schemas/package_collection.py @@ -5,7 +5,6 @@ """ from dataclasses import dataclass -from typing import Iterable import strawberry @@ -26,14 +25,18 @@ class PackageCollection: packages: list[PackageMultiVersion] @classmethod - def iter(cls) -> Iterable["PackageMultiVersion"]: + def iter(cls) -> list["PackageMultiVersion"]: """Get an iterator over PackageCollection objects. Returns: Iterable[PackageCollection]: An iterator of PackageCollection objects. """ - return map(cls.from_package, app.spack.packages()) + if app.spack.packagesUpdated: + cls.packages = list(map(cls.from_package, app.spack.packages())) + app.spack.packagesUpdated = False + + return cls.packages @classmethod def from_package(cls, package: Package) -> PackageMultiVersion: diff --git a/softpack_core/spack.py b/softpack_core/spack.py index 0edc4fb..be72f15 100644 --- a/softpack_core/spack.py +++ b/softpack_core/spack.py @@ -30,6 +30,8 @@ class Package(PackageBase): class Spack: """Spack interface class.""" + packagesUpdated: bool = True + def __init__( self, spack_exe: str = "spack", @@ -97,6 +99,7 @@ def store_packages_from_spack( json.loads(jsonData), ) ) + self.packagesUpdated = True def __readPackagesFromCacheOnce(self) -> Tuple[bytes, bool]: if len(self.stored_packages) > 0 or self.cacheDir == "":