github.com/m3db/m3@v1.5.0/src/cmd/services/m3comparator/main/parser/parser.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 parser 22 23 import ( 24 "encoding/json" 25 "fmt" 26 "sort" 27 "strconv" 28 "strings" 29 "time" 30 ) 31 32 // Series is a flat JSON serializable representation of the series. 33 type Series struct { 34 id string 35 36 Start time.Time `json:"start"` 37 End time.Time `json:"end"` 38 Tags Tags `json:"tags"` 39 Datapoints Datapoints `json:"datapoints"` 40 } 41 42 // Tag is a simple JSON serializable representation of a tag. 43 type Tag [2]string 44 45 // NewTag creates a new tag with a given name and value. 46 func NewTag(name, value string) Tag { 47 return Tag{name, value} 48 } 49 50 // Name returns the tag name. 51 func (t Tag) Name() string { 52 return t[0] 53 } 54 55 // Value returns the tag value. 56 func (t Tag) Value() string { 57 return t[1] 58 } 59 60 // Tags is a simple JSON serializable representation of tags. 61 type Tags []Tag 62 63 // Get returns a list of tag values with the given name. 64 func (t Tags) Get(name string) []string { 65 // NB: this is almost always going to be 0 66 values := make([]string, 0, 2) 67 // NB: This list isn't expected to get very long so it uses array lookup. 68 // If this is a problem in the future, `Tags` be converted to a map. 69 for _, t := range t { 70 if t.Name() == name { 71 values = append(values, t.Value()) 72 } 73 } 74 75 return values 76 } 77 78 // Datapoints is a JSON serializeable list of values for the series. 79 type Datapoints []Datapoint 80 81 // Datapoint is a JSON serializeable datapoint for the series. 82 type Datapoint struct { 83 Value Value `json:"val"` 84 Timestamp time.Time `json:"ts"` 85 } 86 87 // Value is a JSON serizlizable float64 that allows NaNs. 88 type Value float64 89 90 // MarshalJSON returns state as the JSON encoding of a Value. 91 func (v Value) MarshalJSON() ([]byte, error) { 92 return json.Marshal(fmt.Sprintf("%g", float64(v))) 93 } 94 95 // UnmarshalJSON unmarshals JSON-encoded data into a Value. 96 func (v *Value) UnmarshalJSON(data []byte) error { 97 var str string 98 err := json.Unmarshal(data, &str) 99 if err != nil { 100 return err 101 } 102 103 f, err := strconv.ParseFloat(str, 64) 104 if err != nil { 105 return err 106 } 107 108 *v = Value(f) 109 return nil 110 } 111 112 // IDOrGenID gets the ID for this result. 113 func (r *Series) IDOrGenID() string { 114 if len(r.id) == 0 { 115 tags := make(sort.StringSlice, len(r.Tags)) 116 for _, v := range r.Tags { 117 tags = append(tags, fmt.Sprintf("%s:%s,", v[0], v[1])) 118 } 119 120 sort.Sort(tags) 121 var sb strings.Builder 122 for _, t := range tags { 123 sb.WriteString(t) 124 } 125 126 r.id = sb.String() 127 } 128 129 return r.id 130 }