Skip to content

Commit

Permalink
refactor(promapi): move MinTime and MaxTime
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Jun 10, 2024
1 parent cad68f2 commit 28b3a72
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
19 changes: 19 additions & 0 deletions internal/promapi/promapi.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
// Package promapi contains generated code for OpenAPI specification.
package promapi

import (
"math"
"time"
)

// https://github.com/prometheus/prometheus/blob/e9b94515caa4c0d7a0e31f722a1534948ebad838/web/api/v1/api.go#L783-L794
var (
// MinTime is the default timestamp used for the begin of optional time ranges.
// Exposed to let downstream projects to reference it.
MinTime = time.Unix(math.MinInt64/1000+62135596801, 0).UTC()

// MaxTime is the default timestamp used for the end of optional time ranges.
// Exposed to let downstream projects to reference it.
MaxTime = time.Unix(math.MaxInt64/1000-62135596801, 999999999).UTC()

minTimeFormatted = MinTime.Format(time.RFC3339Nano)
maxTimeFormatted = MaxTime.Format(time.RFC3339Nano)
)
17 changes: 4 additions & 13 deletions internal/promhandler/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,9 @@ import (
"github.com/go-faster/oteldb/internal/promapi"
)

// / https://github.com/prometheus/prometheus/blob/e9b94515caa4c0d7a0e31f722a1534948ebad838/web/api/v1/api.go#L783-L794
var (
// MinTime is the default timestamp used for the begin of optional time ranges.
// Exposed to let downstream projects to reference it.
MinTime = time.Unix(math.MinInt64/1000+62135596801, 0).UTC()

// MaxTime is the default timestamp used for the end of optional time ranges.
// Exposed to let downstream projects to reference it.
MaxTime = time.Unix(math.MaxInt64/1000-62135596801, 999999999).UTC()

minTimeFormatted = MinTime.Format(time.RFC3339Nano)
maxTimeFormatted = MaxTime.Format(time.RFC3339Nano)
minTimeFormatted = promapi.MinTime.Format(time.RFC3339Nano)
maxTimeFormatted = promapi.MaxTime.Format(time.RFC3339Nano)
)

func parseOptTimestamp(t promapi.OptPrometheusTimestamp, or time.Time) (time.Time, error) {
Expand All @@ -64,9 +55,9 @@ func parseTimestamp[S ~string](raw S) (time.Time, error) {

switch s {
case minTimeFormatted:
return MinTime, nil
return promapi.MinTime, nil
case maxTimeFormatted:
return MaxTime, nil
return promapi.MaxTime, nil
}
return time.Time{}, errors.Errorf("cannot parse %q to a valid timestamp", s)
}
Expand Down
12 changes: 6 additions & 6 deletions internal/promhandler/promhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ func NewPromAPI(
// GetLabelValues implements getLabelValues operation.
// GET /api/v1/label/{label}/values
func (h *PromAPI) GetLabelValues(ctx context.Context, params promapi.GetLabelValuesParams) (*promapi.LabelValuesResponse, error) {
mint, err := parseOptTimestamp(params.Start, MinTime)
mint, err := parseOptTimestamp(params.Start, promapi.MinTime)
if err != nil {
return nil, validationErr("parse start", err)
}
maxt, err := parseOptTimestamp(params.End, MaxTime)
maxt, err := parseOptTimestamp(params.End, promapi.MaxTime)
if err != nil {
return nil, validationErr("parse end", err)
}
Expand Down Expand Up @@ -121,11 +121,11 @@ func (h *PromAPI) GetLabelValues(ctx context.Context, params promapi.GetLabelVal
//
// GET /api/v1/labels
func (h *PromAPI) GetLabels(ctx context.Context, params promapi.GetLabelsParams) (*promapi.LabelsResponse, error) {
mint, err := parseOptTimestamp(params.Start, MinTime)
mint, err := parseOptTimestamp(params.Start, promapi.MinTime)
if err != nil {
return nil, validationErr("parse start", err)
}
maxt, err := parseOptTimestamp(params.End, MaxTime)
maxt, err := parseOptTimestamp(params.End, promapi.MaxTime)
if err != nil {
return nil, validationErr("parse end", err)
}
Expand Down Expand Up @@ -383,11 +383,11 @@ func (h *PromAPI) GetRules(context.Context, promapi.GetRulesParams) (*promapi.Ru
//
// GET /api/v1/series
func (h *PromAPI) GetSeries(ctx context.Context, params promapi.GetSeriesParams) (*promapi.SeriesResponse, error) {
mint, err := parseOptTimestamp(params.Start, MinTime)
mint, err := parseOptTimestamp(params.Start, promapi.MinTime)
if err != nil {
return nil, validationErr("parse start", err)
}
maxt, err := parseOptTimestamp(params.End, MaxTime)
maxt, err := parseOptTimestamp(params.End, promapi.MaxTime)
if err != nil {
return nil, validationErr("parse end", err)
}
Expand Down

0 comments on commit 28b3a72

Please sign in to comment.