Skip to content

Commit

Permalink
fix(stats): gets stats with no structure
Browse files Browse the repository at this point in the history
Hook the `lib.Get` (which fetches `Stats` if `Selection="stats"`, into the `statsHandler`. `lib.Get` correctly fetches fsi datasets as well as versioned datasets.
If we don't have a structure, such as a case where we are using `fsi` and the user has removed their `structure.json`, stats should detect a structure and use that detected structure.schema to calculate stats.
  • Loading branch information
ramfox committed Nov 14, 2019
1 parent df5bedc commit a2046d6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
17 changes: 12 additions & 5 deletions api/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,17 +653,24 @@ func (h DatasetHandlers) bodyHandler(w http.ResponseWriter, r *http.Request) {
}

func (h DatasetHandlers) statsHandler(w http.ResponseWriter, r *http.Request) {
p := &lib.StatsParams{
Ref: HTTPPathToQriPath(r.URL.Path[len("/stats/"):]),
p := lib.GetParams{
Path: HTTPPathToQriPath(r.URL.Path[len("/stats/"):]),
UseFSI: r.FormValue("fsi") == "true",
Selector: "stats",
}
res := &lib.StatsResponse{}
if err := h.Stats(p, res); err != nil {
res := lib.GetResult{}
err := h.Get(&p, &res)
if err != nil {
if err == repo.ErrNoHistory || err == fsi.ErrNoLink {
util.WriteErrResponse(w, http.StatusUnprocessableEntity, err)
return
}
util.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}

statsMap := &[]map[string]interface{}{}
if err := json.Unmarshal(res.StatsBytes, statsMap); err != nil {
if err := json.Unmarshal(res.Bytes, statsMap); err != nil {
log.Errorf("error unmarshalling stats: %s", err)
util.WriteErrResponse(w, http.StatusInternalServerError, fmt.Errorf("error writing stats"))
return
Expand Down
16 changes: 16 additions & 0 deletions lib/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"io/ioutil"
"net/rpc"
"os"
"path/filepath"

"github.com/ghodss/yaml"
"github.com/qri-io/dag"
"github.com/qri-io/dataset"
"github.com/qri-io/dataset/detect"
"github.com/qri-io/jsonschema"
"github.com/qri-io/qfs"
"github.com/qri-io/qri/base"
Expand Down Expand Up @@ -901,6 +903,20 @@ func (r *DatasetRequests) Stats(p *StatsParams, res *StatsResponse) (err error)
return
}
}
if p.Dataset.Structure == nil || p.Dataset.Structure.IsEmpty() {
p.Dataset.Structure = &dataset.Structure{}
p.Dataset.Structure.Format = filepath.Ext(p.Dataset.BodyFile().FileName())
p.Dataset.Structure.Schema, _, err = detect.Schema(p.Dataset.Structure, p.Dataset.BodyFile())
if err != nil {
return err
}
// TODO (ramfox): this feels gross, but since we consume the reader when
// detecting the schema, we need to open up the file again, since we don't
// have the option to seek back to the front
if err = p.Dataset.OpenBodyFile(ctx, r.node.Repo.Filesystem()); err != nil {
return err
}
}
reader, err := r.inst.stats.JSON(ctx, p.Dataset)
if err != nil {
return err
Expand Down

0 comments on commit a2046d6

Please sign in to comment.