@@ -14,8 +14,8 @@ import (
14
14
"github.com/lomik/graphite-clickhouse/helper/clickhouse"
15
15
"github.com/lomik/graphite-clickhouse/helper/point"
16
16
"github.com/lomik/graphite-clickhouse/helper/rollup"
17
+ "github.com/lomik/graphite-clickhouse/pkg/alias"
17
18
"github.com/lomik/graphite-clickhouse/pkg/dry"
18
- "github.com/lomik/graphite-clickhouse/pkg/reverse"
19
19
"github.com/lomik/graphite-clickhouse/pkg/scope"
20
20
"github.com/lomik/graphite-clickhouse/pkg/where"
21
21
@@ -106,37 +106,21 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
106
106
return
107
107
}
108
108
109
- aliases := make (map [string ][]string )
110
- targets := make ([]string , 0 )
111
-
112
- for t := 0 ; t < len (r .Form ["target" ]); t ++ {
113
- target := r .Form ["target" ][t ]
114
- if len (target ) == 0 {
115
- continue
116
- }
117
- targets = append (targets , target )
109
+ am := alias .New ()
110
+ targets := dry .RemoveEmptyStrings (r .Form ["target" ])
118
111
112
+ for _ , target := range targets {
119
113
// Search in small index table first
120
114
fndResult , err := finder .Find (h .config , r .Context (), target , fromTimestamp , untilTimestamp )
121
115
if err != nil {
122
116
clickhouse .HandleError (w , err )
123
117
return
124
118
}
125
119
126
- fndSeries := fndResult .Series ()
127
-
128
- for i := 0 ; i < len (fndSeries ); i ++ {
129
- key := string (fndSeries [i ])
130
- abs := string (fndResult .Abs (fndSeries [i ]))
131
- if x , ok := aliases [key ]; ok {
132
- aliases [key ] = append (x , abs , target )
133
- } else {
134
- aliases [key ] = []string {abs , target }
135
- }
136
- }
120
+ am .MergeTarget (fndResult , target )
137
121
}
138
122
139
- logger .Info ("finder" , zap .Int ("metrics" , len ( aliases )))
123
+ logger .Info ("finder" , zap .Int ("metrics" , am . Len ( )))
140
124
141
125
pointsTable , isReverse , rollupObj := SelectDataTable (h .config , fromTimestamp , untilTimestamp , targets , config .ContextGraphite )
142
126
if pointsTable == "" {
@@ -150,46 +134,33 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
150
134
now := time .Now ().Unix ()
151
135
age := uint32 (dry .Max (0 , now - fromTimestamp ))
152
136
153
- // make Path IN (...), calculate max step
154
- metricList := make ([]string , 0 , len (aliases ))
155
- for m := range aliases {
156
- if len (m ) == 0 {
157
- continue
158
- }
159
-
160
- step , _ := rollupObj .Lookup (m , age )
161
- if step > maxStep {
162
- maxStep = step
163
- }
164
-
165
- if isReverse {
166
- metricList = append (metricList , reverse .String (m ))
167
- } else {
168
- metricList = append (metricList , m )
169
- }
170
- }
137
+ metricList := am .Series (isReverse )
171
138
172
139
if len (metricList ) == 0 {
173
140
// Return empty response
174
141
h .Reply (w , r , EmptyData , 0 , 0 , "" , nil )
175
142
return
176
143
}
177
144
178
- preWhere := where .New ()
179
- preWhere .Andf (
180
- "Date >='%s' AND Date <= '%s'" ,
181
- time .Unix (fromTimestamp , 0 ).Format ("2006-01-02" ),
182
- time .Unix (untilTimestamp , 0 ).Format ("2006-01-02" ),
183
- )
145
+ // calculate max step
146
+ for _ , m := range metricList {
147
+ step , _ := rollupObj .Lookup (m , age )
148
+ if step > maxStep {
149
+ maxStep = step
150
+ }
151
+ }
152
+
153
+ pw := where .New ()
154
+ pw .And (where .DateBetween ("Date" , time .Unix (fromTimestamp , 0 ), time .Unix (untilTimestamp , 0 )))
184
155
185
156
wr := where .New ()
186
157
wr .And (where .In ("Path" , metricList ))
187
158
188
159
until := untilTimestamp - untilTimestamp % int64 (maxStep ) + int64 (maxStep ) - 1
189
- wr .Andf ( "Time >= %d AND Time <= %d " , fromTimestamp , until )
160
+ wr .And ( where . TimestampBetween ( "Time" , fromTimestamp , until ) )
190
161
191
162
query := fmt .Sprintf (`SELECT Path, Time, Value, Timestamp FROM %s %s %s FORMAT RowBinary` ,
192
- pointsTable , preWhere .PreWhereSQL (), wr .SQL (),
163
+ pointsTable , pw .PreWhereSQL (), wr .SQL (),
193
164
)
194
165
195
166
// start carbonlink request
@@ -231,7 +202,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
231
202
logger .Debug ("sort" , zap .String ("runtime" , d .String ()), zap .Duration ("runtime_ns" , d ))
232
203
233
204
data .Points .Uniq ()
234
- data .Aliases = aliases
205
+ data .Aliases = am
235
206
236
207
// pp.Println(points)
237
208
h .Reply (w , r , data , uint32 (fromTimestamp ), uint32 (untilTimestamp ), prefix , rollupObj )
0 commit comments