@@ -642,3 +642,106 @@ func TestGetMetricGroupBy(t *testing.T) {
642642 assert .Contains (t , err .Error (), "groupby dimension has no alias" )
643643 })
644644}
645+
646+ func TestQueryMetrics_GroupBySpaceID_SkipSpaceIds (t * testing.T ) {
647+ t .Parallel ()
648+ ctrl := gomock .NewController (t )
649+ defer ctrl .Finish ()
650+
651+ repoMock := repomocks .NewMockIMetricRepo (ctrl )
652+ tenantMock := tenantmocks .NewMockITenantProvider (ctrl )
653+ builderMock := traceServicemocks .NewMockTraceFilterProcessorBuilder (ctrl )
654+ filterMock := spanfiltermocks .NewMockFilter (ctrl )
655+ traceConfigMock := configmocks .NewMockITraceConfig (ctrl )
656+
657+ // Setup mocks
658+ tenantMock .EXPECT ().GetMetricTenantsByPlatformType (gomock .Any (), gomock .Any ()).Return ([]string {"tenant-1" }, nil )
659+ builderMock .EXPECT ().BuildPlatformRelatedFilter (gomock .Any (), gomock .Any ()).Return (filterMock , nil )
660+
661+ // BuildBasicSpanFilter returns filter with SpaceId=0
662+ filterMock .EXPECT ().BuildBasicSpanFilter (gomock .Any (), gomock .Any ()).DoAndReturn (
663+ func (_ context.Context , env * spanfilter.SpanEnv ) ([]* loop_span.FilterField , bool , error ) {
664+ return []* loop_span.FilterField {
665+ {
666+ FieldName : loop_span .SpanFieldSpaceId ,
667+ Values : []string {"0" },
668+ },
669+ }, true , nil
670+ },
671+ )
672+
673+ // TraceConfig returns SkipSpaceIds
674+ traceConfigMock .EXPECT ().GetMetricOfflineCalculateConfig (gomock .Any ()).Return (config.MetricOfflineCalculateConfig {
675+ SkipSpaceIds : []string {"space_skip_1" , "space_skip_2" },
676+ })
677+ traceConfigMock .EXPECT ().GetMetricQueryConfig (gomock .Any ()).Return (& config.MetricQueryConfig {
678+ SupportOffline : false ,
679+ }).AnyTimes ()
680+
681+ requestFilter := & loop_span.FilterFields {
682+ FilterFields : []* loop_span.FilterField {
683+ {FieldName : "some_req_field" , Values : []string {"val" }},
684+ },
685+ }
686+
687+ // Assert that filters are modified
688+ repoMock .EXPECT ().GetMetrics (gomock .Any (), gomock .Any ()).DoAndReturn (
689+ func (_ context.Context , param * repo.GetMetricsParam ) (* repo.GetMetricsResult , error ) {
690+ assert .NotNil (t , param .Filters )
691+ assert .Equal (t , loop_span .QueryAndOrEnumAnd , * param .Filters .QueryAndOr )
692+ // Should have 2 fields: space_id not in, and requestFilter
693+ assert .Len (t , param .Filters .FilterFields , 2 )
694+
695+ // Check first field: space_id not in skip_ids
696+ f1 := param .Filters .FilterFields [0 ]
697+ assert .Equal (t , loop_span .SpanFieldSpaceId , f1 .FieldName )
698+ assert .NotNil (t , f1 .QueryType )
699+ assert .Equal (t , loop_span .QueryTypeEnumNotIn , * f1 .QueryType )
700+ assert .ElementsMatch (t , []string {"space_skip_1" , "space_skip_2" }, f1 .Values )
701+
702+ // Check second field: requestFilter
703+ f2 := param .Filters .FilterFields [1 ]
704+ assert .Equal (t , loop_span .QueryAndOrEnumAnd , * f2 .QueryAndOr )
705+ assert .Equal (t , requestFilter , f2 .SubFilter )
706+
707+ return & repo.GetMetricsResult {
708+ Data : []map [string ]any {},
709+ }, nil
710+ },
711+ )
712+
713+ metricDef := & testMetricDefinition {
714+ name : "metric_a" ,
715+ metricType : entity .MetricTypeTimeSeries ,
716+ where : []* loop_span.FilterField {},
717+ }
718+ pMetrics := & entity.PlatformMetrics {
719+ MetricGroups : map [string ]* entity.MetricGroup {
720+ "test_group" : {
721+ MetricDefinitions : []entity.IMetricDefinition {metricDef },
722+ },
723+ },
724+ DrillDownObjects : map [string ]* loop_span.FilterField {},
725+ PlatformMetricDefs : map [loop_span.PlatformType ]* entity.PlatformMetricDef {
726+ loop_span .PlatformType ("loop" ): {
727+ MetricGroups : []string {"test_group" },
728+ },
729+ },
730+ }
731+
732+ svc , err := NewMetricsService (repoMock , nil , tenantMock , builderMock , traceConfigMock , pMetrics )
733+ assert .NoError (t , err )
734+
735+ _ , err = svc .QueryMetrics (context .Background (), & QueryMetricsReq {
736+ PlatformType : loop_span .PlatformType ("loop" ),
737+ WorkspaceID : 1 ,
738+ MetricsNames : []string {"metric_a" },
739+ Granularity : entity .MetricGranularity1Hour ,
740+ StartTime : 0 ,
741+ EndTime : 0 ,
742+ GroupBySpaceID : true ,
743+ FilterFields : requestFilter ,
744+ })
745+ assert .NoError (t , err )
746+ }
747+
0 commit comments