Skip to content

Commit

Permalink
handle url from settings when filling the default query
Browse files Browse the repository at this point in the history
  • Loading branch information
yesoreyeram committed Nov 20, 2024
1 parent 44b48c2 commit 1b2ff49
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 23 deletions.
27 changes: 10 additions & 17 deletions pkg/models/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ type InfinityDataOverride struct {
Override string `json:"override"`
}

func ApplyDefaultsToQuery(ctx context.Context, query Query) Query {
func ApplyDefaultsToQuery(ctx context.Context, query Query, settings InfinitySettings) Query {
if query.Type == "" {
query.Type = QueryTypeJSON
if query.Source == "" {
Expand All @@ -198,20 +198,13 @@ func ApplyDefaultsToQuery(ctx context.Context, query Query) Query {
if query.Type == QueryTypeJSON && query.Source == "inline" && query.Data == "" {
query.Data = "[]"
}
if (query.Type == QueryTypeJSON || query.Type == QueryTypeGraphQL || query.Type == QueryTypeUQL || query.Type == QueryTypeGROQ) && query.Source == "url" && query.URL == "" {
query.URL = "https://raw.githubusercontent.com/grafana/grafana-infinity-datasource/main/testdata/users.json"
}
if query.Type == QueryTypeCSV && query.Source == "url" && query.URL == "" {
query.URL = "https://raw.githubusercontent.com/grafana/grafana-infinity-datasource/main/testdata/users.csv"
}
if query.Type == QueryTypeTSV && query.Source == "url" && query.URL == "" {
query.URL = "https://raw.githubusercontent.com/grafana/grafana-infinity-datasource/main/testdata/users.tsv"
}
if query.Type == QueryTypeXML && query.Source == "url" && query.URL == "" {
query.URL = "https://raw.githubusercontent.com/grafana/grafana-infinity-datasource/main/testdata/users.xml"
}
if query.Type == QueryTypeHTML && query.Source == "url" && query.URL == "" {
query.URL = "https://raw.githubusercontent.com/grafana/grafana-infinity-datasource/main/testdata/users.html"
if query.Source == "url" && query.URL == "" && strings.TrimSpace(settings.URL) == "" {
switch query.Type {
case QueryTypeJSON, QueryTypeCSV, QueryTypeTSV, QueryTypeXML, QueryTypeHTML:
query.URL = fmt.Sprintf("https://raw.githubusercontent.com/grafana/grafana-infinity-datasource/main/testdata/users.%s", strings.ToLower(string(query.Type)))
case QueryTypeGraphQL, QueryTypeUQL, QueryTypeGROQ:
query.URL = "https://raw.githubusercontent.com/grafana/grafana-infinity-datasource/main/testdata/users.json"
}
}
if query.Source == "url" && strings.ToUpper(query.URLOptions.Method) == "POST" {
if query.URLOptions.BodyType == "" {
Expand Down Expand Up @@ -306,14 +299,14 @@ func ApplyDefaultsToQuery(ctx context.Context, query Query) Query {
return query
}

func LoadQuery(ctx context.Context, backendQuery backend.DataQuery, pluginContext backend.PluginContext) (Query, error) {
func LoadQuery(ctx context.Context, backendQuery backend.DataQuery, pluginContext backend.PluginContext, settings InfinitySettings) (Query, error) {
var query Query
err := json.Unmarshal(backendQuery.JSON, &query)
if err != nil {
// Plugin error as the user should not have been able to send a bad query
return query, errorsource.PluginError(fmt.Errorf("error while parsing the query json. %w", err), false)
}
query = ApplyDefaultsToQuery(ctx, query)
query = ApplyDefaultsToQuery(ctx, query, settings)
if query.PageMode == PaginationModeList && strings.TrimSpace(query.PageParamListFieldName) == "" {
// Downstream error as user input is not correct
return query, errorsource.DownstreamError(errors.New("pagination_param_list_field_name cannot be empty"), false)
Expand Down
3 changes: 2 additions & 1 deletion pkg/models/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestLoadQuery(t *testing.T) {
tests := []struct {
name string
queryJSON string
settings models.InfinitySettings
want models.Query
wantErr error
}{
Expand Down Expand Up @@ -121,7 +122,7 @@ func TestLoadQuery(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
q := &backend.DataQuery{JSON: []byte(tt.queryJSON)}
got, err := models.LoadQuery(context.Background(), *q, backend.PluginContext{})
got, err := models.LoadQuery(context.Background(), *q, backend.PluginContext{}, tt.settings)
if tt.wantErr != nil {
require.NotNil(t, err)
assert.Equal(t, tt.wantErr, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/pluginhost/handler_querydata.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (ds *DataSource) QueryData(ctx context.Context, req *backend.QueryDataReque
return response, errorsource.PluginError(errors.New("invalid infinity client"), false)
}
for _, q := range req.Queries {
query, err := models.LoadQuery(ctx, q, req.PluginContext)
query, err := models.LoadQuery(ctx, q, req.PluginContext, ds.client.Settings)
if err != nil {
span.RecordError(err)
logger.Error("error un-marshaling the query", "error", err.Error())
Expand Down Expand Up @@ -55,7 +55,7 @@ func QueryData(ctx context.Context, backendQuery backend.DataQuery, infClient in
logger := backend.Logger.FromContext(ctx)
ctx, span := tracing.DefaultTracer().Start(ctx, "QueryData")
defer span.End()
query, err := models.LoadQuery(ctx, backendQuery, pluginContext)
query, err := models.LoadQuery(ctx, backendQuery, pluginContext, infClient.Settings)
if err != nil {
span.RecordError(err)
span.SetStatus(500, err.Error())
Expand Down
4 changes: 2 additions & 2 deletions pkg/testsuite/handler_querydata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ func TestInlineSources(t *testing.T) {
queryJSON = "{}"
}
bq := backend.DataQuery{JSON: []byte(queryJSON), TimeRange: tt.timeRange}
query, err := models.LoadQuery(context.Background(), bq, backend.PluginContext{})
query, err := models.LoadQuery(context.Background(), bq, backend.PluginContext{}, models.InfinitySettings{})
require.Nil(t, err)
frame, err := infinity.GetFrameForInlineSources(context.TODO(), query)
if tt.wantErr != nil {
Expand Down Expand Up @@ -924,7 +924,7 @@ func TestRemoteSources(t *testing.T) {
queryJSON = "{}"
}
bq := backend.DataQuery{JSON: []byte(queryJSON), TimeRange: tt.timeRange}
query, err := models.LoadQuery(context.Background(), bq, backend.PluginContext{})
query, err := models.LoadQuery(context.Background(), bq, backend.PluginContext{}, models.InfinitySettings{})
require.Nil(t, err)
client := tt.client
if client == nil {
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const DefaultInfinityQuery: InfinityQuery = {
type: 'json',
source: 'url',
format: 'table',
url: 'https://github.com/grafana/grafana-infinity-datasource/blob/main/testdata/users.json',
url: '',
url_options: { method: 'GET', data: '' },
root_selector: '',
columns: [],
Expand Down

0 comments on commit 1b2ff49

Please sign in to comment.