2016-09-12 23:30:03 +00:00
|
|
|
package mock
|
2016-09-12 23:12:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/go-openapi/runtime/middleware"
|
|
|
|
"github.com/go-openapi/strfmt"
|
|
|
|
"github.com/influxdata/mrfusion"
|
|
|
|
"github.com/influxdata/mrfusion/models"
|
|
|
|
op "github.com/influxdata/mrfusion/restapi/operations"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-09-12 23:30:03 +00:00
|
|
|
type Handler struct {
|
2016-09-15 16:28:51 +00:00
|
|
|
Store mrfusion.ExplorationStore
|
|
|
|
TimeSeries mrfusion.TimeSeries
|
2016-09-12 23:12:31 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 23:30:03 +00:00
|
|
|
func NewHandler() Handler {
|
|
|
|
return Handler{
|
|
|
|
DefaultExplorationStore,
|
2016-09-15 16:28:51 +00:00
|
|
|
DefaultTimeSeries,
|
2016-09-12 23:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 02:08:32 +00:00
|
|
|
func sampleSource() *models.Source {
|
|
|
|
name := "muh name"
|
|
|
|
influxType := "influx-enterprise"
|
|
|
|
|
|
|
|
return &models.Source{
|
|
|
|
ID: "1",
|
|
|
|
Links: &models.SourceLinks{
|
|
|
|
Self: "/chronograf/v1/sources/1",
|
|
|
|
Proxy: "/chronograf/v1/sources/1/proxy",
|
|
|
|
},
|
|
|
|
Name: &name,
|
|
|
|
Type: &influxType,
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Handler) Sources(ctx context.Context, params op.GetSourcesParams) middleware.Responder {
|
|
|
|
res := &models.Sources{
|
|
|
|
Sources: []*models.Source{
|
|
|
|
sampleSource(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return op.NewGetSourcesOK().WithPayload(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Handler) SourcesID(ctx context.Context, params op.GetSourcesIDParams) middleware.Responder {
|
|
|
|
if params.ID != "1" {
|
|
|
|
return op.NewGetSourcesIDNotFound()
|
|
|
|
}
|
|
|
|
return op.NewGetSourcesIDOK().WithPayload(sampleSource())
|
|
|
|
}
|
|
|
|
|
2016-09-12 23:30:03 +00:00
|
|
|
func (m *Handler) Proxy(ctx context.Context, params op.PostSourcesIDProxyParams) middleware.Responder {
|
2016-09-21 15:47:01 +00:00
|
|
|
query := mrfusion.Query{
|
2016-09-21 22:11:29 +00:00
|
|
|
Command: *params.Query.Query,
|
2016-09-21 22:37:01 +00:00
|
|
|
DB: params.Query.Db,
|
2016-09-21 22:11:29 +00:00
|
|
|
RP: params.Query.Rp,
|
2016-09-21 15:47:01 +00:00
|
|
|
}
|
|
|
|
response, err := m.TimeSeries.Query(ctx, mrfusion.Query(query))
|
2016-09-12 23:30:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return op.NewPostSourcesIDProxyDefault(500)
|
|
|
|
}
|
2016-09-15 16:28:51 +00:00
|
|
|
|
2016-09-12 23:30:03 +00:00
|
|
|
res := &models.ProxyResponse{
|
2016-09-21 22:03:07 +00:00
|
|
|
Results: response,
|
2016-09-12 23:30:03 +00:00
|
|
|
}
|
|
|
|
return op.NewPostSourcesIDProxyOK().WithPayload(res)
|
|
|
|
}
|
|
|
|
|
2016-09-15 16:28:51 +00:00
|
|
|
func (m *Handler) MonitoredServices(ctx context.Context, params op.GetSourcesIDMonitoredParams) middleware.Responder {
|
|
|
|
srvs, err := m.TimeSeries.MonitoredServices(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return op.NewGetSourcesIDMonitoredDefault(500)
|
|
|
|
}
|
|
|
|
res := &models.Services{}
|
|
|
|
for _, s := range srvs {
|
|
|
|
res.Services = append(res.Services, &models.Service{
|
|
|
|
TagKey: s.TagKey,
|
|
|
|
TagValue: s.TagValue,
|
|
|
|
Type: s.Type,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return op.NewGetSourcesIDMonitoredOK().WithPayload(res)
|
|
|
|
}
|
|
|
|
|
2016-09-12 23:30:03 +00:00
|
|
|
func (m *Handler) Explorations(ctx context.Context, params op.GetSourcesIDUsersUserIDExplorationsParams) middleware.Responder {
|
2016-09-12 23:12:31 +00:00
|
|
|
id, err := strconv.Atoi(params.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return op.NewGetSourcesIDUsersUserIDExplorationsDefault(500)
|
|
|
|
}
|
|
|
|
exs, err := m.Store.Query(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return op.NewGetSourcesIDUsersUserIDExplorationsNotFound()
|
|
|
|
}
|
|
|
|
res := &models.Explorations{}
|
|
|
|
for _, e := range exs {
|
|
|
|
res.Explorations = append(res.Explorations, &models.Exploration{
|
|
|
|
Data: e.Data,
|
|
|
|
Name: e.Name,
|
|
|
|
UpdatedAt: strfmt.DateTime(e.UpdatedAt),
|
|
|
|
CreatedAt: strfmt.DateTime(e.CreatedAt),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return op.NewGetSourcesIDUsersUserIDExplorationsOK().WithPayload(res)
|
|
|
|
}
|
|
|
|
|
2016-09-12 23:30:03 +00:00
|
|
|
func (m *Handler) Exploration(ctx context.Context, params op.GetSourcesIDUsersUserIDExplorationsExplorationIDParams) middleware.Responder {
|
2016-09-12 23:12:31 +00:00
|
|
|
eID, err := strconv.Atoi(params.ExplorationID)
|
|
|
|
if err != nil {
|
|
|
|
return op.NewGetSourcesIDUsersUserIDExplorationsExplorationIDDefault(500)
|
|
|
|
}
|
|
|
|
|
|
|
|
e, err := m.Store.Get(ctx, eID)
|
|
|
|
if err != nil {
|
|
|
|
return op.NewGetSourcesIDUsersUserIDExplorationsExplorationIDNotFound()
|
|
|
|
}
|
|
|
|
res := &models.Exploration{
|
|
|
|
Data: e.Data,
|
|
|
|
Name: e.Name,
|
|
|
|
UpdatedAt: strfmt.DateTime(e.UpdatedAt),
|
|
|
|
CreatedAt: strfmt.DateTime(e.CreatedAt),
|
|
|
|
}
|
|
|
|
return op.NewGetSourcesIDUsersUserIDExplorationsExplorationIDOK().WithPayload(res)
|
|
|
|
}
|
|
|
|
|
2016-09-12 23:30:03 +00:00
|
|
|
func (m *Handler) UpdateExploration(ctx context.Context, params op.PatchSourcesIDUsersUserIDExplorationsExplorationIDParams) middleware.Responder {
|
2016-09-12 23:12:31 +00:00
|
|
|
eID, err := strconv.Atoi(params.ExplorationID)
|
|
|
|
if err != nil {
|
|
|
|
return op.NewPatchSourcesIDUsersUserIDExplorationsExplorationIDDefault(500)
|
|
|
|
}
|
|
|
|
|
|
|
|
e, err := m.Store.Get(ctx, eID)
|
|
|
|
if err != nil {
|
|
|
|
return op.NewPatchSourcesIDUsersUserIDExplorationsExplorationIDNotFound()
|
|
|
|
}
|
|
|
|
if params.Exploration != nil {
|
|
|
|
e.Data = params.Exploration.Data.(string)
|
|
|
|
e.Name = params.Exploration.Name
|
|
|
|
m.Store.Update(ctx, e)
|
|
|
|
}
|
|
|
|
return op.NewPatchSourcesIDUsersUserIDExplorationsExplorationIDNoContent()
|
|
|
|
}
|
|
|
|
|
2016-09-12 23:30:03 +00:00
|
|
|
func (m *Handler) NewExploration(ctx context.Context, params op.PostSourcesIDUsersUserIDExplorationsParams) middleware.Responder {
|
2016-09-12 23:12:31 +00:00
|
|
|
id, err := strconv.Atoi(params.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return op.NewPostSourcesIDUsersUserIDExplorationsDefault(500)
|
|
|
|
}
|
|
|
|
|
|
|
|
exs, err := m.Store.Query(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return op.NewPostSourcesIDUsersUserIDExplorationsNotFound()
|
|
|
|
}
|
|
|
|
eID := len(exs)
|
|
|
|
|
|
|
|
if params.Exploration != nil {
|
|
|
|
e := mrfusion.Exploration{
|
|
|
|
Data: params.Exploration.Data.(string),
|
|
|
|
Name: params.Exploration.Name,
|
|
|
|
ID: eID,
|
|
|
|
}
|
|
|
|
m.Store.Add(ctx, e)
|
|
|
|
}
|
|
|
|
return op.NewPostSourcesIDUsersUserIDExplorationsCreated()
|
|
|
|
}
|
|
|
|
|
2016-09-12 23:30:03 +00:00
|
|
|
func (m *Handler) DeleteExploration(ctx context.Context, params op.DeleteSourcesIDUsersUserIDExplorationsExplorationIDParams) middleware.Responder {
|
2016-09-12 23:12:31 +00:00
|
|
|
ID, err := strconv.Atoi(params.ExplorationID)
|
|
|
|
if err != nil {
|
|
|
|
return op.NewDeleteSourcesIDUsersUserIDExplorationsExplorationIDDefault(500)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.Store.Delete(ctx, mrfusion.Exploration{ID: ID}); err != nil {
|
|
|
|
return op.NewDeleteSourcesIDUsersUserIDExplorationsExplorationIDNotFound()
|
|
|
|
}
|
|
|
|
return op.NewDeleteSourcesIDUsersUserIDExplorationsExplorationIDNoContent()
|
|
|
|
}
|