grafana / cog

Code Generation with a human touch
Apache License 2.0
43 stars 2 forks source link

Builders: Tests for panel builders #434

Closed spinillos closed 3 months ago

spinillos commented 3 months ago

It adds tests for Panels to see how its generated in Java since its a bit different comparing with the other languages. The example it isn't the same panel as we could have in Grafana but we can verify that its generated.

I saw that I forgot to add the defaults in panel builders for Java 🙈.

github-actions[bot] commented 3 months ago

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` ```patch diff --git a/go/heatmap/panel_builder_gen.go b/go/heatmap/panel_builder_gen.go index 9c62343..045788c 100644 --- a/go/heatmap/panel_builder_gen.go +++ b/go/heatmap/panel_builder_gen.go @@ -626,6 +626,16 @@ func (builder *PanelBuilder) ExemplarsColor(color string) *PanelBuilder { return builder } +// Controls which axis to allow selection on +func (builder *PanelBuilder) SelectionMode(selectionMode HeatmapSelectionMode) *PanelBuilder { + if builder.internal.Options == nil { + builder.internal.Options = &Options{} + } + builder.internal.Options.(*Options).SelectionMode = &selectionMode + + return builder +} + func (builder *PanelBuilder) ScaleDistribution(scaleDistribution cog.Builder[common.ScaleDistributionConfig]) *PanelBuilder { if builder.internal.FieldConfig == nil { builder.internal.FieldConfig = &dashboard.FieldConfigSource{} @@ -678,4 +688,5 @@ func (builder *PanelBuilder) applyDefaults() { builder.ShowValue("auto") builder.CellGap(1) builder.ExemplarsColor("rgba(255,0,255,0.7)") + builder.SelectionMode("x") } diff --git a/go/heatmap/types_gen.go b/go/heatmap/types_gen.go index 7938615..583f986 100644 --- a/go/heatmap/types_gen.go +++ b/go/heatmap/types_gen.go @@ -25,6 +25,15 @@ const ( HeatmapColorScaleExponential HeatmapColorScale = "exponential" ) +// Controls which axis to allow selection on +type HeatmapSelectionMode string + +const ( + HeatmapSelectionModeX HeatmapSelectionMode = "x" + HeatmapSelectionModeY HeatmapSelectionMode = "y" + HeatmapSelectionModeXy HeatmapSelectionMode = "xy" +) + // Controls various color options type HeatmapColorOptions struct { // Sets the color mode @@ -152,6 +161,8 @@ type Options struct { Tooltip HeatmapTooltip `json:"tooltip"` // Controls exemplar options Exemplars ExemplarConfig `json:"exemplars"` + // Controls which axis to allow selection on + SelectionMode *HeatmapSelectionMode `json:"selectionMode,omitempty"` } type FieldConfig struct { diff --git a/jsonschema/heatmap.jsonschema.json b/jsonschema/heatmap.jsonschema.json index a0e8d5e..ee7a168 100644 --- a/jsonschema/heatmap.jsonschema.json +++ b/jsonschema/heatmap.jsonschema.json @@ -15,6 +15,14 @@ ], "description": "Controls the color scale of the heatmap" }, + "HeatmapSelectionMode": { + "enum": [ + "x", + "y", + "xy" + ], + "description": "Controls which axis to allow selection on" + }, "HeatmapColorOptions": { "type": "object", "additionalProperties": false, @@ -308,6 +316,11 @@ "default": { "color": "rgba(255,0,255,0.7)" } + }, + "selectionMode": { + "$ref": "#/definitions/HeatmapSelectionMode", + "description": "Controls which axis to allow selection on", + "default": "x" } } }, diff --git a/openapi/heatmap.openapi.json b/openapi/heatmap.openapi.json index 80cb0b9..b6d4cb4 100644 --- a/openapi/heatmap.openapi.json +++ b/openapi/heatmap.openapi.json @@ -24,6 +24,14 @@ ], "description": "Controls the color scale of the heatmap" }, + "HeatmapSelectionMode": { + "enum": [ + "x", + "y", + "xy" + ], + "description": "Controls which axis to allow selection on" + }, "HeatmapColorOptions": { "type": "object", "additionalProperties": false, @@ -317,6 +325,11 @@ "default": { "color": "rgba(255,0,255,0.7)" } + }, + "selectionMode": { + "$ref": "#/components/schemas/HeatmapSelectionMode", + "description": "Controls which axis to allow selection on", + "default": "x" } } }, diff --git a/python/grafana_foundation_sdk/builders/heatmap.py b/python/grafana_foundation_sdk/builders/heatmap.py index fc6d8af..9546e30 100644 --- a/python/grafana_foundation_sdk/builders/heatmap.py +++ b/python/grafana_foundation_sdk/builders/heatmap.py @@ -717,6 +717,18 @@ class Panel(cogbuilder.Builder[dashboard.Panel]): return self + def selection_mode(self, selection_mode: heatmap.HeatmapSelectionMode) -> typing.Self: + """ + Controls which axis to allow selection on + """ + + if self._internal.options is None: + self._internal.options = heatmap.Options() + assert isinstance(self._internal.options, heatmap.Options) + self._internal.options.selection_mode = selection_mode + + return self + def scale_distribution(self, scale_distribution: cogbuilder.Builder[common.ScaleDistributionConfig]) -> typing.Self: if self._internal.field_config is None: self._internal.field_config = dashboard.FieldConfigSource() diff --git a/python/grafana_foundation_sdk/cog/plugins.py b/python/grafana_foundation_sdk/cog/plugins.py index ea5481d..c9c0098 100644 --- a/python/grafana_foundation_sdk/cog/plugins.py +++ b/python/grafana_foundation_sdk/cog/plugins.py @@ -1,39 +1,39 @@ # Code generated - EDITING IS FUTILE. DO NOT EDIT. from ..models import dashboardlist -from ..models import debug -from ..models import histogram -from ..models import stat -from ..models import tempo -from ..models import expr -from ..models import barchart from ..models import datagrid -from ..models import grafanapyroscope +from ..models import gauge +from ..models import histogram +from ..models import debug from ..models import heatmap -from ..models import nodegraph -from ..models import cloudwatch -from ..models import text +from ..models import statushistory from ..models import timeseries -from ..models import canvas -from ..models import googlecloudmonitoring -from ..models import loki from ..models import trend -from ..models import statushistory -from ..models import prometheus +from ..models import xychart from ..models import annotationslist -from ..models import statetimeline -from ..models import bargauge -from ..models import gauge -from ..models import logs -from ..models import azuremonitor from ..models import candlestick +from ..models import cloudwatch +from ..models import logs +from ..models import table from ..models import elasticsearch +from ..models import azuremonitor +from ..models import grafanapyroscope +from ..models import nodegraph +from ..models import parca from ..models import piechart -from ..models import xychart -from ..models import geomap +from ..models import bargauge +from ..models import googlecloudmonitoring from ..models import news -from ..models import parca -from ..models import table +from ..models import stat +from ..models import tempo +from ..models import text +from ..models import barchart +from ..models import expr +from ..models import prometheus +from ..models import canvas +from ..models import statetimeline +from ..models import geomap +from ..models import loki from . import runtime as cogruntime diff --git a/python/grafana_foundation_sdk/models/expr.py b/python/grafana_foundation_sdk/models/expr.py index 0f8654e..5dd1737 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[TypeClassicConditions], typing.Type[TypeThreshold], typing.Type[TypeSql], typing.Type[TypeMath], typing.Type[TypeReduce], typing.Type[TypeResample]]] = {"classic_conditions": TypeClassicConditions, "threshold": TypeThreshold, "sql": TypeSql, "math": TypeMath, "reduce": TypeReduce, "resample": TypeResample} + decoding_map: dict[str, typing.Union[typing.Type[TypeMath], typing.Type[TypeReduce], typing.Type[TypeResample], typing.Type[TypeClassicConditions], typing.Type[TypeThreshold], typing.Type[TypeSql]]] = {"math": TypeMath, "reduce": TypeReduce, "resample": TypeResample, "classic_conditions": TypeClassicConditions, "threshold": TypeThreshold, "sql": TypeSql} 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/heatmap.py b/python/grafana_foundation_sdk/models/heatmap.py index 1a9a40b..c20a649 100644 --- a/python/grafana_foundation_sdk/models/heatmap.py +++ b/python/grafana_foundation_sdk/models/heatmap.py @@ -24,6 +24,16 @@ class HeatmapColorScale(enum.StrEnum): EXPONENTIAL = "exponential" +class HeatmapSelectionMode(enum.StrEnum): + """ + Controls which axis to allow selection on + """ + + X = "x" + Y = "y" + XY = "xy" + + class HeatmapColorOptions: """ Controls various color options @@ -464,8 +474,10 @@ class Options: tooltip: 'HeatmapTooltip' # Controls exemplar options exemplars: 'ExemplarConfig' + # Controls which axis to allow selection on + selection_mode: typing.Optional['HeatmapSelectionMode'] - def __init__(self, calculate: typing.Optional[bool] = False, calculation: typing.Optional[common.HeatmapCalculationOptions] = None, color: typing.Optional['HeatmapColorOptions'] = None, filter_values: typing.Optional['FilterValueRange'] = None, rows_frame: typing.Optional['RowsHeatmapOptions'] = None, show_value: typing.Optional[common.VisibilityMode] = None, cell_gap: typing.Optional[int] = 1, cell_radius: typing.Optional[float] = None, cell_values: typing.Optional['CellValues'] = None, y_axis: typing.Optional['YAxisConfig'] = None, legend: typing.Optional['HeatmapLegend'] = None, tooltip: typing.Optional['HeatmapTooltip'] = None, exemplars: typing.Optional['ExemplarConfig'] = None): + def __init__(self, calculate: typing.Optional[bool] = False, calculation: typing.Optional[common.HeatmapCalculationOptions] = None, color: typing.Optional['HeatmapColorOptions'] = None, filter_values: typing.Optional['FilterValueRange'] = None, rows_frame: typing.Optional['RowsHeatmapOptions'] = None, show_value: typing.Optional[common.VisibilityMode] = None, cell_gap: typing.Optional[int] = 1, cell_radius: typing.Optional[float] = None, cell_values: typing.Optional['CellValues'] = None, y_axis: typing.Optional['YAxisConfig'] = None, legend: typing.Optional['HeatmapLegend'] = None, tooltip: typing.Optional['HeatmapTooltip'] = None, exemplars: typing.Optional['ExemplarConfig'] = None, selection_mode: typing.Optional['HeatmapSelectionMode'] = None): self.calculate = calculate self.calculation = calculation self.color = color if color is not None else HeatmapColorOptions(exponent=0.5, fill="dark-orange", reverse=False, scheme="Oranges", steps=64) @@ -479,6 +491,7 @@ class Options: self.legend = legend if legend is not None else HeatmapLegend(show=True) self.tooltip = tooltip if tooltip is not None else HeatmapTooltip() self.exemplars = exemplars if exemplars is not None else ExemplarConfig(color="rgba(255,0,255,0.7)") + self.selection_mode = selection_mode if selection_mode is not None else HeatmapSelectionMode.X def to_json(self) -> dict[str, object]: payload: dict[str, object] = { @@ -503,6 +516,8 @@ class Options: payload["cellRadius"] = self.cell_radius if self.cell_values is not None: payload["cellValues"] = self.cell_values + if self.selection_mode is not None: + payload["selectionMode"] = self.selection_mode return payload @classmethod @@ -534,7 +549,9 @@ class Options: if "tooltip" in data: args["tooltip"] = HeatmapTooltip.from_json(data["tooltip"]) if "exemplars" in data: - args["exemplars"] = ExemplarConfig.from_json(data["exemplars"]) + args["exemplars"] = ExemplarConfig.from_json(data["exemplars"]) + if "selectionMode" in data: + args["selection_mode"] = data["selectionMode"] return cls(**args) diff --git a/python/pyproject.toml b/python/pyproject.toml index 7b9c1fb..a949f24 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -13,7 +13,7 @@ keywords = [ "traces", "metrics" ] -version = "1717501769!next" +version = "1717683578!next" dependencies = [] requires-python = ">=3.11" classifiers = [ diff --git a/typescript/package.json b/typescript/package.json index 763fd20..d79db00 100644 --- a/typescript/package.json +++ b/typescript/package.json @@ -1,6 +1,6 @@ { "name": "@grafana/grafana-foundation-sdk", - "version": "next-cogv0.0.x.1717501769", + "version": "next-cogv0.0.x.1717683578", "description": "A set of tools, types and libraries for building and manipulating Grafana objects.", "keywords": [ "observability", diff --git a/typescript/src/heatmap/panelBuilder.gen.ts b/typescript/src/heatmap/panelBuilder.gen.ts index b291b75..cf8608d 100644 --- a/typescript/src/heatmap/panelBuilder.gen.ts +++ b/typescript/src/heatmap/panelBuilder.gen.ts @@ -569,6 +569,15 @@ export class PanelBuilder implements cog.Builder { return this; } + // Controls which axis to allow selection on + selectionMode(selectionMode: heatmap.HeatmapSelectionMode): this { + if (!this.internal.options) { + this.internal.options = heatmap.defaultOptions(); + } + this.internal.options.selectionMode = selectionMode; + return this; + } + scaleDistribution(scaleDistribution: cog.Builder): this { if (!this.internal.fieldConfig) { this.internal.fieldConfig = dashboard.defaultFieldConfigSource(); diff --git a/typescript/src/heatmap/types.gen.ts b/typescript/src/heatmap/types.gen.ts index a6c60a0..ab349e5 100644 --- a/typescript/src/heatmap/types.gen.ts +++ b/typescript/src/heatmap/types.gen.ts @@ -19,6 +19,15 @@ export enum HeatmapColorScale { export const defaultHeatmapColorScale = (): HeatmapColorScale => (HeatmapColorScale.Linear); +// Controls which axis to allow selection on +export enum HeatmapSelectionMode { + X = "x", + Y = "y", + Xy = "xy", +} + +export const defaultHeatmapSelectionMode = (): HeatmapSelectionMode => (HeatmapSelectionMode.X); + // Controls various color options export interface HeatmapColorOptions { // Sets the color mode @@ -178,6 +187,8 @@ export interface Options { tooltip: HeatmapTooltip; // Controls exemplar options exemplars: ExemplarConfig; + // Controls which axis to allow selection on + selectionMode?: HeatmapSelectionMode; } export const defaultOptions = (): Options => ({ @@ -190,6 +201,7 @@ export const defaultOptions = (): Options => ({ legend: { show: true, }, tooltip: defaultHeatmapTooltip(), exemplars: { color: "rgba(255,0,255,0.7)", }, + selectionMode: HeatmapSelectionMode.X, }); export interface FieldConfig { ```