Skip to content

Commit

Permalink
Merge pull request #4788 from filecoin-project/feat/client-dealstats
Browse files Browse the repository at this point in the history
Add client deal-stats CLI
  • Loading branch information
arajasek authored Nov 10, 2020
2 parents 735c04f + 60debfb commit c02341c
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"math"
"math/rand"
"os"
"path/filepath"
Expand Down Expand Up @@ -81,6 +82,7 @@ var clientCmd = &cli.Command{
WithCategory("storage", clientListDeals),
WithCategory("storage", clientGetDealCmd),
WithCategory("storage", clientListAsksCmd),
WithCategory("storage", clientDealStatsCmd),
WithCategory("data", clientImportCmd),
WithCategory("data", clientDropCmd),
WithCategory("data", clientLocalCmd),
Expand Down Expand Up @@ -1112,6 +1114,80 @@ var clientRetrieveCmd = &cli.Command{
},
}

var clientDealStatsCmd = &cli.Command{
Name: "deal-stats",
Usage: "Print statistics about local storage deals",
Flags: []cli.Flag{
&cli.DurationFlag{
Name: "newer-than",
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)

localDeals, err := api.ClientListDeals(ctx)
if err != nil {
return err
}

var totalSize uint64
byState := map[storagemarket.StorageDealStatus][]uint64{}
for _, deal := range localDeals {
if cctx.IsSet("newer-than") {
if time.Now().Sub(deal.CreationTime) > cctx.Duration("newer-than") {
continue
}
}

totalSize += deal.Size
byState[deal.State] = append(byState[deal.State], deal.Size)
}

fmt.Printf("Total: %d deals, %s\n", len(localDeals), types.SizeStr(types.NewInt(totalSize)))

type stateStat struct {
state storagemarket.StorageDealStatus
count int
bytes uint64
}

stateStats := make([]stateStat, 0, len(byState))
for state, deals := range byState {
if state == storagemarket.StorageDealActive {
state = math.MaxUint64 // for sort
}

st := stateStat{
state: state,
count: len(deals),
}
for _, b := range deals {
st.bytes += b
}

stateStats = append(stateStats, st)
}

sort.Slice(stateStats, func(i, j int) bool {
return int64(stateStats[i].state) < int64(stateStats[j].state)
})

for _, st := range stateStats {
if st.state == math.MaxUint64 {
st.state = storagemarket.StorageDealActive
}
fmt.Printf("%s: %d deals, %s\n", storagemarket.DealStates[st.state], st.count, types.SizeStr(types.NewInt(st.bytes)))
}

return nil
},
}

var clientListAsksCmd = &cli.Command{
Name: "list-asks",
Usage: "List asks for top miners",
Expand Down

0 comments on commit c02341c

Please sign in to comment.