Skip to content

Commit 399e190

Browse files
committed
style: code cleanup and readability improvements
Signed-off-by: rubywtl <[email protected]>
1 parent 81ae2bd commit 399e190

File tree

5 files changed

+29
-32
lines changed

5 files changed

+29
-32
lines changed

pkg/querier/tripperware/logicalplan_gen.go renamed to pkg/querier/tripperware/distributed_query.go

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,40 @@ const (
1515
stepBatch = 10
1616
)
1717

18-
func LogicalPlanGenMiddleware(enablePerStepStats bool, lookBackDelta time.Duration) Middleware {
18+
func DistributedQueryMiddleware(enablePerStepStats bool, lookBackDelta time.Duration) Middleware {
1919
return MiddlewareFunc(func(next Handler) Handler {
20-
return logicalPlanGen{
20+
return distributedQueryMiddleware{
2121
next: next,
2222
enablePerStepStats: enablePerStepStats,
2323
lookBackDelta: lookBackDelta,
2424
}
2525
})
2626
}
2727

28-
type logicalPlanGen struct {
28+
func getStartAndEnd(start time.Time, end time.Time, step time.Duration) (time.Time, time.Time) {
29+
if step == 0 {
30+
return start, start
31+
}
32+
return start, end
33+
}
34+
35+
type distributedQueryMiddleware struct {
2936
next Handler
3037
enablePerStepStats bool
3138
lookBackDelta time.Duration
3239
}
3340

34-
func (l logicalPlanGen) NewLogicalPlan(qs string, start time.Time, end time.Time, step time.Duration) (*logicalplan.Plan, error) {
41+
func (d distributedQueryMiddleware) newLogicalPlan(qs string, start time.Time, end time.Time, step time.Duration) (*logicalplan.Plan, error) {
3542

36-
qOpts := query.Options{}
37-
if step == 0 {
38-
qOpts = query.Options{
39-
Start: start,
40-
End: start,
41-
Step: 0,
42-
StepsBatch: stepBatch,
43-
LookbackDelta: l.lookBackDelta,
44-
EnablePerStepStats: l.enablePerStepStats,
45-
}
46-
} else {
47-
qOpts = query.Options{
48-
Start: start,
49-
End: end,
50-
Step: step,
51-
StepsBatch: stepBatch,
52-
LookbackDelta: l.lookBackDelta,
53-
EnablePerStepStats: l.enablePerStepStats,
54-
}
43+
start, end = getStartAndEnd(start, end, step)
44+
45+
qOpts := query.Options{
46+
Start: start,
47+
End: end,
48+
Step: step,
49+
StepsBatch: stepBatch,
50+
LookbackDelta: d.lookBackDelta,
51+
EnablePerStepStats: d.enablePerStepStats,
5552
}
5653

5754
expr, err := parser.NewParser(qs, parser.WithFunctions(parser.Functions)).ParseExpr()
@@ -71,7 +68,7 @@ func (l logicalPlanGen) NewLogicalPlan(qs string, start time.Time, end time.Time
7168
return &optimizedPlan, nil
7269
}
7370

74-
func (l logicalPlanGen) Do(ctx context.Context, r Request) (Response, error) {
71+
func (d distributedQueryMiddleware) Do(ctx context.Context, r Request) (Response, error) {
7572
promReq, ok := r.(*PrometheusRequest)
7673
if !ok {
7774
return nil, httpgrpc.Errorf(http.StatusBadRequest, "invalid request format")
@@ -83,12 +80,12 @@ func (l logicalPlanGen) Do(ctx context.Context, r Request) (Response, error) {
8380

8481
var err error
8582

86-
newLogicalPlan, err := l.NewLogicalPlan(promReq.Query, startTime, endTime, step)
83+
newLogicalPlan, err := d.newLogicalPlan(promReq.Query, startTime, endTime, step)
8784
if err != nil {
8885
return nil, err
8986
}
9087

9188
promReq.LogicalPlan = *newLogicalPlan
9289

93-
return l.next.Do(ctx, r)
90+
return d.next.Do(ctx, r)
9491
}

pkg/querier/tripperware/logical_plan_gen_test.go renamed to pkg/querier/tripperware/distributed_query_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func TestLogicalPlanGeneration(t *testing.T) {
121121
t.Run(strconv.Itoa(i)+"_"+tc.name, func(t *testing.T) {
122122
t.Parallel()
123123

124-
middleware := LogicalPlanGenMiddleware(false, 5*time.Minute)
124+
middleware := DistributedQueryMiddleware(false, 5*time.Minute)
125125

126126
handler := middleware.Wrap(HandlerFunc(func(_ context.Context, req Request) (Response, error) {
127127
return nil, nil

pkg/querier/tripperware/instantquery/instant_query_middlewares.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func Middlewares(
2525

2626
if distributedExecEnabled {
2727
m = append(m,
28-
tripperware.LogicalPlanGenMiddleware(enablePerStepStats, lookbackDelta))
28+
tripperware.DistributedQueryMiddleware(enablePerStepStats, lookbackDelta))
2929
}
3030

3131
return m, nil

pkg/querier/tripperware/query.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ func (m *PrometheusRequest) GetStats() string {
223223
}
224224

225225
func (m *PrometheusRequest) GetLogicalPlan() logicalplan.Plan {
226-
if m != nil {
227-
return m.LogicalPlan
226+
if m == nil {
227+
return nil
228228
}
229-
return nil
229+
return m.LogicalPlan
230230
}
231231

232232
// WithStartEnd clones the current `PrometheusRequest` with a new `start` and `end` timestamp.

pkg/querier/tripperware/queryrange/query_range_middlewares.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func Middlewares(
142142
if distributedExecEnabled {
143143
queryRangeMiddleware = append(queryRangeMiddleware,
144144
tripperware.InstrumentMiddleware("range_logical_plan_gen", metrics),
145-
tripperware.LogicalPlanGenMiddleware(enablePerStepStats, lookbackDelta))
145+
tripperware.DistributedQueryMiddleware(enablePerStepStats, lookbackDelta))
146146
}
147147

148148
return queryRangeMiddleware, c, nil

0 commit comments

Comments
 (0)