Skip to content

Commit

Permalink
Extract queryData function
Browse files Browse the repository at this point in the history
  • Loading branch information
fridgepoet committed Sep 7, 2023
1 parent 2a64804 commit 0ab0269
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/opensearch/opensearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,22 @@ func (ds *OpenSearchDatasource) CheckHealth(ctx context.Context, req *backend.Ch
// The QueryDataResponse contains a map of RefID to the response for each query, and each response
// contains Frames ([]*Frame).
func (ds *OpenSearchDatasource) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
if len(req.Queries) == 0 {
return queryData(ctx, req.Queries, req.PluginContext.DataSourceInstanceSettings, ds.httpClient)
}

// separate function to allow testing the whole transformation and query flow
func queryData(ctx context.Context, queries []backend.DataQuery, dsSettings *backend.DataSourceInstanceSettings, httpClient *http.Client) (*backend.QueryDataResponse, error) {
if len(queries) == 0 {
return nil, fmt.Errorf("query contains no queries")
}

timeRange := req.Queries[0].TimeRange
osClient, err := client.NewClient(ctx, req.PluginContext.DataSourceInstanceSettings, ds.httpClient, &timeRange)
timeRange := queries[0].TimeRange
osClient, err := client.NewClient(ctx, dsSettings, httpClient, &timeRange)
if err != nil {
return nil, err
}

query := newTimeSeriesQuery(osClient, req.Queries, intervalCalculator)
query := newTimeSeriesQuery(osClient, queries, intervalCalculator)
response, err := wrapError(query.execute())
return response, err
}
Expand Down

0 comments on commit 0ab0269

Please sign in to comment.