diff --git a/packages/grafana-prometheus/src/dataquery.ts b/packages/grafana-prometheus/src/dataquery.ts index 5aebae04a1f..74bf37d2986 100644 --- a/packages/grafana-prometheus/src/dataquery.ts +++ b/packages/grafana-prometheus/src/dataquery.ts @@ -1,5 +1,5 @@ // Core Grafana history https://github.com/grafana/grafana/blob/v11.0.0-preview/public/app/plugins/datasource/prometheus/dataquery.ts -import { ScopeSpec, ScopeSpecFilter } from '@grafana/data'; +import { Scope, ScopeSpec, ScopeSpecFilter } from '@grafana/data'; import * as common from '@grafana/schema'; export enum QueryEditorMode { @@ -43,7 +43,7 @@ export interface Prometheus extends common.DataQuery { * Returns a Range vector, comprised of a set of time series containing a range of data points over time for each time series */ range?: boolean; - scopes?: ScopeSpec[]; + scopes?: Array>; adhocFilters?: ScopeSpecFilter[]; groupByKeys?: string[]; } diff --git a/packages/grafana-prometheus/src/datasource.ts b/packages/grafana-prometheus/src/datasource.ts index 938ea4a690a..89c48b9b1f7 100644 --- a/packages/grafana-prometheus/src/datasource.ts +++ b/packages/grafana-prometheus/src/datasource.ts @@ -374,7 +374,10 @@ export class PrometheusDatasource }; if (config.featureToggles.promQLScope) { - processedTarget.scopes = (request.scopes ?? []).map((scope) => scope.spec); + processedTarget.scopes = (request.scopes ?? []).map((scope) => ({ + name: scope.metadata.name, + ...scope.spec, + })); } if (config.featureToggles.groupByVariable) { diff --git a/pkg/promlib/models/query.go b/pkg/promlib/models/query.go index c8c93e7e9b2..02031d4cb16 100644 --- a/pkg/promlib/models/query.go +++ b/pkg/promlib/models/query.go @@ -76,8 +76,9 @@ type PrometheusQueryProperties struct { } // ScopeSpec is a hand copy of the ScopeSpec struct from pkg/apis/scope/v0alpha1/types.go -// to avoid import (temp fix) +// to avoid import (temp fix). This also has metadata.name inlined. type ScopeSpec struct { + Name string `json:"name"` // This is the identifier from metadata.name of the scope model. Title string `json:"title"` Type string `json:"type"` Description string `json:"description"` diff --git a/pkg/promlib/models/query.panel.schema.json b/pkg/promlib/models/query.panel.schema.json index c6a7112bddc..0d3ca2c5d2e 100644 --- a/pkg/promlib/models/query.panel.schema.json +++ b/pkg/promlib/models/query.panel.schema.json @@ -177,9 +177,10 @@ "description": "A set of filters applied to apply to the query", "type": "array", "items": { - "description": "ScopeSpec is a hand copy of the ScopeSpec struct from pkg/apis/scope/v0alpha1/types.go to avoid import (temp fix)", + "description": "ScopeSpec is a hand copy of the ScopeSpec struct from pkg/apis/scope/v0alpha1/types.go to avoid import (temp fix).", "type": "object", "required": [ + "name", "title", "type", "description", @@ -217,6 +218,10 @@ "additionalProperties": false } }, + "name": { + "description": "This is the identifier from metadata.name of the scope model.", + "type": "string" + }, "title": { "type": "string" }, diff --git a/pkg/promlib/models/query.request.schema.json b/pkg/promlib/models/query.request.schema.json index ef8c6b038ef..27bbd8310d0 100644 --- a/pkg/promlib/models/query.request.schema.json +++ b/pkg/promlib/models/query.request.schema.json @@ -187,9 +187,10 @@ "description": "A set of filters applied to apply to the query", "type": "array", "items": { - "description": "ScopeSpec is a hand copy of the ScopeSpec struct from pkg/apis/scope/v0alpha1/types.go to avoid import (temp fix)", + "description": "ScopeSpec is a hand copy of the ScopeSpec struct from pkg/apis/scope/v0alpha1/types.go to avoid import (temp fix).", "type": "object", "required": [ + "name", "title", "type", "description", @@ -227,6 +228,10 @@ "additionalProperties": false } }, + "name": { + "description": "This is the identifier from metadata.name of the scope model.", + "type": "string" + }, "title": { "type": "string" }, diff --git a/pkg/promlib/models/query.types.json b/pkg/promlib/models/query.types.json index 8aa5e1ca116..1d6a5b71fbf 100644 --- a/pkg/promlib/models/query.types.json +++ b/pkg/promlib/models/query.types.json @@ -8,7 +8,7 @@ { "metadata": { "name": "default", - "resourceVersion": "1715781995240", + "resourceVersion": "1715871691891", "creationTimestamp": "2024-03-25T13:19:04Z" }, "spec": { @@ -96,7 +96,7 @@ "description": "A set of filters applied to apply to the query", "items": { "additionalProperties": false, - "description": "ScopeSpec is a hand copy of the ScopeSpec struct from pkg/apis/scope/v0alpha1/types.go to avoid import (temp fix)", + "description": "ScopeSpec is a hand copy of the ScopeSpec struct from pkg/apis/scope/v0alpha1/types.go to avoid import (temp fix).", "properties": { "category": { "type": "string" @@ -128,6 +128,10 @@ }, "type": "array" }, + "name": { + "description": "This is the identifier from metadata.name of the scope model.", + "type": "string" + }, "title": { "type": "string" }, @@ -136,6 +140,7 @@ } }, "required": [ + "name", "title", "type", "description", diff --git a/public/app/features/dashboard-scene/scene/ScopesFiltersScene.tsx b/public/app/features/dashboard-scene/scene/ScopesFiltersScene.tsx index ff95952ab94..632223b818e 100644 --- a/public/app/features/dashboard-scene/scene/ScopesFiltersScene.tsx +++ b/public/app/features/dashboard-scene/scene/ScopesFiltersScene.tsx @@ -53,13 +53,17 @@ export class ScopesFiltersScene extends SceneObjectBase } updateFromUrl(values: SceneObjectUrlValues) { - let scopes = values.scopes ?? []; - scopes = Array.isArray(scopes) ? scopes : [scopes]; + let scopesNames = values.scopes ?? []; + scopesNames = Array.isArray(scopesNames) ? scopesNames : [scopesNames]; - const scopesPromises = scopes.map((scopeName) => this.server.get(scopeName)); + const scopesPromises = scopesNames.map((scopeName) => this.server.get(scopeName)); Promise.all(scopesPromises).then((scopes) => { - this.setState({ scopes }); + this.setState({ + scopes: scopesNames.map((scopeName, scopeNameIdx) => + this.mergeScopeNameWithScopes(scopeName, scopes[scopeNameIdx] ?? {}) + ), + }); }); } @@ -134,21 +138,37 @@ export class ScopesFiltersScene extends SceneObjectBase } public async toggleScope(linkId: string) { - let scopes = this.state.scopes; + let scopes = [...this.state.scopes]; const selectedIdx = scopes.findIndex((scope) => scope.metadata.name === linkId); if (selectedIdx === -1) { - const scope = await this.server.get(linkId); + const receivedScope = await this.server.get(linkId); - if (scope) { - scopes = [...scopes, scope]; - } + scopes.push(this.mergeScopeNameWithScopes(linkId, receivedScope ?? {})); } else { scopes.splice(selectedIdx, 1); } this.setState({ scopes }); } + + private mergeScopeNameWithScopes(scopeName: string, scope: Partial): Scope { + return { + ...scope, + metadata: { + name: scopeName, + ...scope.metadata, + }, + spec: { + filters: [], + title: scopeName, + type: '', + category: '', + description: '', + ...scope.spec, + }, + }; + } } export function ScopesFiltersSceneRenderer({ model }: SceneComponentProps) {