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-12 23:30:03 +00:00
|
|
|
func (m *Handler) Proxy(ctx context.Context, params op.PostSourcesIDProxyParams) middleware.Responder {
|
2016-09-15 16:28:51 +00:00
|
|
|
query := params.Query.Query
|
|
|
|
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
|
|
|
|
|
|
|
results, err := response.Results()
|
|
|
|
if err != nil {
|
|
|
|
return op.NewPostSourcesIDProxyDefault(500)
|
|
|
|
}
|
|
|
|
|
2016-09-12 23:30:03 +00:00
|
|
|
res := &models.ProxyResponse{
|
|
|
|
Results: results,
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
}
|