cog icon indicating copy to clipboard operation
cog copied to clipboard

Update the schema metadata in DataqueryIdentification

Open K-Phoen opened this issue 1 year ago • 1 comments

When a dataquery is successfully identified by the DataqueryIdentification compiler pass, it should update the schema metadata to reflect that fact.

Possible alternative to #398

K-Phoen avatar May 23 '24 08:05 K-Phoen

Note: in addition to the changes introduced by this PR, the diff includes unreleased changes living in main.

🔎 Changes to grafana-foundation-sdk@next+cog-v0.0.x

diff --git a/go/cog/plugins/variants.go b/go/cog/plugins/variants.go
index 7645e7b..bdfe062 100644
--- a/go/cog/plugins/variants.go
+++ b/go/cog/plugins/variants.go
@@ -15,6 +15,7 @@ import (
 	datagrid "github.com/grafana/grafana-foundation-sdk/go/datagrid"
 	debug "github.com/grafana/grafana-foundation-sdk/go/debug"
 	elasticsearch "github.com/grafana/grafana-foundation-sdk/go/elasticsearch"
+	expr "github.com/grafana/grafana-foundation-sdk/go/expr"
 	gauge "github.com/grafana/grafana-foundation-sdk/go/gauge"
 	geomap "github.com/grafana/grafana-foundation-sdk/go/geomap"
 	googlecloudmonitoring "github.com/grafana/grafana-foundation-sdk/go/googlecloudmonitoring"
@@ -72,6 +73,7 @@ func RegisterDefaultPlugins() {
 	runtime.RegisterDataqueryVariant(azuremonitor.VariantConfig())
 	runtime.RegisterDataqueryVariant(cloudwatch.VariantConfig())
 	runtime.RegisterDataqueryVariant(elasticsearch.VariantConfig())
+	runtime.RegisterDataqueryVariant(expr.VariantConfig())
 	runtime.RegisterDataqueryVariant(googlecloudmonitoring.VariantConfig())
 	runtime.RegisterDataqueryVariant(grafanapyroscope.VariantConfig())
 	runtime.RegisterDataqueryVariant(loki.VariantConfig())
diff --git a/go/common/dataquery_builder_gen.go b/go/common/dataquery_builder_gen.go
index 72bf43c..a34c2ef 100644
--- a/go/common/dataquery_builder_gen.go
+++ b/go/common/dataquery_builder_gen.go
@@ -4,10 +4,9 @@ package common
 
 import (
 	cog "github.com/grafana/grafana-foundation-sdk/go/cog"
-	cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants"
 )
 
-var _ cog.Builder[cogvariants.Dataquery] = (*DataQueryBuilder)(nil)
+var _ cog.Builder[DataQuery] = (*DataQueryBuilder)(nil)
 
 // These are the common properties available to all queries in all datasources.
 // Specific implementations will *extend* this interface, adding the required
@@ -29,7 +28,7 @@ func NewDataQueryBuilder() *DataQueryBuilder {
 	return builder
 }
 
-func (builder *DataQueryBuilder) Build() (cogvariants.Dataquery, error) {
+func (builder *DataQueryBuilder) Build() (DataQuery, error) {
 	var errs cog.BuildErrors
 
 	for _, err := range builder.errors {
diff --git a/go/common/types_gen.go b/go/common/types_gen.go
index 64201f2..4cb5641 100644
--- a/go/common/types_gen.go
+++ b/go/common/types_gen.go
@@ -41,8 +41,6 @@ type DataQuery struct {
 	Datasource any `json:"datasource,omitempty"`
 }
 
-func (resource DataQuery) ImplementsDataqueryVariant() {}
-
 type BaseDimensionConfig struct {
 	// fixed: T -- will be added by each element
 	Field *string `json:"field,omitempty"`
diff --git a/go/common/types_json_marshalling_gen.go b/go/common/types_json_marshalling_gen.go
index 708e1e7..8a380db 100644
--- a/go/common/types_json_marshalling_gen.go
+++ b/go/common/types_json_marshalling_gen.go
@@ -6,25 +6,8 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
-
-	cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants"
 )
 
-func VariantConfig() cogvariants.DataqueryConfig {
-	return cogvariants.DataqueryConfig{
-		Identifier: "",
-		DataqueryUnmarshaler: func(raw []byte) (cogvariants.Dataquery, error) {
-			dataquery := DataQuery{}
-
-			if err := json.Unmarshal(raw, &dataquery); err != nil {
-				return nil, err
-			}
-
-			return dataquery, nil
-		},
-	}
-}
-
 func (resource BoolOrFloat64) MarshalJSON() ([]byte, error) {
 	if resource.Bool != nil {
 		return json.Marshal(resource.Bool)
diff --git a/openapi/expr.openapi.json b/openapi/expr.openapi.json
index 9e45b48..72eedcd 100644
--- a/openapi/expr.openapi.json
+++ b/openapi/expr.openapi.json
@@ -4,7 +4,8 @@
     "title": "expr",
     "version": "0.0.0",
     "x-schema-identifier": "__expr__",
-    "x-schema-kind": ""
+    "x-schema-kind": "composable",
+    "x-schema-variant": "dataquery"
   },
   "paths": {},
   "components": {
diff --git a/openapi/testdata.openapi.json b/openapi/testdata.openapi.json
index 1cf10b9..ac9ed8e 100644
--- a/openapi/testdata.openapi.json
+++ b/openapi/testdata.openapi.json
@@ -4,7 +4,8 @@
     "title": "testdata",
     "version": "0.0.0",
     "x-schema-identifier": "",
-    "x-schema-kind": ""
+    "x-schema-kind": "composable",
+    "x-schema-variant": "dataquery"
   },
   "paths": {},
   "components": {
diff --git a/python/grafana_foundation_sdk/cog/plugins.py b/python/grafana_foundation_sdk/cog/plugins.py
index b2cec56..10d633b 100644
--- a/python/grafana_foundation_sdk/cog/plugins.py
+++ b/python/grafana_foundation_sdk/cog/plugins.py
@@ -1,38 +1,39 @@
 # Code generated - EDITING IS FUTILE. DO NOT EDIT.
 
-from ..models import azuremonitor
-from ..models import parca
-from ..models import xychart
-from ..models import news
+from ..models import debug
+from ..models import logs
 from ..models import annotationslist
-from ..models import datagrid
-from ..models import geomap
-from ..models import googlecloudmonitoring
-from ..models import grafanapyroscope
-from ..models import nodegraph
+from ..models import barchart
 from ..models import cloudwatch
-from ..models import logs
-from ..models import trend
-from ..models import statetimeline
-from ..models import statushistory
-from ..models import bargauge
-from ..models import candlestick
 from ..models import dashboardlist
-from ..models import elasticsearch
+from ..models import nodegraph
 from ..models import stat
-from ..models import text
+from ..models import trend
+from ..models import news
+from ..models import table
 from ..models import canvas
-from ..models import debug
-from ..models import histogram
+from ..models import googlecloudmonitoring
+from ..models import piechart
 from ..models import prometheus
-from ..models import barchart
+from ..models import azuremonitor
+from ..models import datagrid
 from ..models import gauge
-from ..models import piechart
+from ..models import bargauge
+from ..models import histogram
+from ..models import statetimeline
+from ..models import timeseries
+from ..models import xychart
+from ..models import expr
+from ..models import elasticsearch
+from ..models import geomap
 from ..models import heatmap
 from ..models import loki
-from ..models import table
+from ..models import statushistory
+from ..models import text
+from ..models import candlestick
+from ..models import grafanapyroscope
+from ..models import parca
 from ..models import tempo
-from ..models import timeseries
 from . import runtime as cogruntime
 
 
@@ -67,6 +68,7 @@ def register_default_plugins():
     cogruntime.register_dataquery_variant(azuremonitor.variant_config())
     cogruntime.register_dataquery_variant(cloudwatch.variant_config())
     cogruntime.register_dataquery_variant(elasticsearch.variant_config())
+    cogruntime.register_dataquery_variant(expr.variant_config())
     cogruntime.register_dataquery_variant(googlecloudmonitoring.variant_config())
     cogruntime.register_dataquery_variant(grafanapyroscope.variant_config())
     cogruntime.register_dataquery_variant(loki.variant_config())
diff --git a/python/grafana_foundation_sdk/models/cloudwatch.py b/python/grafana_foundation_sdk/models/cloudwatch.py
index 761fcb8..8ef9b81 100644
--- a/python/grafana_foundation_sdk/models/cloudwatch.py
+++ b/python/grafana_foundation_sdk/models/cloudwatch.py
@@ -332,7 +332,7 @@ class SQLExpression:
         if "select" in data:
             args["select"] = QueryEditorFunctionExpression.from_json(data["select"])
         if "from" in data:
-            decoding_map: dict[str, typing.Union[typing.Type[QueryEditorPropertyExpression], typing.Type[QueryEditorFunctionExpression]]] = {"property": QueryEditorPropertyExpression, "function": QueryEditorFunctionExpression}
+            decoding_map: dict[str, typing.Union[typing.Type[QueryEditorFunctionExpression], typing.Type[QueryEditorPropertyExpression]]] = {"function": QueryEditorFunctionExpression, "property": QueryEditorPropertyExpression}
             args["from_val"] = decoding_map[data["from"]["type"]].from_json(data["from"])
         if "where" in data:
             args["where"] = QueryEditorArrayExpression.from_json(data["where"])
@@ -894,7 +894,7 @@ CloudWatchQuery: typing.TypeAlias = typing.Union['CloudWatchMetricsQuery', 'Clou
 
 
 def variant_config() -> cogruntime.DataqueryConfig:
-    decoding_map: dict[str, typing.Union[typing.Type[CloudWatchMetricsQuery], typing.Type[CloudWatchLogsQuery], typing.Type[CloudWatchAnnotationQuery]]] = {"Metrics": CloudWatchMetricsQuery, "Logs": CloudWatchLogsQuery, "Annotations": CloudWatchAnnotationQuery}
+    decoding_map: dict[str, typing.Union[typing.Type[CloudWatchAnnotationQuery], typing.Type[CloudWatchMetricsQuery], typing.Type[CloudWatchLogsQuery]]] = {"Annotations": CloudWatchAnnotationQuery, "Metrics": CloudWatchMetricsQuery, "Logs": CloudWatchLogsQuery}
     return cogruntime.DataqueryConfig(
         identifier="cloudwatch",
         from_json_hook=lambda data: decoding_map[data["queryMode"]].from_json(data),
diff --git a/python/grafana_foundation_sdk/models/common.py b/python/grafana_foundation_sdk/models/common.py
index 5f1df13..56a8688 100644
--- a/python/grafana_foundation_sdk/models/common.py
+++ b/python/grafana_foundation_sdk/models/common.py
@@ -2,8 +2,6 @@
 
 import enum
 import typing
-from ..cog import variants as cogvariants
-from ..cog import runtime as cogruntime
 
 
 class DataTopic(enum.StrEnum):
@@ -68,7 +66,7 @@ class DataSourceJsonData:
         return cls(**args)
 
 
-class DataQuery(cogvariants.Dataquery):
+class DataQuery:
     """
     These are the common properties available to all queries in all datasources.
     Specific implementations will *extend* this interface, adding the required
@@ -124,13 +122,6 @@ class DataQuery(cogvariants.Dataquery):
         return cls(**args)
 
 
-def variant_config() -> cogruntime.DataqueryConfig:
-    return cogruntime.DataqueryConfig(
-        identifier="",
-        from_json_hook=DataQuery.from_json,
-    )
-
-
 class BaseDimensionConfig:
     # fixed: T -- will be added by each element
     field: typing.Optional[str]
diff --git a/python/grafana_foundation_sdk/models/expr.py b/python/grafana_foundation_sdk/models/expr.py
index ea417d3..83eedf4 100644
--- a/python/grafana_foundation_sdk/models/expr.py
+++ b/python/grafana_foundation_sdk/models/expr.py
@@ -9,7 +9,7 @@ Expr: typing.TypeAlias = typing.Union['TypeMath', 'TypeReduce', 'TypeResample',
 
 
 def variant_config() -> cogruntime.DataqueryConfig:
-    decoding_map: dict[str, typing.Union[typing.Type[TypeThreshold], typing.Type[TypeSql], typing.Type[TypeMath], typing.Type[TypeReduce], typing.Type[TypeResample], typing.Type[TypeClassicConditions]]] = {"threshold": TypeThreshold, "sql": TypeSql, "math": TypeMath, "reduce": TypeReduce, "resample": TypeResample, "classic_conditions": TypeClassicConditions}
+    decoding_map: dict[str, typing.Union[typing.Type[TypeResample], typing.Type[TypeClassicConditions], typing.Type[TypeThreshold], typing.Type[TypeSql], typing.Type[TypeMath], typing.Type[TypeReduce]]] = {"resample": TypeResample, "classic_conditions": TypeClassicConditions, "threshold": TypeThreshold, "sql": TypeSql, "math": TypeMath, "reduce": TypeReduce}
     return cogruntime.DataqueryConfig(
         identifier="__expr__",
         from_json_hook=lambda data: decoding_map[data["type"]].from_json(data),
diff --git a/python/grafana_foundation_sdk/models/rolebinding.py b/python/grafana_foundation_sdk/models/rolebinding.py
index e8f302c..eafa911 100644
--- a/python/grafana_foundation_sdk/models/rolebinding.py
+++ b/python/grafana_foundation_sdk/models/rolebinding.py
@@ -25,7 +25,7 @@ class RoleBinding:
         args: dict[str, typing.Any] = {}
         
         if "role" in data:
-            decoding_map: dict[str, typing.Union[typing.Type[BuiltinRoleRef], typing.Type[CustomRoleRef]]] = {"BuiltinRole": BuiltinRoleRef, "Role": CustomRoleRef}
+            decoding_map: dict[str, typing.Union[typing.Type[CustomRoleRef], typing.Type[BuiltinRoleRef]]] = {"Role": CustomRoleRef, "BuiltinRole": BuiltinRoleRef}
             args["role"] = decoding_map[data["role"]["kind"]].from_json(data["role"])
         if "subject" in data:
             args["subject"] = RoleBindingSubject.from_json(data["subject"])        
diff --git a/python/pyproject.toml b/python/pyproject.toml
index 6610f18..d3acb3c 100644
--- a/python/pyproject.toml
+++ b/python/pyproject.toml
@@ -13,7 +13,7 @@ keywords = [
     "traces",
     "metrics"
 ]
-version = "1716368518!next"
+version = "1716455321!next"
 dependencies = []
 requires-python = ">=3.11"
 classifiers = [
diff --git a/typescript/package.json b/typescript/package.json
index e9ad580..c811fbd 100644
--- a/typescript/package.json
+++ b/typescript/package.json
@@ -1,6 +1,6 @@
 {
   "name": "@grafana/grafana-foundation-sdk",
-  "version": "next-cogv0.0.x.1716368518",
+  "version": "next-cogv0.0.x.1716455321",
   "description": "A set of tools, types and libraries for building and manipulating Grafana objects.",
   "keywords": [
     "observability",
diff --git a/typescript/src/common/dataQueryBuilder.gen.ts b/typescript/src/common/dataQueryBuilder.gen.ts
index 90ec218..d30bdc6 100644
--- a/typescript/src/common/dataQueryBuilder.gen.ts
+++ b/typescript/src/common/dataQueryBuilder.gen.ts
@@ -6,7 +6,7 @@ import * as common from '../common';
 // These are the common properties available to all queries in all datasources.
 // Specific implementations will *extend* this interface, adding the required
 // properties for the given context.
-export class DataQueryBuilder implements cog.Builder<cog.Dataquery> {
+export class DataQueryBuilder implements cog.Builder<common.DataQuery> {
     protected readonly internal: common.DataQuery;
 
     constructor() {
diff --git a/typescript/src/common/types.gen.ts b/typescript/src/common/types.gen.ts
index 0807a98..5bfe0d4 100644
--- a/typescript/src/common/types.gen.ts
+++ b/typescript/src/common/types.gen.ts
@@ -40,12 +40,10 @@ export interface DataQuery {
 	// TODO find a better way to do this ^ that's friendly to schema
 	// TODO this shouldn't be unknown but DataSourceRef | null
 	datasource?: any;
-	_implementsDataqueryVariant(): void;
 }
 
 export const defaultDataQuery = (): DataQuery => ({
 	refId: "",
-	_implementsDataqueryVariant: () => {},
 });
 
 export interface BaseDimensionConfig {

github-actions[bot] avatar May 23 '24 08:05 github-actions[bot]