2424 KernelId ,
2525 SessionId ,
2626)
27+ from ai .backend .manager .api .gql_legacy .stat_converter import LegacyLiveStatConverter
2728from ai .backend .manager .data .kernel .types import KernelStatus
2829from ai .backend .manager .defs import DEFAULT_ROLE
2930from ai .backend .manager .models .group import groups
4243 QueryFilterParser ,
4344)
4445from ai .backend .manager .models .user import UserRole , users
46+ from ai .backend .manager .services .metric .actions .live_stat import ContainerLiveStatAction
4547
4648from .base import (
4749 BigInt ,
6769)
6870
6971
72+ async def _batch_load_kernel_live_stat (
73+ ctx : GraphQueryContext ,
74+ kernel_ids : Sequence [KernelId ],
75+ ) -> list [dict [str , Any ] | None ]:
76+ """Prometheus-backed replacement for the old Valkey `KernelStatistics.by_kernel`
77+ loader. Returns the legacy `dict[metric_name, MetricValue]` shape (or `None`
78+ when the kernel has no Prometheus samples) preserving wire compatibility.
79+ """
80+ if not kernel_ids :
81+ return []
82+ action_result = await ctx .processors .metric .query_container_live_stat .wait_for_complete (
83+ ContainerLiveStatAction (kernel_ids = list (kernel_ids ))
84+ )
85+ converted = LegacyLiveStatConverter ().convert (action_result .stats )
86+ return [converted .get (kid ) for kid in kernel_ids ]
87+
88+
7089class KernelNode (graphene .ObjectType ): # type: ignore[misc]
7190 class Meta :
7291 interfaces = (AsyncNode ,)
@@ -190,17 +209,10 @@ async def resolve_image(self, info: graphene.ResolveInfo) -> ImageNode | None:
190209 async def resolve_live_stat (self , info : graphene .ResolveInfo ) -> dict [str , Any ] | None :
191210 graph_ctx : GraphQueryContext = info .context
192211 loader = graph_ctx .dataloader_manager .get_loader_by_func (
193- graph_ctx , self . batch_load_live_stat
212+ graph_ctx , _batch_load_kernel_live_stat
194213 )
195214 return cast (dict [str , Any ] | None , await loader .load (self .row_id ))
196215
197- @classmethod
198- async def batch_load_live_stat (
199- cls , ctx : GraphQueryContext , kernel_ids : Sequence [KernelId ]
200- ) -> list [dict [str , Any ] | None ]:
201- kernel_ids_str = [str (kid ) for kid in kernel_ids ]
202- return await ctx .valkey_stat .get_session_statistics_batch (kernel_ids_str )
203-
204216
205217class KernelConnection (Connection ):
206218 class Meta :
@@ -313,7 +325,9 @@ def from_row(cls, ctx: GraphQueryContext, row: KernelRow | None) -> ComputeConta
313325 # we can leave last_stat value for legacy support, as an alias to last_stat
314326 async def resolve_live_stat (self , info : graphene .ResolveInfo ) -> Mapping [str , Any ] | None :
315327 graph_ctx : GraphQueryContext = info .context
316- loader = graph_ctx .dataloader_manager .get_loader (graph_ctx , "KernelStatistics.by_kernel" )
328+ loader = graph_ctx .dataloader_manager .get_loader_by_func (
329+ graph_ctx , _batch_load_kernel_live_stat
330+ )
317331 return cast (Mapping [str , Any ] | None , await loader .load (self .id ))
318332
319333 async def resolve_last_stat (self , info : graphene .ResolveInfo ) -> Mapping [str , Any ] | None :
@@ -606,7 +620,9 @@ class Meta:
606620 # we can leave last_stat value for legacy support, as an alias to last_stat
607621 async def resolve_live_stat (self , info : graphene .ResolveInfo ) -> Mapping [str , Any ] | None :
608622 graph_ctx : GraphQueryContext = info .context
609- loader = graph_ctx .dataloader_manager .get_loader (graph_ctx , "KernelStatistics.by_kernel" )
623+ loader = graph_ctx .dataloader_manager .get_loader_by_func (
624+ graph_ctx , _batch_load_kernel_live_stat
625+ )
610626 return cast (Mapping [str , Any ] | None , await loader .load (self .id ))
611627
612628 async def resolve_last_stat (self , info : graphene .ResolveInfo ) -> Mapping [str , Any ] | None :
@@ -632,7 +648,9 @@ async def _resolve_legacy_metric(
632648 if value is None :
633649 return convert_type (0 )
634650 return convert_type (value )
635- loader = graph_ctx .dataloader_manager .get_loader (graph_ctx , "KernelStatistics.by_kernel" )
651+ loader = graph_ctx .dataloader_manager .get_loader_by_func (
652+ graph_ctx , _batch_load_kernel_live_stat
653+ )
636654 kstat = await loader .load (self .id )
637655 if kstat is None :
638656 return convert_type (0 )
0 commit comments