|
| 1 | +package util |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "github.com/grafana/grafana-plugin-sdk-go/data" |
| 7 | + "reflect" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +// the purpose of this file is to parse JSON data with configuration from a ParsingOption |
| 13 | + |
| 14 | +type ParsingOption struct { |
| 15 | + // The path from the root to the array. This is dot-delimited |
| 16 | + DataPath string `json:"dataPath"` |
| 17 | + // the time path relative to the data path. |
| 18 | + TimePath string `json:"timePath"` |
| 19 | +} |
| 20 | + |
| 21 | +func ParseData(graphQlResponseData map[string]interface{}, parsingOption ParsingOption) (*data.Frame, error) { |
| 22 | + if len(parsingOption.DataPath) == 0 { |
| 23 | + return nil, errors.New("data path cannot be empty") |
| 24 | + } |
| 25 | + split := strings.Split(parsingOption.DataPath, ".") |
| 26 | + |
| 27 | + var currentData map[string]interface{} = graphQlResponseData |
| 28 | + for _, part := range split[:len(split)-1] { |
| 29 | + newData, exists := currentData[part] |
| 30 | + if !exists { |
| 31 | + return nil, errors.New(fmt.Sprintf("Part of data path: %s does not exist! dataPath: %s", part, parsingOption.DataPath)) |
| 32 | + } |
| 33 | + switch value := newData.(type) { |
| 34 | + case map[string]interface{}: |
| 35 | + currentData = value |
| 36 | + default: |
| 37 | + return nil, errors.New(fmt.Sprintf("Part of data path: %s is not a nested object! dataPath: %s", part, parsingOption.DataPath)) |
| 38 | + } |
| 39 | + } |
| 40 | + // after this for loop, currentData should be an array if everything is going well |
| 41 | + finalData, finalDataExists := currentData[split[len(split)-1]] |
| 42 | + if !finalDataExists { |
| 43 | + return nil, errors.New(fmt.Sprintf("Final part of data path: %s does not exist! dataPath: %s", split[len(split)-1], parsingOption.DataPath)) |
| 44 | + } |
| 45 | + |
| 46 | + var dataArray []map[string]interface{} |
| 47 | + switch value := finalData.(type) { |
| 48 | + case []interface{}: |
| 49 | + dataArray = make([]map[string]interface{}, len(value)) |
| 50 | + for i, element := range value { |
| 51 | + switch typedElement := element.(type) { |
| 52 | + case map[string]interface{}: |
| 53 | + dataArray[i] = typedElement |
| 54 | + default: |
| 55 | + return nil, errors.New(fmt.Sprintf("One of the elements inside the data array is not an object! element: %d is of type: %v", i, reflect.TypeOf(element))) |
| 56 | + } |
| 57 | + } |
| 58 | + default: |
| 59 | + 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))) |
| 60 | + } |
| 61 | + |
| 62 | + // fieldMap is a map of keys to array of data points. Upon first initialization of a particular key's value, |
| 63 | + // an array should be chosen corresponding to the first value of that key. |
| 64 | + // Upon subsequent element insertion, if the type of the array does not match that elements type, an error is thrown. |
| 65 | + // This error is never expected to occur because a correct GraphQL response should never have a particular field be of different types |
| 66 | + fieldMap := map[string]interface{}{} |
| 67 | + |
| 68 | + for _, dataElement := range dataArray { |
| 69 | + flatData := map[string]interface{}{} |
| 70 | + flattenData(dataElement, "", flatData) |
| 71 | + for key, value := range flatData { |
| 72 | + existingFieldValues, fieldValuesExist := fieldMap[key] |
| 73 | + |
| 74 | + if key == parsingOption.TimePath { |
| 75 | + var timeValue time.Time |
| 76 | + switch valueValue := value.(type) { |
| 77 | + case string: |
| 78 | + // TODO allow user to customize time format |
| 79 | + parsedTime, err := time.Parse(time.RFC3339, valueValue) |
| 80 | + if err != nil { |
| 81 | + return nil, errors.New(fmt.Sprintf("Time could not be parsed! Time: %s", valueValue)) |
| 82 | + } |
| 83 | + timeValue = parsedTime |
| 84 | + case float64: |
| 85 | + timeValue = time.UnixMilli(int64(valueValue)) |
| 86 | + case bool: |
| 87 | + return nil, errors.New("time field is a bool") |
| 88 | + default: |
| 89 | + // This case should never happen because we never expect other types to pop up here |
| 90 | + return nil, errors.New(fmt.Sprintf("Unsupported time type! Time: %s type: %v", valueValue, reflect.TypeOf(valueValue))) |
| 91 | + } |
| 92 | + var fieldValues []time.Time |
| 93 | + if fieldValuesExist { |
| 94 | + switch typedExistingFieldValues := existingFieldValues.(type) { |
| 95 | + case []time.Time: |
| 96 | + fieldValues = typedExistingFieldValues |
| 97 | + default: |
| 98 | + return nil, errors.New(fmt.Sprintf("This error should never occur. The existing array for time field values is of the type: %v", reflect.TypeOf(existingFieldValues))) |
| 99 | + } |
| 100 | + } else { |
| 101 | + fieldValues = []time.Time{} |
| 102 | + } |
| 103 | + fieldValues = append(fieldValues, timeValue) |
| 104 | + fieldMap[key] = fieldValues |
| 105 | + } else { |
| 106 | + if fieldValuesExist { |
| 107 | + switch typedExistingFieldValues := existingFieldValues.(type) { |
| 108 | + case []float64: |
| 109 | + switch typedValue := value.(type) { |
| 110 | + case float64: |
| 111 | + fieldMap[key] = append(typedExistingFieldValues, typedValue) |
| 112 | + default: |
| 113 | + return nil, errors.New(fmt.Sprintf("Existing field values for key: %s is float64, but got value with type: %v", key, reflect.TypeOf(value))) |
| 114 | + } |
| 115 | + case []string: |
| 116 | + switch typedValue := value.(type) { |
| 117 | + case string: |
| 118 | + fieldMap[key] = append(typedExistingFieldValues, typedValue) |
| 119 | + default: |
| 120 | + return nil, errors.New(fmt.Sprintf("Existing field values for key: %s is string, but got value with type: %v", key, reflect.TypeOf(value))) |
| 121 | + } |
| 122 | + case []bool: |
| 123 | + switch typedValue := value.(type) { |
| 124 | + case bool: |
| 125 | + fieldMap[key] = append(typedExistingFieldValues, typedValue) |
| 126 | + default: |
| 127 | + return nil, errors.New(fmt.Sprintf("Existing field values for key: %s is bool, but got value with type: %v", key, reflect.TypeOf(value))) |
| 128 | + } |
| 129 | + default: |
| 130 | + return nil, errors.New(fmt.Sprintf("This error should never occur. The existing array for time field values is of the type: %v", reflect.TypeOf(existingFieldValues))) |
| 131 | + } |
| 132 | + } else { |
| 133 | + switch typedValue := value.(type) { |
| 134 | + case float64: |
| 135 | + fieldMap[key] = []float64{typedValue} |
| 136 | + case string: |
| 137 | + fieldMap[key] = []string{typedValue} |
| 138 | + case bool: |
| 139 | + fieldMap[key] = []bool{typedValue} |
| 140 | + default: |
| 141 | + return nil, errors.New(fmt.Sprintf("Unsupported and unexpected type for key: %s. Type is: %v", key, reflect.TypeOf(value))) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // create data frame response. |
| 149 | + // For an overview on data frames and how grafana handles them: |
| 150 | + // https://grafana.com/developers/plugin-tools/introduction/data-frames |
| 151 | + // The goal here is to output a long format. If needed, prepare time series can transform it |
| 152 | + // https://grafana.com/docs/grafana/latest/panels-visualizations/query-transform-data/transform-data/#prepare-time-series |
| 153 | + |
| 154 | + frame := data.NewFrame("response") |
| 155 | + |
| 156 | + for key, values := range fieldMap { |
| 157 | + frame.Fields = append(frame.Fields, |
| 158 | + data.NewField(key, nil, values), |
| 159 | + ) |
| 160 | + } |
| 161 | + |
| 162 | + return frame, nil |
| 163 | +} |
| 164 | + |
| 165 | +func flattenArray[T interface{}](array []T, prefix string, flattenedData map[string]interface{}) { |
| 166 | + for key, value := range array { |
| 167 | + flattenedData[fmt.Sprintf("%s%d", prefix, key)] = value |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func flattenData(originalData map[string]interface{}, prefix string, flattenedData map[string]interface{}) { |
| 172 | + for key, value := range originalData { |
| 173 | + switch valueValue := value.(type) { |
| 174 | + case map[string]interface{}: // an object |
| 175 | + flattenData(valueValue, prefix+key+".", flattenedData) |
| 176 | + case []map[string]interface{}: // an array of objects |
| 177 | + for subKey, subValue := range valueValue { |
| 178 | + flattenData(subValue, fmt.Sprintf("%s%s.%d", prefix, key, subKey), flattenedData) |
| 179 | + } |
| 180 | + case []int: |
| 181 | + flattenArray(valueValue, prefix+key+".", flattenedData) |
| 182 | + case []int64: |
| 183 | + flattenArray(valueValue, prefix+key+".", flattenedData) |
| 184 | + case []float32: |
| 185 | + flattenArray(valueValue, prefix+key+".", flattenedData) |
| 186 | + case []float64: |
| 187 | + flattenArray(valueValue, prefix+key+".", flattenedData) |
| 188 | + case []bool: |
| 189 | + flattenArray(valueValue, prefix+key+".", flattenedData) |
| 190 | + case []uint: |
| 191 | + flattenArray(valueValue, prefix+key+".", flattenedData) |
| 192 | + case []uint64: |
| 193 | + flattenArray(valueValue, prefix+key+".", flattenedData) |
| 194 | + case []*int: |
| 195 | + flattenArray(valueValue, prefix+key+".", flattenedData) |
| 196 | + case []*int64: |
| 197 | + flattenArray(valueValue, prefix+key+".", flattenedData) |
| 198 | + case []*float32: |
| 199 | + flattenArray(valueValue, prefix+key+".", flattenedData) |
| 200 | + case []*float64: |
| 201 | + flattenArray(valueValue, prefix+key+".", flattenedData) |
| 202 | + case []*bool: |
| 203 | + flattenArray(valueValue, prefix+key+".", flattenedData) |
| 204 | + case []*uint: |
| 205 | + flattenArray(valueValue, prefix+key+".", flattenedData) |
| 206 | + case []*uint64: |
| 207 | + flattenArray(valueValue, prefix+key+".", flattenedData) |
| 208 | + default: |
| 209 | + flattenedData[prefix+key] = valueValue |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments