2020-12-26 01:16:43 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package multinode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/rpc/rpcstatus"
|
|
|
|
"storj.io/storj/private/multinodepb"
|
|
|
|
"storj.io/storj/storagenode/apikeys"
|
|
|
|
"storj.io/storj/storagenode/bandwidth"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ multinodepb.DRPCBandwidthServer = (*BandwidthEndpoint)(nil)
|
|
|
|
|
|
|
|
// BandwidthEndpoint implements multinode bandwidth endpoint.
|
|
|
|
//
|
|
|
|
// architecture: Endpoint
|
|
|
|
type BandwidthEndpoint struct {
|
2021-03-29 09:58:04 +01:00
|
|
|
multinodepb.DRPCBandwidthUnimplementedServer
|
|
|
|
|
2020-12-26 01:16:43 +00:00
|
|
|
log *zap.Logger
|
|
|
|
apiKeys *apikeys.Service
|
|
|
|
db bandwidth.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBandwidthEndpoint creates new multinode bandwidth endpoint.
|
|
|
|
func NewBandwidthEndpoint(log *zap.Logger, apiKeys *apikeys.Service, db bandwidth.DB) *BandwidthEndpoint {
|
|
|
|
return &BandwidthEndpoint{
|
|
|
|
log: log,
|
|
|
|
apiKeys: apiKeys,
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MonthSummary returns bandwidth used current month.
|
|
|
|
func (bandwidth *BandwidthEndpoint) MonthSummary(ctx context.Context, req *multinodepb.BandwidthMonthSummaryRequest) (_ *multinodepb.BandwidthMonthSummaryResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err = authenticate(ctx, bandwidth.apiKeys, req.GetHeader()); err != nil {
|
|
|
|
return nil, rpcstatus.Wrap(rpcstatus.Unauthenticated, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
used, err := bandwidth.db.MonthSummary(ctx, time.Now())
|
|
|
|
if err != nil {
|
|
|
|
return nil, rpcstatus.Wrap(rpcstatus.Internal, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &multinodepb.BandwidthMonthSummaryResponse{
|
|
|
|
Used: used,
|
|
|
|
}, nil
|
|
|
|
}
|