Skip to content

Commit

Permalink
refactor(API): using config field
Browse files Browse the repository at this point in the history
  • Loading branch information
remoterami committed Dec 19, 2024
1 parent c6d5fa4 commit 2eda72e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
22 changes: 11 additions & 11 deletions backend/pkg/api/handlers/handler_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"

"github.com/gobitfly/beaconchain/pkg/commons/log"
"github.com/gobitfly/beaconchain/pkg/commons/utils"
"github.com/invopop/jsonschema"

"github.com/alexedwards/scs/v2"
Expand All @@ -24,13 +23,14 @@ import (
)

type HandlerService struct {
daService dataaccess.DataAccessor
daDummy dataaccess.DataAccessor
scs *scs.SessionManager
isPostMachineMetricsEnabled bool // if more config options are needed, consider having the whole config in here
daService dataaccess.DataAccessor
daDummy dataaccess.DataAccessor
scs *scs.SessionManager

cfg *commontypes.Config
}

func NewHandlerService(dataAccessor dataaccess.DataAccessor, dummy dataaccess.DataAccessor, sessionManager *scs.SessionManager, enablePostMachineMetrics bool) *HandlerService {
func NewHandlerService(dataAccessor dataaccess.DataAccessor, dummy dataaccess.DataAccessor, sessionManager *scs.SessionManager, cfg *commontypes.Config) *HandlerService {
if allNetworks == nil {
networks, err := dataAccessor.GetAllNetworks()
if err != nil {
Expand All @@ -40,10 +40,10 @@ func NewHandlerService(dataAccessor dataaccess.DataAccessor, dummy dataaccess.Da
}

return &HandlerService{
daService: dataAccessor,
daDummy: dummy,
scs: sessionManager,
isPostMachineMetricsEnabled: enablePostMachineMetrics,
daService: dataAccessor,
daDummy: dummy,
scs: sessionManager,
cfg: cfg,
}
}

Expand Down Expand Up @@ -193,7 +193,7 @@ func (h *HandlerService) getCurrentChartTimeLimitsForDashboard(ctx context.Conte
return limits, err
}
limits.MinAllowedTs = limits.LatestExportedTs - min(maxAge, limits.LatestExportedTs) // min to prevent underflow
secondsPerEpoch := utils.Config.Chain.ClConfig.SlotsPerEpoch * utils.Config.Chain.ClConfig.SecondsPerSlot // TODO: fetch dashboards chain id and use correct value for network once available
secondsPerEpoch := h.cfg.Chain.ClConfig.SlotsPerEpoch * h.cfg.Chain.ClConfig.SecondsPerSlot // TODO: fetch dashboards chain id and use correct value for network once available
limits.MaxAllowedInterval = chartDatapointLimit*uint64(aggregation.Duration(secondsPerEpoch).Seconds()) - 1 // -1 to make sure we don't go over the limit

return limits, nil
Expand Down
2 changes: 1 addition & 1 deletion backend/pkg/api/handlers/machine_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (h *HandlerService) LegacyPostUserMachineMetrics(w http.ResponseWriter, r *
apiKey = r.Header.Get("apikey")
}

if !h.isPostMachineMetricsEnabled {
if h.cfg.Frontend.DisableStatsInserts {
returnError(w, r, http.StatusServiceUnavailable, fmt.Errorf("machine metrics pushing is temporarily disabled"))
return
}
Expand Down
4 changes: 4 additions & 0 deletions backend/pkg/api/handlers/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,10 @@ func (h *HandlerService) PublicGetValidatorDashboardRewardsChart(w http.Response
handleErr(w, r, v)
return
}
if afterTs < chartLimits.MinAllowedTs || beforeTs < chartLimits.MinAllowedTs {
returnConflict(w, r, fmt.Errorf("requested time range is too old, minimum timestamp for dashboard owner's premium subscription for this aggregation is %v", chartLimits.MinAllowedTs))
return
}

data, err := h.getDataAccessor(r).GetValidatorDashboardRewardsChart(r.Context(), *dashboardId, groupIds, protocolModes, aggregation, afterTs, beforeTs)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion backend/pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewApiRouter(dataAccessor dataaccess.DataAccessor, dummy dataaccess.DataAcc
if !(cfg.Frontend.CsrfInsecure || cfg.Frontend.Debug) {
internalRouter.Use(getCsrfProtectionMiddleware(cfg), csrfInjecterMiddleware)
}
handlerService := handlers.NewHandlerService(dataAccessor, dummy, sessionManager, !cfg.Frontend.DisableStatsInserts)
handlerService := handlers.NewHandlerService(dataAccessor, dummy, sessionManager, cfg)

// store user id in context, if available
publicRouter.Use(handlerService.StoreUserIdByApiKeyMiddleware)
Expand Down

0 comments on commit 2eda72e

Please sign in to comment.