1+ from datetime import datetime
12from typing import TYPE_CHECKING , Dict , List , Optional , Union
23
34import aiohttp
1314 CrnV1List ,
1415 CrnV2List ,
1516 DictLikeModel ,
17+ VmResources ,
1618)
1719from aleph .sdk .utils import extract_valid_eth_address , sanitize_url
1820
1921if TYPE_CHECKING :
2022 from aleph .sdk .client .http import AlephHttpClient
2123
2224
25+ class CpuLoad (BaseModel ):
26+ load1 : float
27+ load5 : float
28+ load15 : float
29+
30+
31+ class CoreFrequencies (BaseModel ):
32+ min : float
33+ max : float
34+
35+
36+ class CpuInfo (BaseModel ):
37+ count : int
38+ load_average : CpuLoad
39+ core_frequencies : CoreFrequencies
40+
41+
42+ class MemoryInfo (BaseModel ):
43+ total_kB : int
44+ available_kB : int
45+
46+
47+ class DiskInfo (BaseModel ):
48+ total_kB : int
49+ available_kB : int
50+
51+
52+ class UsagePeriod (BaseModel ):
53+ start_timestamp : datetime
54+ duration_seconds : int
55+
56+
57+ class SystemUsage (BaseModel ):
58+ cpu : CpuInfo
59+ mem : MemoryInfo
60+ disk : DiskInfo
61+ period : UsagePeriod
62+ properties : dict
63+ gpu : dict
64+ active : bool
65+
66+
2367class GPU (BaseModel ):
2468 vendor : str
2569 model : str
@@ -47,6 +91,7 @@ class CRN(DictLikeModel):
4791 gpu_support : Optional [bool ] = False
4892 confidential_support : Optional [bool ] = False
4993 qemu_support : Optional [bool ] = False
94+ system_usage : Optional [SystemUsage ] = None
5095
5196 version : Optional [str ] = "0.0.0"
5297 payment_receiver_address : Optional [str ] # Can be None if not configured
@@ -102,6 +147,7 @@ def filter_crn(
102147 stream_address : bool = False ,
103148 confidential : bool = False ,
104149 gpu : bool = False ,
150+ vm_resources : Optional [VmResources ] = None ,
105151 ) -> list [CRN ]:
106152 """Filter compute resource node list, unfiltered by default.
107153 Args:
@@ -110,6 +156,7 @@ def filter_crn(
110156 stream_address (bool): Filter invalid payment receiver address.
111157 confidential (bool): Filter by confidential computing support.
112158 gpu (bool): Filter by GPU support.
159+ vm_resources (VmResources): Filter by VM need, vcpus, memory, disk.
113160 Returns:
114161 list[CRN]: List of compute resource nodes. (if no filter applied, return all)
115162 """
@@ -140,6 +187,28 @@ def filter_crn(
140187 if gpu and (not crn_ .gpu_support or not available_gpu ):
141188 continue
142189
190+ # Filter VM resources
191+ if vm_resources :
192+ sys = crn_ .system_usage
193+ if not sys :
194+ continue
195+
196+ # Check CPU count
197+ if sys .cpu .count < vm_resources .vcpus :
198+ continue
199+
200+ # Convert MiB to kB (1 MiB = 1024 kB) for proper comparison
201+ memory_kb_required = vm_resources .memory * 1024
202+ disk_kb_required = vm_resources .disk_mib * 1024
203+
204+ # Check free memory
205+ if sys .mem .available_kB < memory_kb_required :
206+ continue
207+
208+ # Check free disk
209+ if sys .disk .available_kB < disk_kb_required :
210+ continue
211+
143212 filtered_crn .append (crn_ )
144213 return filtered_crn
145214
0 commit comments