2019-01-30 21:44:50 +00:00
|
|
|
// 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"
|
2019-02-14 21:55:21 +00:00
|
|
|
"go.uber.org/zap"
|
2019-01-30 21:44:50 +00:00
|
|
|
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/satellite/accounting"
|
2019-01-30 21:44:50 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb"
|
|
|
|
)
|
|
|
|
|
2019-02-27 21:55:19 +00:00
|
|
|
// generateCSV creates a report with node usage data for all nodes in a given period which can be used for payments
|
2019-01-30 21:44:50 +00:00
|
|
|
func generateCSV(ctx context.Context, start time.Time, end time.Time, output io.Writer) error {
|
2019-02-27 21:55:19 +00:00
|
|
|
db, err := satellitedb.New(zap.L().Named("db"), nodeUsageCfg.Database)
|
2019-01-30 21:44:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return errs.New("error connecting to master database on satellite: %+v", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, db.Close())
|
|
|
|
}()
|
|
|
|
|
2019-05-10 20:05:42 +01:00
|
|
|
rows, err := db.StoragenodeAccounting().QueryPaymentInfo(ctx, start, end)
|
2019-01-30 21:44:50 +00:00
|
|
|
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",
|
2019-02-28 17:13:59 +00:00
|
|
|
"bytes:BWGet",
|
2019-01-30 21:44:50 +00:00
|
|
|
"walletAddress",
|
2019-06-21 15:21:15 +01:00
|
|
|
"disqualified",
|
2019-01-30 21:44:50 +00:00
|
|
|
}
|
|
|
|
if err := w.Write(headers); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, row := range rows {
|
2019-03-01 17:46:34 +00:00
|
|
|
nid := row.NodeID
|
|
|
|
|
2019-04-04 17:34:36 +01:00
|
|
|
node, err := db.OverlayCache().Get(ctx, nid)
|
2019-03-01 17:46:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-04 17:34:36 +01:00
|
|
|
row.Wallet = node.Operator.Wallet
|
2019-01-30 21:44:50 +00:00
|
|
|
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 {
|
2019-02-27 21:55:19 +00:00
|
|
|
fmt.Println("Generated node usage report for payments")
|
2019-01-30 21:44:50 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func structToStringSlice(s *accounting.CSVRow) []string {
|
2019-06-21 15:21:15 +01:00
|
|
|
dqStr := ""
|
|
|
|
if s.Disqualified != nil {
|
|
|
|
dqStr = s.Disqualified.Format("2006-01-02")
|
|
|
|
}
|
2019-01-30 21:44:50 +00:00
|
|
|
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),
|
2019-02-28 17:13:59 +00:00
|
|
|
strconv.FormatInt(s.GetTotal, 10),
|
2019-01-30 21:44:50 +00:00
|
|
|
s.Wallet,
|
2019-06-21 15:21:15 +01:00
|
|
|
dqStr,
|
2019-01-30 21:44:50 +00:00
|
|
|
}
|
|
|
|
return record
|
|
|
|
}
|