Skip to content

Commit 1a148e4

Browse files
A couple of changes. Started on the backend component - I'm getting familiar with it
1 parent 9b48b85 commit 1a148e4

File tree

6 files changed

+75
-12
lines changed

6 files changed

+75
-12
lines changed

.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ indent_size = 2
1515
[{*.js,*.css,*.ts,*.tsx,*.jsx,*.graphql}]
1616
indent_style = space
1717
indent_size = 2
18+
19+
[*.go]
20+
indent_style = tab
21+
indent_size = 4

README.md

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
# Wild GraphQL Datasource
22

3-
This is a Grafana datasource that aims to make requesting timeseries data via a GraphQL endpoint easy.
3+
**This is work in progress and is not in a working state.**
4+
5+
This is a Grafana datasource that aims to make requesting time series data via a GraphQL endpoint easy.
6+
This datasource is similar to https://github.com/fifemon/graphql-datasource, but is not compatible.
7+
This datasource tries to reimagine how GraphQL queries should be made from Grafana.
8+
9+
10+
## Development
11+
12+
https://grafana.com/developers/plugin-tools/create-a-plugin/develop-a-plugin/best-practices
13+
14+
https://grafana.com/developers/plugin-tools/tutorials/build-a-data-source-backend-plugin
15+
416

517
## Building and Development
618

@@ -23,14 +35,15 @@ Customize this setup to your liking.
2335
# install nvm https://github.com/nvm-sh/nvm#installing-and-updating
2436
nvm install 20
2537

26-
# NOTICE: This `rm` command will remove your existing go installation
27-
rm -rf ~/go
28-
wget -c https://dl.google.com/go/go1.21.5.linux-amd64.tar.gz -O - | tar -xz -C ~/
29-
echo 'export PATH="$PATH:$HOME/go/bin"' >> ~/.bashrc
38+
rm -rf /usr/local/go
39+
wget -c https://dl.google.com/go/go1.21.5.linux-amd64.tar.gz -O - | sudo tar -xz -C /usr/local
40+
# /usr/local/go is GOROOT $HOME/go is GOPATH, so add both bins to path
41+
echo 'export PATH="$PATH:/usr/local/go/bin:$HOME/go/bin"' >> ~/.bashrc
3042

3143
cd ~/Documents/Clones
3244
git clone https://github.com/magefile/mage
3345
cd mage
46+
# This will install to GOPATH, which is $HOME/go by default
3447
go run bootstrap.go
3548

3649
# *docker installation not shown*
@@ -39,22 +52,29 @@ go run bootstrap.go
3952

4053
### Setup build environment
4154

55+
Go setup
4256
```shell
57+
# This will install to your GOPATH, which is $HOME/go by default.
4358
go get -u github.com/grafana/grafana-plugin-sdk-go
4459
go mod tidy
4560
mage -v
4661
mage -l
62+
```
63+
64+
Node setup:
4765

66+
```shell
4867
npm install
49-
npm run dev
50-
npm run build
5168
```
5269

70+
5371
### Test inside Grafana instance
5472

5573
Note that `npm run server` uses `docker compose` under the hood.
5674

5775
```shell
76+
npm run dev
77+
mage -v build:linux
5878
npm run server
5979
```
6080

@@ -64,3 +84,11 @@ You may choose to use VS Code, which has free tools for developing TypeScript an
6484
IntelliJ IDEA Ultimate is a non-free choice that has support for both TypeScript and Go code.
6585
Alternatively, WebStorm (also not free) covers TypeScript development and GoLand covers Go development.
6686

87+
If you are using IntelliJ IDEA Ultimate, make sure go to "Language & Frameworks > Go Modules" and click "Enable go modules integration".
88+
89+
If you are using VS Code, this is a good read: https://github.com/golang/vscode-go/blob/master/docs/gopath.md
90+
91+
92+
## To-Do
93+
94+
* Add metrics to backend component: https://grafana.com/developers/plugin-tools/create-a-plugin/extend-a-plugin/add-logs-metrics-traces-for-backend-plugins#implement-metrics-in-your-plugin

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "wild-graphql-datasource",
33
"version": "1.0.0",
4-
"description": "Grafana datasource to interpret graph ql query results as timeseries data",
4+
"description": "Grafana datasource to interpret GraphQL query results as timeseries data",
55
"scripts": {
66
"build": "webpack -c ./.config/webpack/webpack.config.ts --env production",
77
"dev": "webpack -w -c ./.config/webpack/webpack.config.ts --env development",
@@ -16,7 +16,7 @@
1616
"sign": "npx --yes @grafana/sign-plugin@latest"
1717
},
1818
"author": "wildmountainfarms",
19-
"license": "Apache-2.0",
19+
"license": "MIT",
2020
"devDependencies": {
2121
"@babel/core": "^7.21.4",
2222
"@grafana/e2e": "10.0.3",

pkg/plugin/datasource.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
78
"math/rand"
89
"time"
910

@@ -59,7 +60,15 @@ func (d *Datasource) QueryData(ctx context.Context, req *backend.QueryDataReques
5960
return response, nil
6061
}
6162

62-
type queryModel struct{}
63+
// queryModel represents data sent from the frontend to perform a query
64+
type queryModel struct {
65+
query string
66+
}
67+
68+
type graphQLRequest struct {
69+
query string
70+
variables map[string]interface{}
71+
}
6372

6473
func (d *Datasource) query(_ context.Context, pCtx backend.PluginContext, query backend.DataQuery) backend.DataResponse {
6574
var response backend.DataResponse
@@ -71,6 +80,25 @@ func (d *Datasource) query(_ context.Context, pCtx backend.PluginContext, query
7180
if err != nil {
7281
return backend.ErrDataResponse(backend.StatusBadRequest, fmt.Sprintf("json unmarshal: %v", err.Error()))
7382
}
83+
// Although the frontend has access to global variable substitution (https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables/#global-variables)
84+
// the backend does not.
85+
// Because of this, it's beneficial to encourage users to write queries that rely as little on the frontend as possible.
86+
// This allows us to support alerting later.
87+
// These variable names that we are "hard coding" should be as similar to possible as those global variables that are available
88+
// Forum post here: https://community.grafana.com/t/how-to-use-template-variables-in-your-data-source/63250#backend-data-sources-3
89+
// More information here: https://grafana.com/docs/grafana/latest/dashboards/variables/
90+
91+
//requestJson := graphQLRequest{
92+
var _ = graphQLRequest{
93+
query: qm.query,
94+
variables: map[string]interface{}{
95+
"from": query.TimeRange.From.UnixMilli(),
96+
"to": query.TimeRange.To.UnixMilli(),
97+
"interval_ms": query.Interval.Milliseconds(),
98+
},
99+
}
100+
101+
// TODO follow this tutorial: https://www.thepolyglotdeveloper.com/2020/02/interacting-with-a-graphql-api-with-golang/
74102

75103
// create data frame response.
76104
// For an overview on data frames and how grafana handles them:
@@ -94,6 +122,9 @@ func (d *Datasource) query(_ context.Context, pCtx backend.PluginContext, query
94122
// datasource configuration page which allows users to verify that
95123
// a datasource is working as expected.
96124
func (d *Datasource) CheckHealth(_ context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
125+
126+
log.DefaultLogger.Info("Did a health check!")
127+
97128
var status = backend.HealthStatusOk
98129
var message = "Data source is working"
99130

src/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"info": {
1010
"description": "Grafana datasource to interpret GraphQL query results as timeseries data",
1111
"author": {
12-
"name": "Wildmountainfarms"
12+
"name": "wildmountainfarms"
1313
},
1414
"keywords": ["datasource"],
1515
"logos": {

0 commit comments

Comments
 (0)