Skip to content

Commit

Permalink
storage: add client for /uploads endpoint
Browse files Browse the repository at this point in the history
Change-Id: Id9eecd9bc5bf7be4ffcdd5a120c7ccc4afe2fb17
Reviewed-on: https://go-review.googlesource.com/36115
Reviewed-by: Russ Cox <[email protected]>
  • Loading branch information
quentinmit committed Feb 2, 2017
1 parent 1aae1da commit b175a58
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
95 changes: 95 additions & 0 deletions storage/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package storage

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -111,4 +112,98 @@ func (q *Query) Close() error {
return q.err
}

// UploadInfo represents an upload summary.
type UploadInfo struct {
Count int
UploadID string
LabelValues benchfmt.Labels `json:",omitempty"`
}

// ListUploads searches for uploads containing results matching the given query string.
// The query may be empty, in which case all uploads will be returned.
// extraLabels specifies other labels to be retrieved.
// If limit is 0, no limit will be provided to the server.
// The uploads are returned starting with the most recent upload.
func (c *Client) ListUploads(q string, extraLabels []string, limit int) *UploadList {
hc := c.httpClient()

v := url.Values{"extra_label": extraLabels}
if q != "" {
v["q"] = []string{q}
}
if limit != 0 {
v["limit"] = []string{fmt.Sprintf("%d", limit)}
}

u := c.BaseURL + "/uploads"
if len(v) > 0 {
u += "?" + v.Encode()
}
resp, err := hc.Get(u)
if err != nil {
return &UploadList{err: err}
}
if resp.StatusCode != 200 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &UploadList{err: err}
}
return &UploadList{err: fmt.Errorf("%s", body)}
}
return &UploadList{body: resp.Body, dec: json.NewDecoder(resp.Body)}
}

// UploadList is the result of ListUploads.
// Use Next to advance through the rows, making sure to call Close when done:
//
// q := db.ListUploads("key:value")
// defer q.Close()
// for q.Next() {
// id, count := q.Row()
// labels := q.LabelValues()
// ...
// }
// err = q.Err() // get any error encountered during iteration
// ...
type UploadList struct {
body io.Closer
dec *json.Decoder
// from last call to Next
ui UploadInfo
err error
}

// Next prepares the next result for reading with the Result
// method. It returns false when there are no more results, either by
// reaching the end of the input or an error.
func (ul *UploadList) Next() bool {
if ul.err != nil {
return false
}
ul.err = ul.dec.Decode(&ul.ui)
return ul.err == nil
}

// Info returns the most recent UploadInfo generated by a call to Next.
func (ul *UploadList) Info() UploadInfo {
return ul.ui
}

// Err returns the error state of the query.
func (ul *UploadList) Err() error {
if ul.err == io.EOF {
return nil
}
return ul.err
}

// Close frees resources associated with the query.
func (ul *UploadList) Close() error {
if ul.body != nil {
return ul.body.Close()
ul.body = nil
}
return ul.Err()
}

// TODO(quentin): Move upload code here from cmd/benchsave?
26 changes: 26 additions & 0 deletions storage/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http/httptest"
"os"
"os/exec"
"reflect"
"testing"

"golang.org/x/perf/storage/benchfmt"
Expand Down Expand Up @@ -67,6 +68,31 @@ func TestQuery(t *testing.T) {
}
}

func TestListUploads(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if have, want := r.URL.RequestURI(), "/uploads?extra_label=key1&extra_label=key2&limit=10&q=key1%3Avalue+key2%3Avalue"; have != want {
t.Errorf("RequestURI = %q, want %q", have, want)
}
fmt.Fprintf(w, "%s\n", `{"UploadID": "id", "Count": 100, "LabelValues": {"key1": "value"}}`)
}))
defer ts.Close()

c := &Client{BaseURL: ts.URL}

r := c.ListUploads("key1:value key2:value", []string{"key1", "key2"}, 10)
defer r.Close()

if !r.Next() {
t.Errorf("Next = false, want true")
}
if have, want := r.Info(), (UploadInfo{Count: 100, UploadID: "id", LabelValues: benchfmt.Labels{"key1": "value"}}); !reflect.DeepEqual(have, want) {
t.Errorf("Info = %#v, want %#v", have, want)
}
if err := r.Err(); err != nil {
t.Fatalf("Err: %v", err)
}
}

// diff returns the output of unified diff on s1 and s2. If the result
// is non-empty, the strings differ or the diff command failed.
func diff(s1, s2 string) string {
Expand Down

0 comments on commit b175a58

Please sign in to comment.