Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 45de4b3

Browse files
PolamarasettyRohithaRohitha
and
Rohitha
authored
feat: add dag stat method (#297)
Co-authored-by: Rohitha <[email protected]>
1 parent 7a377cf commit 45de4b3

File tree

4 files changed

+147
-1
lines changed

4 files changed

+147
-1
lines changed

Diff for: dag.go

+83
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
files "github.com/ipfs/boxo/files"
12+
"github.com/ipfs/go-cid"
1213
"github.com/ipfs/go-ipfs-api/options"
1314
)
1415

@@ -37,6 +38,46 @@ type DagImportOutput struct {
3738
Stats *DagImportStats
3839
}
3940

41+
type DagStat struct {
42+
Cid cid.Cid `json:",omitempty"`
43+
Size uint64 `json:",omitempty"`
44+
NumBlocks int64 `json:",omitempty"`
45+
}
46+
47+
type DagStatOutput struct {
48+
redundantSize uint64 `json:"-"`
49+
UniqueBlocks int `json:",omitempty"`
50+
TotalSize uint64 `json:",omitempty"`
51+
SharedSize uint64 `json:",omitempty"`
52+
Ratio float32 `json:",omitempty"`
53+
DagStatsArray []*DagStat `json:"DagStats,omitempty"`
54+
}
55+
56+
func (s *DagStat) UnmarshalJSON(data []byte) error {
57+
/*
58+
We can't rely on cid.Cid.UnmarshalJSON since it uses the {"/": "..."}
59+
format. To make the output consistent and follow the Kubo API patterns
60+
we use the Cid.Parse method
61+
*/
62+
63+
type Alias DagStat
64+
aux := struct {
65+
Cid string `json:"Cid"`
66+
*Alias
67+
}{
68+
Alias: (*Alias)(s),
69+
}
70+
if err := json.Unmarshal(data, &aux); err != nil {
71+
return err
72+
}
73+
Cid, err := cid.Parse(aux.Cid)
74+
if err != nil {
75+
return err
76+
}
77+
s.Cid = Cid
78+
return nil
79+
}
80+
4081
func (s *Shell) DagGet(ref string, out interface{}) error {
4182
return s.Request("dag/get", ref).Exec(context.Background(), out)
4283
}
@@ -151,3 +192,45 @@ func dagToFilesReader(data interface{}) (*files.MultiFileReader, error) {
151192

152193
return fileReader, nil
153194
}
195+
196+
// DagStat gets stats for dag with default options
197+
func (s *Shell) DagStat(data string) (DagStatOutput, error) {
198+
return s.DagStatWithOpts(data)
199+
}
200+
201+
// DagStatWithOpts gets stats for dag
202+
func (s *Shell) DagStatWithOpts(data string, opts ...options.DagStatOption) (DagStatOutput, error) {
203+
var out DagStatOutput
204+
cfg, err := options.DagStatOptions(opts...)
205+
if err != nil {
206+
return out, err
207+
}
208+
209+
resp, err := s.
210+
Request("dag/stat", data).
211+
Option("progress", cfg.Progress).
212+
Send(context.Background())
213+
214+
if err != nil {
215+
return out, err
216+
}
217+
218+
defer resp.Close()
219+
220+
if resp.Error != nil {
221+
return out, resp.Error
222+
}
223+
224+
dec := json.NewDecoder(resp.Output)
225+
for {
226+
var v DagStatOutput
227+
if err := dec.Decode(&v); err == io.EOF {
228+
break
229+
} else if err != nil {
230+
return out, err
231+
}
232+
out = v
233+
}
234+
235+
return out, nil
236+
}

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module github.com/ipfs/go-ipfs-api
55
require (
66
github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927
77
github.com/ipfs/boxo v0.8.0
8+
github.com/ipfs/go-cid v0.4.0
89
github.com/libp2p/go-libp2p v0.26.3
910
github.com/mitchellh/go-homedir v1.1.0
1011
github.com/multiformats/go-multiaddr v0.8.0
@@ -16,7 +17,6 @@ require (
1617
github.com/benbjohnson/clock v1.3.0 // indirect
1718
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3 // indirect
1819
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
19-
github.com/ipfs/go-cid v0.4.0 // indirect
2020
github.com/klauspost/cpuid/v2 v2.2.3 // indirect
2121
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
2222
github.com/libp2p/go-flow-metrics v0.1.0 // indirect

Diff for: options/dag_stat.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package options
2+
3+
// DagStatSettings is a set of Dag stat options.
4+
type DagStatSettings struct {
5+
Progress bool
6+
}
7+
8+
// DagStatOption is a single Dag option.
9+
type DagStatOption func(opts *DagStatSettings) error
10+
11+
// DagStatOptions applies the given option to a DagStatSettings instance.
12+
func DagStatOptions(opts ...DagStatOption) (*DagStatSettings, error) {
13+
options := &DagStatSettings{
14+
Progress: false,
15+
}
16+
17+
for _, opt := range opts {
18+
err := opt(options)
19+
if err != nil {
20+
return nil, err
21+
}
22+
}
23+
24+
return options, nil
25+
}
26+
27+
// Progress is an option for Dag.Stat which returns progressive data while reading through the DAG
28+
func (dagOpts) Progress(progress bool) DagStatOption {
29+
return func(opts *DagStatSettings) error {
30+
opts.Progress = progress
31+
return nil
32+
}
33+
}

Diff for: shell_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -630,3 +630,33 @@ func TestSwarmPeeringAdd(t *testing.T) {
630630
_, err := s.SwarmPeeringAdd(context.Background(), addr)
631631
is.Nil(err)
632632
}
633+
634+
func TestDagStat(t *testing.T) {
635+
is := is.New(t)
636+
s := NewShell(shellUrl)
637+
638+
result, err := s.DagStat("QmUwp4xYq4pt1xavfCnpJ2aoVETf83AsvK3W8KvUGtyzFB")
639+
is.Nil(err)
640+
is.Equal(result.TotalSize, 3383728)
641+
642+
is.Equal(result.DagStatsArray[0].Cid.String(), "QmUwp4xYq4pt1xavfCnpJ2aoVETf83AsvK3W8KvUGtyzFB")
643+
is.Equal(result.UniqueBlocks, 15)
644+
is.Equal(result.redundantSize, 0)
645+
is.Equal(result.SharedSize, 0)
646+
is.Equal(result.Ratio, 1)
647+
}
648+
649+
func TestDagStatWithOpts(t *testing.T) {
650+
is := is.New(t)
651+
s := NewShell(shellUrl)
652+
653+
result, err := s.DagStatWithOpts("QmUwp4xYq4pt1xavfCnpJ2aoVETf83AsvK3W8KvUGtyzFB", options.Dag.Progress(true))
654+
is.Nil(err)
655+
is.Equal(result.TotalSize, 3383728)
656+
657+
is.Equal(result.DagStatsArray[0].Cid.String(), "QmUwp4xYq4pt1xavfCnpJ2aoVETf83AsvK3W8KvUGtyzFB")
658+
is.Equal(result.UniqueBlocks, 15)
659+
is.Equal(result.redundantSize, 0)
660+
is.Equal(result.SharedSize, 0)
661+
is.Equal(result.Ratio, 1)
662+
}

0 commit comments

Comments
 (0)