Commit 9d88d2cd authored by Hector Sanjuan's avatar Hector Sanjuan

RepoStat: address review comments

License: MIT
Signed-off-by: default avatarHector Sanjuan <hector@protocol.ai>
parent 37d49896
......@@ -150,7 +150,7 @@ var repoStatCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Get stats for the currently used repo.",
ShortDescription: `
'ipfs repo stat' provides information about the local set of
'ipfs repo stat' provides information about the local set of
stored objects. It outputs:
RepoSize int Size in bytes that the repo is currently taking.
......@@ -171,26 +171,33 @@ Version string The repo version.
return
}
statF := corerepo.RepoStat
sizeOnly, _ := req.Options["size-only"].(bool)
if sizeOnly {
statF = corerepo.RepoSize
sizeStat, err := corerepo.RepoSize(req.Context, n)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
cmds.EmitOnce(res, &corerepo.Stat{
SizeStat: sizeStat,
})
return
}
stat, err := statF(req.Context, n)
stat, err := corerepo.RepoStat(req.Context, n)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
cmds.EmitOnce(res, stat)
cmds.EmitOnce(res, &stat)
},
Type: corerepo.Stat{},
Type: &corerepo.Stat{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
stat, ok := v.(*corerepo.Stat)
if !ok {
fmt.Println("adios")
return e.TypeErr(stat, v)
}
......@@ -200,32 +207,28 @@ Version string The repo version.
human, _ := req.Options["human"].(bool)
sizeOnly, _ := req.Options["size-only"].(bool)
sizeInMiB := stat.RepoSize / (1024 * 1024)
if human && sizeInMiB > 0 {
fmt.Fprintf(wtr, "RepoSize (MiB):\t%d\n", sizeInMiB)
} else {
fmt.Fprintf(wtr, "RepoSize:\t%d\n", stat.RepoSize)
}
if stat.StorageMax != corerepo.NoLimit {
maxSizeInMiB := stat.StorageMax / (1024 * 1024)
if human && maxSizeInMiB > 0 {
fmt.Fprintf(wtr, "StorageMax (MiB):\t%d\n", maxSizeInMiB)
printSize := func(name string, size uint64) {
sizeInMiB := size / (1024 * 1024)
if human && sizeInMiB > 0 {
fmt.Fprintf(wtr, "%s (MiB):\t%d\n", name, sizeInMiB)
} else {
fmt.Fprintf(wtr, "StorageMax:\t%d\n", stat.StorageMax)
fmt.Fprintf(wtr, "%s:\t%d\n", name, size)
}
}
if sizeOnly {
return nil
if !sizeOnly {
fmt.Fprintf(wtr, "NumObjects:\t%d\n", stat.NumObjects)
}
fmt.Fprintf(wtr, "NumObjects:\t%d\n", stat.NumObjects)
fmt.Fprintf(wtr, "RepoPath:\t%s\n", stat.RepoPath)
fmt.Fprintf(wtr, "Version:\t%s\n", stat.Version)
printSize("RepoSize", stat.RepoSize)
printSize("StorageMax", stat.StorageMax)
return nil
if !sizeOnly {
fmt.Fprintf(wtr, "RepoPath:\t%s\n", stat.RepoPath)
fmt.Fprintf(wtr, "Version:\t%s\n", stat.Version)
}
return nil
}),
},
}
......
......@@ -12,10 +12,15 @@ import (
humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
)
// Stat wraps information about the objects stored on disk.
type Stat struct {
// SizeStat wraps information about the repository size and its limit.
type SizeStat struct {
RepoSize uint64 // size in bytes
StorageMax uint64 // size in bytes
}
// Stat wraps information about the objects stored on disk.
type Stat struct {
SizeStat
NumObjects uint64
RepoPath string
Version string
......@@ -25,15 +30,15 @@ type Stat struct {
const NoLimit uint64 = math.MaxUint64
// RepoStat returns a *Stat object with all the fields set.
func RepoStat(ctx context.Context, n *core.IpfsNode) (*Stat, error) {
func RepoStat(ctx context.Context, n *core.IpfsNode) (Stat, error) {
sizeStat, err := RepoSize(ctx, n)
if err != nil {
return nil, err
return Stat{}, err
}
allKeys, err := n.Blockstore.AllKeysChan(ctx)
if err != nil {
return nil, err
return Stat{}, err
}
count := uint64(0)
......@@ -43,41 +48,43 @@ func RepoStat(ctx context.Context, n *core.IpfsNode) (*Stat, error) {
path, err := fsrepo.BestKnownPath()
if err != nil {
return nil, err
return Stat{}, err
}
return &Stat{
return Stat{
SizeStat: SizeStat{
RepoSize: sizeStat.RepoSize,
StorageMax: sizeStat.StorageMax,
},
NumObjects: count,
RepoSize: sizeStat.RepoSize,
StorageMax: sizeStat.StorageMax,
RepoPath: path,
Version: fmt.Sprintf("fs-repo@%d", fsrepo.RepoVersion),
}, nil
}
// RepoSize returns a *Stat object with the RepoSize and StorageMax fields set.
func RepoSize(ctx context.Context, n *core.IpfsNode) (*Stat, error) {
func RepoSize(ctx context.Context, n *core.IpfsNode) (SizeStat, error) {
r := n.Repo
cfg, err := r.Config()
if err != nil {
return nil, err
return SizeStat{}, err
}
usage, err := r.GetStorageUsage()
if err != nil {
return nil, err
return SizeStat{}, err
}
storageMax := NoLimit
if cfg.Datastore.StorageMax != "" {
storageMax, err = humanize.ParseBytes(cfg.Datastore.StorageMax)
if err != nil {
return nil, err
return SizeStat{}, err
}
}
return &Stat{
return SizeStat{
RepoSize: usage,
StorageMax: storageMax,
}, nil
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment