Skip to content

Commit

Permalink
feat(update_cache): Add power control to cache
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKevinWeiss committed Feb 22, 2024
1 parent 6cf691f commit 6a9a4af
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/inet_nm/cli_update_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import inet_nm.config as cfg
import inet_nm.location as loc
from inet_nm._helpers import nm_print
from inet_nm.power_control import DEFAULT_MAX_ALLOWED_NODES, PowerControl


def _main():
Expand All @@ -14,9 +15,17 @@ def _main():
nodes = cfg.NodesConfig(config_dir=args.config).load()
loc_cache = cfg.LocationCache(config_dir=args.config)
loc_cache.check_file(writable=True)

cache = loc.get_location_cache(nodes, loc_mapping)

caches = []
with PowerControl(
locations=loc_mapping,
nodes=nodes,
max_powered_devices=DEFAULT_MAX_ALLOWED_NODES,
) as pc:
while not pc.power_on_complete:
pc.power_on_chunk()
caches.append(loc.get_location_cache(nodes, loc_mapping))
pc.power_off_unused()
cache = loc.merge_location_cache_chunks(caches)
loc_cache.save(cache)
nm_print(f"Updated {loc_cache.file_path}")

Expand Down
33 changes: 33 additions & 0 deletions src/inet_nm/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,39 @@
from inet_nm.data_types import NmNode


def merge_location_cache_chunks(caches: List[List[Dict]]):
"""
Merge location cache chunks into a single cache.
Due to only being able to power on a chunk at a time we need to sort
through each of the location caches and look through all id_paths that
have a missing state and see if they are available in another chunk.
If they are then probably they were just powered off.
Args:
caches: List of location cache chunks.
Returns:
The merged location cache.
"""
# TODO: Also check if all id_paths that are attached have the same node_uid
tmp_cache = {}
for chunk in caches:
for entry in chunk:
# If entry is empty, skip it
if not entry:
continue
if entry["state"] != "missing":
tmp_cache[entry["id_path"]] = entry
continue
if entry["state"] == "missing" and entry["id_path"] not in tmp_cache:
tmp_cache[entry["id_path"]] = entry
# Convert tmp_cache to list
cache = list(tmp_cache.values())
cache.sort(key=lambda x: x["id_path"])
return cache


def get_location_cache(nodes: List[NmNode], id_paths: Dict):
"""
Get the location cache for a list of NmNode objects.
Expand Down
53 changes: 53 additions & 0 deletions tests/test_location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import pytest

import inet_nm.location as loc


@pytest.mark.parametrize(
"caches",
[
[[{}]],
[[{"id_path": "1", "node_uid": "1", "state": "attached"}]],
[
[
{"id_path": "1", "node_uid": "1", "state": "attached"},
],
[{"id_path": "1", "node_uid": "1", "state": "missing"}],
],
[
[
{"id_path": "1", "node_uid": "1", "state": "unassigned"},
{"id_path": "2", "node_uid": "2", "state": "missing"},
],
[{"id_path": "2", "node_uid": "2", "state": "attached"}],
],
],
)
def test_merge_location_cache_chunks_no_missing(caches):
cache = loc.merge_location_cache_chunks(caches)
assert not any(entry["state"] == "missing" for entry in cache), cache


@pytest.mark.parametrize(
"caches",
[
[[{"id_path": "1", "node_uid": "1", "state": "missing"}]],
[
[
{"id_path": "1", "node_uid": "1", "state": "attached"},
{"id_path": "2", "node_uid": "2", "state": "missing"},
],
],
[
[
{"id_path": "1", "node_uid": "1", "state": "missing"},
],
[
{"id_path": "1", "node_uid": "1", "state": "missing"},
],
],
],
)
def test_merge_location_cache_chunks_missing(caches):
cache = loc.merge_location_cache_chunks(caches)
assert any(entry["state"] == "missing" for entry in cache), cache

0 comments on commit 6a9a4af

Please sign in to comment.