Skip to content

Commit

Permalink
Merge branch 'master' into inspect-verify-tombstone
Browse files Browse the repository at this point in the history
  • Loading branch information
serenibyss committed Jun 10, 2021
2 parents f2363e1 + fa31037 commit 0591683
Show file tree
Hide file tree
Showing 8 changed files with 1,004 additions and 40 deletions.
16 changes: 8 additions & 8 deletions annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,24 @@ type AnnotationService interface {
// ListAnnotations lists all annotations matching the filter.
ListAnnotations(ctx context.Context, orgID platform.ID, filter AnnotationListFilter) (ReadAnnotations, error)
// GetAnnotation gets an annotation by id.
GetAnnotation(ctx context.Context, orgID, id platform.ID) (*AnnotationEvent, error)
GetAnnotation(ctx context.Context, id platform.ID) (*AnnotationEvent, error)
// DeleteAnnotations deletes annotations matching the filter.
DeleteAnnotations(ctx context.Context, orgID platform.ID, delete AnnotationDeleteFilter) error
// DeleteAnnotation deletes an annotation by id.
DeleteAnnotation(ctx context.Context, orgID, id platform.ID) error
DeleteAnnotation(ctx context.Context, id platform.ID) error
// UpdateAnnotation updates an annotation.
UpdateAnnotation(ctx context.Context, orgID, id platform.ID, update AnnotationCreate) (*AnnotationEvent, error)
UpdateAnnotation(ctx context.Context, id platform.ID, update AnnotationCreate) (*AnnotationEvent, error)

// ListStreams lists all streams matching the filter.
ListStreams(ctx context.Context, orgID platform.ID, filter StreamListFilter) ([]ReadStream, error)
// CreateOrUpdateStream creates or updates the matching stream by name.
CreateOrUpdateStream(ctx context.Context, orgID platform.ID, stream Stream) (*ReadStream, error)
CreateOrUpdateStream(ctx context.Context, stream Stream) (*ReadStream, error)
// UpdateStream updates the stream by the ID.
UpdateStream(ctx context.Context, orgID, id platform.ID, stream Stream) (*ReadStream, error)
// DeleteStream deletes the stream metadata by name.
DeleteStream(ctx context.Context, orgID platform.ID, streamName string) error
UpdateStream(ctx context.Context, id platform.ID, stream Stream) (*ReadStream, error)
// DeleteStreams deletes one or more streams by name.
DeleteStreams(ctx context.Context, delete BasicStream) error
// DeleteStreamByID deletes the stream metadata by id.
DeleteStreamByID(ctx context.Context, orgID, id platform.ID) error
DeleteStreamByID(ctx context.Context, id platform.ID) error
}

// AnnotationEvent contains fields for annotating an event.
Expand Down
236 changes: 236 additions & 0 deletions annotations/transport/annotations_router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package transport

import (
"encoding/json"
"net/http"
"time"

"github.com/go-chi/chi"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kit/platform"
)

func (h *AnnotationHandler) annotationsRouter() http.Handler {
r := chi.NewRouter()

r.Post("/", h.handleCreateAnnotations)
r.Get("/", h.handleGetAnnotations)
r.Delete("/", h.handleDeleteAnnotations)

r.Route("/{id}", func(r chi.Router) {
r.Get("/", h.handleGetAnnotation)
r.Delete("/", h.handleDeleteAnnotation)
r.Put("/", h.handleUpdateAnnotation)
})

return r
}

func (h *AnnotationHandler) handleCreateAnnotations(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

o, err := platform.IDFromString(r.URL.Query().Get("orgID"))
if err != nil {
h.api.Err(w, r, errBadOrg)
return
}

c, err := decodeCreateAnnotationsRequest(r)
if err != nil {
h.api.Err(w, r, err)
return
}

l, err := h.annotationService.CreateAnnotations(ctx, *o, c)
if err != nil {
h.api.Err(w, r, err)
return
}

h.api.Respond(w, r, http.StatusOK, l)
}

func (h *AnnotationHandler) handleGetAnnotations(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

o, err := platform.IDFromString(r.URL.Query().Get("orgID"))
if err != nil {
h.api.Err(w, r, errBadOrg)
return
}

f, err := decodeListAnnotationsRequest(r)
if err != nil {
h.api.Err(w, r, err)
return
}

l, err := h.annotationService.ListAnnotations(ctx, *o, *f)
if err != nil {
h.api.Err(w, r, err)
return
}

h.api.Respond(w, r, http.StatusOK, l)
}

func (h *AnnotationHandler) handleDeleteAnnotations(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

o, err := platform.IDFromString(r.URL.Query().Get("orgID"))
if err != nil {
h.api.Err(w, r, errBadOrg)
return
}

f, err := decodeDeleteAnnotationsRequest(r)
if err != nil {
h.api.Err(w, r, err)
return
}

if err = h.annotationService.DeleteAnnotations(ctx, *o, *f); err != nil {
h.api.Err(w, r, err)
return
}

h.api.Respond(w, r, http.StatusNoContent, nil)
}

func (h *AnnotationHandler) handleGetAnnotation(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

id, err := platform.IDFromString(chi.URLParam(r, "id"))
if err != nil {
h.api.Err(w, r, errBadAnnotationId)
return
}

a, err := h.annotationService.GetAnnotation(ctx, *id)
if err != nil {
h.api.Err(w, r, err)
return
}

h.api.Respond(w, r, http.StatusOK, a)
}

func (h *AnnotationHandler) handleDeleteAnnotation(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

id, err := platform.IDFromString(chi.URLParam(r, "id"))
if err != nil {
h.api.Err(w, r, errBadAnnotationId)
return
}

if err := h.annotationService.DeleteAnnotation(ctx, *id); err != nil {
h.api.Err(w, r, err)
return
}

h.api.Respond(w, r, http.StatusNoContent, nil)
}

func (h *AnnotationHandler) handleUpdateAnnotation(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

id, err := platform.IDFromString(chi.URLParam(r, "id"))
if err != nil {
h.api.Err(w, r, errBadAnnotationId)
return
}

u, err := decodeUpdateAnnotationRequest(r)
if err != nil {
h.api.Err(w, r, err)
return
}

a, err := h.annotationService.UpdateAnnotation(ctx, *id, *u)
if err != nil {
h.api.Err(w, r, err)
return
}

h.api.Respond(w, r, http.StatusOK, a)
}

func decodeCreateAnnotationsRequest(r *http.Request) ([]influxdb.AnnotationCreate, error) {
cs := []influxdb.AnnotationCreate{}
if err := json.NewDecoder(r.Body).Decode(&cs); err != nil {
return nil, err
}

for _, c := range cs {
if err := c.Validate(time.Now); err != nil {
return nil, err
}
}

return cs, nil
}

func decodeListAnnotationsRequest(r *http.Request) (*influxdb.AnnotationListFilter, error) {
startTime, endTime, err := tFromReq(r)
if err != nil {
return nil, err
}

f := &influxdb.AnnotationListFilter{
StreamIncludes: r.URL.Query()["streamIncludes"],
BasicFilter: influxdb.BasicFilter{
EndTime: endTime,
StartTime: startTime,
},
}
f.SetStickerIncludes(r.URL.Query())
if err := f.Validate(time.Now); err != nil {
return nil, err
}

return f, nil
}

func decodeDeleteAnnotationsRequest(r *http.Request) (*influxdb.AnnotationDeleteFilter, error) {
// Try to get a stream ID from the query params. The stream ID is not required,
// so if one is not set we can leave streamID as the zero value.
var streamID platform.ID
if qid := chi.URLParam(r, "streamID"); qid != "" {
id, err := platform.IDFromString(qid)
// if a streamID parameter was provided but is not valid, return an error
if err != nil {
return nil, errBadStreamId
}
streamID = *id
}

startTime, endTime, err := tFromReq(r)
if err != nil {
return nil, err
}

f := &influxdb.AnnotationDeleteFilter{
StreamTag: r.URL.Query().Get("stream"),
StreamID: streamID,
EndTime: endTime,
StartTime: startTime,
}
f.SetStickers(r.URL.Query())
if err := f.Validate(); err != nil {
return nil, err
}

return f, nil
}

func decodeUpdateAnnotationRequest(r *http.Request) (*influxdb.AnnotationCreate, error) {
u := &influxdb.AnnotationCreate{}
if err := json.NewDecoder(r.Body).Decode(u); err != nil {
return nil, err
} else if err := u.Validate(time.Now); err != nil {
return nil, err
}

return u, nil
}
Loading

0 comments on commit 0591683

Please sign in to comment.