MCPcopy Index your code
hub / github.com/NdoleStudio/httpsms / UsageHistory

Method UsageHistory

api/pkg/handlers/billing_handler.go:60–87  ·  view source on GitHub ↗

UsageHistory returns the usage history of a user @Summary Get billing usage history. @Description Get billing usage records of sent and received messages for a user in the past. It will be sorted by timestamp in descending order. @Security ApiKeyAuth @Tags Billing @Accept json @

(c fiber.Ctx)

Source from the content-addressed store, hash-verified

58// @Failure 500 {object} responses.InternalServerError
59// @Router /billing/usage-history [get]
60func (h *BillingHandler) UsageHistory(c fiber.Ctx) error {
61 ctx, span := h.tracer.StartFromFiberCtx(c)
62 defer span.End()
63
64 ctxLogger := h.tracer.CtxLogger(h.logger, span)
65
66 var request requests.BillingUsageHistory
67 if err := c.Bind().Query(&request); err != nil {
68 msg := fmt.Sprintf("cannot marshall params [%s] into %T", c.Body(), request)
69 ctxLogger.Warn(stacktrace.Propagate(err, msg))
70 return h.responseBadRequest(c, err)
71 }
72
73 if errors := h.validator.ValidateHistory(ctx, request.Sanitize()); len(errors) != 0 {
74 msg := fmt.Sprintf("validation errors [%s], while fetching heartbeats [%+#v]", spew.Sdump(errors), request)
75 ctxLogger.Warn(stacktrace.NewError(msg))
76 return h.responseUnprocessableEntity(c, errors, "validation errors while fetching usage history")
77 }
78
79 heartbeats, err := h.service.GetUsageHistory(ctx, h.userIDFomContext(c), request.ToIndexParams())
80 if err != nil {
81 msg := fmt.Sprintf("cannot get billing usage history with params [%+#v]", request)
82 ctxLogger.Error(stacktrace.Propagate(err, msg))
83 return h.responseInternalServerError(c)
84 }
85
86 return h.responseOK(c, fmt.Sprintf("fetched %d billing usage %s", len(*heartbeats), h.pluralize("record", len(*heartbeats))), heartbeats)
87}
88
89// Usage returns the current usage history of a user
90// @Summary Get Billing Usage.

Callers

nothing calls this directly

Calls 14

SanitizeMethod · 0.95
ToIndexParamsMethod · 0.95
responseBadRequestMethod · 0.80
ValidateHistoryMethod · 0.80
GetUsageHistoryMethod · 0.80
userIDFomContextMethod · 0.80
responseOKMethod · 0.80
pluralizeMethod · 0.80
StartFromFiberCtxMethod · 0.65
CtxLoggerMethod · 0.65

Tested by

no test coverage detected