|
9 | 9 | "strings"
|
10 | 10 |
|
11 | 11 | files "github.com/ipfs/boxo/files"
|
| 12 | + "github.com/ipfs/go-cid" |
12 | 13 | "github.com/ipfs/go-ipfs-api/options"
|
13 | 14 | )
|
14 | 15 |
|
@@ -37,6 +38,46 @@ type DagImportOutput struct {
|
37 | 38 | Stats *DagImportStats
|
38 | 39 | }
|
39 | 40 |
|
| 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 | + |
40 | 81 | func (s *Shell) DagGet(ref string, out interface{}) error {
|
41 | 82 | return s.Request("dag/get", ref).Exec(context.Background(), out)
|
42 | 83 | }
|
@@ -151,3 +192,45 @@ func dagToFilesReader(data interface{}) (*files.MultiFileReader, error) {
|
151 | 192 |
|
152 | 193 | return fileReader, nil
|
153 | 194 | }
|
| 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 | +} |
0 commit comments