Expressions: Create json schema for query types (#84032)

This commit is contained in:
Ryan McKinley
2024-03-26 06:58:56 +02:00
committed by GitHub
parent 2e06677240
commit 4cda34ff7d
40 changed files with 4649 additions and 654 deletions
+162
View File
@@ -0,0 +1,162 @@
{
"type": "table",
"targets": [
{
"refId": "A",
"datasource": {
"type": "__expr__",
"uid": "TheUID"
},
"expression": "$A + 10",
"type": "math"
},
{
"refId": "B",
"datasource": {
"type": "__expr__",
"uid": "TheUID"
},
"type": "math",
"expression": "$A - $B"
},
{
"refId": "C",
"datasource": {
"type": "__expr__",
"uid": "TheUID"
},
"type": "reduce",
"expression": "$A",
"reducer": "max",
"settings": {
"mode": "dropNN"
}
},
{
"refId": "D",
"datasource": {
"type": "__expr__",
"uid": "TheUID"
},
"expression": "$A",
"window": "1d",
"downsampler": "last",
"upsampler": "pad",
"type": "resample"
},
{
"refId": "E",
"datasource": {
"type": "__expr__",
"uid": "TheUID"
},
"conditions": [
{
"evaluator": {
"params": [
5
],
"type": "gt"
},
"operator": {
"type": "and"
},
"query": {
"params": [
"A"
]
},
"reducer": {
"type": "max"
}
}
],
"type": "classic_conditions"
},
{
"refId": "F",
"datasource": {
"type": "__expr__",
"uid": "TheUID"
},
"expression": "A",
"conditions": [
{
"evaluator": {
"params": [
5
],
"type": "gt"
}
}
],
"type": "threshold"
},
{
"refId": "G",
"datasource": {
"type": "__expr__",
"uid": "TheUID"
},
"expression": "B",
"conditions": [
{
"evaluator": {
"params": [
100
],
"type": "gt"
},
"loadedDimensions": {
"data": {
"values": [
[
18446744073709552000,
2,
3,
4,
5
]
]
},
"schema": {
"fields": [
{
"name": "fingerprints",
"type": "number",
"typeInfo": {
"frame": "uint64"
}
}
],
"meta": {
"type": "fingerprints",
"typeVersion": [
1,
0
]
},
"name": "test"
}
},
"unloadEvaluator": {
"params": [
31
],
"type": "lt"
}
}
],
"type": "threshold"
},
{
"refId": "H",
"datasource": {
"type": "__expr__",
"uid": "TheUID"
},
"expression": "SELECT * FROM A limit 1",
"type": "sql"
}
]
}
+989
View File
@@ -0,0 +1,989 @@
{
"type": "object",
"required": [
"targets",
"type"
],
"properties": {
"targets": {
"type": "array",
"items": {
"type": "object",
"oneOf": [
{
"type": "object",
"required": [
"expression",
"type",
"refId"
],
"properties": {
"datasource": {
"description": "The datasource",
"type": "object",
"required": [
"type"
],
"properties": {
"type": {
"description": "The datasource plugin type",
"type": "string",
"pattern": "^__expr__$"
},
"uid": {
"description": "Datasource UID",
"type": "string"
}
},
"additionalProperties": false
},
"expression": {
"description": "General math expression",
"type": "string",
"minLength": 1,
"examples": [
"$A + 1",
"$A/$B"
]
},
"hide": {
"description": "true if query is disabled (ie should not be returned to the dashboard)\nNOTE: this does not always imply that the query should not be executed since\nthe results from a hidden query may be used as the input to other queries (SSE etc)",
"type": "boolean"
},
"queryType": {
"description": "QueryType is an optional identifier for the type of query.\nIt can be used to distinguish different types of queries.",
"type": "string"
},
"refId": {
"description": "RefID is the unique identifier of the query, set by the frontend call.",
"type": "string"
},
"resultAssertions": {
"description": "Optionally define expected query result behavior",
"type": "object",
"required": [
"typeVersion"
],
"properties": {
"maxFrames": {
"description": "Maximum frame count",
"type": "integer"
},
"type": {
"description": "Type asserts that the frame matches a known type structure.\n\n\nPossible enum values:\n - `\"\"` \n - `\"timeseries-wide\"` \n - `\"timeseries-long\"` \n - `\"timeseries-many\"` \n - `\"timeseries-multi\"` \n - `\"directory-listing\"` \n - `\"table\"` \n - `\"numeric-wide\"` \n - `\"numeric-multi\"` \n - `\"numeric-long\"` \n - `\"log-lines\"` ",
"type": "string",
"enum": [
"",
"timeseries-wide",
"timeseries-long",
"timeseries-many",
"timeseries-multi",
"directory-listing",
"table",
"numeric-wide",
"numeric-multi",
"numeric-long",
"log-lines"
],
"x-enum-description": {}
},
"typeVersion": {
"description": "TypeVersion is the version of the Type property. Versions greater than 0.0 correspond to the dataplane\ncontract documentation https://grafana.github.io/dataplane/contract/.",
"type": "array",
"maxItems": 2,
"minItems": 2,
"items": {
"type": "integer"
}
}
},
"additionalProperties": false
},
"timeRange": {
"description": "TimeRange represents the query range\nNOTE: unlike generic /ds/query, we can now send explicit time values in each query\nNOTE: the values for timeRange are not saved in a dashboard, they are constructed on the fly",
"type": "object",
"required": [
"from",
"to"
],
"properties": {
"from": {
"description": "From is the start time of the query.",
"type": "string",
"default": "now-6h",
"examples": [
"now-1h"
]
},
"to": {
"description": "To is the end time of the query.",
"type": "string",
"default": "now",
"examples": [
"now"
]
}
},
"additionalProperties": false
},
"type": {
"type": "string",
"pattern": "^math$"
}
},
"additionalProperties": false,
"$schema": "https://json-schema.org/draft-04/schema"
},
{
"type": "object",
"required": [
"expression",
"reducer",
"type",
"refId"
],
"properties": {
"datasource": {
"description": "The datasource",
"type": "object",
"required": [
"type"
],
"properties": {
"type": {
"description": "The datasource plugin type",
"type": "string",
"pattern": "^__expr__$"
},
"uid": {
"description": "Datasource UID",
"type": "string"
}
},
"additionalProperties": false
},
"expression": {
"description": "Reference to single query result",
"type": "string",
"minLength": 1,
"examples": [
"$A"
]
},
"hide": {
"description": "true if query is disabled (ie should not be returned to the dashboard)\nNOTE: this does not always imply that the query should not be executed since\nthe results from a hidden query may be used as the input to other queries (SSE etc)",
"type": "boolean"
},
"queryType": {
"description": "QueryType is an optional identifier for the type of query.\nIt can be used to distinguish different types of queries.",
"type": "string"
},
"reducer": {
"description": "The reducer\n\n\nPossible enum values:\n - `\"sum\"` \n - `\"mean\"` \n - `\"min\"` \n - `\"max\"` \n - `\"count\"` \n - `\"last\"` ",
"type": "string",
"enum": [
"sum",
"mean",
"min",
"max",
"count",
"last"
],
"x-enum-description": {}
},
"refId": {
"description": "RefID is the unique identifier of the query, set by the frontend call.",
"type": "string"
},
"resultAssertions": {
"description": "Optionally define expected query result behavior",
"type": "object",
"required": [
"typeVersion"
],
"properties": {
"maxFrames": {
"description": "Maximum frame count",
"type": "integer"
},
"type": {
"description": "Type asserts that the frame matches a known type structure.\n\n\nPossible enum values:\n - `\"\"` \n - `\"timeseries-wide\"` \n - `\"timeseries-long\"` \n - `\"timeseries-many\"` \n - `\"timeseries-multi\"` \n - `\"directory-listing\"` \n - `\"table\"` \n - `\"numeric-wide\"` \n - `\"numeric-multi\"` \n - `\"numeric-long\"` \n - `\"log-lines\"` ",
"type": "string",
"enum": [
"",
"timeseries-wide",
"timeseries-long",
"timeseries-many",
"timeseries-multi",
"directory-listing",
"table",
"numeric-wide",
"numeric-multi",
"numeric-long",
"log-lines"
],
"x-enum-description": {}
},
"typeVersion": {
"description": "TypeVersion is the version of the Type property. Versions greater than 0.0 correspond to the dataplane\ncontract documentation https://grafana.github.io/dataplane/contract/.",
"type": "array",
"maxItems": 2,
"minItems": 2,
"items": {
"type": "integer"
}
}
},
"additionalProperties": false
},
"settings": {
"description": "Reducer Options",
"type": "object",
"required": [
"mode"
],
"properties": {
"mode": {
"description": "Non-number reduce behavior\n\n\nPossible enum values:\n - `\"dropNN\"` Drop non-numbers\n - `\"replaceNN\"` Replace non-numbers",
"type": "string",
"enum": [
"dropNN",
"replaceNN"
],
"x-enum-description": {
"dropNN": "Drop non-numbers",
"replaceNN": "Replace non-numbers"
}
},
"replaceWithValue": {
"description": "Only valid when mode is replace",
"type": "number"
}
},
"additionalProperties": false
},
"timeRange": {
"description": "TimeRange represents the query range\nNOTE: unlike generic /ds/query, we can now send explicit time values in each query\nNOTE: the values for timeRange are not saved in a dashboard, they are constructed on the fly",
"type": "object",
"required": [
"from",
"to"
],
"properties": {
"from": {
"description": "From is the start time of the query.",
"type": "string",
"default": "now-6h",
"examples": [
"now-1h"
]
},
"to": {
"description": "To is the end time of the query.",
"type": "string",
"default": "now",
"examples": [
"now"
]
}
},
"additionalProperties": false
},
"type": {
"type": "string",
"pattern": "^reduce$"
}
},
"additionalProperties": false,
"$schema": "https://json-schema.org/draft-04/schema"
},
{
"description": "QueryType = resample",
"type": "object",
"required": [
"expression",
"window",
"downsampler",
"upsampler",
"type",
"refId"
],
"properties": {
"datasource": {
"description": "The datasource",
"type": "object",
"required": [
"type"
],
"properties": {
"type": {
"description": "The datasource plugin type",
"type": "string",
"pattern": "^__expr__$"
},
"uid": {
"description": "Datasource UID",
"type": "string"
}
},
"additionalProperties": false
},
"downsampler": {
"description": "The downsample function\n\n\nPossible enum values:\n - `\"sum\"` \n - `\"mean\"` \n - `\"min\"` \n - `\"max\"` \n - `\"count\"` \n - `\"last\"` ",
"type": "string",
"enum": [
"sum",
"mean",
"min",
"max",
"count",
"last"
],
"x-enum-description": {}
},
"expression": {
"description": "The math expression",
"type": "string",
"minLength": 1,
"examples": [
"$A + 1",
"$A"
]
},
"hide": {
"description": "true if query is disabled (ie should not be returned to the dashboard)\nNOTE: this does not always imply that the query should not be executed since\nthe results from a hidden query may be used as the input to other queries (SSE etc)",
"type": "boolean"
},
"queryType": {
"description": "QueryType is an optional identifier for the type of query.\nIt can be used to distinguish different types of queries.",
"type": "string"
},
"refId": {
"description": "RefID is the unique identifier of the query, set by the frontend call.",
"type": "string"
},
"resultAssertions": {
"description": "Optionally define expected query result behavior",
"type": "object",
"required": [
"typeVersion"
],
"properties": {
"maxFrames": {
"description": "Maximum frame count",
"type": "integer"
},
"type": {
"description": "Type asserts that the frame matches a known type structure.\n\n\nPossible enum values:\n - `\"\"` \n - `\"timeseries-wide\"` \n - `\"timeseries-long\"` \n - `\"timeseries-many\"` \n - `\"timeseries-multi\"` \n - `\"directory-listing\"` \n - `\"table\"` \n - `\"numeric-wide\"` \n - `\"numeric-multi\"` \n - `\"numeric-long\"` \n - `\"log-lines\"` ",
"type": "string",
"enum": [
"",
"timeseries-wide",
"timeseries-long",
"timeseries-many",
"timeseries-multi",
"directory-listing",
"table",
"numeric-wide",
"numeric-multi",
"numeric-long",
"log-lines"
],
"x-enum-description": {}
},
"typeVersion": {
"description": "TypeVersion is the version of the Type property. Versions greater than 0.0 correspond to the dataplane\ncontract documentation https://grafana.github.io/dataplane/contract/.",
"type": "array",
"maxItems": 2,
"minItems": 2,
"items": {
"type": "integer"
}
}
},
"additionalProperties": false
},
"timeRange": {
"description": "TimeRange represents the query range\nNOTE: unlike generic /ds/query, we can now send explicit time values in each query\nNOTE: the values for timeRange are not saved in a dashboard, they are constructed on the fly",
"type": "object",
"required": [
"from",
"to"
],
"properties": {
"from": {
"description": "From is the start time of the query.",
"type": "string",
"default": "now-6h",
"examples": [
"now-1h"
]
},
"to": {
"description": "To is the end time of the query.",
"type": "string",
"default": "now",
"examples": [
"now"
]
}
},
"additionalProperties": false
},
"type": {
"type": "string",
"pattern": "^resample$"
},
"upsampler": {
"description": "The upsample function\n\n\nPossible enum values:\n - `\"pad\"` Use the last seen value\n - `\"backfilling\"` backfill\n - `\"fillna\"` Do not fill values (nill)",
"type": "string",
"enum": [
"pad",
"backfilling",
"fillna"
],
"x-enum-description": {
"backfilling": "backfill",
"fillna": "Do not fill values (nill)",
"pad": "Use the last seen value"
}
},
"window": {
"description": "The time duration",
"type": "string",
"minLength": 1,
"examples": [
"1d",
"10m"
]
}
},
"additionalProperties": false,
"$schema": "https://json-schema.org/draft-04/schema"
},
{
"type": "object",
"required": [
"conditions",
"type",
"refId"
],
"properties": {
"conditions": {
"type": "array",
"items": {
"description": "ConditionJSON is the JSON model for a single condition in ConditionsCmd.",
"type": "object",
"required": [
"evaluator",
"operator",
"query",
"reducer"
],
"properties": {
"evaluator": {
"type": "object",
"required": [
"params",
"type"
],
"properties": {
"params": {
"type": "array",
"items": {
"type": "number"
}
},
"type": {
"description": "e.g. \"gt\"",
"type": "string"
}
},
"additionalProperties": false
},
"operator": {
"type": "object",
"required": [
"type"
],
"properties": {
"type": {
"type": "string",
"enum": [
"and",
"or"
],
"x-enum-description": {}
}
},
"additionalProperties": false
},
"query": {
"type": "object",
"required": [
"params"
],
"properties": {
"params": {
"type": "array",
"items": {
"type": "string"
}
}
},
"additionalProperties": false
},
"reducer": {
"type": "object",
"required": [
"type"
],
"properties": {
"type": {
"type": "string"
}
},
"additionalProperties": false
}
},
"additionalProperties": false
}
},
"datasource": {
"description": "The datasource",
"type": "object",
"required": [
"type"
],
"properties": {
"type": {
"description": "The datasource plugin type",
"type": "string",
"pattern": "^__expr__$"
},
"uid": {
"description": "Datasource UID",
"type": "string"
}
},
"additionalProperties": false
},
"hide": {
"description": "true if query is disabled (ie should not be returned to the dashboard)\nNOTE: this does not always imply that the query should not be executed since\nthe results from a hidden query may be used as the input to other queries (SSE etc)",
"type": "boolean"
},
"queryType": {
"description": "QueryType is an optional identifier for the type of query.\nIt can be used to distinguish different types of queries.",
"type": "string"
},
"refId": {
"description": "RefID is the unique identifier of the query, set by the frontend call.",
"type": "string"
},
"resultAssertions": {
"description": "Optionally define expected query result behavior",
"type": "object",
"required": [
"typeVersion"
],
"properties": {
"maxFrames": {
"description": "Maximum frame count",
"type": "integer"
},
"type": {
"description": "Type asserts that the frame matches a known type structure.\n\n\nPossible enum values:\n - `\"\"` \n - `\"timeseries-wide\"` \n - `\"timeseries-long\"` \n - `\"timeseries-many\"` \n - `\"timeseries-multi\"` \n - `\"directory-listing\"` \n - `\"table\"` \n - `\"numeric-wide\"` \n - `\"numeric-multi\"` \n - `\"numeric-long\"` \n - `\"log-lines\"` ",
"type": "string",
"enum": [
"",
"timeseries-wide",
"timeseries-long",
"timeseries-many",
"timeseries-multi",
"directory-listing",
"table",
"numeric-wide",
"numeric-multi",
"numeric-long",
"log-lines"
],
"x-enum-description": {}
},
"typeVersion": {
"description": "TypeVersion is the version of the Type property. Versions greater than 0.0 correspond to the dataplane\ncontract documentation https://grafana.github.io/dataplane/contract/.",
"type": "array",
"maxItems": 2,
"minItems": 2,
"items": {
"type": "integer"
}
}
},
"additionalProperties": false
},
"timeRange": {
"description": "TimeRange represents the query range\nNOTE: unlike generic /ds/query, we can now send explicit time values in each query\nNOTE: the values for timeRange are not saved in a dashboard, they are constructed on the fly",
"type": "object",
"required": [
"from",
"to"
],
"properties": {
"from": {
"description": "From is the start time of the query.",
"type": "string",
"default": "now-6h",
"examples": [
"now-1h"
]
},
"to": {
"description": "To is the end time of the query.",
"type": "string",
"default": "now",
"examples": [
"now"
]
}
},
"additionalProperties": false
},
"type": {
"type": "string",
"pattern": "^classic_conditions$"
}
},
"additionalProperties": false,
"$schema": "https://json-schema.org/draft-04/schema"
},
{
"type": "object",
"required": [
"expression",
"conditions",
"type",
"refId"
],
"properties": {
"conditions": {
"description": "Threshold Conditions",
"type": "array",
"items": {
"type": "object",
"required": [
"evaluator"
],
"properties": {
"evaluator": {
"type": "object",
"required": [
"params",
"type"
],
"properties": {
"params": {
"type": "array",
"items": {
"type": "number"
}
},
"type": {
"description": "e.g. \"gt\"",
"type": "string",
"enum": [
"gt",
"lt",
"within_range",
"outside_range"
],
"x-enum-description": {}
}
},
"additionalProperties": false
},
"loadedDimensions": {
"type": "object",
"additionalProperties": true,
"x-grafana-type": "data.DataFrame"
},
"unloadEvaluator": {
"type": "object",
"required": [
"params",
"type"
],
"properties": {
"params": {
"type": "array",
"items": {
"type": "number"
}
},
"type": {
"description": "e.g. \"gt\"",
"type": "string",
"enum": [
"gt",
"lt",
"within_range",
"outside_range"
],
"x-enum-description": {}
}
},
"additionalProperties": false
}
},
"additionalProperties": false
}
},
"datasource": {
"description": "The datasource",
"type": "object",
"required": [
"type"
],
"properties": {
"type": {
"description": "The datasource plugin type",
"type": "string",
"pattern": "^__expr__$"
},
"uid": {
"description": "Datasource UID",
"type": "string"
}
},
"additionalProperties": false
},
"expression": {
"description": "Reference to single query result",
"type": "string",
"minLength": 1,
"examples": [
"$A"
]
},
"hide": {
"description": "true if query is disabled (ie should not be returned to the dashboard)\nNOTE: this does not always imply that the query should not be executed since\nthe results from a hidden query may be used as the input to other queries (SSE etc)",
"type": "boolean"
},
"queryType": {
"description": "QueryType is an optional identifier for the type of query.\nIt can be used to distinguish different types of queries.",
"type": "string"
},
"refId": {
"description": "RefID is the unique identifier of the query, set by the frontend call.",
"type": "string"
},
"resultAssertions": {
"description": "Optionally define expected query result behavior",
"type": "object",
"required": [
"typeVersion"
],
"properties": {
"maxFrames": {
"description": "Maximum frame count",
"type": "integer"
},
"type": {
"description": "Type asserts that the frame matches a known type structure.\n\n\nPossible enum values:\n - `\"\"` \n - `\"timeseries-wide\"` \n - `\"timeseries-long\"` \n - `\"timeseries-many\"` \n - `\"timeseries-multi\"` \n - `\"directory-listing\"` \n - `\"table\"` \n - `\"numeric-wide\"` \n - `\"numeric-multi\"` \n - `\"numeric-long\"` \n - `\"log-lines\"` ",
"type": "string",
"enum": [
"",
"timeseries-wide",
"timeseries-long",
"timeseries-many",
"timeseries-multi",
"directory-listing",
"table",
"numeric-wide",
"numeric-multi",
"numeric-long",
"log-lines"
],
"x-enum-description": {}
},
"typeVersion": {
"description": "TypeVersion is the version of the Type property. Versions greater than 0.0 correspond to the dataplane\ncontract documentation https://grafana.github.io/dataplane/contract/.",
"type": "array",
"maxItems": 2,
"minItems": 2,
"items": {
"type": "integer"
}
}
},
"additionalProperties": false
},
"timeRange": {
"description": "TimeRange represents the query range\nNOTE: unlike generic /ds/query, we can now send explicit time values in each query\nNOTE: the values for timeRange are not saved in a dashboard, they are constructed on the fly",
"type": "object",
"required": [
"from",
"to"
],
"properties": {
"from": {
"description": "From is the start time of the query.",
"type": "string",
"default": "now-6h",
"examples": [
"now-1h"
]
},
"to": {
"description": "To is the end time of the query.",
"type": "string",
"default": "now",
"examples": [
"now"
]
}
},
"additionalProperties": false
},
"type": {
"type": "string",
"pattern": "^threshold$"
}
},
"additionalProperties": false,
"$schema": "https://json-schema.org/draft-04/schema"
},
{
"description": "SQLQuery requires the sqlExpression feature flag",
"type": "object",
"required": [
"expression",
"type",
"refId"
],
"properties": {
"datasource": {
"description": "The datasource",
"type": "object",
"required": [
"type"
],
"properties": {
"type": {
"description": "The datasource plugin type",
"type": "string",
"pattern": "^__expr__$"
},
"uid": {
"description": "Datasource UID",
"type": "string"
}
},
"additionalProperties": false
},
"expression": {
"type": "string",
"minLength": 1,
"examples": [
"SELECT * FROM A LIMIT 1"
]
},
"hide": {
"description": "true if query is disabled (ie should not be returned to the dashboard)\nNOTE: this does not always imply that the query should not be executed since\nthe results from a hidden query may be used as the input to other queries (SSE etc)",
"type": "boolean"
},
"queryType": {
"description": "QueryType is an optional identifier for the type of query.\nIt can be used to distinguish different types of queries.",
"type": "string"
},
"refId": {
"description": "RefID is the unique identifier of the query, set by the frontend call.",
"type": "string"
},
"resultAssertions": {
"description": "Optionally define expected query result behavior",
"type": "object",
"required": [
"typeVersion"
],
"properties": {
"maxFrames": {
"description": "Maximum frame count",
"type": "integer"
},
"type": {
"description": "Type asserts that the frame matches a known type structure.\n\n\nPossible enum values:\n - `\"\"` \n - `\"timeseries-wide\"` \n - `\"timeseries-long\"` \n - `\"timeseries-many\"` \n - `\"timeseries-multi\"` \n - `\"directory-listing\"` \n - `\"table\"` \n - `\"numeric-wide\"` \n - `\"numeric-multi\"` \n - `\"numeric-long\"` \n - `\"log-lines\"` ",
"type": "string",
"enum": [
"",
"timeseries-wide",
"timeseries-long",
"timeseries-many",
"timeseries-multi",
"directory-listing",
"table",
"numeric-wide",
"numeric-multi",
"numeric-long",
"log-lines"
],
"x-enum-description": {}
},
"typeVersion": {
"description": "TypeVersion is the version of the Type property. Versions greater than 0.0 correspond to the dataplane\ncontract documentation https://grafana.github.io/dataplane/contract/.",
"type": "array",
"maxItems": 2,
"minItems": 2,
"items": {
"type": "integer"
}
}
},
"additionalProperties": false
},
"timeRange": {
"description": "TimeRange represents the query range\nNOTE: unlike generic /ds/query, we can now send explicit time values in each query\nNOTE: the values for timeRange are not saved in a dashboard, they are constructed on the fly",
"type": "object",
"required": [
"from",
"to"
],
"properties": {
"from": {
"description": "From is the start time of the query.",
"type": "string",
"default": "now-6h",
"examples": [
"now-1h"
]
},
"to": {
"description": "To is the end time of the query.",
"type": "string",
"default": "now",
"examples": [
"now"
]
}
},
"additionalProperties": false
},
"type": {
"type": "string",
"pattern": "^sql$"
}
},
"additionalProperties": false,
"$schema": "https://json-schema.org/draft-04/schema"
}
],
"$schema": "https://json-schema.org/draft-04/schema#"
}
},
"type": {
"description": "the panel type",
"type": "string"
}
},
"additionalProperties": true,
"$schema": "https://json-schema.org/draft-04/schema#"
}
+147
View File
@@ -0,0 +1,147 @@
{
"from": "now-1h",
"to": "now",
"queries": [
{
"refId": "A",
"maxDataPoints": 1000,
"intervalMs": 5,
"expression": "$A + 10",
"type": "math"
},
{
"refId": "B",
"maxDataPoints": 1000,
"intervalMs": 5,
"type": "math",
"expression": "$A - $B"
},
{
"refId": "C",
"maxDataPoints": 1000,
"intervalMs": 5,
"expression": "$A",
"reducer": "max",
"settings": {
"mode": "dropNN"
},
"type": "reduce"
},
{
"refId": "D",
"maxDataPoints": 1000,
"intervalMs": 5,
"upsampler": "pad",
"type": "resample",
"expression": "$A",
"window": "1d",
"downsampler": "last"
},
{
"refId": "E",
"maxDataPoints": 1000,
"intervalMs": 5,
"conditions": [
{
"evaluator": {
"params": [
5
],
"type": "gt"
},
"operator": {
"type": "and"
},
"query": {
"params": [
"A"
]
},
"reducer": {
"type": "max"
}
}
],
"type": "classic_conditions"
},
{
"refId": "F",
"maxDataPoints": 1000,
"intervalMs": 5,
"expression": "A",
"conditions": [
{
"evaluator": {
"params": [
5
],
"type": "gt"
}
}
],
"type": "threshold"
},
{
"refId": "G",
"maxDataPoints": 1000,
"intervalMs": 5,
"expression": "B",
"conditions": [
{
"evaluator": {
"params": [
100
],
"type": "gt"
},
"loadedDimensions": {
"data": {
"values": [
[
18446744073709552000,
2,
3,
4,
5
]
]
},
"schema": {
"fields": [
{
"name": "fingerprints",
"type": "number",
"typeInfo": {
"frame": "uint64"
}
}
],
"meta": {
"type": "fingerprints",
"typeVersion": [
1,
0
]
},
"name": "test"
}
},
"unloadEvaluator": {
"params": [
31
],
"type": "lt"
}
}
],
"type": "threshold"
},
{
"refId": "H",
"maxDataPoints": 1000,
"intervalMs": 5,
"expression": "SELECT * FROM A limit 1",
"type": "sql"
}
]
}
File diff suppressed because it is too large Load Diff
+579
View File
@@ -0,0 +1,579 @@
{
"kind": "QueryTypeDefinitionList",
"apiVersion": "query.grafana.app/v0alpha1",
"metadata": {
"resourceVersion": "1709168280033"
},
"items": [
{
"metadata": {
"name": "math",
"resourceVersion": "1709915973363",
"creationTimestamp": "2024-02-21T22:09:26Z"
},
"spec": {
"discriminators": [
{
"field": "type",
"value": "math"
}
],
"schema": {
"$schema": "https://json-schema.org/draft-04/schema",
"additionalProperties": false,
"properties": {
"expression": {
"description": "General math expression",
"examples": [
"$A + 1",
"$A/$B"
],
"minLength": 1,
"type": "string"
}
},
"required": [
"expression"
],
"type": "object"
},
"examples": [
{
"name": "constant addition",
"saveModel": {
"expression": "$A + 10"
}
},
{
"name": "math with two queries",
"saveModel": {
"expression": "$A - $B"
}
}
]
}
},
{
"metadata": {
"name": "reduce",
"resourceVersion": "1709915979242",
"creationTimestamp": "2024-02-21T22:09:26Z"
},
"spec": {
"discriminators": [
{
"field": "type",
"value": "reduce"
}
],
"schema": {
"$schema": "https://json-schema.org/draft-04/schema",
"additionalProperties": false,
"properties": {
"expression": {
"description": "Reference to single query result",
"examples": [
"$A"
],
"minLength": 1,
"type": "string"
},
"reducer": {
"description": "The reducer\n\n\nPossible enum values:\n - `\"sum\"` \n - `\"mean\"` \n - `\"min\"` \n - `\"max\"` \n - `\"count\"` \n - `\"last\"` ",
"enum": [
"sum",
"mean",
"min",
"max",
"count",
"last"
],
"type": "string",
"x-enum-description": {}
},
"settings": {
"additionalProperties": false,
"description": "Reducer Options",
"properties": {
"mode": {
"description": "Non-number reduce behavior\n\n\nPossible enum values:\n - `\"dropNN\"` Drop non-numbers\n - `\"replaceNN\"` Replace non-numbers",
"enum": [
"dropNN",
"replaceNN"
],
"type": "string",
"x-enum-description": {
"dropNN": "Drop non-numbers",
"replaceNN": "Replace non-numbers"
}
},
"replaceWithValue": {
"description": "Only valid when mode is replace",
"type": "number"
}
},
"required": [
"mode"
],
"type": "object"
}
},
"required": [
"expression",
"reducer"
],
"type": "object"
},
"examples": [
{
"name": "get max value",
"saveModel": {
"expression": "$A",
"reducer": "max",
"settings": {
"mode": "dropNN"
}
}
}
]
}
},
{
"metadata": {
"name": "resample",
"resourceVersion": "1709915973363",
"creationTimestamp": "2024-02-21T22:09:26Z"
},
"spec": {
"discriminators": [
{
"field": "type",
"value": "resample"
}
],
"schema": {
"$schema": "https://json-schema.org/draft-04/schema",
"additionalProperties": false,
"description": "QueryType = resample",
"properties": {
"downsampler": {
"description": "The downsample function\n\n\nPossible enum values:\n - `\"sum\"` \n - `\"mean\"` \n - `\"min\"` \n - `\"max\"` \n - `\"count\"` \n - `\"last\"` ",
"enum": [
"sum",
"mean",
"min",
"max",
"count",
"last"
],
"type": "string",
"x-enum-description": {}
},
"expression": {
"description": "The math expression",
"examples": [
"$A + 1",
"$A"
],
"minLength": 1,
"type": "string"
},
"upsampler": {
"description": "The upsample function\n\n\nPossible enum values:\n - `\"pad\"` Use the last seen value\n - `\"backfilling\"` backfill\n - `\"fillna\"` Do not fill values (nill)",
"enum": [
"pad",
"backfilling",
"fillna"
],
"type": "string",
"x-enum-description": {
"backfilling": "backfill",
"fillna": "Do not fill values (nill)",
"pad": "Use the last seen value"
}
},
"window": {
"description": "The time duration",
"examples": [
"1d",
"10m"
],
"minLength": 1,
"type": "string"
}
},
"required": [
"expression",
"window",
"downsampler",
"upsampler"
],
"type": "object"
},
"examples": [
{
"name": "resample at a every day",
"saveModel": {
"downsampler": "last",
"expression": "$A",
"upsampler": "pad",
"window": "1d"
}
}
]
}
},
{
"metadata": {
"name": "classic_conditions",
"resourceVersion": "1709915973363",
"creationTimestamp": "2024-02-21T22:09:26Z"
},
"spec": {
"discriminators": [
{
"field": "type",
"value": "classic_conditions"
}
],
"schema": {
"$schema": "https://json-schema.org/draft-04/schema",
"additionalProperties": false,
"properties": {
"conditions": {
"items": {
"additionalProperties": false,
"description": "ConditionJSON is the JSON model for a single condition in ConditionsCmd.",
"properties": {
"evaluator": {
"additionalProperties": false,
"properties": {
"params": {
"items": {
"type": "number"
},
"type": "array"
},
"type": {
"description": "e.g. \"gt\"",
"type": "string"
}
},
"required": [
"params",
"type"
],
"type": "object"
},
"operator": {
"additionalProperties": false,
"properties": {
"type": {
"enum": [
"and",
"or"
],
"type": "string",
"x-enum-description": {}
}
},
"required": [
"type"
],
"type": "object"
},
"query": {
"additionalProperties": false,
"properties": {
"params": {
"items": {
"type": "string"
},
"type": "array"
}
},
"required": [
"params"
],
"type": "object"
},
"reducer": {
"additionalProperties": false,
"properties": {
"type": {
"type": "string"
}
},
"required": [
"type"
],
"type": "object"
}
},
"required": [
"evaluator",
"operator",
"query",
"reducer"
],
"type": "object"
},
"type": "array"
}
},
"required": [
"conditions"
],
"type": "object"
},
"examples": [
{
"name": "Where query A \u003e 5",
"saveModel": {
"conditions": [
{
"evaluator": {
"params": [
5
],
"type": "gt"
},
"operator": {
"type": "and"
},
"query": {
"params": [
"A"
]
},
"reducer": {
"type": "max"
}
}
]
}
}
]
}
},
{
"metadata": {
"name": "threshold",
"resourceVersion": "1709915973363",
"creationTimestamp": "2024-02-21T22:09:26Z"
},
"spec": {
"discriminators": [
{
"field": "type",
"value": "threshold"
}
],
"schema": {
"$schema": "https://json-schema.org/draft-04/schema",
"additionalProperties": false,
"properties": {
"conditions": {
"description": "Threshold Conditions",
"items": {
"additionalProperties": false,
"properties": {
"evaluator": {
"additionalProperties": false,
"properties": {
"params": {
"items": {
"type": "number"
},
"type": "array"
},
"type": {
"description": "e.g. \"gt\"",
"enum": [
"gt",
"lt",
"within_range",
"outside_range"
],
"type": "string",
"x-enum-description": {}
}
},
"required": [
"params",
"type"
],
"type": "object"
},
"loadedDimensions": {
"additionalProperties": true,
"type": "object",
"x-grafana-type": "data.DataFrame"
},
"unloadEvaluator": {
"additionalProperties": false,
"properties": {
"params": {
"items": {
"type": "number"
},
"type": "array"
},
"type": {
"description": "e.g. \"gt\"",
"enum": [
"gt",
"lt",
"within_range",
"outside_range"
],
"type": "string",
"x-enum-description": {}
}
},
"required": [
"params",
"type"
],
"type": "object"
}
},
"required": [
"evaluator"
],
"type": "object"
},
"type": "array"
},
"expression": {
"description": "Reference to single query result",
"examples": [
"$A"
],
"minLength": 1,
"type": "string"
}
},
"required": [
"expression",
"conditions"
],
"type": "object"
},
"examples": [
{
"name": "Where query A \u003e 5",
"saveModel": {
"conditions": [
{
"evaluator": {
"params": [
5
],
"type": "gt"
}
}
],
"expression": "A"
}
},
{
"name": "With loaded+unloaded evaluators",
"saveModel": {
"conditions": [
{
"evaluator": {
"params": [
100
],
"type": "gt"
},
"loadedDimensions": {
"data": {
"values": [
[
18446744073709552000,
2,
3,
4,
5
]
]
},
"schema": {
"fields": [
{
"name": "fingerprints",
"type": "number",
"typeInfo": {
"frame": "uint64"
}
}
],
"meta": {
"type": "fingerprints",
"typeVersion": [
1,
0
]
},
"name": "test"
}
},
"unloadEvaluator": {
"params": [
31
],
"type": "lt"
}
}
],
"expression": "B"
}
}
]
}
},
{
"metadata": {
"name": "sql",
"resourceVersion": "1709915973363",
"creationTimestamp": "2024-02-29T00:58:00Z"
},
"spec": {
"discriminators": [
{
"field": "type",
"value": "sql"
}
],
"schema": {
"$schema": "https://json-schema.org/draft-04/schema",
"additionalProperties": false,
"description": "SQLQuery requires the sqlExpression feature flag",
"properties": {
"expression": {
"examples": [
"SELECT * FROM A LIMIT 1"
],
"minLength": 1,
"type": "string"
}
},
"required": [
"expression"
],
"type": "object"
},
"examples": [
{
"name": "Select the first row from A",
"saveModel": {
"expression": "SELECT * FROM A limit 1"
}
}
]
}
}
]
}
+174
View File
@@ -0,0 +1,174 @@
package expr
import (
"encoding/json"
"reflect"
"testing"
data "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
"github.com/grafana/grafana-plugin-sdk-go/experimental/schemabuilder"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/expr/classic"
"github.com/grafana/grafana/pkg/expr/mathexp"
)
func TestQueryTypeDefinitions(t *testing.T) {
builder, err := schemabuilder.NewSchemaBuilder(
schemabuilder.BuilderOptions{
PluginID: []string{DatasourceType},
ScanCode: []schemabuilder.CodePaths{{
BasePackage: "github.com/grafana/grafana/pkg/expr",
CodePath: "./",
}},
Enums: []reflect.Type{
reflect.TypeOf(mathexp.ReducerSum), // pick an example value (not the root)
reflect.TypeOf(mathexp.UpsamplerPad), // pick an example value (not the root)
reflect.TypeOf(ReduceModeDrop), // pick an example value (not the root)
reflect.TypeOf(ThresholdIsAbove),
reflect.TypeOf(classic.ConditionOperatorAnd),
},
})
require.NoError(t, err)
err = builder.AddQueries(
schemabuilder.QueryTypeInfo{
Discriminators: data.NewDiscriminators("type", QueryTypeMath),
GoType: reflect.TypeOf(&MathQuery{}),
Examples: []data.QueryExample{
{
Name: "constant addition",
SaveModel: data.AsUnstructured(MathQuery{
Expression: "$A + 10",
}),
},
{
Name: "math with two queries",
SaveModel: data.AsUnstructured(MathQuery{
Expression: "$A - $B",
}),
},
},
},
schemabuilder.QueryTypeInfo{
Discriminators: data.NewDiscriminators("type", QueryTypeReduce),
GoType: reflect.TypeOf(&ReduceQuery{}),
Examples: []data.QueryExample{
{
Name: "get max value",
SaveModel: data.AsUnstructured(ReduceQuery{
Expression: "$A",
Reducer: mathexp.ReducerMax,
Settings: &ReduceSettings{
Mode: ReduceModeDrop,
},
}),
},
},
},
schemabuilder.QueryTypeInfo{
Discriminators: data.NewDiscriminators("type", QueryTypeResample),
GoType: reflect.TypeOf(&ResampleQuery{}),
Examples: []data.QueryExample{
{
Name: "resample at a every day",
SaveModel: data.AsUnstructured(ResampleQuery{
Expression: "$A",
Window: "1d",
Downsampler: mathexp.ReducerLast,
Upsampler: mathexp.UpsamplerPad,
}),
},
},
},
schemabuilder.QueryTypeInfo{
Discriminators: data.NewDiscriminators("type", QueryTypeSQL),
GoType: reflect.TypeOf(&SQLExpression{}),
Examples: []data.QueryExample{
{
Name: "Select the first row from A",
SaveModel: data.AsUnstructured(SQLExpression{
Expression: "SELECT * FROM A limit 1",
}),
},
},
},
schemabuilder.QueryTypeInfo{
Discriminators: data.NewDiscriminators("type", QueryTypeClassic),
GoType: reflect.TypeOf(&ClassicQuery{}),
Examples: []data.QueryExample{
{
Name: "Where query A > 5",
SaveModel: data.AsUnstructured(ClassicQuery{
Conditions: []classic.ConditionJSON{
{
Query: classic.ConditionQueryJSON{
Params: []string{"A"},
},
Reducer: classic.ConditionReducerJSON{
Type: "max",
},
Operator: classic.ConditionOperatorJSON{
Type: "and",
},
Evaluator: classic.ConditionEvalJSON{
Type: "gt",
Params: []float64{5},
},
},
},
}),
},
},
},
schemabuilder.QueryTypeInfo{
Discriminators: data.NewDiscriminators("type", QueryTypeThreshold),
GoType: reflect.TypeOf(&ThresholdQuery{}),
Examples: []data.QueryExample{
{
Name: "Where query A > 5",
SaveModel: data.AsUnstructured(ThresholdQuery{
Expression: "A",
Conditions: []ThresholdConditionJSON{{
Evaluator: ConditionEvalJSON{
Type: ThresholdIsAbove,
Params: []float64{5},
},
}},
}),
},
{
Name: "With loaded+unloaded evaluators",
SaveModel: toUnstructured(`{
"expression": "B",
"conditions": [
{
"evaluator": {
"params": [
100
],
"type": "gt"
},
"unloadEvaluator": {
"params": [
31
],
"type": "lt"
},
"loadedDimensions": {"schema":{"name":"test","meta":{"type":"fingerprints","typeVersion":[1,0]},"fields":[{"name":"fingerprints","type":"number","typeInfo":{"frame":"uint64"}}]},"data":{"values":[[18446744073709551615,2,3,4,5]]}}
}
]
}`),
},
},
},
)
require.NoError(t, err)
_ = builder.UpdateQueryDefinition(t, "./")
}
func toUnstructured(ex string) data.Unstructured {
v := data.Unstructured{}
_ = json.Unmarshal([]byte(ex), &v.Object)
return v
}
+8
View File
@@ -1,6 +1,7 @@
package expr
import (
"embed"
"fmt"
"strings"
@@ -177,6 +178,13 @@ func (h *ExpressionQueryReader) ReadQuery(
return eq, err
}
//go:embed query.types.json
var f embed.FS
func (h *ExpressionQueryReader) QueryTypeDefinitionListJSON() ([]byte, error) {
return f.ReadFile("query.types.json")
}
func getReferenceVar(exp string, refId string) (string, error) {
exp = strings.TrimPrefix(exp, "$")
if exp == "" {
+2 -2
View File
@@ -173,8 +173,8 @@ type ThresholdCommandConfig struct {
type ThresholdConditionJSON struct {
Evaluator ConditionEvalJSON `json:"evaluator"`
UnloadEvaluator *ConditionEvalJSON `json:"unloadEvaluator"`
LoadedDimensions *data.Frame `json:"loadedDimensions"`
UnloadEvaluator *ConditionEvalJSON `json:"unloadEvaluator,omitempty"`
LoadedDimensions *data.Frame `json:"loadedDimensions,omitempty"`
}
// IsHysteresisExpression returns true if the raw model describes a hysteresis command: