From 28b3a72f9bafd95d432b16edb3b8eb492b943729 Mon Sep 17 00:00:00 2001 From: tdakkota Date: Mon, 10 Jun 2024 21:58:42 +0300 Subject: [PATCH] refactor(promapi): move `MinTime` and `MaxTime` --- internal/promapi/promapi.go | 19 +++++++++++++++++++ internal/promhandler/params.go | 17 ++++------------- internal/promhandler/promhandler.go | 12 ++++++------ 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/internal/promapi/promapi.go b/internal/promapi/promapi.go index f6caada0..d2590db0 100644 --- a/internal/promapi/promapi.go +++ b/internal/promapi/promapi.go @@ -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) +) diff --git a/internal/promhandler/params.go b/internal/promhandler/params.go index df58aeb0..1b5b2bed 100644 --- a/internal/promhandler/params.go +++ b/internal/promhandler/params.go @@ -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) { @@ -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) } diff --git a/internal/promhandler/promhandler.go b/internal/promhandler/promhandler.go index e9fbebe6..5365fe86 100644 --- a/internal/promhandler/promhandler.go +++ b/internal/promhandler/promhandler.go @@ -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) } @@ -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) } @@ -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) }