Skip to content

Commit

Permalink
update store/event/event.go (chaos-mesh#526)
Browse files Browse the repository at this point in the history
* events/all

* update store/event

* update

* update StatusCode

* address comment

* address comment

* address comment

* address conflict

* conflict

* update events

* fix typo

* address comment

* update url

* address comment
  • Loading branch information
fewdan authored May 22, 2020
1 parent 15ceb64 commit e9401fa
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 12 deletions.
53 changes: 49 additions & 4 deletions pkg/apiserver/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
package event

import (
"context"
"fmt"
"net/http"

"github.com/gin-gonic/gin"

"github.com/pingcap/chaos-mesh/pkg/apiserver/utils"
"github.com/pingcap/chaos-mesh/pkg/config"
"github.com/pingcap/chaos-mesh/pkg/core"

Expand Down Expand Up @@ -47,11 +52,51 @@ func NewService(

// Register mounts our HTTP handler on the mux.
func Register(r *gin.RouterGroup, s *Service) {
endpoint := r.Group("/event")
endpoint := r.Group("/events")

// TODO: add more api handlers
endpoint.GET("/all", s.listEvents)
endpoint.GET("/", s.listEvents)
}

// TODO: need to be implemented
func (s *Service) listEvents(c *gin.Context) {}
// @Summary Get all events from db.
// @Description Get all chaos events from db.
// @Tags events
// @Produce json
// @Success 200 {array} core.Event
// @Router /api/events/all [get]
// @Failure 500 {object} utils.APIError
func (s *Service) listEvents(c *gin.Context) {
name := c.Query("name")
namespace := c.Query("namespace")
//eventList := make([]*core.Event, 0)
var eventList []*core.Event
var err error

if name != "" && namespace == "" {
c.Status(http.StatusInternalServerError)
_ = c.Error(utils.ErrInternalServer.WrapWithNoMessage(fmt.Errorf("namespace is empty")))
return
} else if name == "" && namespace == "" {
eventList, err = s.event.List(context.Background())
if err != nil {
c.Status(http.StatusInternalServerError)
_ = c.Error(utils.ErrInternalServer.WrapWithNoMessage(err))
return
}
} else if name == "" && namespace != "" {
eventList, err = s.event.ListByNamespace(context.Background(), namespace)
if err != nil {
c.Status(http.StatusInternalServerError)
_ = c.Error(utils.ErrInternalServer.WrapWithNoMessage(err))
return
}
} else {
eventList, err = s.event.ListByPod(context.Background(), namespace, name)
if err != nil {
c.Status(http.StatusInternalServerError)
_ = c.Error(utils.ErrInternalServer.WrapWithNoMessage(err))
return
}
}
c.JSON(http.StatusOK, eventList)
}
5 changes: 4 additions & 1 deletion pkg/core/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ type EventStore interface {
// ListByExperiment returns a event list by the name and namespace of the experiment.
ListByExperiment(context.Context, string, string) ([]*Event, error)

// ListByNamespace returns a event list by the namespace of the pod.
ListByNamespace(context.Context, string) ([]*Event, error)

// ListByPod returns a event list by the name and namespace of the pod.
ListByPod(context.Context, string, string) ([]*Event, error)

// Find returns a event from the datastore by ID.
Find(context.Context, int64) (*Event, error)
Find(context.Context, uint) (*Event, error)

// FindByExperimentAndStartTime returns a event by the experiment and start time.
FindByExperimentAndStartTime(context.Context, string, string, *time.Time) (*Event, error)
Expand Down
134 changes: 127 additions & 7 deletions pkg/store/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,135 @@ type eventStore struct {
db *dbstore.DB
}

// TODO: implement core.EventStore interface
func (e *eventStore) List(context.Context) ([]*core.Event, error) { return nil, nil }
func (e *eventStore) ListByExperiment(context.Context, string, string) ([]*core.Event, error) {
return nil, nil
// findPodRecordsByEventID returns the list of PodRecords according to the eventID
func (e *eventStore) findPodRecordsByEventID(_ context.Context, id uint) ([]*core.PodRecord, error) {
pods := make([]*core.PodRecord,0)
if err := e.db.Where(
"event_id = ?", id).
Find(&pods).Error; err != nil && !gorm.IsRecordNotFoundError(err) {
return nil, err
}
return pods, nil
}

// List returns the list of events
func (e *eventStore) List(_ context.Context) ([]*core.Event, error) {
var resList []core.Event
eventList := make([]*core.Event, 0)

if err := e.db.Find(&resList).Error; err != nil && !gorm.IsRecordNotFoundError(err) {
return nil, err
}

for _, et := range(resList) {
pods, err:= e.findPodRecordsByEventID(context.Background(), et.ID)
if err != nil {
return nil, err
}
et.Pods = pods
eventList = append(eventList, &et)
}

return eventList, nil
}

// ListByExperiment returns a event list by the name and namespace of the experiment.
func (e *eventStore) ListByExperiment(_ context.Context, namespace string, experiment string) ([]*core.Event, error) {
var resList []core.Event

if err := e.db.Where(
"namespace = ? and experiment = ? ",
namespace, experiment).
Find(resList).Error; err != nil && !gorm.IsRecordNotFoundError(err) {
return nil, err
}

eventList := make([]*core.Event, 0, len(resList))
for _, et := range resList {
pods, err:= e.findPodRecordsByEventID(context.Background(), et.ID)
if err != nil {
return nil, err
}
et.Pods = pods
eventList = append(eventList, &et)
}

return eventList, nil
}
func (e *eventStore) ListByPod(context.Context, string, string) ([]*core.Event, error) {
return nil, nil

// ListByNamespace returns the list of events according to the namespace
func (e *eventStore) ListByNamespace(_ context.Context, namespace string) ([]*core.Event, error) {
podRecords := make([]*core.PodRecord, 0)

if err := e.db.Where(
&core.PodRecord{Namespace: namespace}).
Find(&podRecords).Error; err != nil && !gorm.IsRecordNotFoundError(err) {
return nil, err
}

et := new(core.Event)
eventList := make([]*core.Event, 0, len(podRecords))
for _, pr := range podRecords {
if err := e.db.Where(
"id = ?", pr.EventID).
First(et).Error; err != nil && !gorm.IsRecordNotFoundError(err) {
return nil, err
}

pods, err:= e.findPodRecordsByEventID(context.Background(), et.ID)
if err != nil {
return nil, err
}
et.Pods = pods
eventList = append(eventList, et)
}
return eventList, nil
}

// ListByPod returns the list of events according to the pod
func (e *eventStore) ListByPod(_ context.Context, namespace string, name string) ([]*core.Event, error) {
podRecords := make([]*core.PodRecord, 0)

if err := e.db.Where(
&core.PodRecord{PodName: name, Namespace: namespace}).
Find(&podRecords).Error; err != nil && !gorm.IsRecordNotFoundError(err) {
return nil, err
}

et := new(core.Event)
eventList := make([]*core.Event, 0, len(podRecords))
for _, pr := range podRecords {
if err := e.db.Where(
"id = ?", pr.EventID).
First(et).Error; err != nil && !gorm.IsRecordNotFoundError(err) {
return nil, err
}

pods, err:= e.findPodRecordsByEventID(context.Background(), et.ID)
if err != nil {
return nil, err
}
et.Pods = pods
eventList = append(eventList, et)
}
return eventList, nil
}

// Find returns a event from the datastore by ID.
func (e *eventStore) Find(_ context.Context, id uint) (*core.Event, error) {
et := new(core.Event)
if err := e.db.Where(
"id = ?", id).
First(et).Error; err != nil && !gorm.IsRecordNotFoundError(err) {
return nil, err
}
pods, err:= e.findPodRecordsByEventID(context.Background(), et.ID)
if err != nil {
return nil, err
}
et.Pods = pods
return et, nil
}
func (e *eventStore) Find(context.Context, int64) (*core.Event, error) { return nil, nil }

func (e *eventStore) FindByExperimentAndStartTime(
_ context.Context,
Expand Down

0 comments on commit e9401fa

Please sign in to comment.