From ee479bfaa1e69c8310ec0aa71d5d7f426a07ab71 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Mon, 11 Sep 2023 22:37:16 +0530 Subject: [PATCH 1/4] add API to return available event category and action Signed-off-by: MUzairS15 --- server/handlers/events_streamer.go | 21 +++++++++++++++++++++ server/models/events.go | 1 + server/models/events_persister.go | 18 ++++++++++++++++++ server/models/handlers.go | 1 + server/router/server.go | 2 ++ 5 files changed, 43 insertions(+) diff --git a/server/handlers/events_streamer.go b/server/handlers/events_streamer.go index 1c58cd338a3..7e428150029 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/v2/events 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..48ed58b1e21 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["actions"] = 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/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))). From cffb7761a14f074b97ff0e48f4b47741d4229243 Mon Sep 17 00:00:00 2001 From: Mohd Uzair Date: Mon, 11 Sep 2023 22:42:16 +0530 Subject: [PATCH 2/4] Update server/handlers/events_streamer.go Signed-off-by: Mohd Uzair --- server/handlers/events_streamer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/handlers/events_streamer.go b/server/handlers/events_streamer.go index 7e428150029..230ba903539 100644 --- a/server/handlers/events_streamer.go +++ b/server/handlers/events_streamer.go @@ -72,7 +72,7 @@ func (h *Handler) GetAllEvents(w http.ResponseWriter, req *http.Request, prefObj } } -// swagger:route GET /api/v2/events EventsAPI idGetEventStreamer +// swagger:route GET /api/events/types EventsAPI idGetEventStreamer // Handle GET request for available event categories and actions. // responses: // 200: From c956a4a605596542fef6e3a84d98a54d09a4237d Mon Sep 17 00:00:00 2001 From: Mohd Uzair Date: Mon, 11 Sep 2023 22:42:38 +0530 Subject: [PATCH 3/4] Update server/models/events_persister.go Signed-off-by: Mohd Uzair --- server/models/events_persister.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/models/events_persister.go b/server/models/events_persister.go index 48ed58b1e21..1252bc3761b 100644 --- a/server/models/events_persister.go +++ b/server/models/events_persister.go @@ -40,7 +40,7 @@ func (e *EventsPersister) GetEventTypes(userID uuid.UUID) (map[string]interface{ return nil, err } - eventTypes["actions"] = actions + eventTypes["action"] = actions return eventTypes, err } From 302969727adbf811c8ceca8e42d311a520addc9b Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Mon, 11 Sep 2023 22:47:15 +0530 Subject: [PATCH 4/4] lint fixes Signed-off-by: MUzairS15 --- server/models/pattern/core/pattern.go | 6 ------ 1 file changed, 6 deletions(-) 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()) -}