Skip to content

Commit

Permalink
Merge branch 'main' into column-selector-fix-for-variables-editor
Browse files Browse the repository at this point in the history
  • Loading branch information
yesoreyeram authored May 20, 2024
2 parents 9d153b1 + 23b6ed4 commit 535f620
Show file tree
Hide file tree
Showing 28 changed files with 71 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-waves-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'grafana-infinity-datasource': minor
---

Alias support for the backend summarize option
2 changes: 2 additions & 0 deletions docs/sources/query/backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ If you want to filter data, you can use filter expression. The expression should
Backend parser also supports summarizing the fields into a metric. For example, `count(something)` or `max(some-other-thing)` or `mean([some other thing])` can be calculated from the array of data. You can also use numeric options such as `sum`,`min`,`max`,`mean`,`first` and `last`. You can also use expressions like `sum(price) / count(id)`.

When specifying the field name, all special characters must be replaced with `-` and also transformed to lower case. Or the field name can also be specified within square brackets if they contain special characters. For example if the field name is `Something Else!`, then that can be specified like `min(something-else-)` or `min([Something Else!])`.

You can optionally specify the alias for the summarize expression using **Summarize alias** option. If nothing specified, `summary` will be used as alias. This alias will be helpful when you want to use merge transformation with results from different queries.
6 changes: 5 additions & 1 deletion pkg/infinity/postprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ func PostProcessFrame(ctx context.Context, frame *data.Frame, query models.Query
return frame, fmt.Errorf("error applying filter. %w", err)
}
if strings.TrimSpace(query.SummarizeExpression) != "" {
return transformations.GetSummaryFrame(frame, query.SummarizeExpression, query.SummarizeBy, "summary")
alias := query.SummarizeAlias
if alias == "" {
alias = "summary"
}
return transformations.GetSummaryFrame(frame, query.SummarizeExpression, query.SummarizeBy, alias)
}
frame.Meta = &data.FrameMeta{Custom: &CustomMeta{Query: query}}
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/models/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type Query struct {
FilterExpression string `json:"filterExpression"`
SummarizeExpression string `json:"summarizeExpression"`
SummarizeBy string `json:"summarizeBy"`
SummarizeAlias string `json:"summarizeAlias"`
UQL string `json:"uql"`
GROQ string `json:"groq"`
SQLiteQuery string `json:"sqlite_query"`
Expand Down
2 changes: 2 additions & 0 deletions pkg/models/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestLoadQuery(t *testing.T) {
"parser" : "backend",
"summarizeExpression": "sum(id)/10",
"summarizeBy" : "none",
"summarizeAlias": "something",
"filterExpression": "id > 3 && id < 8",
"url" : "https://foo.com" ,
"url_options" : {
Expand Down Expand Up @@ -90,6 +91,7 @@ func TestLoadQuery(t *testing.T) {
Parser: "backend",
SummarizeExpression: "sum(id)/10",
SummarizeBy: "none",
SummarizeAlias: "something",
FilterExpression: "id > 3 && id < 8",
RootSelector: "my-root_selector",
Columns: []models.InfinityColumn{{Selector: "s", Text: "t", Type: "string", TimeStampFormat: "2006"}},
Expand Down
2 changes: 2 additions & 0 deletions pkg/testsuite/golden/backend-computed-columns.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -116,6 +117,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"sqlite_query": "",
Expand Down
2 changes: 2 additions & 0 deletions pkg/testsuite/golden/backend-filter-computed-columns.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "!(name IN ('Macbook','Macbook Air'))",
// "summarizeExpression": "sum(cost)",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -120,6 +121,7 @@
"filterExpression": "!(name IN ('Macbook','Macbook Air'))",
"summarizeExpression": "sum(cost)",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"sqlite_query": "",
Expand Down
2 changes: 2 additions & 0 deletions pkg/testsuite/golden/csv_backend_inline_default.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -109,6 +110,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"sqlite_query": "",
Expand Down
2 changes: 2 additions & 0 deletions pkg/testsuite/golden/csv_backend_url_default.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -108,6 +109,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"sqlite_query": "",
Expand Down
2 changes: 2 additions & 0 deletions pkg/testsuite/golden/csv_default_url_default.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -102,6 +103,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"sqlite_query": "",
Expand Down
2 changes: 2 additions & 0 deletions pkg/testsuite/golden/html_backend_url_default.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -143,6 +144,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"sqlite_query": "",
Expand Down
2 changes: 2 additions & 0 deletions pkg/testsuite/golden/html_default_url_default.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -133,6 +134,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"sqlite_query": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -102,6 +103,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"sqlite_query": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -107,6 +108,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"sqlite_query": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "parse-json | count",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -102,6 +103,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "parse-json | count",
"groq": "",
"sqlite_query": "",
Expand Down
2 changes: 2 additions & 0 deletions pkg/testsuite/golden/json_default_url_default.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -104,6 +105,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"sqlite_query": "",
Expand Down
2 changes: 2 additions & 0 deletions pkg/testsuite/golden/remote/csv_query.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -102,6 +103,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"sqlite_query": "",
Expand Down
2 changes: 2 additions & 0 deletions pkg/testsuite/golden/remote/groq_query.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "*{1,2,3}",
// "sqlite_query": "",
Expand Down Expand Up @@ -102,6 +103,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "*{1,2,3}",
"sqlite_query": "",
Expand Down
2 changes: 2 additions & 0 deletions pkg/testsuite/golden/remote/json_query.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -106,6 +107,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"sqlite_query": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -102,6 +103,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"sqlite_query": "",
Expand Down
2 changes: 2 additions & 0 deletions pkg/testsuite/golden/remote/uql_query.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "parse-json | count",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -102,6 +103,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "parse-json | count",
"groq": "",
"sqlite_query": "",
Expand Down
2 changes: 2 additions & 0 deletions pkg/testsuite/golden/scenario_azure_cost_management.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -325,6 +326,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"sqlite_query": "",
Expand Down
2 changes: 2 additions & 0 deletions pkg/testsuite/golden/transformations_limit_default_A.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -114,6 +115,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"sqlite_query": "",
Expand Down
2 changes: 2 additions & 0 deletions pkg/testsuite/golden/transformations_limit_default_B.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -114,6 +115,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"sqlite_query": "",
Expand Down
2 changes: 2 additions & 0 deletions pkg/testsuite/golden/xml_backend_url_default.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "sqlite_query": "",
Expand Down Expand Up @@ -108,6 +109,7 @@
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"sqlite_query": "",
Expand Down
3 changes: 2 additions & 1 deletion pkg/testsuite/handler_querydata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,13 +768,14 @@ func TestInlineSources(t *testing.T) {
"data": "[{\"Sex\":\"Male\"},{\"Sex\":\"Male\"},{\"Sex\":null},{\"Sex\":\"Female\"},{\"Sex\":\"Others\"}]",
"filterExpression": "Sex != 'Female' && Sex != null",
"summarizeExpression": "count(Sex)",
"summarizeAlias": "Count by gender",
"summarizeBy": "Sex"
}`,
skipGoldenCheck: true,
test: func(t *testing.T, frame *data.Frame) {
require.NotNil(t, frame)
assert.Equal(t, data.NewField("Sex", nil, []*string{toSP("Male"), toSP(""), toSP("Others")}), frame.Fields[0])
assert.Equal(t, data.NewField("summary", nil, []*float64{toFP(2), toFP(1), toFP(1)}), frame.Fields[1])
assert.Equal(t, data.NewField("Count by gender", nil, []*float64{toFP(2), toFP(1), toFP(1)}), frame.Fields[1])
},
},
{
Expand Down
Loading

0 comments on commit 535f620

Please sign in to comment.