-
Notifications
You must be signed in to change notification settings - Fork 402
/
usage.go
96 lines (85 loc) · 2.15 KB
/
usage.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"encoding/csv"
"fmt"
"io"
"os"
"strconv"
"time"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/storj/satellite/accounting"
"storj.io/storj/satellite/satellitedb"
)
// generateNodeUsageCSV creates a report with node usage data for all nodes in a given period which can be used for payments.
func generateNodeUsageCSV(ctx context.Context, start time.Time, end time.Time, output io.Writer) error {
db, err := satellitedb.Open(ctx, zap.L().Named("db"), nodeUsageCfg.Database, satellitedb.Options{ApplicationName: "satellite-nodeusage"})
if err != nil {
return errs.New("error connecting to master database on satellite: %+v", err)
}
defer func() {
err = errs.Combine(err, db.Close())
}()
rows, err := db.StoragenodeAccounting().QueryPaymentInfo(ctx, start, end)
if err != nil {
return err
}
w := csv.NewWriter(output)
headers := []string{
"nodeID",
"nodeCreationDate",
"byte-hours:AtRest",
"bytes:BWRepair-GET",
"bytes:BWRepair-PUT",
"bytes:BWAudit",
"bytes:BWPut",
"bytes:BWGet",
"walletAddress",
"disqualified",
}
if err := w.Write(headers); err != nil {
return err
}
for _, row := range rows {
nid := row.NodeID
node, err := db.OverlayCache().Get(ctx, nid)
if err != nil {
return err
}
row.Wallet = node.Operator.Wallet
record := structToStringSlice(row)
if err := w.Write(record); err != nil {
return err
}
}
if err := w.Error(); err != nil {
return err
}
w.Flush()
if output != os.Stdout {
fmt.Println("Generated node usage report for payments")
}
return err
}
func structToStringSlice(s *accounting.CSVRow) []string {
dqStr := ""
if s.Disqualified != nil {
dqStr = s.Disqualified.Format("2006-01-02")
}
record := []string{
s.NodeID.String(),
s.NodeCreationDate.Format("2006-01-02"),
strconv.FormatFloat(s.AtRestTotal, 'f', 5, 64),
strconv.FormatInt(s.GetRepairTotal, 10),
strconv.FormatInt(s.PutRepairTotal, 10),
strconv.FormatInt(s.GetAuditTotal, 10),
strconv.FormatInt(s.PutTotal, 10),
strconv.FormatInt(s.GetTotal, 10),
s.Wallet,
dqStr,
}
return record
}