Skip to content

Commit

Permalink
A couple of changes. Started on the backend component - I'm getting f…
Browse files Browse the repository at this point in the history
…amiliar with it
  • Loading branch information
retrodaredevil committed Jan 4, 2024
1 parent 9b48b85 commit 1a148e4
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ indent_size = 2
[{*.js,*.css,*.ts,*.tsx,*.jsx,*.graphql}]
indent_style = space
indent_size = 2

[*.go]
indent_style = tab
indent_size = 4
42 changes: 35 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# Wild GraphQL Datasource

This is a Grafana datasource that aims to make requesting timeseries data via a GraphQL endpoint easy.
**This is work in progress and is not in a working state.**

This is a Grafana datasource that aims to make requesting time series data via a GraphQL endpoint easy.
This datasource is similar to https://github.com/fifemon/graphql-datasource, but is not compatible.
This datasource tries to reimagine how GraphQL queries should be made from Grafana.


## Development

https://grafana.com/developers/plugin-tools/create-a-plugin/develop-a-plugin/best-practices

https://grafana.com/developers/plugin-tools/tutorials/build-a-data-source-backend-plugin


## Building and Development

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

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

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

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

### Setup build environment

Go setup
```shell
# This will install to your GOPATH, which is $HOME/go by default.
go get -u github.com/grafana/grafana-plugin-sdk-go
go mod tidy
mage -v
mage -l
```

Node setup:

```shell
npm install
npm run dev
npm run build
```


### Test inside Grafana instance

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

```shell
npm run dev
mage -v build:linux
npm run server
```

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

If you are using IntelliJ IDEA Ultimate, make sure go to "Language & Frameworks > Go Modules" and click "Enable go modules integration".

If you are using VS Code, this is a good read: https://github.com/golang/vscode-go/blob/master/docs/gopath.md


## To-Do

* 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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "wild-graphql-datasource",
"version": "1.0.0",
"description": "Grafana datasource to interpret graph ql query results as timeseries data",
"description": "Grafana datasource to interpret GraphQL query results as timeseries data",
"scripts": {
"build": "webpack -c ./.config/webpack/webpack.config.ts --env production",
"dev": "webpack -w -c ./.config/webpack/webpack.config.ts --env development",
Expand All @@ -16,7 +16,7 @@
"sign": "npx --yes @grafana/sign-plugin@latest"
},
"author": "wildmountainfarms",
"license": "Apache-2.0",
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.21.4",
"@grafana/e2e": "10.0.3",
Expand Down
33 changes: 32 additions & 1 deletion pkg/plugin/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"math/rand"
"time"

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

type queryModel struct{}
// queryModel represents data sent from the frontend to perform a query
type queryModel struct {
query string
}

type graphQLRequest struct {
query string
variables map[string]interface{}
}

func (d *Datasource) query(_ context.Context, pCtx backend.PluginContext, query backend.DataQuery) backend.DataResponse {
var response backend.DataResponse
Expand All @@ -71,6 +80,25 @@ func (d *Datasource) query(_ context.Context, pCtx backend.PluginContext, query
if err != nil {
return backend.ErrDataResponse(backend.StatusBadRequest, fmt.Sprintf("json unmarshal: %v", err.Error()))
}
// Although the frontend has access to global variable substitution (https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables/#global-variables)
// the backend does not.
// Because of this, it's beneficial to encourage users to write queries that rely as little on the frontend as possible.
// This allows us to support alerting later.
// These variable names that we are "hard coding" should be as similar to possible as those global variables that are available
// Forum post here: https://community.grafana.com/t/how-to-use-template-variables-in-your-data-source/63250#backend-data-sources-3
// More information here: https://grafana.com/docs/grafana/latest/dashboards/variables/

//requestJson := graphQLRequest{
var _ = graphQLRequest{
query: qm.query,
variables: map[string]interface{}{
"from": query.TimeRange.From.UnixMilli(),
"to": query.TimeRange.To.UnixMilli(),
"interval_ms": query.Interval.Milliseconds(),
},
}

// TODO follow this tutorial: https://www.thepolyglotdeveloper.com/2020/02/interacting-with-a-graphql-api-with-golang/

// create data frame response.
// For an overview on data frames and how grafana handles them:
Expand All @@ -94,6 +122,9 @@ func (d *Datasource) query(_ context.Context, pCtx backend.PluginContext, query
// datasource configuration page which allows users to verify that
// a datasource is working as expected.
func (d *Datasource) CheckHealth(_ context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {

log.DefaultLogger.Info("Did a health check!")

var status = backend.HealthStatusOk
var message = "Data source is working"

Expand Down
2 changes: 1 addition & 1 deletion src/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"info": {
"description": "Grafana datasource to interpret GraphQL query results as timeseries data",
"author": {
"name": "Wildmountainfarms"
"name": "wildmountainfarms"
},
"keywords": ["datasource"],
"logos": {
Expand Down

0 comments on commit 1a148e4

Please sign in to comment.