Skip to content

Commit 7bb47d7

Browse files
committed
refactor
1 parent da84e5a commit 7bb47d7

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

packages/aws-library/src/aws_library/ec2/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
EC2InstanceData,
1818
EC2InstanceType,
1919
EC2Tags,
20+
GenericResourceValueType,
2021
Resources,
2122
)
2223

@@ -36,6 +37,7 @@
3637
"EC2NotConnectedError",
3738
"EC2RuntimeError",
3839
"EC2Tags",
40+
"GenericResourceValueType",
3941
"Resources",
4042
"SimcoreEC2API",
4143
)

packages/aws-library/src/aws_library/ec2/_models.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
from pydantic.config import JsonDict
2323
from types_aiobotocore_ec2.literals import InstanceStateNameType, InstanceTypeType
2424

25-
GenericResourceValue: TypeAlias = StrictInt | StrictFloat | str
25+
GenericResourceValueType: TypeAlias = StrictInt | StrictFloat | str
2626

2727

2828
class Resources(BaseModel, frozen=True):
2929
cpus: NonNegativeFloat
3030
ram: ByteSize
3131
generic_resources: Annotated[
32-
dict[str, GenericResourceValue],
32+
dict[str, GenericResourceValueType],
3333
Field(
3434
default_factory=dict,
3535
description=(
@@ -83,7 +83,7 @@ def __add__(self, other: "Resources") -> "Resources":
8383
Note that only numeric generic resources are added
8484
Non-numeric generic resources are ignored
8585
"""
86-
merged: dict[str, GenericResourceValue] = {}
86+
merged: dict[str, GenericResourceValueType] = {}
8787
keys = set(self.generic_resources) | set(other.generic_resources)
8888
for k in keys:
8989
a = self.generic_resources.get(k)
@@ -107,7 +107,7 @@ def __sub__(self, other: "Resources") -> "Resources":
107107
Note that only numeric generic resources are subtracted
108108
Non-numeric generic resources are ignored
109109
"""
110-
merged: dict[str, GenericResourceValue] = {}
110+
merged: dict[str, GenericResourceValueType] = {}
111111
keys = set(self.generic_resources) | set(other.generic_resources)
112112
for k in keys:
113113
a = self.generic_resources.get(k)
@@ -129,7 +129,7 @@ def __sub__(self, other: "Resources") -> "Resources":
129129
def __hash__(self) -> int:
130130
"""Deterministic hash including cpus, ram (in bytes) and generic_resources."""
131131
# sort generic_resources items to ensure order-independent hashing
132-
generic_items: tuple[tuple[str, GenericResourceValue], ...] = tuple(
132+
generic_items: tuple[tuple[str, GenericResourceValueType], ...] = tuple(
133133
sorted(self.generic_resources.items())
134134
)
135135
return hash((self.cpus, self.ram, generic_items))

services/autoscaling/src/simcore_service_autoscaling/modules/cluster_scaling/_utils_computational.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
2-
from typing import Final
2+
from typing import Final, cast
33

4-
from aws_library.ec2 import Resources
4+
from aws_library.ec2 import GenericResourceValueType, Resources
55
from dask_task_models_library.resource_constraints import (
66
DaskTaskResources,
77
get_ec2_instance_type_from_resources,
@@ -30,7 +30,8 @@ def resources_from_dask_task(task: DaskTask) -> Resources:
3030
) # merge with defaults to ensure there is always some minimal resource defined
3131

3232
return Resources.from_flat_dict(
33-
task_resources, mapping=DASK_TO_RESOURCE_NAME_MAPPING
33+
cast(dict[str, GenericResourceValueType], task_resources),
34+
mapping=DASK_TO_RESOURCE_NAME_MAPPING,
3435
)
3536

3637

services/autoscaling/src/simcore_service_autoscaling/modules/dask.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ async def _list_cluster_known_tasks(
135135
def _list_on_scheduler(
136136
dask_scheduler: distributed.Scheduler,
137137
) -> _DaskClusterTasks:
138-
139138
worker_to_processing_tasks = defaultdict(list)
140139
unrunnable_tasks = {}
141140
for task_key, task_state in dask_scheduler.tasks.items():
@@ -319,13 +318,13 @@ async def compute_cluster_total_resources(
319318
continue
320319
# get dask information about resources
321320
worker_dask_resources = worker_details["resources"]
322-
worker_threads = worker_details["nthreads"]
323-
worker_dask_resources: dict[str, int | float | str] = {
324-
**worker_dask_resources,
325-
DASK_WORKER_THREAD_RESOURCE_NAME: worker_threads,
326-
}
321+
worker_dask_nthreads = worker_details["nthreads"]
327322
cluster_resources += Resources.from_flat_dict(
328-
worker_dask_resources, mapping=DASK_TO_RESOURCE_NAME_MAPPING
323+
{
324+
**worker_dask_resources,
325+
DASK_WORKER_THREAD_RESOURCE_NAME: worker_dask_nthreads,
326+
},
327+
mapping=DASK_TO_RESOURCE_NAME_MAPPING,
329328
)
330329

331330
return cluster_resources

0 commit comments

Comments
 (0)