Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove all duplicated logic, move all macroses and query parsing functionality to golang, and compile golang parser to WASM #690

Open
wants to merge 100 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 55 commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
14d0910
refactored structure and move two methods to datasource.ts
lunaticusgreen Dec 8, 2024
845b46d
move scanner dependencies to one place
lunaticusgreen Dec 8, 2024
1a1c6b9
add backend replace implementation
lunaticusgreen Dec 8, 2024
8738583
add backend replace implementation
lunaticusgreen Dec 8, 2024
8765a74
add backend replace implementation
lunaticusgreen Dec 8, 2024
fd3cb98
change replace to endpoint request
lunaticusgreen Dec 8, 2024
d556272
Finalize migration of sql_query
lunaticusgreen Dec 8, 2024
b150ac2
remove sql-query-macros.ts
lunaticusgreen Dec 8, 2024
7ab94be
cleanup tests
lunaticusgreen Dec 8, 2024
4d4b479
sql-query-helper.ts remove unused methods
lunaticusgreen Dec 8, 2024
409441d
Deprecate modifyQuery method, it wasn't used and was replaced with to…
lunaticusgreen Dec 8, 2024
10e8b41
get rid of unnecessary use of scanner.raw()
lunaticusgreen Dec 8, 2024
a3fe164
refactoring
lunaticusgreen Dec 9, 2024
f9614b4
move adhocs processing to backend
lunaticusgreen Dec 9, 2024
185d08b
remove scanner.ts
lunaticusgreen Dec 9, 2024
43bcc1b
cleanup SQLQueryHelper imports
lunaticusgreen Dec 9, 2024
6c94ddb
Add replace time filter function to backend
lunaticusgreen Dec 9, 2024
e9dc130
Remove SQL query helper
lunaticusgreen Dec 9, 2024
4d29222
Merge branch 'master' into v4
lunaticusgreen Dec 9, 2024
c1cdde4
move replace.go to separate directory
lunaticusgreen Dec 9, 2024
c05c6bb
move resource handlers to separate directories
lunaticusgreen Dec 9, 2024
81409e9
Fixed lost tests
lunaticusgreen Dec 10, 2024
70ce512
move to observable
lunaticusgreen Dec 10, 2024
8930f76
rework query to support observable + remove deprecated tests
lunaticusgreen Dec 10, 2024
25c0420
remove unused packages
lunaticusgreen Dec 10, 2024
78142d7
remove temporary directory
lunaticusgreen Dec 10, 2024
d52c235
more refactoring
lunaticusgreen Dec 10, 2024
bc7861a
fix interval parsing
lunaticusgreen Dec 10, 2024
feab6ff
Merge branch 'master' of github.com:Altinity/clickhouse-grafana into v4
Slach Dec 11, 2024
b6f4b87
fix error handling
Slach Dec 11, 2024
343dd58
go fmt
Slach Dec 11, 2024
f74f5c2
go mod tidy applied
Slach Dec 11, 2024
920a391
fix parseInterval
Slach Dec 11, 2024
afe7cd1
Merge branch 'master' of github.com:Altinity/clickhouse-grafana into v4
Slach Dec 11, 2024
d4903a5
refactor and split into smaller files
lunaticusgreen Dec 10, 2024
d9385d9
fix missing metadata
lunaticusgreen Dec 11, 2024
453cab4
fix options.interval
lunaticusgreen Dec 11, 2024
d4cc79d
fix missing IntervalFactor
lunaticusgreen Dec 11, 2024
0d4c489
change rounding algorithm on BE
lunaticusgreen Dec 12, 2024
9153f33
Rewrite replace adhoc sript
lunaticusgreen Dec 13, 2024
79e3c96
Test adhoc rewrite
lunaticusgreen Dec 13, 2024
7fcac9d
fix tests
lunaticusgreen Dec 13, 2024
f5d750b
fix tests
lunaticusgreen Dec 13, 2024
2ca7e5f
temp disable test
lunaticusgreen Dec 13, 2024
6ffd972
fix wrong parameter
lunaticusgreen Dec 13, 2024
5f44b48
fix wrong parameter
lunaticusgreen Dec 13, 2024
ed947f8
replaces undefined with empty string
lunaticusgreen Dec 13, 2024
c3026d9
Change tests issues
lunaticusgreen Dec 13, 2024
71af90a
fix ast parsing
lunaticusgreen Dec 13, 2024
fb62f80
add create query endpoint
lunaticusgreen Dec 13, 2024
168459e
add queries optimization
lunaticusgreen Dec 14, 2024
daecb6f
Fix parsing error
lunaticusgreen Dec 16, 2024
171178a
Fix parsing error
lunaticusgreen Dec 16, 2024
e19e528
remove console.logs
lunaticusgreen Dec 16, 2024
539002b
remove console.logs
lunaticusgreen Dec 16, 2024
a4e3fe3
Add wasm test
lunaticusgreen Dec 17, 2024
06a9e1d
Add wasm test
lunaticusgreen Dec 17, 2024
ea6740b
Add wasm test
lunaticusgreen Dec 17, 2024
7df8a47
Add wasm test
lunaticusgreen Dec 17, 2024
dca7a76
remove replaceTimeFilters
lunaticusgreen Dec 18, 2024
90bc81d
remove getAstProperty
lunaticusgreen Dec 18, 2024
603162e
remove getAstProperty
lunaticusgreen Dec 18, 2024
2e789d2
Cleanup
lunaticusgreen Dec 18, 2024
c39f533
fix typo
lunaticusgreen Dec 18, 2024
94f8135
Merge branch 'master' of github.com:Altinity/clickhouse-grafana into v4
Slach Dec 18, 2024
1e0ee61
merged master to v4, update docker-compose to build wasm
Slach Dec 19, 2024
e511078
fix ci.yaml
Slach Dec 19, 2024
ce21fab
fix ci.yaml, is-compatible.yml, release.yml
Slach Dec 19, 2024
9614cc1
fuck where isStatement ;(
Slach Dec 19, 2024
bd18578
fix eval_query.go
Slach Dec 19, 2024
19e8304
fix testflows tests
Slach Dec 19, 2024
33292e6
Add compression of wasm file
lunaticusgreen Dec 19, 2024
7530940
Optimize build command
lunaticusgreen Dec 19, 2024
93a1d2b
merged master
Slach Dec 19, 2024
1f0c38e
Merge remote-tracking branch 'origin/v4' into v4
Slach Dec 19, 2024
e2f1c37
fix test command
lunaticusgreen Dec 23, 2024
169606f
fix test command
lunaticusgreen Dec 23, 2024
07b7f5c
fix test command
lunaticusgreen Dec 23, 2024
d885dba
Merge branch 'master' into v4
lunaticusgreen Dec 26, 2024
25e2d90
temp disable window_functions tests
lunaticusgreen Dec 28, 2024
2b7de24
Merge branch 'master' into v4
lunaticusgreen Dec 28, 2024
9edf6ce
Move compilation to tinygo
lunaticusgreen Jan 7, 2025
7720a3e
add tinygo setup step
lunaticusgreen Jan 7, 2025
1271e40
add tinygo setup step
lunaticusgreen Jan 7, 2025
40bd53b
add tinygo setup step
lunaticusgreen Jan 7, 2025
f770ae7
add tinygo setup step
lunaticusgreen Jan 7, 2025
8359ff8
add tinygo setup step
lunaticusgreen Jan 7, 2025
2327895
add tinygo setup step
lunaticusgreen Jan 7, 2025
8d062bf
add tinygo setup step
lunaticusgreen Jan 7, 2025
266e6ef
add tinygo setup step
lunaticusgreen Jan 7, 2025
3c8ad62
add tinygo setup step
lunaticusgreen Jan 7, 2025
bd95e17
add tinygo setup step
lunaticusgreen Jan 7, 2025
d83c8e2
add tinygo setup step
lunaticusgreen Jan 7, 2025
75f144e
merge with master and resolve conflicts in sql_series_specs.jest.ts
Slach Jan 9, 2025
68ea587
add tinygo to testflows.yml
Slach Jan 9, 2025
7c42f9e
add tinygo to docker/builder/Dockerfile
Slach Jan 9, 2025
8869965
Add proper initialisation
lunaticusgreen Jan 12, 2025
2eb7e50
Merge branch 'master' of github.com:Altinity/clickhouse-grafana into v4
Slach Jan 20, 2025
d79ac38
merge master to v4, conflict in src/datasource/datasource.ts
Slach Jan 21, 2025
0c5c30f
merge master to v4
Slach Jan 27, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ require (
github.com/andybalholm/brotli v1.1.1
github.com/dlclark/regexp2 v1.11.4
github.com/grafana/grafana-plugin-sdk-go v0.260.1
github.com/hyperjumptech/jiffy v1.0.0
github.com/klauspost/compress v1.17.11
github.com/stretchr/testify v1.10.0
golang.org/x/sync v0.9.0
)

require (
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/antlr/antlr4 v0.0.0-20200124162019-2d7f727a00b7 // indirect
github.com/apache/arrow/go/v15 v15.0.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
Expand Down Expand Up @@ -53,7 +55,6 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
Expand Down
168 changes: 9 additions & 159 deletions go.sum

Large diffs are not rendered by default.

110 changes: 81 additions & 29 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,14 @@
"node": ">=16"
},
"dependencies": {
"@emotion/css": "^11.1.3",
"@grafana/data": "11.3.0",
"@grafana/runtime": "11.3.0",
"@grafana/ui": "11.3.0",
"date-fns": "^4.1.0",
"date-fns-tz": "^3.2.0",
"dayjs": "^1.11.13",
"jest-resolve": "^29.7.0",
"lodash-es": "^4.17.21",
"luxon": "^3.5.0",
"react": "18.3.0",
"react-dom": "18.3.0",
"tslib": "2.5.3"
"react-dom": "18.3.0"
},
"packageManager": "[email protected]"
}
35 changes: 30 additions & 5 deletions pkg/datasource.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
package main

import (
"context"
"encoding/json"
"fmt"
"golang.org/x/sync/errgroup"

"context"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
"github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter"
"golang.org/x/sync/errgroup"
"net/http"
)

func newResourceHandler() backend.CallResourceHandler {
mux := http.NewServeMux()
mux.HandleFunc("/replace", func(w http.ResponseWriter, r *http.Request) {
replaceQuery(w, r)
})
mux.HandleFunc("/get-ast-property", func(w http.ResponseWriter, r *http.Request) {
getAstProperty(w, r)
})
mux.HandleFunc("/get-ast", func(w http.ResponseWriter, r *http.Request) {
getCompleteAst(w, r)
})
mux.HandleFunc("/apply-adhoc-filters", func(w http.ResponseWriter, r *http.Request) {
applyAdhocFilters(w, r)
})
mux.HandleFunc("/replace-time-filters", func(w http.ResponseWriter, r *http.Request) {
Slach marked this conversation as resolved.
Show resolved Hide resolved
replaceTimeFilters(w, r)
})
mux.HandleFunc("/create-query", func(w http.ResponseWriter, r *http.Request) {
createQuery(w, r)
})
return httpadapter.New(mux)
}

func GetDatasourceServeOpts() datasource.ServeOpts {
ds := &ClickHouseDatasource{
im: datasource.NewInstanceManager(NewDatasourceSettings),
}

return datasource.ServeOpts{
QueryDataHandler: ds,
CheckHealthHandler: ds,
QueryDataHandler: ds,
CheckHealthHandler: ds,
CallResourceHandler: newResourceHandler(),
}
}

Expand Down
85 changes: 75 additions & 10 deletions pkg/eval_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package main

import (
"fmt"
"github.com/dlclark/regexp2"
"github.com/hyperjumptech/jiffy"
"math"
"regexp"
"strconv"
"strings"
"time"

"github.com/dlclark/regexp2"
"unicode"
)

/* var NumberOnlyRegexp = regexp.MustCompile(`^[+-]?\d+(\.\d+)?$`) */
Expand Down Expand Up @@ -51,10 +53,64 @@ type EvalQuery struct {
Database string `json:"database"`
Table string `json:"table"`
MaxDataPoints int64
FrontendDatasource bool `json:"frontendDatasource"`
From time.Time
To time.Time
}

func parseInterval(interval string, intervalFactor int) (int, int, error) {
// Parse the duration
dur, err := jiffy.DurationOf(interval)
if err != nil {
return 0, 0, err
}

// Describe the duration (for a formatted string output)
durationSeconds := strings.Map(func(r rune) rune {
if unicode.IsDigit(r) {
return r
}
return -1
}, jiffy.DescribeDuration(dur, &jiffy.Want{
Second: true,
Millisecond: false,
Verbose: false,
}))

durationMilliseconds := strings.Map(func(r rune) rune {
if unicode.IsDigit(r) {
return r
}
return -1
}, jiffy.DescribeDuration(dur, &jiffy.Want{
Second: false,
Millisecond: true,
Verbose: false,
}))

var seconds, milliseconds int
// Calculate seconds and milliseconds
if durationSeconds != "" {
seconds, err = strconv.Atoi(durationSeconds)
if err != nil {
return 0, 0, err
}
}

if durationMilliseconds != "" {
milliseconds, err = strconv.Atoi(durationMilliseconds)
if err != nil {
return 0, 0, err
}
}

if seconds == 0 {
seconds = 1
}

return seconds * intervalFactor, milliseconds * intervalFactor, nil
}

func (q *EvalQuery) ApplyMacrosAndTimeRangeToQuery() (string, error) {
query, err := q.replace(q.Query)
if err != nil {
Expand All @@ -76,12 +132,12 @@ func (q *EvalQuery) replace(query string) (string, error) {
i := 1 * time.Second
ms := 1 * time.Millisecond
if q.Interval != "" {
duration, err := time.ParseDuration(q.Interval)
intervalSeconds, intervalMs, err := parseInterval(q.Interval, q.IntervalFactor)
if err != nil {
return "", err
}
q.IntervalSec = int(math.Ceil(duration.Seconds()))
q.IntervalMs = int(duration.Milliseconds())
q.IntervalSec = intervalSeconds
q.IntervalMs = intervalMs
}
if q.IntervalSec <= 0 {
if q.MaxDataPoints > 0 {
Expand Down Expand Up @@ -322,15 +378,15 @@ func (q *EvalQuery) applyMacros(query string, ast *EvalAST) (string, error) {
if q.contain(ast, "$perSecondColumnsAggregated") {
return q.perSecondColumnsAggregated(query, ast)
}
if q.contain(ast, "$deltaColumnsAggregated") {
return q.deltaColumnsAggregated(query, ast)
}
if q.contain(ast, "$delta") {
return q.delta(query, ast)
}
if q.contain(ast, "$deltaColumns") {
return q.deltaColumns(query, ast)
}
if q.contain(ast, "$deltaColumnsAggregated") {
return q.deltaColumnsAggregated(query, ast)
}
if q.contain(ast, "$increase") {
return q.increase(query, ast)
}
Expand Down Expand Up @@ -1203,7 +1259,12 @@ func (q *EvalQuery) round(dt time.Time, round int) time.Time {
if round == 0 {
return dt
}
return dt.Truncate(time.Duration(round) * time.Second)

// Round to the nearest multiple of `round` seconds
coefficient := round
rounded := time.Unix(dt.Unix()/int64(coefficient)*int64(coefficient), 0)

return rounded
}

func (q *EvalQuery) convertInterval(interval string, intervalFactor int, ms bool) (int, error) {
Expand All @@ -1217,6 +1278,7 @@ func (q *EvalQuery) convertInterval(interval string, intervalFactor int, ms bool
if ms {
return int(math.Ceil(float64(d.Milliseconds()) * float64(intervalFactor))), nil
}

return int(math.Ceil(d.Seconds() * float64(intervalFactor))), nil
}

Expand Down Expand Up @@ -1708,6 +1770,9 @@ func (s *EvalQueryScanner) RemoveComments(query string) (string, error) {
}

func (s *EvalQueryScanner) AddMetadata(query string, q *EvalQuery) string {
if q.FrontendDatasource {
return "/* grafana dashboard=$__dashboard, user=$__user */\n" + query
}
return "/* grafana alerts rule=" + q.RuleUid + " query=" + q.RefId + " */ " + query
}

Expand Down Expand Up @@ -2145,7 +2210,7 @@ func printAST(AST *EvalAST, tab string) string {
result += printItems(AST.Obj["prewhere"].(*EvalAST), tab, "")
}

if AST.hasOwnProperty("where") {
if AST.hasOwnProperty("where") && len(AST.Obj["where"].(*EvalAST).Arr) > 0 {
result += newLine + tab + "WHERE"
result += printItems(AST.Obj["where"].(*EvalAST), tab, "")
}
Expand Down
Loading
Loading