github.com/m3db/m3@v1.5.0/scripts/comparator/utils/compare_utilities_grafana.go (about) 1 // Copyright (c) 2020 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package utils 22 23 import ( 24 "strings" 25 26 "go.uber.org/zap" 27 ) 28 29 // GrafanaQueries is a list of Grafana dashboard compatible queries. 30 type GrafanaQueries struct { 31 // QueryGroup is the general category for these queries. 32 QueryGroup string 33 // Queries is a list of Grafana dashboard compatible queries. 34 Queries []GrafanaQuery 35 // Index is this query group's index. 36 Index int 37 } 38 39 // GrafanaQuery is a Grafana dashboard compatible query. 40 type GrafanaQuery struct { 41 // Query is the query. 42 Query string 43 // Interval is the step size. 44 Interval string 45 // Index is this query's index. 46 Index int 47 // Left indicates if this panel is on the left. 48 Left bool 49 } 50 51 // constructGrafanaQueries constructs a list of Grafana dashboard compatible 52 // queries. 53 func (q InputQueries) constructGrafanaQueries() []GrafanaQueries { 54 queries := make([]GrafanaQueries, 0, len(q)) 55 idx := 0 56 for _, inQuery := range q { 57 query, index := inQuery.constructGrafanaQuery(idx) 58 idx = index 59 // NB: don't add empty queries if they exist for whatever reason. 60 if len(query.Queries) > 0 { 61 queries = append(queries, query) 62 } 63 } 64 65 return queries 66 } 67 68 func (q InputQuery) constructGrafanaQuery(idx int) (GrafanaQueries, int) { 69 grafanaQueries := GrafanaQueries{ 70 QueryGroup: q.QueryGroup, 71 Index: idx, 72 } 73 74 queries := make([]GrafanaQuery, 0, len(q.Queries)*len(q.Steps)) 75 left := true 76 for _, inQuery := range q.Queries { 77 for _, inStep := range q.Steps { 78 idx++ 79 queries = append(queries, GrafanaQuery{ 80 Query: strings.ReplaceAll(inQuery, `"`, `\"`), 81 Interval: inStep, 82 Index: idx, 83 Left: left, 84 }) 85 86 left = !left 87 } 88 } 89 90 grafanaQueries.Queries = queries 91 return grafanaQueries, idx + 1 92 } 93 94 // ParseFileToGrafanaQueries parses a JSON queries file into Grafana dashboard 95 // compatible queries. 96 func ParseFileToGrafanaQueries( 97 fileName string, 98 log *zap.Logger, 99 ) ([]GrafanaQueries, error) { 100 queries, err := parseFileToQueries(fileName, log) 101 if err != nil { 102 return nil, err 103 } 104 105 return queries.constructGrafanaQueries(), nil 106 }