diff --git a/server/handlers/events_streamer.go b/server/handlers/events_streamer.go index 1c58cd338a3..230ba903539 100644 --- a/server/handlers/events_streamer.go +++ b/server/handlers/events_streamer.go @@ -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. diff --git a/server/models/events.go b/server/models/events.go index d2d61735bdd..fbf2c7ffb22 100644 --- a/server/models/events.go +++ b/server/models/events.go @@ -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) diff --git a/server/models/events_persister.go b/server/models/events_persister.go index d4ab81971ff..1252bc3761b 100644 --- a/server/models/events_persister.go +++ b/server/models/events_persister.go @@ -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) diff --git a/server/models/handlers.go b/server/models/handlers.go index e4caee4020b..da953a53405 100644 --- a/server/models/handlers.go +++ b/server/models/handlers.go @@ -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) diff --git a/server/models/pattern/core/pattern.go b/server/models/pattern/core/pattern.go index 1a8b8329a80..7433d337042 100644 --- a/server/models/pattern/core/pattern.go +++ b/server/models/pattern/core/pattern.go @@ -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" @@ -699,7 +697,3 @@ func getCytoscapeJSPosition(svc *Service) (cytoscapejs.Position, error) { return pos, nil } - -func init() { - mathrand.Seed(time.Now().Unix()) -} diff --git a/server/router/server.go b/server/router/server.go index 8fdadf42205..9b288ee773d 100644 --- a/server/router/server.go +++ b/server/router/server.go @@ -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))).