Skip to content

Commit

Permalink
Merge pull request #2137 from peekeri/influx-2050
Browse files Browse the repository at this point in the history
Refactor Results to Response (#2050)
  • Loading branch information
pauldix committed Apr 4, 2015
2 parents fbca2b1 + 7046914 commit fb52703
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 71 deletions.
8 changes: 4 additions & 4 deletions client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ func queryDB(con *client.Client, cmd string) (res []client.Result, err error) {
Command: cmd,
Database: MyDB,
}
if results, err := con.Query(q); err == nil {
if results.Error() != nil {
return res, results.Error()
if response, err := con.Query(q); err == nil {
if response.Error() != nil {
return res, response.Error()
}
res = results.Results
res = response.Results
}
return
}
Expand Down
4 changes: 2 additions & 2 deletions client/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func ExampleClient_Query() {
Command: "select count(value) from shapes",
Database: "square_holes",
}
if results, err := con.Query(q); err == nil && results.Error() == nil {
log.Println(results.Results)
if response, err := con.Query(q); err == nil && response.Error() == nil {
log.Println(response.Results)
}
}

Expand Down
36 changes: 18 additions & 18 deletions client/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func NewClient(c Config) (*Client, error) {
return &client, nil
}

// Query sends a command to the server and returns the Results
func (c *Client) Query(q Query) (*Results, error) {
// Query sends a command to the server and returns the Response
func (c *Client) Query(q Query) (*Response, error) {
u := c.url

u.Path = "query"
Expand All @@ -78,20 +78,20 @@ func (c *Client) Query(q Query) (*Results, error) {
}
defer resp.Body.Close()

var results Results
var response Response
dec := json.NewDecoder(resp.Body)
dec.UseNumber()
err = dec.Decode(&results)
err = dec.Decode(&response)
if err != nil {
return nil, err
}
return &results, nil
return &response, nil
}

// Write takes BatchPoints and allows for writing of multiple points with defaults
// If successful, error is nil and Results is nil
// If an error occurs, Results may contain additional information if populated.
func (c *Client) Write(bp BatchPoints) (*Results, error) {
// If successful, error is nil and Response is nil
// If an error occurs, Response may contain additional information if populated.
func (c *Client) Write(bp BatchPoints) (*Response, error) {
c.url.Path = "write"

b, err := json.Marshal(&bp)
Expand All @@ -111,16 +111,16 @@ func (c *Client) Write(bp BatchPoints) (*Results, error) {
}
defer resp.Body.Close()

var results Results
var response Response
dec := json.NewDecoder(resp.Body)
dec.UseNumber()
err = dec.Decode(&results)
err = dec.Decode(&response)
if err != nil && err.Error() != "EOF" {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return &results, results.Error()
return &response, response.Error()
}

return nil, nil
Expand Down Expand Up @@ -217,14 +217,14 @@ func (r *Result) UnmarshalJSON(b []byte) error {
return nil
}

// Results represents a list of statement results.
type Results struct {
// Response represents a list of statement results.
type Response struct {
Results []Result
Err error
}

// MarshalJSON encodes the result into JSON.
func (r *Results) MarshalJSON() ([]byte, error) {
// MarshalJSON encodes the response into JSON.
func (r *Response) MarshalJSON() ([]byte, error) {
// Define a struct that outputs "error" as a string.
var o struct {
Results []Result `json:"results,omitempty"`
Expand All @@ -240,8 +240,8 @@ func (r *Results) MarshalJSON() ([]byte, error) {
return json.Marshal(&o)
}

// UnmarshalJSON decodes the data into the Results struct
func (r *Results) UnmarshalJSON(b []byte) error {
// UnmarshalJSON decodes the data into the Response struct
func (r *Response) UnmarshalJSON(b []byte) error {
var o struct {
Results []Result `json:"results,omitempty"`
Err string `json:"error,omitempty"`
Expand All @@ -262,7 +262,7 @@ func (r *Results) UnmarshalJSON(b []byte) error {

// Error returns the first error from any statement.
// Returns nil if no errors occurred on any statements.
func (r Results) Error() error {
func (r Response) Error() error {
if r.Err != nil {
return r.Err
}
Expand Down
6 changes: 3 additions & 3 deletions client/influxdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestClient_Ping(t *testing.T) {

func TestClient_Query(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var data influxdb.Results
var data influxdb.Response
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(data)
}))
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestClient_BasicAuth(t *testing.T) {

func TestClient_Write(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var data influxdb.Results
var data influxdb.Response
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(data)
}))
Expand All @@ -124,7 +124,7 @@ func TestClient_UserAgent(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
receivedUserAgent = r.UserAgent()

var data influxdb.Results
var data influxdb.Response
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(data)
}))
Expand Down
30 changes: 15 additions & 15 deletions cmd/influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,41 +271,41 @@ func (c *CommandLine) dump() {
}

func (c *CommandLine) executeQuery(query string) {
results, err := c.Client.Query(client.Query{Command: query, Database: c.Database})
response, err := c.Client.Query(client.Query{Command: query, Database: c.Database})
if err != nil {
fmt.Printf("ERR: %s\n", err)
return
}
c.FormatResults(results, os.Stdout)
if results.Error() != nil {
fmt.Printf("ERR: %s\n", results.Error())
c.FormatResponse(response, os.Stdout)
if response.Error() != nil {
fmt.Printf("ERR: %s\n", response.Error())
if c.Database == "" {
fmt.Println("Warning: It is possible this error is due to not setting a database.")
fmt.Println(`Please set a database with the command "use <database>".`)
}
}
}

func (c *CommandLine) FormatResults(results *client.Results, w io.Writer) {
func (c *CommandLine) FormatResponse(response *client.Response, w io.Writer) {
switch c.Format {
case "json":
c.writeJSON(results, w)
c.writeJSON(response, w)
case "csv":
c.writeCSV(results, w)
c.writeCSV(response, w)
case "column":
c.writeColumns(results, w)
c.writeColumns(response, w)
default:
fmt.Fprintf(w, "Unknown output format %q.\n", c.Format)
}
}

func (c *CommandLine) writeJSON(results *client.Results, w io.Writer) {
func (c *CommandLine) writeJSON(response *client.Response, w io.Writer) {
var data []byte
var err error
if c.Pretty {
data, err = json.MarshalIndent(results, "", " ")
data, err = json.MarshalIndent(response, "", " ")
} else {
data, err = json.Marshal(results)
data, err = json.Marshal(response)
}
if err != nil {
fmt.Fprintf(w, "Unable to parse json: %s\n", err)
Expand All @@ -314,9 +314,9 @@ func (c *CommandLine) writeJSON(results *client.Results, w io.Writer) {
fmt.Fprintln(w, string(data))
}

func (c *CommandLine) writeCSV(results *client.Results, w io.Writer) {
func (c *CommandLine) writeCSV(response *client.Response, w io.Writer) {
csvw := csv.NewWriter(w)
for _, result := range results.Results {
for _, result := range response.Results {
// Create a tabbed writer for each result as they won't always line up
rows := c.formatResults(result, "\t")
for _, r := range rows {
Expand All @@ -326,8 +326,8 @@ func (c *CommandLine) writeCSV(results *client.Results, w io.Writer) {
}
}

func (c *CommandLine) writeColumns(results *client.Results, w io.Writer) {
for _, result := range results.Results {
func (c *CommandLine) writeColumns(response *client.Response, w io.Writer) {
for _, result := range response.Results {
// Create a tabbed writer for each result a they won't always line up
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 1, '\t', 0)
Expand Down
4 changes: 2 additions & 2 deletions cmd/influxd/server_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1442,11 +1442,11 @@ func TestClientLibrary(t *testing.T) {
for _, q := range test.queries {
if q.query.Command != "" {
time.Sleep(500 * time.Millisecond)
queryResult, err := c.Query(q.query)
queryResponse, err := c.Query(q.query)
if q.err != errToString(err) {
t.Errorf("unexpected error. expected: %s, got %v", q.err, err)
}
jsonResult := mustMarshalJSON(queryResult)
jsonResult := mustMarshalJSON(queryResponse)
if q.expected != jsonResult {
t.Logf("query expected result: %s\n", q.expected)
t.Logf("query got result: %s\n", jsonResult)
Expand Down
10 changes: 5 additions & 5 deletions httpd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (h *Handler) serveQuery(w http.ResponseWriter, r *http.Request, user *influ
}

// if we're not chunking, this will be the in memory buffer for all results before sending to client
res := influxdb.Results{Results: make([]*influxdb.Result, 0)}
res := influxdb.Response{Results: make([]*influxdb.Result, 0)}
statusWritten := false

// pull all results from the channel
Expand Down Expand Up @@ -320,7 +320,7 @@ func (h *Handler) showMeasurements(db string, user *influxdb.User) ([]string, er
if err != nil {
return measurements, err
}
results := influxdb.Results{}
results := influxdb.Response{}

for r := range c {
results.Results = append(results.Results, r)
Expand Down Expand Up @@ -691,12 +691,12 @@ func httpError(w http.ResponseWriter, error string, pretty bool, code int) {
w.Header().Add("content-type", "application/json")
w.WriteHeader(code)

results := influxdb.Results{Err: errors.New(error)}
response := influxdb.Response{Err: errors.New(error)}
var b []byte
if pretty {
b, _ = json.MarshalIndent(results, "", " ")
b, _ = json.MarshalIndent(response, "", " ")
} else {
b, _ = json.Marshal(results)
b, _ = json.Marshal(response)
}
w.Write(b)
}
Expand Down
24 changes: 12 additions & 12 deletions httpd/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ func TestHandler_serveWriteSeriesNonZeroTime(t *testing.T) {
t.Errorf("unexpected status: %d", status)
}

r := &influxdb.Results{}
r := &influxdb.Response{}
if err := json.Unmarshal([]byte(body), r); err != nil {
t.Logf("query : %s\n", query)
t.Log(body)
Expand Down Expand Up @@ -1401,7 +1401,7 @@ func TestHandler_serveWriteSeriesZeroTime(t *testing.T) {
t.Errorf("unexpected status: %d", status)
}

r := &influxdb.Results{}
r := &influxdb.Response{}
if err := json.Unmarshal([]byte(body), r); err != nil {
t.Logf("query : %s\n", query)
t.Log(body)
Expand Down Expand Up @@ -1489,7 +1489,7 @@ func TestHandler_serveWriteSeriesBatch(t *testing.T) {
t.Errorf("unexpected status: %d", status)
}

r := &influxdb.Results{}
r := &influxdb.Response{}
if err := json.Unmarshal([]byte(body), r); err != nil {
t.Logf("query : %s\n", query)
t.Log(body)
Expand Down Expand Up @@ -1531,7 +1531,7 @@ func TestHandler_serveWriteSeriesFieldTypeConflict(t *testing.T) {
t.Errorf("unexpected status: %d", status)
}

r := &influxdb.Results{}
r := &influxdb.Response{}
if err := json.Unmarshal([]byte(body), r); err != nil {
t.Log(body)
t.Error(err)
Expand Down Expand Up @@ -1646,25 +1646,25 @@ func TestHandler_ChunkedResponses(t *testing.T) {
if err != nil {
t.Fatalf("error reading response: %s", err.Error())
}
results := &influxdb.Results{}
err = json.Unmarshal(chunk[0:n], results)
response := &influxdb.Response{}
err = json.Unmarshal(chunk[0:n], response)
if err != nil {
t.Fatalf("error unmarshaling resultsz: %s", err.Error())
}
if len(results.Results) != 1 {
t.Fatalf("didn't get 1 result: %s\n", mustMarshalJSON(results))
if len(response.Results) != 1 {
t.Fatalf("didn't get 1 result: %s\n", mustMarshalJSON(response))
}
if len(results.Results[0].Series) != 1 {
t.Fatalf("didn't get 1 series: %s\n", mustMarshalJSON(results))
if len(response.Results[0].Series) != 1 {
t.Fatalf("didn't get 1 series: %s\n", mustMarshalJSON(response))
}
var vals [][]interface{}
if i == 0 {
vals = [][]interface{}{{"2009-11-10T23:00:00Z", 100}}
} else {
vals = [][]interface{}{{"2009-11-10T23:30:00Z", 25}}
}
if mustMarshalJSON(vals) != mustMarshalJSON(results.Results[0].Series[0].Values) {
t.Fatalf("values weren't what was expected:\n exp: %s\n got: %s", mustMarshalJSON(vals), mustMarshalJSON(results.Results[0].Series[0].Values))
if mustMarshalJSON(vals) != mustMarshalJSON(response.Results[0].Series[0].Values) {
t.Fatalf("values weren't what was expected:\n exp: %s\n got: %s", mustMarshalJSON(vals), mustMarshalJSON(response.Results[0].Series[0].Values))
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3318,14 +3318,14 @@ func (r *Result) UnmarshalJSON(b []byte) error {
return nil
}

// Results represents a list of statement results.
type Results struct {
// Response represents a list of statement results.
type Response struct {
Results []*Result
Err error
}

// MarshalJSON encodes a Results struct into JSON.
func (r Results) MarshalJSON() ([]byte, error) {
// MarshalJSON encodes a Response struct into JSON.
func (r Response) MarshalJSON() ([]byte, error) {
// Define a struct that outputs "error" as a string.
var o struct {
Results []*Result `json:"results,omitempty"`
Expand All @@ -3341,8 +3341,8 @@ func (r Results) MarshalJSON() ([]byte, error) {
return json.Marshal(&o)
}

// UnmarshalJSON decodes the data into the Results struct
func (r *Results) UnmarshalJSON(b []byte) error {
// UnmarshalJSON decodes the data into the Response struct
func (r *Response) UnmarshalJSON(b []byte) error {
var o struct {
Results []*Result `json:"results,omitempty"`
Err string `json:"error,omitempty"`
Expand All @@ -3361,7 +3361,7 @@ func (r *Results) UnmarshalJSON(b []byte) error {

// Error returns the first error from any statement.
// Returns nil if no errors occurred on any statements.
func (r *Results) Error() error {
func (r *Response) Error() error {
if r.Err != nil {
return r.Err
}
Expand Down
6 changes: 3 additions & 3 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1945,12 +1945,12 @@ func (s *Server) MustWriteSeries(database, retentionPolicy string, points []infl
return index
}

func (s *Server) executeQuery(q *influxql.Query, db string, user *influxdb.User) influxdb.Results {
func (s *Server) executeQuery(q *influxql.Query, db string, user *influxdb.User) influxdb.Response {
results, err := s.ExecuteQuery(q, db, user, 10000)
if err != nil {
return influxdb.Results{Err: err}
return influxdb.Response{Err: err}
}
res := influxdb.Results{}
res := influxdb.Response{}
for r := range results {
l := len(res.Results)
if l == 0 {
Expand Down

0 comments on commit fb52703

Please sign in to comment.