2017-02-17 20:02:02 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/influxdata/chronograf"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ValidInfluxRequest checks if queries specify a command.
|
|
|
|
func ValidInfluxRequest(p chronograf.Query) error {
|
|
|
|
if p.Command == "" {
|
|
|
|
return fmt.Errorf("query field required")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type postInfluxResponse struct {
|
|
|
|
Results interface{} `json:"results"` // results from influx
|
|
|
|
}
|
|
|
|
|
|
|
|
// Influx proxies requests to infludb.
|
|
|
|
func (h *Service) Influx(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id, err := paramID("id", r)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var req chronograf.Query
|
|
|
|
if err = json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
invalidJSON(w, h.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = ValidInfluxRequest(req); err != nil {
|
|
|
|
invalidData(w, err, h.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
src, err := h.SourcesStore.Get(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
notFound(w, id, h.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-24 03:54:20 +00:00
|
|
|
ts, err := h.TimeSeries(src)
|
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("Unable to connect to source %d", id)
|
|
|
|
Error(w, http.StatusBadRequest, msg, h.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = ts.Connect(ctx, &src); err != nil {
|
2017-02-17 20:02:02 +00:00
|
|
|
msg := fmt.Sprintf("Unable to connect to source %d", id)
|
|
|
|
Error(w, http.StatusBadRequest, msg, h.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-24 03:54:20 +00:00
|
|
|
response, err := ts.Query(ctx, req)
|
2017-02-17 20:02:02 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == chronograf.ErrUpstreamTimeout {
|
|
|
|
msg := "Timeout waiting for Influx response"
|
|
|
|
Error(w, http.StatusRequestTimeout, msg, h.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO: Here I want to return the error code from influx.
|
|
|
|
Error(w, http.StatusBadRequest, err.Error(), h.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := postInfluxResponse{
|
|
|
|
Results: response,
|
|
|
|
}
|
|
|
|
encodeJSON(w, http.StatusOK, res, h.Logger)
|
|
|
|
}
|