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

Cache expensive data collection. #57

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions softpack_core/artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class Artifacts:
users_folder_name = "users"
groups_folder_name = "groups"
credentials_callback = None
updated = True

@dataclass
class Object:
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 8 additions & 2 deletions softpack_core/schemas/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 = []
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions softpack_core/schemas/package_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""

from dataclasses import dataclass
from typing import Iterable

import strawberry

Expand All @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions softpack_core/spack.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Package(PackageBase):
class Spack:
"""Spack interface class."""

packagesUpdated: bool = True

def __init__(
self,
spack_exe: str = "spack",
Expand Down Expand Up @@ -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 == "":
Expand Down
Loading