Skip to content

Commit eb3411d

Browse files
authored
Update promql.go
1 parent 2debaeb commit eb3411d

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

promql.go

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package gopromql
1+
package vtm
22

33
import (
44
"fmt"
@@ -98,9 +98,9 @@ type BinaryOp struct {
9898
type Matcher string
9999

100100
const (
101-
EqualMacther Matcher = "="
101+
EqualMatcher Matcher = "="
102102
NotEqualMatcher Matcher = "!="
103-
LikeMatcher Matcher = "!="
103+
LikeMatcher Matcher = "=~"
104104
NotLikeMatcher Matcher = "!~"
105105
)
106106

@@ -110,7 +110,7 @@ type Label struct {
110110
Matcher
111111
}
112112

113-
type PromqlBuilder struct {
113+
type PromQLBuilder struct {
114114
metricName string
115115
funcName string
116116
labels []Label
@@ -120,92 +120,92 @@ type PromqlBuilder struct {
120120
agg Aggregation
121121
}
122122

123-
func NewPromqlBuilder(metricName string) *PromqlBuilder {
124-
return &PromqlBuilder{metricName: metricName}
123+
func NewPromQLBuilder(metricName string) *PromQLBuilder {
124+
return &PromQLBuilder{metricName: metricName}
125125
}
126126

127127
// see prometheus functions: https://prometheus.io/docs/prometheus/latest/querying/functions/
128-
// WithFuncName, basicQL will be format as "funcName(basicql)"
129-
func (pb *PromqlBuilder) WithFuncName(funcName string) *PromqlBuilder {
128+
// WithFuncName, basicQL will be format as "funcName(basicQL)"
129+
func (pb *PromQLBuilder) WithFuncName(funcName string) *PromQLBuilder {
130130
pb.funcName = funcName
131131
return pb
132132
}
133133

134134
// WithLabels basicQL will be format as `{labelName1}={labelValue1},{labelName2}=~{labelValue2|labelValue2x}...{labelNameN}!={labelValueN}`
135135
// and append to metric name
136-
func (pb *PromqlBuilder) WithLabels(labels Label) *PromqlBuilder {
136+
func (pb *PromQLBuilder) WithLabels(labels Label) *PromQLBuilder {
137137
pb.labels = append(pb.labels, labels)
138138

139139
return pb
140140
}
141141

142142
// WithOffset basicQL will be format as "offset {offset}"
143-
func (pb *PromqlBuilder) WithOffset(offset string) *PromqlBuilder {
143+
func (pb *PromQLBuilder) WithOffset(offset string) *PromQLBuilder {
144144
pb.offset = offset
145145
return pb
146146
}
147147

148148
// WithWindow basicQL will be format as "[window]"
149-
func (pb *PromqlBuilder) WithWindow(window string) *PromqlBuilder {
149+
func (pb *PromQLBuilder) WithWindow(window string) *PromQLBuilder {
150150
pb.window = window
151151
return pb
152152
}
153153

154154
// WithComp basicQL will be format as "comp1 and comp2 and ... and compN"
155155
// when compare length > 1, combine with "and" as default
156-
func (pb *PromqlBuilder) WithComp(comp Compare) *PromqlBuilder {
156+
func (pb *PromQLBuilder) WithComp(comp Compare) *PromQLBuilder {
157157
pb.compOps = append(pb.compOps, comp)
158158
return pb
159159
}
160160

161-
// WithAgg basicQL will be format as "agg(basicql) by/without (labelName1,labelName2,...,labelNameN)"
162-
func (pb *PromqlBuilder) WithAgg(agg Aggregation) *PromqlBuilder {
161+
// WithAgg basicQL will be format as "agg(basicQL) by/without (labelName1,labelName2,...,labelNameN)"
162+
func (pb *PromQLBuilder) WithAgg(agg Aggregation) *PromQLBuilder {
163163
pb.agg = agg
164164
return pb
165165
}
166166

167-
// Build build basicql
168-
func (pb *PromqlBuilder) Build() (string, error) {
167+
// Build build basicQL
168+
func (pb *PromQLBuilder) Build() (string, error) {
169169
if pb.metricName == "" {
170170
return "", fmt.Errorf("metric name is required")
171171
}
172-
basicql := fmt.Sprintf(pb.metricName)
172+
basicQL :=pb.metricName
173173
if len(pb.labels) > 0 {
174174
tmp := make([]string, len(pb.labels))
175175
for index, label := range pb.labels {
176176
tmp[index] = fmt.Sprintf(`%s%s"%s"`, label.Name, label.Matcher, label.Value)
177177
}
178-
basicql += fmt.Sprintf("{%s}", strings.Join(tmp, ","))
178+
basicQL += fmt.Sprintf("{%s}", strings.Join(tmp, ","))
179179
}
180180

181181
if pb.window != "" {
182-
basicql += fmt.Sprintf("[%s]", pb.window)
182+
basicQL += fmt.Sprintf("[%s]", pb.window)
183183
}
184184
if len(pb.compOps) > 0 {
185185
tmp := make([]string, len(pb.compOps))
186186
for index, comp := range pb.compOps {
187-
tmp[index] = fmt.Sprintf("%s%s%.0f", basicql, comp.Op, comp.Value)
187+
tmp[index] = fmt.Sprintf("%s%s%.0f", basicQL, comp.Op, comp.Value)
188188
}
189189
fmt.Println(tmp)
190190
fmt.Println(len(tmp))
191-
basicql = fmt.Sprintf(" %s", strings.Join(tmp, " and "))
191+
basicQL = fmt.Sprintf(" %s", strings.Join(tmp, " and "))
192192

193193
}
194194
if pb.funcName != "" {
195-
basicql = fmt.Sprintf("%s(%s)", pb.funcName, basicql)
195+
basicQL = fmt.Sprintf("%s(%s)", pb.funcName, basicQL)
196196
}
197197

198198
if pb.offset != "" {
199-
basicql += fmt.Sprintf(" offset %s", pb.offset)
199+
basicQL += fmt.Sprintf(" offset %s", pb.offset)
200200
}
201201

202202
if !reflect.DeepEqual(pb.agg, Aggregation{}) {
203203
if pb.agg.Op != "" {
204-
basicql = fmt.Sprintf("%s(%s)", pb.agg.Op, basicql)
204+
basicQL = fmt.Sprintf("%s(%s)", pb.agg.Op, basicQL)
205205
}
206206
if pb.agg.AggWay != "" && pb.agg.By != nil {
207-
basicql = fmt.Sprintf("%s %s (%s)", basicql, pb.agg.AggWay, strings.Join(pb.agg.By, ","))
207+
basicQL = fmt.Sprintf("%s %s (%s)", basicQL, pb.agg.AggWay, strings.Join(pb.agg.By, ","))
208208
}
209209
}
210-
return basicql, nil
210+
return basicQL, nil
211211
}

0 commit comments

Comments
 (0)