@@ -54,21 +54,52 @@ def __init__(self, rstatus=None, allocated_ranks=None):
5454 if allocated_ranks is None :
5555 allocated_ranks = IDset ()
5656
57+ self .rstatus = rstatus
5758 self .rset = ResourceSet (rstatus ["R" ])
59+ self .nodelist = self .rset .nodelist
60+ self .allocated_ranks = allocated_ranks
61+
62+ self ._recalculate ()
63+
64+ def filter (self , include ):
65+ """
66+ Filter the reported resources in a ResourceStatus object
67+ Args:
68+ include (str, IDset, Hostlist): restrict the current set of
69+ reported ranks to the given ranks or hosts.
70+ """
71+ try :
72+ include_ranks = IDset (include )
73+ except ValueError :
74+ include_ranks = self .nodelist .index (include , ignore_nomatch = True )
75+ self ._recalculate (include_ranks )
5876
59- # get idset of all ranks and nodelist:
77+ def _recalculate (self , include_ranks = None ):
78+ """
79+ Recalculate derived idsets and drain_info, only including ranks
80+ in the IDset 'include_ranks' if given.
81+ Args:
82+ include_ranks (IDset): restrict the current set of reported ranks.
83+ """
84+ # get idset of all ranks:
6085 self .all = self .rset .ranks
61- self .nodelist = self .rset .nodelist
6286
6387 # offline/online
64- self .offline = IDset (rstatus ["offline" ])
65- self .online = IDset (rstatus ["online" ])
88+ self .offline = IDset (self . rstatus ["offline" ])
89+ self .online = IDset (self . rstatus ["online" ])
6690
6791 # excluded: excluded by configuration
68- self .exclude = IDset (rstatus ["exclude" ])
92+ self .exclude = IDset (self . rstatus ["exclude" ])
6993
7094 # allocated: online and allocated by scheduler
71- self .allocated = allocated_ranks
95+ self .allocated = self .allocated_ranks
96+
97+ # If include_ranks was provided, filter all idsets to only those
98+ # that intersect the provided idset
99+ if include_ranks is not None :
100+ for name in ("all" , "offline" , "online" , "exclude" , "allocated" ):
101+ result = getattr (self , name ).intersect (include_ranks )
102+ setattr (self , name , result )
72103
73104 # drained: free+drain
74105 self .drained = IDset ()
@@ -77,8 +108,10 @@ def __init__(self, rstatus=None, allocated_ranks=None):
77108
78109 # drain_info: ranks, timestamp, reason tuples for all drained resources
79110 self .drain_info = []
80- for drain_ranks , entry in rstatus ["drain" ].items ():
111+ for drain_ranks , entry in self . rstatus ["drain" ].items ():
81112 ranks = IDset (drain_ranks )
113+ if include_ranks is not None :
114+ ranks = ranks .intersect (include_ranks )
82115 self .drained += ranks - self .allocated
83116 self .draining += ranks - self .drained
84117 info = DrainInfo (ranks , entry ["timestamp" ], entry ["reason" ])
0 commit comments