Skip to content

Commit

Permalink
repo-stat
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: David Dias <daviddias.p@gmail.com>
  • Loading branch information
daviddias committed Mar 2, 2016
1 parent 288f7bc commit ed112fe
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 47 deletions.
90 changes: 43 additions & 47 deletions core/commands/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@ package commands
import (
"bytes"
"fmt"
"io"
"strings"

humanize "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/dustin/go-humanize"
cmds "github.com/ipfs/go-ipfs/commands"
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
"io"
"strconv"
)

type RepoStat struct {
repoPath string
repoSize uint64 // size in bytes
numBlocks uint64
}

var RepoCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Manipulate the IPFS repo.",
Expand Down Expand Up @@ -108,59 +99,64 @@ order to reclaim hard disk space.

var repoStatCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Print status of the local repo.",
ShortDescription: ``,
Tagline: "Get stats for the currently used repo.",
ShortDescription: `
'ipfs repo stat' is a plumbing command that will scan the local
set of stored objects and print repo statistics. It outputs to stdout:
NumObjects int number of objects in the local repo
RepoSize int size that the repo is currently taking
RepoPath string the path to the repo being currently used
`,
},
Run: func(req cmds.Request, res cmds.Response) {
ctx := req.Context()
n, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

usage, err := n.Repo.GetStorageUsage()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

allKeys, err := n.Blockstore.AllKeysChan(ctx)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

count := uint64(0)
for range allKeys {
count++
}

path, err := fsrepo.BestKnownPath()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
outChan := make(chan interface{})
res.SetOutput((<-chan interface{})(outChan))

res.SetOutput(&RepoStat{
repoPath: path,
repoSize: usage,
numBlocks: count,
})
go func() {
defer close(outChan)
stat, err := corerepo.RepoStat(n, req.Context())
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
outChan <- stat
}()
},
Type: RepoStat{},
Type: corerepo.Stat{},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
stat, ok := res.Output().(*RepoStat)
outChan, ok := res.Output().(<-chan interface{})
if !ok {
return nil, u.ErrCast()
}

out := fmt.Sprintf(
"Path: %s\nSize: %s\nBlocks: %d\n",
stat.repoPath, humanize.Bytes(stat.repoSize), stat.numBlocks)
marshal := func(v interface{}) (io.Reader, error) {
obj, ok := v.(*corerepo.Stat)
if !ok {
return nil, u.ErrCast()
}

return strings.NewReader(out), nil
buf := new(bytes.Buffer)
numObjects := "NumObjects \t" + strconv.FormatUint(obj.NumObjects, 10) + "\n"
repoSize := "RepoSize \t" + strconv.FormatUint(obj.RepoSize, 10) + "\n"
repoPath := "RepoPath \t" + obj.RepoPath + "\n"

buf = bytes.NewBufferString(numObjects + repoSize + repoPath)

return buf, nil
}

return &cmds.ChannelMarshaler{
Channel: outChan,
Marshaler: marshal,
Res: res,
}, nil
},
},
}
43 changes: 43 additions & 0 deletions core/corerepo/stat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package corerepo

import (
"github.com/ipfs/go-ipfs/core"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
)

type Stat struct {
NumObjects uint64
RepoSize uint64 // size in bytes
RepoPath string
}

func RepoStat(n *core.IpfsNode, ctx context.Context) (*Stat, error) {
r := n.Repo

usage, err := r.GetStorageUsage()
if err != nil {
return nil, err
}

allKeys, err := n.Blockstore.AllKeysChan(ctx)
if err != nil {
return nil, err
}

count := uint64(0)
for range allKeys {
count++
}

path, err := fsrepo.BestKnownPath()
if err != nil {
return nil, err
}

return &Stat{
NumObjects: count,
RepoSize: usage,
RepoPath: path,
}, nil
}

0 comments on commit ed112fe

Please sign in to comment.