|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/grafana/grafana-plugin-sdk-go/backend" |
| 8 | + "github.com/grafana/grafana-plugin-sdk-go/data" |
| 9 | +) |
| 10 | + |
| 11 | +/** |
| 12 | + * Represents node |
| 13 | + */ |
| 14 | +type nodeEntry struct { |
| 15 | + id string |
| 16 | + title string |
| 17 | + subTitle string |
| 18 | + mainStat string |
| 19 | + arc int64 |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Represents edge |
| 24 | + */ |
| 25 | +type edgeEntry struct { |
| 26 | + id string |
| 27 | + source string |
| 28 | + target string |
| 29 | + mainStat string |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * GRAPH.QUERY <Graph name> {query} |
| 34 | + * |
| 35 | + * Executes the given query against a specified graph. |
| 36 | + * @see https://oss.redislabs.com/redisgraph/commands/#graphquery |
| 37 | + */ |
| 38 | +func queryGraphQuery(qm queryModel, client redisClient) backend.DataResponse { |
| 39 | + response := backend.DataResponse{} |
| 40 | + |
| 41 | + var result []interface{} |
| 42 | + |
| 43 | + // Run command |
| 44 | + err := client.RunFlatCmd(&result, "GRAPH.QUERY", qm.Key, qm.Cypher) |
| 45 | + |
| 46 | + // Check error |
| 47 | + if err != nil { |
| 48 | + return errorHandler(response, err) |
| 49 | + } |
| 50 | + |
| 51 | + // New Frame for nodes |
| 52 | + frameWithNodes := data.NewFrame("nodes") |
| 53 | + frameWithNodes.Meta = &data.FrameMeta{ |
| 54 | + PreferredVisualization: "nodeGraph", |
| 55 | + } |
| 56 | + frameWithNodes.Fields = append(frameWithNodes.Fields, data.NewField("id", nil, []string{})) |
| 57 | + frameWithNodes.Fields = append(frameWithNodes.Fields, data.NewField("title", nil, []string{})) |
| 58 | + frameWithNodes.Fields = append(frameWithNodes.Fields, data.NewField("subTitle", nil, []string{})) |
| 59 | + frameWithNodes.Fields = append(frameWithNodes.Fields, data.NewField("mainStat", nil, []string{})) |
| 60 | + frameWithNodes.Fields = append(frameWithNodes.Fields, data.NewField("arc__", nil, []int64{})) |
| 61 | + |
| 62 | + // New Frame for edges |
| 63 | + frameWithEdges := data.NewFrame("edges") |
| 64 | + frameWithEdges.Meta = &data.FrameMeta{ |
| 65 | + PreferredVisualization: "nodeGraph", |
| 66 | + } |
| 67 | + frameWithEdges.Fields = append(frameWithEdges.Fields, data.NewField("id", nil, []string{})) |
| 68 | + frameWithEdges.Fields = append(frameWithEdges.Fields, data.NewField("source", nil, []string{})) |
| 69 | + frameWithEdges.Fields = append(frameWithEdges.Fields, data.NewField("target", nil, []string{})) |
| 70 | + frameWithEdges.Fields = append(frameWithEdges.Fields, data.NewField("mainStat", nil, []string{})) |
| 71 | + |
| 72 | + // Adding frames to response |
| 73 | + response.Frames = append(response.Frames, frameWithNodes) |
| 74 | + response.Frames = append(response.Frames, frameWithEdges) |
| 75 | + |
| 76 | + existingNodes := map[string]bool{} |
| 77 | + |
| 78 | + for _, entries := range result[1].([]interface{}) { |
| 79 | + nodes, edges := findAllNodesAndEdges(entries) |
| 80 | + for _, node := range nodes { |
| 81 | + // Add each nodeEntry only once |
| 82 | + if _, ok := existingNodes[node.id]; !ok { |
| 83 | + frameWithNodes.AppendRow(node.id, node.title, node.subTitle, node.mainStat, node.arc) |
| 84 | + existingNodes[node.id] = true |
| 85 | + } |
| 86 | + } |
| 87 | + for _, edge := range edges { |
| 88 | + frameWithEdges.AppendRow(edge.id, edge.source, edge.target, edge.mainStat) |
| 89 | + } |
| 90 | + } |
| 91 | + return response |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Parse array of entries and find |
| 96 | + * either Nodes https://oss.redislabs.com/redisgraph/result_structure/#nodes |
| 97 | + * or Relations https://oss.redislabs.com/redisgraph/result_structure/#relations |
| 98 | + * and create corresponding nodeEntry or edgeEntry |
| 99 | + **/ |
| 100 | +func findAllNodesAndEdges(input interface{}) ([]nodeEntry, []edgeEntry) { |
| 101 | + nodes := []nodeEntry{} |
| 102 | + edges := []edgeEntry{} |
| 103 | + |
| 104 | + if entries, ok := input.([]interface{}); ok { |
| 105 | + for _, entry := range entries { |
| 106 | + entryFields := entry.([]interface{}) |
| 107 | + |
| 108 | + // Node https://oss.redislabs.com/redisgraph/result_structure/#nodes |
| 109 | + if len(entryFields) == 3 { |
| 110 | + node := nodeEntry{arc: 1} |
| 111 | + idArray := entryFields[0].([]interface{}) |
| 112 | + node.id = strconv.FormatInt(idArray[1].(int64), 10) |
| 113 | + |
| 114 | + // Assume first label will be a title if exists |
| 115 | + labelsArray := entryFields[1].([]interface{}) |
| 116 | + labels := labelsArray[1].([]interface{}) |
| 117 | + if len(labels) > 0 { |
| 118 | + node.title = string(labels[0].([]byte)) |
| 119 | + } |
| 120 | + |
| 121 | + // Assume first property will be a mainStat if exists |
| 122 | + propertiesArray := entryFields[2].([]interface{}) |
| 123 | + properties := propertiesArray[1].([]interface{}) |
| 124 | + if len(properties) > 0 { |
| 125 | + propertyArray := properties[0].([]interface{}) |
| 126 | + switch propValue := propertyArray[1].(type) { |
| 127 | + case []byte: |
| 128 | + node.mainStat = string(propValue) |
| 129 | + case int64: |
| 130 | + node.mainStat = strconv.FormatInt(propValue, 10) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + nodes = append(nodes, node) |
| 135 | + } |
| 136 | + |
| 137 | + // Relation https://oss.redislabs.com/redisgraph/result_structure/#relations |
| 138 | + if len(entryFields) == 5 { |
| 139 | + edge := edgeEntry{} |
| 140 | + idArray := entryFields[0].([]interface{}) |
| 141 | + edge.id = strconv.FormatInt(idArray[1].(int64), 10) |
| 142 | + |
| 143 | + // Main Stat |
| 144 | + typeArray := entryFields[1].([]interface{}) |
| 145 | + edge.mainStat = string(typeArray[1].([]byte)) |
| 146 | + |
| 147 | + // Source |
| 148 | + srcArray := entryFields[2].([]interface{}) |
| 149 | + edge.source = strconv.FormatInt(srcArray[1].(int64), 10) |
| 150 | + |
| 151 | + // Target |
| 152 | + destArray := entryFields[3].([]interface{}) |
| 153 | + edge.target = strconv.FormatInt(destArray[1].(int64), 10) |
| 154 | + |
| 155 | + edges = append(edges, edge) |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + return nodes, edges |
| 160 | +} |
| 161 | + |
| 162 | +/** |
| 163 | + * GRAPH.SLOWLOG <Graph name> |
| 164 | + * |
| 165 | + * Returns a list containing up to 10 of the slowest queries issued against the given graph ID. |
| 166 | + * @see https://oss.redislabs.com/redisgraph/commands/#graphslowlog |
| 167 | + */ |
| 168 | +func queryGraphSlowlog(qm queryModel, client redisClient) backend.DataResponse { |
| 169 | + response := backend.DataResponse{} |
| 170 | + |
| 171 | + var result [][]string |
| 172 | + |
| 173 | + // Run command |
| 174 | + err := client.RunFlatCmd(&result, "GRAPH.SLOWLOG", qm.Key) |
| 175 | + |
| 176 | + // Check error |
| 177 | + if err != nil { |
| 178 | + return errorHandler(response, err) |
| 179 | + } |
| 180 | + |
| 181 | + // New Frame |
| 182 | + frame := data.NewFrame("GRAPH.SLOWLOG") |
| 183 | + frame.Fields = append(frame.Fields, data.NewField("timestamp", nil, []time.Time{})) |
| 184 | + frame.Fields = append(frame.Fields, data.NewField("command", nil, []string{})) |
| 185 | + frame.Fields = append(frame.Fields, data.NewField("query", nil, []string{})) |
| 186 | + frame.Fields = append(frame.Fields, data.NewField("duration", nil, []float64{})) |
| 187 | + response.Frames = append(response.Frames, frame) |
| 188 | + |
| 189 | + // Set Field Config |
| 190 | + frame.Fields[3].Config = &data.FieldConfig{Unit: "µs"} |
| 191 | + |
| 192 | + // Entries |
| 193 | + for _, entry := range result { |
| 194 | + timestamp, _ := strconv.ParseInt(entry[0], 10, 64) |
| 195 | + duration, _ := strconv.ParseFloat(entry[3], 64) |
| 196 | + frame.AppendRow(time.Unix(timestamp, 0), entry[1], entry[2], duration) |
| 197 | + } |
| 198 | + |
| 199 | + // Return |
| 200 | + return response |
| 201 | +} |
0 commit comments