Skip to content

Commit

Permalink
Small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
retrodaredevil committed Apr 2, 2024
1 parent caf3c50 commit fafdf36
Show file tree
Hide file tree
Showing 5 changed files with 1,879 additions and 168 deletions.
2 changes: 1 addition & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ If you want to test a released, but unsigned plugin, follow this.
https://grafana.com/docs/grafana/latest/cli/#override-default-plugin-zip-url

```shell
grafana cli --pluginUrl https://github.com/wildmountainfarms/wild-graphql-datasource/releases/download/v0.0.5/retrodaredevil-wildgraphql-datasource-0.0.5.zip plugins install retrodaredevil-wildgraphql-datasource
grafana cli --pluginUrl https://github.com/wildmountainfarms/wild-graphql-datasource/releases/download/v0.0.6/retrodaredevil-wildgraphql-datasource-0.0.6.zip plugins install retrodaredevil-wildgraphql-datasource
```

Then update `grafana.ini` with
Expand Down
1 change: 0 additions & 1 deletion pkg/plugin/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ func (d *Datasource) query(ctx context.Context, pCtx backend.PluginContext, quer
Status: backend.StatusBadRequest,
}, nil
}
//log.DefaultLogger.Info("Query text is: " + qm.QueryText)

// use later: pCtx.AppInstanceSettings.DecryptedSecureJSONData
variables, _ := queryvariables.ParseVariables(query, qm.Variables)
Expand Down
28 changes: 17 additions & 11 deletions pkg/plugin/parsing/parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,37 @@ const (
UNKNOWN_ERROR ParseDataErrorType = 2
)

func ParseData(graphQlResponseData *jsonnode.Object, parsingOption querymodel.ParsingOption) (data.Frames, error, ParseDataErrorType) {
if len(parsingOption.DataPath) == 0 {
return nil, errors.New("data path cannot be empty"), FRIENDLY_ERROR
func getNodeFromDataPath(graphQlResponseData *jsonnode.Object, dataPath string) (jsonnode.Node, error) {
if len(dataPath) == 0 {
return graphQlResponseData, nil
}
split := strings.Split(parsingOption.DataPath, ".")
split := strings.Split(dataPath, ".")

var currentData *jsonnode.Object = graphQlResponseData
for _, part := range split[:len(split)-1] {
newData := currentData.Get(part)
if newData == nil {
return nil, errors.New(fmt.Sprintf("Part of data path: %s does not exist! dataPath: %s", part, parsingOption.DataPath)), FRIENDLY_ERROR
return nil, errors.New(fmt.Sprintf("Part of data path: %s does not exist! dataPath: %s", part, dataPath))
}
switch value := newData.(type) {
case *jsonnode.Object:
currentData = value
default:
return nil, errors.New(fmt.Sprintf("Part of data path: %s is not a nested object! dataPath: %s", part, parsingOption.DataPath)), FRIENDLY_ERROR
return nil, errors.New(fmt.Sprintf("Part of data path: %s is not a nested object! dataPath: %s", part, dataPath))
}
}
// after this for loop, currentData should be an array if everything is going well
// after this for loop, currentData should be an array or an object if everything is going well
finalData := currentData.Get(split[len(split)-1])
if finalData == nil {
return nil, errors.New(fmt.Sprintf("Final part of data path: %s does not exist! dataPath: %s", split[len(split)-1], parsingOption.DataPath)), FRIENDLY_ERROR
return nil, errors.New(fmt.Sprintf("Final part of data path: %s does not exist! dataPath: %s", split[len(split)-1], dataPath))
}
return finalData, nil
}

func ParseData(graphQlResponseData *jsonnode.Object, parsingOption querymodel.ParsingOption) (data.Frames, error, ParseDataErrorType) {
finalData, err := getNodeFromDataPath(graphQlResponseData, parsingOption.DataPath)
if err != nil {
return nil, err, FRIENDLY_ERROR
}

var dataArray []*jsonnode.Object
Expand All @@ -66,7 +74,7 @@ func ParseData(graphQlResponseData *jsonnode.Object, parsingOption querymodel.Pa
value,
}
default:
return nil, errors.New(fmt.Sprintf("Final part of data path: is not an array! dataPath: %s type of result: %v", parsingOption.DataPath, reflect.TypeOf(value))), FRIENDLY_ERROR
return nil, errors.New(fmt.Sprintf("Final part of data path: is not an array or object! dataPath: %s type of result: %v", parsingOption.DataPath, reflect.TypeOf(value))), FRIENDLY_ERROR
}

// We store a fieldMap inside of this frameMap.
Expand All @@ -76,8 +84,6 @@ func ParseData(graphQlResponseData *jsonnode.Object, parsingOption querymodel.Pa
// This error is never expected to occur because a correct GraphQL response should never have a particular field be of different types
fm := framemap.New()

//labelsToFieldMapMap := map[Labels]map[string]interface{}{}

for _, dataElement := range dataArray {
flatData := jsonnode.NewObject()
flattenData(dataElement, "", flatData)
Expand Down
Loading

0 comments on commit fafdf36

Please sign in to comment.