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

optimize various bits around scan profiles #4050

Merged
merged 21 commits into from
Feb 12, 2025
Merged
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
c818bea
optimize loops in recalculate_scan_profiles
underdarknl Jan 28, 2025
2882956
Update service.py
underdarknl Jan 28, 2025
8cc1c57
Update service.py
underdarknl Jan 28, 2025
cc52e42
add type hinting to satisfy MyPy
underdarknl Jan 28, 2025
9acafe9
Only split once, when extracting the object_type form a reference.
underdarknl Jan 28, 2025
a61647d
Update service.py
underdarknl Jan 28, 2025
d7e4194
Update __init__.py
underdarknl Jan 28, 2025
f36c6b1
Merge branch 'main' into optimize/recalculate_scan_profiles_loops
underdarknl Jan 29, 2025
655f016
Update scan_profile_repository.py
underdarknl Jan 30, 2025
6da83c3
Update scan_profile_repository.py
underdarknl Jan 30, 2025
456f1fd
Update scan_profile_repository.py
underdarknl Jan 30, 2025
22f9709
Merge branch 'main' into optimize/recalculate_scan_profiles_loops
underdarknl Jan 30, 2025
9c45a6f
Update scan_profile_repository.py
underdarknl Jan 30, 2025
8f68bb3
Update octopoes/octopoes/models/__init__.py
underdarknl Jan 30, 2025
d76623d
Merge branch 'main' into optimize/recalculate_scan_profiles_loops
underdarknl Feb 5, 2025
f027df5
Update __init__.py
underdarknl Feb 5, 2025
d4f7834
Update __init__.py
underdarknl Feb 5, 2025
d4d6966
Update __init__.py
underdarknl Feb 5, 2025
2b77979
Merge branch 'main' into optimize/recalculate_scan_profiles_loops
underdarknl Feb 6, 2025
fbf4a6a
Merge branch 'main' into optimize/recalculate_scan_profiles_loops
underdarknl Feb 6, 2025
1292b4f
Merge branch 'main' into optimize/recalculate_scan_profiles_loops
Donnype Feb 12, 2025
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
29 changes: 13 additions & 16 deletions octopoes/octopoes/core/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,21 +264,19 @@ def recalculate_scan_profiles(self, valid_time: datetime) -> None:
# fetch all scan profiles
all_scan_profiles = self.scan_profile_repository.list_scan_profiles(None, valid_time=valid_time)

# cache all declared
all_declared_scan_profiles = {
scan_profile for scan_profile in all_scan_profiles if isinstance(scan_profile, DeclaredScanProfile)
}
# cache all inherited
inherited_scan_profiles = {
scan_profile.reference: scan_profile
for scan_profile in all_scan_profiles
if isinstance(scan_profile, InheritedScanProfile)
}

# track all scan level assignments
assigned_scan_levels: dict[Reference, ScanLevel] = {
scan_profile.reference: scan_profile.level for scan_profile in all_declared_scan_profiles
}
all_declared_scan_profiles: set[DeclaredScanProfile] = set()
inherited_scan_profiles: dict[Reference, InheritedScanProfile] = {}
assigned_scan_levels: dict[Reference, ScanLevel] = {}
source_scan_profile_references: set[Reference] = set()

# fill profile caches
for scan_profile in all_scan_profiles:
if isinstance(scan_profile, DeclaredScanProfile):
all_declared_scan_profiles.add(scan_profile)
assigned_scan_levels[scan_profile.reference] = scan_profile.level
source_scan_profile_references.add(scan_profile.reference)
elif isinstance(scan_profile, InheritedScanProfile):
inherited_scan_profiles[scan_profile.reference] = scan_profile

for current_level in range(4, 0, -1):
# start point: all scan profiles with current level + all higher scan levels
Expand Down Expand Up @@ -331,7 +329,6 @@ def recalculate_scan_profiles(self, valid_time: datetime) -> None:

# Save all assigned scan levels
update_count = 0
source_scan_profile_references = {sp.reference for sp in all_declared_scan_profiles}
for reference, scan_level in assigned_scan_levels.items():
# Skip source scan profiles
if reference in source_scan_profile_references:
Expand Down
4 changes: 3 additions & 1 deletion octopoes/octopoes/repositories/scan_profile_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from octopoes.xtdb.client import XTDBSession
from octopoes.xtdb.query_builder import generate_pull_query

scan_profile_adapter = TypeAdapter(ScanProfile)


class ScanProfileRepository(Repository):
def __init__(self, event_manager: EventManager):
Expand Down Expand Up @@ -62,7 +64,7 @@ def serialize(cls, scan_profile: ScanProfile) -> dict[str, Any]:

@classmethod
def deserialize(cls, data: dict[str, Any]) -> ScanProfileBase:
return TypeAdapter(ScanProfile).validate_python(data)
return scan_profile_adapter.validate_python(data)

def list_scan_profiles(self, scan_profile_type: str | None, valid_time: datetime) -> list[ScanProfileBase]:
where = {"type": self.object_type}
Expand Down
Loading