Skip to content

Commit

Permalink
Merge pull request #1 from ryantxu/standalone
Browse files Browse the repository at this point in the history
Chore: support standalone grpc server
  • Loading branch information
ryantxu committed Apr 25, 2021
2 parents c28eafe + 265112c commit 9501a61
Show file tree
Hide file tree
Showing 16 changed files with 3,348 additions and 2,959 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Expand Up @@ -20,5 +20,3 @@ cypress/report.json
cypress/screenshots/actual
cypress/videos/
provisioning

process.yml
16 changes: 16 additions & 0 deletions .vscode/launch.json
@@ -0,0 +1,16 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Run standalone plugin",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/pkg/main.go",
"env": {},
"args": [
"--standalone=true",
]
}
]
}
11 changes: 6 additions & 5 deletions go.mod
@@ -1,10 +1,11 @@
module github.com/ryantxu/noaa-datasource

go 1.14
go 1.16

// replace github.com/grafana/grafana-plugin-sdk-go => ../../more/grafana-plugin-sdk-go

require (
github.com/grafana/grafana-plugin-sdk-go v0.79.1-0.20201210164851-dc5eb763cf16
github.com/magefile/mage v1.10.0
github.com/stretchr/testify v1.6.1
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
github.com/grafana/grafana-plugin-sdk-go v0.92.0
github.com/magefile/mage v1.11.0
github.com/stretchr/testify v1.7.0
)
388 changes: 351 additions & 37 deletions go.sum

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Expand Up @@ -8,13 +8,13 @@
"dev": "rm -rf node_modules/@grafana/data/node_modules; grafana-toolkit plugin:dev",
"watch": "grafana-toolkit plugin:dev --watch"
},
"repository": "github:ryantxu/noaa-tidesandcurrents",
"repository": "github:ryantxu/noaa-datasource",
"author": "Ryan McKinley",
"license": "Apache-2.0",
"devDependencies": {
"@grafana/data": "next",
"@grafana/runtime": "next",
"@grafana/toolkit": "next",
"@grafana/ui": "next"
"@grafana/data": "latest",
"@grafana/runtime": "latest",
"@grafana/toolkit": "latest",
"@grafana/ui": "latest"
}
}
6 changes: 2 additions & 4 deletions pkg/main.go
Expand Up @@ -4,14 +4,12 @@ import (
"os"

"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
"github.com/grafana/grafana-plugin-sdk-go/experimental"
"github.com/ryantxu/noaa-datasource/pkg/plugin"
)

func main() {
backend.SetupPluginEnvironment("oas-datasource")

err := datasource.Serve(plugin.GetDatasourceServeOpts())
err := experimental.DoGRPC("noaa-datasource", plugin.GetDatasourceServeOpts())

// Log any error if we could start the plugin.
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/models/query.go
Expand Up @@ -12,7 +12,7 @@ const (
)

type NOAAQuery struct {
Station int32 `json:"station,omitempty"`
Station string `json:"station,omitempty"`
Product string `json:"product,omitempty"`
Units string `json:"units,omitempty"` // 'metric' | 'english';
Date string `json:"date,omitempty"` // 'query' | 'today' | 'recent';...
Expand Down
15 changes: 12 additions & 3 deletions pkg/noaa/client.go
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"time"

"github.com/grafana/grafana-plugin-sdk-go/data"
Expand Down Expand Up @@ -47,9 +48,17 @@ func NewNOAAClient() NOAAClient {
const layoutXXX = "20060102 15:04" // "yyyyMMdd HH:mm"

func (c *NOAAClient) getQueryString(q *models.NOAAQuery) (string, error) {
if q.Station == "" {
return "", fmt.Errorf("missing station in query")
}
stationId, err := strconv.ParseInt(q.Station, 10, 64)
if err != nil {
return "", fmt.Errorf("invalid station id")
}

qstr := "time_zone=gmt&application=Grafana&format=json&datum=STND"
if q.Station < 100 {
return "", fmt.Errorf("missing station")
if stationId < 100 {
return "", fmt.Errorf("invalid stationId (too small)")
}
if q.Product == "" {
return "", fmt.Errorf("missing product")
Expand Down Expand Up @@ -77,7 +86,7 @@ func (c *NOAAClient) getQueryString(q *models.NOAAQuery) (string, error) {
qstr += fmt.Sprintf("&date=%s", q.Date)
}

qstr += fmt.Sprintf("&station=%d", q.Station)
qstr += fmt.Sprintf("&station=%d", stationId)
qstr += fmt.Sprintf("&product=%s", q.Product)
return qstr, nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugin/datasource.go
Expand Up @@ -129,9 +129,9 @@ func (ds *Datasource) getNextHighLow(ctx context.Context, query *models.NOAAQuer
}

// GetNextHighLow is a helper for the rasberry pi project
func GetNextHighLow(station int32, units string) (*data.Frame, error) {
func GetNextHighLow(station int64, units string) (*data.Frame, error) {
query := &models.NOAAQuery{
Station: station,
Station: fmt.Sprintf("%d", station),
Units: units,
}
ds := NewDatasource()
Expand Down
Binary file removed pkg/plugin/debug.test
Binary file not shown.
26 changes: 9 additions & 17 deletions src/DataSource.ts
@@ -1,10 +1,9 @@
import { DataSourceInstanceSettings, ScopedVars } from '@grafana/data';
import { DataSourceWithBackend } from '@grafana/runtime';
import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime';

import { NOAAQuery, NOAAOptions } from './types';
import { NOAAQuery, NOAAOptions, TCProduct } from './types';

export class DataSource extends DataSourceWithBackend<NOAAQuery, NOAAOptions> {
// Easy access for QueryEditor
readonly options: NOAAOptions;

constructor(instanceSettings: DataSourceInstanceSettings<NOAAOptions>) {
Expand All @@ -23,22 +22,15 @@ export class DataSource extends DataSourceWithBackend<NOAAQuery, NOAAOptions> {
}

getQueryDisplayText(query: NOAAQuery): string {
return 'TODO: ' + JSON.stringify(query);
return JSON.stringify(query);
}

applyTemplateVariables(query: NOAAQuery, scopedVars: ScopedVars): NOAAQuery {
// if (!query.rawQuery) {
// return query;
// }

// const templateSrv = getTemplateSrv();
// return {
// ...query,
// database: templateSrv.replace(query.database || '', scopedVars),
// table: templateSrv.replace(query.table || '', scopedVars),
// measure: templateSrv.replace(query.measure || '', scopedVars),
// rawQuery: templateSrv.replace(query.rawQuery), // DO NOT include scopedVars! it uses $__interval_ms!!!!!
// };
return query;
const templateSrv = getTemplateSrv();
return {
...query,
product: templateSrv.replace(query.product || '', scopedVars) as TCProduct,
station: templateSrv.replace(query.station || '', scopedVars),
};
}
}
8 changes: 4 additions & 4 deletions src/components/QueryEditor.tsx
Expand Up @@ -42,7 +42,7 @@ export class QueryEditor extends PureComponent<Props> {

onStationChange = (txt: string) => {
const { onChange, query, onRunQuery } = this.props;
onChange({ ...query, station: +txt });
onChange({ ...query, station: txt });
onRunQuery();
};

Expand All @@ -55,7 +55,7 @@ export class QueryEditor extends PureComponent<Props> {
<InlineField label="Product" labelWidth={labelWidth} grow={true}>
<Select
options={tidesAndCurrentsProducts}
value={tidesAndCurrentsProducts.find(v => v.value === query.product)}
value={tidesAndCurrentsProducts.find((v) => v.value === query.product)}
onChange={this.onProductChange}
placeholder="Select query type"
menuPlacement="bottom"
Expand All @@ -64,7 +64,7 @@ export class QueryEditor extends PureComponent<Props> {
<InlineField label="Unit">
<Select
options={units}
value={units.find(v => v.value === query.units) || units[0]}
value={units.find((v) => v.value === query.units) || units[0]}
onChange={this.onUnitsChange}
placeholder="Select units"
menuPlacement="bottom"
Expand All @@ -73,7 +73,7 @@ export class QueryEditor extends PureComponent<Props> {
<InlineField label="Date">
<Select
options={dateOptions}
value={dateOptions.find(v => v.value === query.date) || dateOptions[0]}
value={dateOptions.find((v) => v.value === query.date) || dateOptions[0]}
onChange={this.onDateChange}
placeholder="Select date"
menuPlacement="bottom"
Expand Down

0 comments on commit 9501a61

Please sign in to comment.