Skip to content

Commit

Permalink
Merge pull request meshery#8715 from MUzairS15/api-for-types
Browse files Browse the repository at this point in the history
[Events] Add API to return available event category and action.
  • Loading branch information
MUzairS15 authored Sep 12, 2023
2 parents 8ad2bf2 + 3029697 commit d92450a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 6 deletions.
21 changes: 21 additions & 0 deletions server/handlers/events_streamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ func (h *Handler) GetAllEvents(w http.ResponseWriter, req *http.Request, prefObj
}
}

// swagger:route GET /api/events/types EventsAPI idGetEventStreamer
// Handle GET request for available event categories and actions.
// responses:
// 200:
func (h *Handler) GetEventTypes (w http.ResponseWriter, req *http.Request, prefObj *models.Preference, user *models.User, provider models.Provider) {
userID := uuid.FromStringOrNil(user.ID)

eventTypes, err := provider.GetEventTypes(userID)
if err != nil {
http.Error(w, fmt.Errorf("error retrieving event cagegories and actions").Error(), http.StatusInternalServerError)
return
}

err = json.NewEncoder(w).Encode(eventTypes)
if err != nil {
h.log.Error(models.ErrMarshal(err, "event types response"))
http.Error(w, models.ErrMarshal(err, "event types response").Error(), http.StatusInternalServerError)
return
}
}

// swagger:route POST /api/events/status/{id} idGetEventStreamer
// Handle POST request to update event status.
// Updates event status for the event associated with the id.
Expand Down
1 change: 1 addition & 0 deletions server/models/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

type MesheryEvents interface {
GetAllEvents(eventFilter *events.EventsFilter, userID uuid.UUID) (*EventsResponse, error)
GetEventTypes(userID uuid.UUID) (map[string]interface{}, error)
PersistEvent(data *events.Event) error
DeleteEvent(eventID uuid.UUID) error
UpdateEventStatus(eventID uuid.UUID, status string) (*events.Event, error)
Expand Down
18 changes: 18 additions & 0 deletions server/models/events_persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ type CountBySeverityLevel struct {
Count int `json:"count"`
}

func (e *EventsPersister) GetEventTypes(userID uuid.UUID) (map[string]interface{}, error) {
eventTypes := make(map[string]interface{}, 2)
var categories, actions []string
err := e.DB.Table("events").Distinct("category").Find(&categories).Error
if err != nil {
return nil, err
}

eventTypes["category"] = categories
err = e.DB.Table("events").Distinct("action").Find(&actions).Error
if err != nil {
return nil, err
}

eventTypes["action"] = actions
return eventTypes, err
}

func (e *EventsPersister) GetAllEvents(eventsFilter *events.EventsFilter, userID uuid.UUID) (*EventsResponse, error) {
eventsDB := []*events.Event{}
finder := e.DB.Model(&events.Event{}).Where("user_id = ?", userID)
Expand Down
1 change: 1 addition & 0 deletions server/models/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type HandlerInterface interface {
AdapterPingHandler(w http.ResponseWriter, req *http.Request, prefObj *Preference, user *User, provider Provider)

GetAllEvents(w http.ResponseWriter, req *http.Request, prefObj *Preference, user *User, provider Provider)
GetEventTypes(w http.ResponseWriter, req *http.Request, prefObj *Preference, user *User, provider Provider)
UpdateEventStatus(w http.ResponseWriter, req *http.Request, prefObj *Preference, user *User, provider Provider)
DeleteEvent(w http.ResponseWriter, req *http.Request, prefObj *Preference, user *User, provider Provider)

Expand Down
6 changes: 0 additions & 6 deletions server/models/pattern/core/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"errors"
"fmt"
"math/big"
mathrand "math/rand"
"strings"
"time"

"github.com/gofrs/uuid"
"github.com/layer5io/meshery/server/models/pattern/utils"
Expand Down Expand Up @@ -699,7 +697,3 @@ func getCytoscapeJSPosition(svc *Service) (cytoscapejs.Position, error) {

return pos, nil
}

func init() {
mathrand.Seed(time.Now().Unix())
}
2 changes: 2 additions & 0 deletions server/router/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ func NewRouter(_ context.Context, h models.HandlerInterface, port int, g http.Ha
// This will be changed to /api/events once the UI is compeltely updated to use new events and SSE is tunred off, otherwise existing events will break.
gMux.Handle("/api/v2/events", h.ProviderMiddleware(h.AuthMiddleware(h.SessionInjectorMiddleware(h.GetAllEvents), models.ProviderAuth))).
Methods("GET")
gMux.Handle("/api/events/types", h.ProviderMiddleware(h.AuthMiddleware(h.SessionInjectorMiddleware(h.GetEventTypes), models.ProviderAuth))).
Methods("GET")
gMux.Handle("/api/events/status/{id}", h.ProviderMiddleware(h.AuthMiddleware(h.SessionInjectorMiddleware(h.UpdateEventStatus), models.ProviderAuth))).
Methods("POST")
gMux.Handle("/api/events/{id}", h.ProviderMiddleware(h.AuthMiddleware(h.SessionInjectorMiddleware(h.DeleteEvent), models.ProviderAuth))).
Expand Down

0 comments on commit d92450a

Please sign in to comment.