Skip to content

datasource-http-backend: Example migration with experimental APIs #403

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion examples/datasource-http-backend/pkg/kinds/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
type DataQuery struct {
v0alpha1.CommonQueryProperties

// Multiplier is the number to multiply the input by
// Multiply is the number to multiply the input by
Multiply int `json:"multiply,omitempty"`

// Deprecated: Use Multiply instead
Multiplier int `json:"multiplier,omitempty"`
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@
"type": "integer"
},
"multiplier": {
"description": "Multiplier is the number to multiply the input by",
"description": "Deprecated: Use Multiply instead",
"type": "integer"
},
"multiply": {
"description": "Multiply is the number to multiply the input by",
"type": "integer"
},
"queryType": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@
"type": "integer"
},
"multiplier": {
"description": "Multiplier is the number to multiply the input by",
"description": "Deprecated: Use Multiply instead",
"type": "integer"
},
"multiply": {
"description": "Multiply is the number to multiply the input by",
"type": "integer"
},
"queryType": {
Expand Down
8 changes: 6 additions & 2 deletions examples/datasource-http-backend/pkg/kinds/query.types.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{
"metadata": {
"name": "default",
"resourceVersion": "1728987397884",
"resourceVersion": "1729779295393",
"creationTimestamp": "2024-10-15T10:11:05Z"
},
"spec": {
Expand Down Expand Up @@ -47,7 +47,11 @@
"type": "integer"
},
"multiplier": {
"description": "Multiplier is the number to multiply the input by",
"description": "Deprecated: Use Multiply instead",
"type": "integer"
},
"multiply": {
"description": "Multiply is the number to multiply the input by",
"type": "integer"
},
"queryType": {
Expand Down
43 changes: 43 additions & 0 deletions examples/datasource-http-backend/pkg/plugin/convert_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package plugin

import (
"context"
"encoding/json"
"fmt"

"github.com/grafana/datasource-http-backend/pkg/kinds"
"github.com/grafana/grafana-plugin-sdk-go/backend"
)

// convertQuery parses a given DataQuery and migrates it if necessary.
func convertQuery(orig backend.DataQuery) (*kinds.DataQuery, error) {
input := &kinds.DataQuery{}
err := json.Unmarshal(orig.JSON, input)
if err != nil {
return nil, fmt.Errorf("unmarshal: %w", err)
}
if input.Multiplier != 0 && input.Multiply == 0 {
input.Multiply = input.Multiplier
input.Multiplier = 0
}
return input, nil
}

// convertQueryRequest migrates a given QueryDataRequest which can contain multiple queries.
func convertQueryRequest(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryConversionResponse, error) {
queries := make([]any, 0, len(req.Queries))
for _, q := range req.Queries {
input, err := convertQuery(q)
if err != nil {
return nil, err
}
q.JSON, err = json.Marshal(input)
if err != nil {
return nil, fmt.Errorf("marshal: %w", err)
}
queries = append(queries, q)
}
return &backend.QueryConversionResponse{
Queries: queries,
}, nil
}
9 changes: 4 additions & 5 deletions examples/datasource-http-backend/pkg/plugin/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strconv"
"time"

"github.com/grafana/datasource-http-backend/pkg/kinds"
"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/httpclient"
Expand Down Expand Up @@ -75,6 +74,7 @@ var DatasourceOpts = datasource.ManageOpts{
attribute.String("my_plugin.my_attribute", "custom value"),
},
},
QueryConversionHandler: backend.ConvertQueryFunc(convertQueryRequest),
}

// Datasource is an example datasource which can respond to data queries, reports
Expand Down Expand Up @@ -174,13 +174,12 @@ func (d *Datasource) query(ctx context.Context, pCtx backend.PluginContext, quer
return backend.DataResponse{}, fmt.Errorf("new request with context: %w", err)
}
if len(query.JSON) > 0 {
input := &kinds.DataQuery{}
err = json.Unmarshal(query.JSON, input)
input, err := convertQuery(query)
if err != nil {
return backend.DataResponse{}, fmt.Errorf("unmarshal: %w", err)
return backend.DataResponse{}, err
}
q := req.URL.Query()
q.Add("multiplier", strconv.Itoa(input.Multiplier))
q.Add("multiplier", strconv.Itoa(input.Multiply))
req.URL.RawQuery = q.Encode()
}
httpResp, err := d.httpClient.Do(req)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export class QueryEditor extends PureComponent<Props> {
type="number"
id="multiplier"
name="multiplier"
value={this.props.query.multiplier}
onChange={(e) => this.props.onChange({ ...this.props.query, multiplier: e.currentTarget.valueAsNumber })}
value={this.props.query.multiply}
onChange={(e) => this.props.onChange({ ...this.props.query, multiply: e.currentTarget.valueAsNumber })}
/>
</HorizontalGroup>
);
Expand Down
14 changes: 11 additions & 3 deletions examples/datasource-http-backend/src/datasource.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { CoreApp, DataSourceInstanceSettings } from '@grafana/data';

import { MyQuery, MyDataSourceOptions } from './types';
import { DataSourceWithBackend } from '@grafana/runtime';
import { DataSourceWithBackend, MigrationHandler } from '@grafana/runtime';
import { DataQuery } from '@grafana/schema';

export class DataSource extends DataSourceWithBackend<MyQuery, MyDataSourceOptions> implements MigrationHandler {
hasBackendMigration: boolean;

export class DataSource extends DataSourceWithBackend<MyQuery, MyDataSourceOptions> {
constructor(instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) {
super(instanceSettings);
this.hasBackendMigration = true;
}

getDefaultQuery(_: CoreApp): Partial<MyQuery> {
return { multiplier: 1 };
return { multiply: 1 };
}

shouldMigrate(query: DataQuery): boolean {
return !query.hasOwnProperty('multiply');
}
}
3 changes: 2 additions & 1 deletion examples/datasource-http-backend/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { DataSource } from './datasource';
import { ConfigEditor } from './components/ConfigEditor';
import { QueryEditor } from './components/QueryEditor';
import { MyQuery, MyDataSourceOptions } from './types';
import { QueryEditorWithMigration } from '@grafana/runtime';

export const plugin = new DataSourcePlugin<DataSource, MyQuery, MyDataSourceOptions>(DataSource)
.setConfigEditor(ConfigEditor)
.setQueryEditor(QueryEditor);
.setQueryEditor(QueryEditorWithMigration(QueryEditor));
6 changes: 2 additions & 4 deletions examples/datasource-http-backend/src/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
"author": {
"name": "Grafana Labs"
},
"keywords": [
"datasource"
],
"keywords": ["datasource"],
"logos": {
"small": "img/logo.svg",
"large": "img/logo.svg"
Expand All @@ -34,7 +32,7 @@
"updated": "%TODAY%"
},
"dependencies": {
"grafanaDependency": ">=10.4.0",
"grafanaDependency": ">=11.4.0",
"plugins": []
}
}
2 changes: 1 addition & 1 deletion examples/datasource-http-backend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { DataSourceJsonData } from '@grafana/data';
import type { DataQuery } from '@grafana/schema';

export interface MyQuery extends DataQuery {
multiplier: number;
multiply: number;
}

/**
Expand Down
Loading