Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: extended stats endpoint for namespace #52

Merged
merged 2 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 84 additions & 3 deletions cmd/api/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions cmd/api/docs/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions cmd/api/docs/swagger.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions cmd/api/handler/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package handler

import (
"encoding/hex"
"errors"
"net/http"

Expand Down Expand Up @@ -272,3 +273,66 @@ func (sh StatsHandler) Series(c echo.Context) error {
}
return returnArray(c, response)
}

type namespaceSeriesRequest struct {
Id string `example:"0011223344" param:"id" swaggertype:"string" validate:"required,hexadecimal,len=56"`
Timeframe string `example:"hour" param:"timeframe" swaggertype:"string" validate:"required,oneof=hour day week month year"`
SeriesName string `example:"size" param:"name" swaggertype:"string" validate:"required,oneof=pfb_count size"`
From uint64 `example:"1692892095" query:"from" swaggertype:"integer" validate:"omitempty,min=1"`
To uint64 `example:"1692892095" query:"to" swaggertype:"integer" validate:"omitempty,min=1"`
}

// NamespaceSeries godoc
//
// @Summary Get histogram for namespace with precomputed stats
// @Description Get histogram for namespace with precomputed stats by series name and timeframe
// @Tags stats
// @ID stats-ns-series
// @Param id path string true "Namespace id in hexadecimal" minlength(56) maxlength(56)
// @Param timeframe path string true "Timeframe" Enums(hour, day, week, month, year)
// @Param name path string true "Series name" Enums(pfb_count, size)
// @Param from query integer false "Time from in unix timestamp" mininum(1)
// @Param to query integer false "Time to in unix timestamp" mininum(1)
// @Produce json
// @Success 200 {array} responses.SeriesItem
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/stats/namespace/series/{id}/{name}/{timeframe} [get]
func (sh StatsHandler) NamespaceSeries(c echo.Context) error {
req, err := bindAndValidate[namespaceSeriesRequest](c)
if err != nil {
return badRequestError(c, err)
}

namespaceId, err := hex.DecodeString(req.Id)
if err != nil {
return badRequestError(c, err)
}

namespace, err := sh.nsRepo.ByNamespaceId(c.Request().Context(), namespaceId)
if err != nil {
return handleError(c, err, sh.nsRepo)
}
if len(namespace) == 0 {
return c.JSON(http.StatusOK, []any{})
}

histogram, err := sh.repo.NamespaceSeries(
c.Request().Context(),
storage.Timeframe(req.Timeframe),
req.SeriesName,
namespace[0].Id,
storage.SeriesRequest{
From: req.From,
To: req.To,
})
if err != nil {
return internalServerError(c, err)
}

response := make([]responses.SeriesItem, len(histogram))
for i := range histogram {
response[i] = responses.NewSeriesItem(histogram[i])
}
return returnArray(c, response)
}
49 changes: 49 additions & 0 deletions cmd/api/handler/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,52 @@ func (s *StatsTestSuite) TestBlockStatsHistogram() {
}
}
}

func (s *StatsTestSuite) TestNamespaceStatsHistogram() {
for _, name := range []string{
storage.SeriesNsPfbCount,
storage.SeriesNsSize,
} {

for _, tf := range []storage.Timeframe{
storage.TimeframeHour,
storage.TimeframeDay,
storage.TimeframeWeek,
storage.TimeframeMonth,
storage.TimeframeYear,
} {
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := s.echo.NewContext(req, rec)
c.SetPath("/v1/stats/namespace/series/:id/:name/:timeframe")
c.SetParamNames("id", "name", "timeframe")
c.SetParamValues("000000000000000000000000000000000000000008E5F679BF7116CB", name, string(tf))

s.ns.EXPECT().
ByNamespaceId(gomock.Any(), gomock.Any()).
Return([]storage.Namespace{
testNamespace,
}, nil)

s.stats.EXPECT().
NamespaceSeries(gomock.Any(), tf, name, testNamespace.Id, gomock.Any()).
Return([]storage.SeriesItem{
{
Time: testTime,
Value: "11234",
},
}, nil)

s.Require().NoError(s.handler.NamespaceSeries(c))
s.Require().Equal(http.StatusOK, rec.Code)

var response []responses.SeriesItem
err := json.NewDecoder(rec.Body).Decode(&response)
s.Require().NoError(err)
s.Require().Len(response, 1)

item := response[0]
s.Require().Equal("11234", item.Value)
}
}
}
1 change: 1 addition & 0 deletions cmd/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ func initHandlers(ctx context.Context, e *echo.Echo, cfg Config, db postgres.Sto
namespace := stats.Group("/namespace")
{
namespace.GET("/usage", statsHandler.NamespaceUsage)
namespace.GET("/series/:id/:name/:timeframe", statsHandler.NamespaceSeries)
}
series := stats.Group("/series")
{
Expand Down
Loading
Loading