Skip to content

Commit

Permalink
fix(list): Flag --num-versions shows number of versions of each dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Feb 25, 2019
1 parent 7fc0f8d commit 2f5a8b1
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 56 deletions.
4 changes: 2 additions & 2 deletions actions/list_datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// ListDatasets lists a peer's datasets
func ListDatasets(node *p2p.QriNode, ds *repo.DatasetRef, limit, offset int, RPC, publishedOnly bool) (res []repo.DatasetRef, err error) {
func ListDatasets(node *p2p.QriNode, ds *repo.DatasetRef, limit, offset int, RPC, publishedOnly, showVersions bool) (res []repo.DatasetRef, err error) {
r := node.Repo
pro, err := r.Profile()
if err != nil {
Expand Down Expand Up @@ -74,5 +74,5 @@ func ListDatasets(node *p2p.QriNode, ds *repo.DatasetRef, limit, offset int, RPC
return
}

return base.ListDatasets(node.Repo, limit, offset, RPC, publishedOnly)
return base.ListDatasets(node.Repo, limit, offset, RPC, publishedOnly, showVersions)
}
24 changes: 22 additions & 2 deletions actions/list_datasets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@ func TestListDatasets(t *testing.T) {
node := newTestNode(t)
addCitiesDataset(t, node)

res, err := ListDatasets(node, &repo.DatasetRef{Peername: "me"}, 1, 0, false, false)
res, err := ListDatasets(node, &repo.DatasetRef{Peername: "me"}, 1, 0, false, false, false)
if err != nil {
t.Error(err.Error())
}

if len(res) != 1 {
t.Error("expected one dataset response")
}
if res[0].Dataset.NumVersions != 0 {
t.Error("expected no versions were requested")
}
}

func TestListDatasetsNotFound(t *testing.T) {
node := newTestNode(t)
addCitiesDataset(t, node)

_, err := ListDatasets(node, &repo.DatasetRef{Peername: "not_found"}, 1, 0, false, false)
_, err := ListDatasets(node, &repo.DatasetRef{Peername: "not_found"}, 1, 0, false, false, false)
if err == nil {
t.Error("expected to get error")
}
Expand All @@ -33,3 +36,20 @@ func TestListDatasetsNotFound(t *testing.T) {
t.Errorf("expected error \"%s\", got \"%s\"", expect, err.Error())
}
}

func TestListDatasetsWithVersions(t *testing.T) {
node := newTestNode(t)
addCitiesDataset(t, node)

res, err := ListDatasets(node, &repo.DatasetRef{Peername: "me"}, 1, 0, false, false, true)
if err != nil {
t.Error(err.Error())
}

if len(res) != 1 {
t.Error("expected one dataset response")
}
if res[0].Dataset.NumVersions != 1 {
t.Error("expected one version")
}
}
2 changes: 0 additions & 2 deletions api/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func TestRegistryHandlers(t *testing.T) {
runHandlerTestCases(t, "registryDatasets", h.RegistryDatasetsHandler, registryDatasetsCases)
}


func TestRegistryGet(t *testing.T) {
node, teardown := newTestNodeWithNumDatasets(t, 1)
defer teardown()
Expand All @@ -51,7 +50,6 @@ func TestRegistryGet(t *testing.T) {
}
}


func TestRegistryGetNotFound(t *testing.T) {
node, teardown := newTestNodeWithNumDatasets(t, 1)
defer teardown()
Expand Down
10 changes: 9 additions & 1 deletion base/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func CloseDataset(ds *dataset.Dataset) (err error) {
}

// ListDatasets lists datasets from a repo
func ListDatasets(r repo.Repo, limit, offset int, RPC, publishedOnly bool) (res []repo.DatasetRef, err error) {
func ListDatasets(r repo.Repo, limit, offset int, RPC, publishedOnly, showVersions bool) (res []repo.DatasetRef, err error) {
store := r.Store()
res, err = r.References(limit, offset)
if err != nil {
Expand Down Expand Up @@ -99,6 +99,14 @@ func ListDatasets(r repo.Repo, limit, offset int, RPC, publishedOnly bool) (res
if RPC {
res[i].Dataset.Structure.Schema = nil
}

if showVersions {
dsVersions, err := DatasetLog(r, ref, 0, 0, false)
if err != nil {
return nil, err
}
res[i].Dataset.NumVersions = len(dsVersions)
}
}

// TODO: If renames.Renames is non-empty, apply it to r
Expand Down
6 changes: 3 additions & 3 deletions base/dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ func TestListDatasets(t *testing.T) {
r := newTestRepo(t)
ref := addCitiesDataset(t, r)

res, err := ListDatasets(r, 1, 0, false, false)
res, err := ListDatasets(r, 1, 0, false, false, false)
if err != nil {
t.Error(err.Error())
}
if len(res) != 1 {
t.Error("expected one dataset response")
}

res, err = ListDatasets(r, 1, 0, false, true)
res, err = ListDatasets(r, 1, 0, false, true, false)
if err != nil {
t.Error(err.Error())
}
Expand All @@ -46,7 +46,7 @@ func TestListDatasets(t *testing.T) {
t.Fatal(err)
}

res, err = ListDatasets(r, 1, 0, false, true)
res, err = ListDatasets(r, 1, 0, false, true, false)
if err != nil {
t.Error(err.Error())
}
Expand Down
75 changes: 33 additions & 42 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ must have ` + "`qri connect`" + ` running in a separate terminal window.`,
cmd.Flags().IntVarP(&o.Limit, "limit", "l", 25, "limit results, default 25")
cmd.Flags().IntVarP(&o.Offset, "offset", "o", 0, "offset results, default 0")
cmd.Flags().BoolVarP(&o.Published, "published", "p", false, "list only published datasets")
cmd.Flags().BoolVarP(&o.NumVersions, "num-versions", "n", false, "show number of versions")

return cmd
}
Expand All @@ -59,11 +60,12 @@ must have ` + "`qri connect`" + ` running in a separate terminal window.`,
type ListOptions struct {
ioes.IOStreams

Format string
Limit int
Offset int
Peername string
Published bool
Format string
Limit int
Offset int
Peername string
Published bool
NumVersions bool

DatasetRequests *lib.DatasetRequests
}
Expand All @@ -79,32 +81,20 @@ func (o *ListOptions) Complete(f Factory, args []string) (err error) {

// Run executes the list command
func (o *ListOptions) Run() (err error) {

refs := []repo.DatasetRef{}

if o.Peername == "" {

p := &lib.ListParams{
Limit: o.Limit,
Offset: o.Offset,
Published: o.Published,
Limit: o.Limit,
Offset: o.Offset,
Published: o.Published,
NumVersions: o.NumVersions,
}
refs := []repo.DatasetRef{}
if err = o.DatasetRequests.List(p, &refs); err != nil {
return err
}

switch o.Format {
case "":
for i, ref := range refs {
printDatasetRefInfo(o.Out, i+1, ref)
}
case dataset.JSONDataFormat.String():
data, err := json.MarshalIndent(refs, "", " ")
if err != nil {
return err
}
fmt.Fprintf(o.Out, "%s\n", string(data))
default:
return fmt.Errorf("unrecognized format: %s", o.Format)
}
} else {
// if user provides "me/my_dataset", split into peername="me" and name="my_dataset"
peername := o.Peername
Expand All @@ -114,13 +104,14 @@ func (o *ListOptions) Run() (err error) {
peername = parts[0]
dsName = parts[1]
}

// TODO: It would be a bit more efficient to pass dsName to the ListParams
// and only retrieve information about that one dataset.
p := &lib.ListParams{
Peername: peername,
Limit: o.Limit,
Offset: o.Offset,
Peername: peername,
Limit: o.Limit,
Offset: o.Offset,
NumVersions: o.NumVersions,
}
refs := []repo.DatasetRef{}
if err = o.DatasetRequests.List(p, &refs); err != nil {
return err
}
Expand Down Expand Up @@ -148,21 +139,21 @@ func (o *ListOptions) Run() (err error) {
}
return
}
}

switch o.Format {
case "":
for i, ref := range refs {
printDatasetRefInfo(o.Out, i+1, ref)
}
case dataset.JSONDataFormat.String():
data, err := json.MarshalIndent(refs, "", " ")
if err != nil {
return err
}
fmt.Fprintf(o.Out, "%s\n", string(data))
default:
return fmt.Errorf("unrecognized format: %s", o.Format)
switch o.Format {
case "":
for i, ref := range refs {
printDatasetRefInfo(o.Out, i+1, ref)
}
case dataset.JSONDataFormat.String():
data, err := json.MarshalIndent(refs, "", " ")
if err != nil {
return err
}
fmt.Fprintf(o.Out, "%s\n", string(data))
default:
return fmt.Errorf("unrecognized format: %s", o.Format)
}

return nil
Expand Down
21 changes: 19 additions & 2 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,27 @@ func printDatasetRefInfo(w io.Writer, i int, ref repo.DatasetRef) {
fmt.Fprintf(w, " %s\n", ref.Path)
}
if ds != nil && ds.Structure != nil {
fmt.Fprintf(w, " %s, %d entries, %d errors", printByteInfo(ds.Structure.Length), ds.Structure.Entries, ds.Structure.ErrCount)
fmt.Fprintf(w, " %s", printByteInfo(ds.Structure.Length))
if ds.Structure.Entries == 1 {
fmt.Fprintf(w, ", %d entry", ds.Structure.Entries)
} else {
fmt.Fprintf(w, ", %d entries", ds.Structure.Entries)
}
if ds.Structure.ErrCount == 1 {
fmt.Fprintf(w, ", %d error", ds.Structure.ErrCount)
} else {
fmt.Fprintf(w, ", %d errors", ds.Structure.ErrCount)
}
if ds.NumVersions == 0 {
// nothing
} else if ds.NumVersions == 1 {
fmt.Fprintf(w, ", %d version", ds.NumVersions)
} else {
fmt.Fprintf(w, ", %d versions", ds.NumVersions)
}
}

fmt.Fprintln(w, "")
fmt.Fprintf(w, "\n")
}

func printSearchResult(w io.Writer, i int, result lib.SearchResult) {
Expand Down
2 changes: 1 addition & 1 deletion lib/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (r *DatasetRequests) List(p *ListParams, res *[]repo.DatasetRef) error {
p.Offset = 0
}

replies, err := actions.ListDatasets(r.node, ds, p.Limit, p.Offset, p.RPC, p.Published)
replies, err := actions.ListDatasets(r.node, ds, p.Limit, p.Offset, p.RPC, p.Published, p.NumVersions)

*res = replies
return err
Expand Down
2 changes: 2 additions & 0 deletions lib/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type ListParams struct {
RPC bool
// Published only applies to listing datasets
Published bool
// NumVersions only applies to listing datasets
NumVersions bool
}

// NewListParams creates a ListParams from page & pagesize, pages are 1-indexed
Expand Down
2 changes: 1 addition & 1 deletion p2p/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (n *QriNode) handleDatasetsList(ws *WrappedStream, msg Message) (hangup boo
dlp.Limit = listMax
}

refs, err := base.ListDatasets(n.Repo, dlp.Limit, dlp.Offset, false, true)
refs, err := base.ListDatasets(n.Repo, dlp.Limit, dlp.Offset, false, true, false)
if err != nil {
log.Error(err)
return
Expand Down

0 comments on commit 2f5a8b1

Please sign in to comment.