Skip to content

Commit 8de8757

Browse files
Revert date-range helper inlining in usage readers
Restore sqliteDateRangeConditions(), pgDateRangeConditions(), and mongoDateRangeFilter() helper functions. The inlining was an unrelated refactoring regression that duplicated identical date-range logic across 12 call sites with no behavioral change. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1c44dd1 commit 8de8757

3 files changed

Lines changed: 79 additions & 223 deletions

File tree

internal/usage/reader_mongodb.go

Lines changed: 26 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,9 @@ func NewMongoDBReader(database *mongo.Database) (*MongoDBReader, error) {
2626
func (r *MongoDBReader) GetSummary(ctx context.Context, params UsageQueryParams) (*UsageSummary, error) {
2727
pipeline := bson.A{}
2828

29-
startZero := params.StartDate.IsZero()
30-
endZero := params.EndDate.IsZero()
31-
32-
if !startZero && !endZero {
33-
pipeline = append(pipeline, bson.D{{Key: "$match", Value: bson.D{
34-
{Key: "timestamp", Value: bson.D{
35-
{Key: "$gte", Value: params.StartDate.UTC()},
36-
{Key: "$lt", Value: params.EndDate.AddDate(0, 0, 1).UTC()},
37-
}},
38-
}}})
39-
} else if !startZero {
40-
pipeline = append(pipeline, bson.D{{Key: "$match", Value: bson.D{
41-
{Key: "timestamp", Value: bson.D{{Key: "$gte", Value: params.StartDate.UTC()}}},
42-
}}})
43-
} else if !endZero {
29+
if tsFilter := mongoDateRangeFilter(params); tsFilter != nil {
4430
pipeline = append(pipeline, bson.D{{Key: "$match", Value: bson.D{
45-
{Key: "timestamp", Value: bson.D{{Key: "$lt", Value: params.EndDate.AddDate(0, 0, 1).UTC()}}},
31+
{Key: "timestamp", Value: tsFilter},
4632
}}})
4733
}
4834

@@ -101,23 +87,9 @@ func (r *MongoDBReader) GetSummary(ctx context.Context, params UsageQueryParams)
10187
func (r *MongoDBReader) GetUsageByModel(ctx context.Context, params UsageQueryParams) ([]ModelUsage, error) {
10288
pipeline := bson.A{}
10389

104-
startZero := params.StartDate.IsZero()
105-
endZero := params.EndDate.IsZero()
106-
107-
if !startZero && !endZero {
108-
pipeline = append(pipeline, bson.D{{Key: "$match", Value: bson.D{
109-
{Key: "timestamp", Value: bson.D{
110-
{Key: "$gte", Value: params.StartDate.UTC()},
111-
{Key: "$lt", Value: params.EndDate.AddDate(0, 0, 1).UTC()},
112-
}},
113-
}}})
114-
} else if !startZero {
115-
pipeline = append(pipeline, bson.D{{Key: "$match", Value: bson.D{
116-
{Key: "timestamp", Value: bson.D{{Key: "$gte", Value: params.StartDate.UTC()}}},
117-
}}})
118-
} else if !endZero {
90+
if tsFilter := mongoDateRangeFilter(params); tsFilter != nil {
11991
pipeline = append(pipeline, bson.D{{Key: "$match", Value: bson.D{
120-
{Key: "timestamp", Value: bson.D{{Key: "$lt", Value: params.EndDate.AddDate(0, 0, 1).UTC()}}},
92+
{Key: "timestamp", Value: tsFilter},
12193
}}})
12294
}
12395

@@ -184,22 +156,8 @@ func (r *MongoDBReader) GetUsageLog(ctx context.Context, params UsageLogParams)
184156

185157
matchFilters := bson.D{}
186158

187-
startZero := params.StartDate.IsZero()
188-
endZero := params.EndDate.IsZero()
189-
190-
if !startZero && !endZero {
191-
matchFilters = append(matchFilters, bson.E{Key: "timestamp", Value: bson.D{
192-
{Key: "$gte", Value: params.StartDate.UTC()},
193-
{Key: "$lt", Value: params.EndDate.AddDate(0, 0, 1).UTC()},
194-
}})
195-
} else if !startZero {
196-
matchFilters = append(matchFilters, bson.E{Key: "timestamp", Value: bson.D{
197-
{Key: "$gte", Value: params.StartDate.UTC()},
198-
}})
199-
} else if !endZero {
200-
matchFilters = append(matchFilters, bson.E{Key: "timestamp", Value: bson.D{
201-
{Key: "$lt", Value: params.EndDate.AddDate(0, 0, 1).UTC()},
202-
}})
159+
if tsFilter := mongoDateRangeFilter(params.UsageQueryParams); tsFilter != nil {
160+
matchFilters = append(matchFilters, bson.E{Key: "timestamp", Value: tsFilter})
203161
}
204162

205163
if params.Model != "" {
@@ -307,6 +265,24 @@ func (r *MongoDBReader) GetUsageLog(ctx context.Context, params UsageLogParams)
307265
}, nil
308266
}
309267

268+
// mongoDateRangeFilter returns a bson.D timestamp filter for the given date range.
269+
// Returns nil if no date filtering is needed.
270+
func mongoDateRangeFilter(params UsageQueryParams) bson.D {
271+
startZero := params.StartDate.IsZero()
272+
endZero := params.EndDate.IsZero()
273+
274+
if !startZero && !endZero {
275+
return bson.D{{Key: "$gte", Value: params.StartDate.UTC()}, {Key: "$lt", Value: params.EndDate.AddDate(0, 0, 1).UTC()}}
276+
}
277+
if !startZero {
278+
return bson.D{{Key: "$gte", Value: params.StartDate.UTC()}}
279+
}
280+
if !endZero {
281+
return bson.D{{Key: "$lt", Value: params.EndDate.AddDate(0, 0, 1).UTC()}}
282+
}
283+
return nil
284+
}
285+
310286
func mongoDateFormat(interval string) string {
311287
switch interval {
312288
case "weekly":
@@ -329,23 +305,9 @@ func (r *MongoDBReader) GetDailyUsage(ctx context.Context, params UsageQueryPara
329305

330306
pipeline := bson.A{}
331307

332-
startZero := params.StartDate.IsZero()
333-
endZero := params.EndDate.IsZero()
334-
335-
if !startZero && !endZero {
336-
pipeline = append(pipeline, bson.D{{Key: "$match", Value: bson.D{
337-
{Key: "timestamp", Value: bson.D{
338-
{Key: "$gte", Value: params.StartDate.UTC()},
339-
{Key: "$lt", Value: params.EndDate.AddDate(0, 0, 1).UTC()},
340-
}},
341-
}}})
342-
} else if !startZero {
343-
pipeline = append(pipeline, bson.D{{Key: "$match", Value: bson.D{
344-
{Key: "timestamp", Value: bson.D{{Key: "$gte", Value: params.StartDate.UTC()}}},
345-
}}})
346-
} else if !endZero {
308+
if tsFilter := mongoDateRangeFilter(params); tsFilter != nil {
347309
pipeline = append(pipeline, bson.D{{Key: "$match", Value: bson.D{
348-
{Key: "timestamp", Value: bson.D{{Key: "$lt", Value: params.EndDate.AddDate(0, 0, 1).UTC()}}},
310+
{Key: "timestamp", Value: tsFilter},
349311
}}})
350312
}
351313

internal/usage/reader_postgresql.go

Lines changed: 28 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,12 @@ func NewPostgreSQLReader(pool *pgxpool.Pool) (*PostgreSQLReader, error) {
2424

2525
// GetSummary returns aggregated usage statistics for the given query parameters.
2626
func (r *PostgreSQLReader) GetSummary(ctx context.Context, params UsageQueryParams) (*UsageSummary, error) {
27-
var query string
28-
var args []interface{}
29-
30-
startZero := params.StartDate.IsZero()
31-
endZero := params.EndDate.IsZero()
27+
conditions, args, _ := pgDateRangeConditions(params, 1)
28+
where := buildWhereClause(conditions)
3229

3330
costCols := `, COALESCE(SUM(input_cost),0), COALESCE(SUM(output_cost),0), COALESCE(SUM(total_cost),0)`
34-
35-
if !startZero && !endZero {
36-
query = `SELECT COUNT(*), COALESCE(SUM(input_tokens), 0), COALESCE(SUM(output_tokens), 0), COALESCE(SUM(total_tokens), 0)` + costCols + `
37-
FROM "usage" WHERE timestamp >= $1 AND timestamp < $2`
38-
args = append(args, params.StartDate.UTC(), params.EndDate.AddDate(0, 0, 1).UTC())
39-
} else if !startZero {
40-
query = `SELECT COUNT(*), COALESCE(SUM(input_tokens), 0), COALESCE(SUM(output_tokens), 0), COALESCE(SUM(total_tokens), 0)` + costCols + `
41-
FROM "usage" WHERE timestamp >= $1`
42-
args = append(args, params.StartDate.UTC())
43-
} else if !endZero {
44-
query = `SELECT COUNT(*), COALESCE(SUM(input_tokens), 0), COALESCE(SUM(output_tokens), 0), COALESCE(SUM(total_tokens), 0)` + costCols + `
45-
FROM "usage" WHERE timestamp < $1`
46-
args = append(args, params.EndDate.AddDate(0, 0, 1).UTC())
47-
} else {
48-
query = `SELECT COUNT(*), COALESCE(SUM(input_tokens), 0), COALESCE(SUM(output_tokens), 0), COALESCE(SUM(total_tokens), 0)` + costCols + `
49-
FROM "usage"`
50-
}
31+
query := `SELECT COUNT(*), COALESCE(SUM(input_tokens), 0), COALESCE(SUM(output_tokens), 0), COALESCE(SUM(total_tokens), 0)` + costCols + `
32+
FROM "usage"` + where
5133

5234
summary := &UsageSummary{}
5335
err := r.pool.QueryRow(ctx, query, args...).Scan(
@@ -63,30 +45,12 @@ func (r *PostgreSQLReader) GetSummary(ctx context.Context, params UsageQueryPara
6345

6446
// GetUsageByModel returns token and cost totals grouped by model and provider.
6547
func (r *PostgreSQLReader) GetUsageByModel(ctx context.Context, params UsageQueryParams) ([]ModelUsage, error) {
66-
var query string
67-
var args []interface{}
68-
69-
startZero := params.StartDate.IsZero()
70-
endZero := params.EndDate.IsZero()
48+
conditions, args, _ := pgDateRangeConditions(params, 1)
49+
where := buildWhereClause(conditions)
7150

7251
costCols := `, COALESCE(SUM(input_cost),0), COALESCE(SUM(output_cost),0), COALESCE(SUM(total_cost),0)`
73-
74-
if !startZero && !endZero {
75-
query = `SELECT model, provider, COALESCE(SUM(input_tokens), 0), COALESCE(SUM(output_tokens), 0)` + costCols + `
76-
FROM "usage" WHERE timestamp >= $1 AND timestamp < $2 GROUP BY model, provider`
77-
args = append(args, params.StartDate.UTC(), params.EndDate.AddDate(0, 0, 1).UTC())
78-
} else if !startZero {
79-
query = `SELECT model, provider, COALESCE(SUM(input_tokens), 0), COALESCE(SUM(output_tokens), 0)` + costCols + `
80-
FROM "usage" WHERE timestamp >= $1 GROUP BY model, provider`
81-
args = append(args, params.StartDate.UTC())
82-
} else if !endZero {
83-
query = `SELECT model, provider, COALESCE(SUM(input_tokens), 0), COALESCE(SUM(output_tokens), 0)` + costCols + `
84-
FROM "usage" WHERE timestamp < $1 GROUP BY model, provider`
85-
args = append(args, params.EndDate.AddDate(0, 0, 1).UTC())
86-
} else {
87-
query = `SELECT model, provider, COALESCE(SUM(input_tokens), 0), COALESCE(SUM(output_tokens), 0)` + costCols + `
88-
FROM "usage" GROUP BY model, provider`
89-
}
52+
query := `SELECT model, provider, COALESCE(SUM(input_tokens), 0), COALESCE(SUM(output_tokens), 0)` + costCols + `
53+
FROM "usage"` + where + ` GROUP BY model, provider`
9054

9155
rows, err := r.pool.Query(ctx, query, args...)
9256
if err != nil {
@@ -114,29 +78,7 @@ func (r *PostgreSQLReader) GetUsageByModel(ctx context.Context, params UsageQuer
11478
func (r *PostgreSQLReader) GetUsageLog(ctx context.Context, params UsageLogParams) (*UsageLogResult, error) {
11579
limit, offset := clampLimitOffset(params.Limit, params.Offset)
11680

117-
var conditions []string
118-
var args []interface{}
119-
argIdx := 1
120-
121-
startZero := params.StartDate.IsZero()
122-
endZero := params.EndDate.IsZero()
123-
124-
if !startZero && !endZero {
125-
conditions = append(conditions, fmt.Sprintf("timestamp >= $%d", argIdx))
126-
args = append(args, params.StartDate.UTC())
127-
argIdx++
128-
conditions = append(conditions, fmt.Sprintf("timestamp < $%d", argIdx))
129-
args = append(args, params.EndDate.AddDate(0, 0, 1).UTC())
130-
argIdx++
131-
} else if !startZero {
132-
conditions = append(conditions, fmt.Sprintf("timestamp >= $%d", argIdx))
133-
args = append(args, params.StartDate.UTC())
134-
argIdx++
135-
} else if !endZero {
136-
conditions = append(conditions, fmt.Sprintf("timestamp < $%d", argIdx))
137-
args = append(args, params.EndDate.AddDate(0, 0, 1).UTC())
138-
argIdx++
139-
}
81+
conditions, args, argIdx := pgDateRangeConditions(params.UsageQueryParams, 1)
14082

14183
if params.Model != "" {
14284
conditions = append(conditions, fmt.Sprintf("model = $%d", argIdx))
@@ -204,6 +146,23 @@ func (r *PostgreSQLReader) GetUsageLog(ctx context.Context, params UsageLogParam
204146
}, nil
205147
}
206148

149+
// pgDateRangeConditions returns WHERE conditions and args for a date range.
150+
// argIdx is the starting $N placeholder index; nextIdx is the next available index.
151+
func pgDateRangeConditions(params UsageQueryParams, argIdx int) (conditions []string, args []interface{}, nextIdx int) {
152+
nextIdx = argIdx
153+
if !params.StartDate.IsZero() {
154+
conditions = append(conditions, fmt.Sprintf("timestamp >= $%d", nextIdx))
155+
args = append(args, params.StartDate.UTC())
156+
nextIdx++
157+
}
158+
if !params.EndDate.IsZero() {
159+
conditions = append(conditions, fmt.Sprintf("timestamp < $%d", nextIdx))
160+
args = append(args, params.EndDate.AddDate(0, 0, 1).UTC())
161+
nextIdx++
162+
}
163+
return conditions, args, nextIdx
164+
}
165+
207166
func pgGroupExpr(interval string) string {
208167
switch interval {
209168
case "weekly":
@@ -225,22 +184,8 @@ func (r *PostgreSQLReader) GetDailyUsage(ctx context.Context, params UsageQueryP
225184
}
226185
groupExpr := pgGroupExpr(interval)
227186

228-
var where string
229-
var args []interface{}
230-
231-
startZero := params.StartDate.IsZero()
232-
endZero := params.EndDate.IsZero()
233-
234-
if !startZero && !endZero {
235-
where = ` WHERE timestamp >= $1 AND timestamp < $2`
236-
args = append(args, params.StartDate.UTC(), params.EndDate.AddDate(0, 0, 1).UTC())
237-
} else if !startZero {
238-
where = ` WHERE timestamp >= $1`
239-
args = append(args, params.StartDate.UTC())
240-
} else if !endZero {
241-
where = ` WHERE timestamp < $1`
242-
args = append(args, params.EndDate.AddDate(0, 0, 1).UTC())
243-
}
187+
conditions, args, _ := pgDateRangeConditions(params, 1)
188+
where := buildWhereClause(conditions)
244189

245190
query := fmt.Sprintf(`SELECT %s as period, COUNT(*), COALESCE(SUM(input_tokens), 0), COALESCE(SUM(output_tokens), 0), COALESCE(SUM(total_tokens), 0)
246191
FROM "usage"%s GROUP BY %s ORDER BY period`, groupExpr, where, groupExpr)

0 commit comments

Comments
 (0)