Skip to content

Commit 44ecfd8

Browse files
committed
use new aliases in /render/ handler
1 parent 118fdc2 commit 44ecfd8

File tree

4 files changed

+26
-56
lines changed

4 files changed

+26
-56
lines changed

render/data.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/lomik/graphite-clickhouse/helper/clickhouse"
1212
"github.com/lomik/graphite-clickhouse/helper/point"
13+
"github.com/lomik/graphite-clickhouse/pkg/alias"
1314
"github.com/lomik/graphite-clickhouse/pkg/reverse"
1415
)
1516

@@ -41,7 +42,7 @@ type Data struct {
4142
length int // readed bytes count
4243
Points *point.Points
4344
nameMap map[string]string
44-
Aliases map[string][]string
45+
Aliases *alias.Map
4546
}
4647

4748
var EmptyData *Data = &Data{Points: point.NewPoints()}

render/handler.go

+20-49
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
"github.com/lomik/graphite-clickhouse/helper/clickhouse"
1515
"github.com/lomik/graphite-clickhouse/helper/point"
1616
"github.com/lomik/graphite-clickhouse/helper/rollup"
17+
"github.com/lomik/graphite-clickhouse/pkg/alias"
1718
"github.com/lomik/graphite-clickhouse/pkg/dry"
18-
"github.com/lomik/graphite-clickhouse/pkg/reverse"
1919
"github.com/lomik/graphite-clickhouse/pkg/scope"
2020
"github.com/lomik/graphite-clickhouse/pkg/where"
2121

@@ -106,37 +106,21 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
106106
return
107107
}
108108

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"])
118111

112+
for _, target := range targets {
119113
// Search in small index table first
120114
fndResult, err := finder.Find(h.config, r.Context(), target, fromTimestamp, untilTimestamp)
121115
if err != nil {
122116
clickhouse.HandleError(w, err)
123117
return
124118
}
125119

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)
137121
}
138122

139-
logger.Info("finder", zap.Int("metrics", len(aliases)))
123+
logger.Info("finder", zap.Int("metrics", am.Len()))
140124

141125
pointsTable, isReverse, rollupObj := SelectDataTable(h.config, fromTimestamp, untilTimestamp, targets, config.ContextGraphite)
142126
if pointsTable == "" {
@@ -150,46 +134,33 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
150134
now := time.Now().Unix()
151135
age := uint32(dry.Max(0, now-fromTimestamp))
152136

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)
171138

172139
if len(metricList) == 0 {
173140
// Return empty response
174141
h.Reply(w, r, EmptyData, 0, 0, "", nil)
175142
return
176143
}
177144

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)))
184155

185156
wr := where.New()
186157
wr.And(where.In("Path", metricList))
187158

188159
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))
190161

191162
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(),
193164
)
194165

195166
// start carbonlink request
@@ -231,7 +202,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
231202
logger.Debug("sort", zap.String("runtime", d.String()), zap.Duration("runtime_ns", d))
232203

233204
data.Points.Uniq()
234-
data.Aliases = aliases
205+
data.Aliases = am
235206

236207
// pp.Println(points)
237208
h.Reply(w, r, data, uint32(fromTimestamp), uint32(untilTimestamp), prefix, rollupObj)

render/reply_pickle.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ func (h *Handler) ReplyPickle(w http.ResponseWriter, r *http.Request, data *Data
108108
}
109109
rollupTime += time.Since(rollupStart)
110110

111-
a := data.Aliases[metricName]
112-
for k := 0; k < len(a); k += 2 {
113-
writeAlias(a[k], a[k+1], points, step)
111+
for _, a := range data.Aliases.Get(metricName) {
112+
writeAlias(a.DisplayName, a.Target, points, step)
114113
}
115114
}
116115
// group by Metric

render/reply_protobuf.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,8 @@ func (h *Handler) ReplyProtobuf(w http.ResponseWriter, r *http.Request, data *Da
124124
return
125125
}
126126

127-
a := data.Aliases[metricName]
128-
for k := 0; k < len(a); k += 2 {
129-
writeAlias(a[k], points, step)
127+
for _, a := range data.Aliases.Get(metricName) {
128+
writeAlias(a.DisplayName, points, step)
130129
}
131130
}
132131

0 commit comments

Comments
 (0)