-
Notifications
You must be signed in to change notification settings - Fork 390
/
endpoint.go
159 lines (136 loc) · 5.17 KB
/
endpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package nodestats
import (
"context"
"github.com/spacemonkeygo/monkit/v3"
"go.uber.org/zap"
"storj.io/common/identity"
"storj.io/common/pb"
"storj.io/common/rpc/rpcstatus"
"storj.io/storj/satellite/accounting"
"storj.io/storj/satellite/overlay"
"storj.io/storj/satellite/payments/paymentsconfig"
"storj.io/storj/satellite/reputation"
)
var (
mon = monkit.Package()
)
// Endpoint for querying node stats for the SNO.
//
// architecture: Endpoint
type Endpoint struct {
pb.DRPCNodeStatsUnimplementedServer
log *zap.Logger
overlay overlay.DB
reputation *reputation.Service
accounting accounting.StoragenodeAccounting
config paymentsconfig.Config
}
// NewEndpoint creates new endpoint.
func NewEndpoint(log *zap.Logger, overlay overlay.DB, reputation *reputation.Service, accounting accounting.StoragenodeAccounting, config paymentsconfig.Config) *Endpoint {
return &Endpoint{
log: log,
overlay: overlay,
reputation: reputation,
accounting: accounting,
config: config,
}
}
// GetStats sends node stats for client node.
func (e *Endpoint) GetStats(ctx context.Context, req *pb.GetStatsRequest) (_ *pb.GetStatsResponse, err error) {
defer mon.Task()(&ctx)(&err)
peer, err := identity.PeerIdentityFromContext(ctx)
if err != nil {
return nil, rpcstatus.Error(rpcstatus.Unauthenticated, err.Error())
}
node, err := e.overlay.Get(ctx, peer.ID)
if err != nil {
if overlay.ErrNodeNotFound.Has(err) {
return nil, nil
}
e.log.Error("overlay.Get failed", zap.Error(err))
return nil, rpcstatus.Error(rpcstatus.Internal, err.Error())
}
reputationInfo, err := e.reputation.Get(ctx, peer.ID)
if err != nil {
e.log.Error("reputation.Get failed", zap.Error(err))
return nil, rpcstatus.Error(rpcstatus.Internal, err.Error())
}
auditScore := calculateReputationScore(
reputationInfo.AuditReputationAlpha,
reputationInfo.AuditReputationBeta)
unknownScore := calculateReputationScore(
reputationInfo.UnknownAuditReputationAlpha,
reputationInfo.UnknownAuditReputationBeta)
return &pb.GetStatsResponse{
AuditCheck: &pb.ReputationStats{
TotalCount: reputationInfo.TotalAuditCount,
SuccessCount: reputationInfo.AuditSuccessCount,
ReputationAlpha: reputationInfo.AuditReputationAlpha,
ReputationBeta: reputationInfo.AuditReputationBeta,
UnknownReputationAlpha: reputationInfo.UnknownAuditReputationAlpha,
UnknownReputationBeta: reputationInfo.UnknownAuditReputationBeta,
ReputationScore: auditScore,
UnknownReputationScore: unknownScore,
},
OnlineScore: reputationInfo.OnlineScore,
Disqualified: node.Disqualified,
Suspended: node.UnknownAuditSuspended,
OfflineSuspended: node.OfflineSuspended,
OfflineUnderReview: reputationInfo.UnderReview,
VettedAt: node.Reputation.Status.VettedAt,
AuditHistory: reputation.AuditHistoryToPB(reputationInfo.AuditHistory),
JoinedAt: node.CreatedAt,
}, nil
}
// DailyStorageUsage returns slice of daily storage usage for given period of time sorted in ASC order by date.
func (e *Endpoint) DailyStorageUsage(ctx context.Context, req *pb.DailyStorageUsageRequest) (_ *pb.DailyStorageUsageResponse, err error) {
defer mon.Task()(&ctx)(&err)
peer, err := identity.PeerIdentityFromContext(ctx)
if err != nil {
return nil, rpcstatus.Error(rpcstatus.Unauthenticated, err.Error())
}
node, err := e.overlay.Get(ctx, peer.ID)
if err != nil {
if overlay.ErrNodeNotFound.Has(err) {
return nil, nil
}
e.log.Error("overlay.Get failed", zap.Error(err))
return nil, rpcstatus.Error(rpcstatus.Internal, err.Error())
}
nodeSpaceUsages, err := e.accounting.QueryStorageNodeUsage(ctx, node.Id, req.GetFrom(), req.GetTo())
if err != nil {
e.log.Error("accounting.QueryStorageNodeUsage failed", zap.Error(err))
return nil, rpcstatus.Error(rpcstatus.Internal, err.Error())
}
return &pb.DailyStorageUsageResponse{
NodeId: node.Id,
DailyStorageUsage: toProtoDailyStorageUsage(nodeSpaceUsages),
}, nil
}
// PricingModel returns pricing model for storagenode.
func (e *Endpoint) PricingModel(ctx context.Context, req *pb.PricingModelRequest) (_ *pb.PricingModelResponse, err error) {
defer mon.Task()(&ctx)(&err)
return &pb.PricingModelResponse{
EgressBandwidthPrice: e.config.NodeEgressBandwidthPrice,
RepairBandwidthPrice: e.config.NodeRepairBandwidthPrice,
DiskSpacePrice: e.config.NodeDiskSpacePrice,
AuditBandwidthPrice: e.config.NodeAuditBandwidthPrice,
}, nil
}
// toProtoDailyStorageUsage converts StorageNodeUsage to PB DailyStorageUsageResponse_StorageUsage.
func toProtoDailyStorageUsage(usages []accounting.StorageNodeUsage) []*pb.DailyStorageUsageResponse_StorageUsage {
var pbUsages []*pb.DailyStorageUsageResponse_StorageUsage
for _, usage := range usages {
pbUsages = append(pbUsages, &pb.DailyStorageUsageResponse_StorageUsage{
AtRestTotal: usage.StorageUsed,
Timestamp: usage.Timestamp,
})
}
return pbUsages
}
// calculateReputationScore is helper method to calculate reputation score value.
func calculateReputationScore(alpha, beta float64) float64 {
return alpha / (alpha + beta)
}