|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "text/tabwriter" |
| 7 | + "time" |
| 8 | + |
| 9 | + humanize "github.com/dustin/go-humanize" |
| 10 | + "github.com/ipfs/boxo/provider" |
| 11 | + cmds "github.com/ipfs/go-ipfs-cmds" |
| 12 | + "github.com/ipfs/kubo/core/commands/cmdenv" |
| 13 | + "github.com/libp2p/go-libp2p-kad-dht/fullrt" |
| 14 | + "golang.org/x/exp/constraints" |
| 15 | +) |
| 16 | + |
| 17 | +type reprovideStats struct { |
| 18 | + provider.ReproviderStats |
| 19 | + fullRT bool |
| 20 | +} |
| 21 | + |
| 22 | +var statReprovideCmd = &cmds.Command{ |
| 23 | + Status: cmds.Experimental, |
| 24 | + Helptext: cmds.HelpText{ |
| 25 | + Tagline: "Returns statistics about the node's reprovider system.", |
| 26 | + ShortDescription: ` |
| 27 | +Returns statistics about the content the node is reproviding every |
| 28 | +Reprovider.Interval according to Reprovider.Strategy: |
| 29 | +https://github.com/ipfs/kubo/blob/master/docs/config.md#reprovider |
| 30 | +
|
| 31 | +This interface is not stable and may change from release to release. |
| 32 | +
|
| 33 | +`, |
| 34 | + }, |
| 35 | + Arguments: []cmds.Argument{}, |
| 36 | + Options: []cmds.Option{}, |
| 37 | + Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { |
| 38 | + nd, err := cmdenv.GetNode(env) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + if !nd.IsOnline { |
| 44 | + return ErrNotOnline |
| 45 | + } |
| 46 | + |
| 47 | + stats, err := nd.Provider.Stat() |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + _, fullRT := nd.DHTClient.(*fullrt.FullRT) |
| 52 | + |
| 53 | + if err := res.Emit(reprovideStats{stats, fullRT}); err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + return nil |
| 58 | + }, |
| 59 | + Encoders: cmds.EncoderMap{ |
| 60 | + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, s reprovideStats) error { |
| 61 | + wtr := tabwriter.NewWriter(w, 1, 2, 1, ' ', 0) |
| 62 | + defer wtr.Flush() |
| 63 | + |
| 64 | + fmt.Fprintf(wtr, "TotalReprovides:\t%s\n", humanNumber(s.TotalReprovides)) |
| 65 | + fmt.Fprintf(wtr, "AvgReprovideDuration:\t%s\n", humanDuration(s.AvgReprovideDuration)) |
| 66 | + fmt.Fprintf(wtr, "LastReprovideDuration:\t%s\n", humanDuration(s.LastReprovideDuration)) |
| 67 | + if !s.LastRun.IsZero() { |
| 68 | + fmt.Fprintf(wtr, "LastReprovide:\t%s\n", humanTime(s.LastRun)) |
| 69 | + if s.fullRT { |
| 70 | + fmt.Fprintf(wtr, "NextReprovide:\t%s\n", humanTime(s.LastRun.Add(s.ReprovideInterval))) |
| 71 | + } |
| 72 | + } |
| 73 | + return nil |
| 74 | + }), |
| 75 | + }, |
| 76 | + Type: reprovideStats{}, |
| 77 | +} |
| 78 | + |
| 79 | +func humanDuration(val time.Duration) string { |
| 80 | + return val.Truncate(time.Microsecond).String() |
| 81 | +} |
| 82 | + |
| 83 | +func humanTime(val time.Time) string { |
| 84 | + return val.Format("2006-01-02 15:04:05") |
| 85 | +} |
| 86 | + |
| 87 | +func humanNumber[T constraints.Float | constraints.Integer](n T) string { |
| 88 | + nf := float64(n) |
| 89 | + str := humanSI(nf, 0) |
| 90 | + fullStr := humanFull(nf, 0) |
| 91 | + if str != fullStr { |
| 92 | + return fmt.Sprintf("%s\t(%s)", str, fullStr) |
| 93 | + } |
| 94 | + return str |
| 95 | +} |
| 96 | + |
| 97 | +func humanSI(val float64, decimals int) string { |
| 98 | + v, unit := humanize.ComputeSI(val) |
| 99 | + return fmt.Sprintf("%s%s", humanFull(v, decimals), unit) |
| 100 | +} |
| 101 | + |
| 102 | +func humanFull(val float64, decimals int) string { |
| 103 | + return humanize.CommafWithDigits(val, decimals) |
| 104 | +} |
0 commit comments