From 47356b60af7da0253910caec6325ebd662ed9301 Mon Sep 17 00:00:00 2001 From: Seungkyu Ahn Date: Thu, 18 Apr 2024 09:36:11 +0900 Subject: [PATCH 1/3] policy statistics widget --- internal/delivery/api/endpoint.go | 1 + internal/delivery/http/dashboard.go | 39 +++++++++++++++++++++++++++++ internal/route/route.go | 1 + pkg/domain/dashboard.go | 4 +++ 4 files changed, 45 insertions(+) diff --git a/internal/delivery/api/endpoint.go b/internal/delivery/api/endpoint.go index f8ddc546..d5993f19 100644 --- a/internal/delivery/api/endpoint.go +++ b/internal/delivery/api/endpoint.go @@ -122,6 +122,7 @@ const ( GetPolicyEnforcementDashboard GetPolicyViolationDashboard GetPolicyViolationLogDashboard + GetPolicyStatisticsDashboard // SystemNotificationTemplate Admin_CreateSystemNotificationTemplate diff --git a/internal/delivery/http/dashboard.go b/internal/delivery/http/dashboard.go index 370368b5..c4502458 100644 --- a/internal/delivery/http/dashboard.go +++ b/internal/delivery/http/dashboard.go @@ -29,17 +29,20 @@ type IDashboardHandler interface { GetPolicyEnforcement(w http.ResponseWriter, r *http.Request) GetPolicyViolation(w http.ResponseWriter, r *http.Request) GetPolicyViolationLog(w http.ResponseWriter, r *http.Request) + GetPolicyStatistics(w http.ResponseWriter, r *http.Request) } type DashboardHandler struct { usecase usecase.IDashboardUsecase organizationUsecase usecase.IOrganizationUsecase + policyUsecase usecase.IPolicyUsecase } func NewDashboardHandler(h usecase.Usecase) IDashboardHandler { return &DashboardHandler{ usecase: h.Dashboard, organizationUsecase: h.Organization, + policyUsecase: h.Policy, } } @@ -652,3 +655,39 @@ func (h *DashboardHandler) GetPolicyViolationLog(w http.ResponseWriter, r *http. ResponseJSON(w, r, http.StatusOK, out) } + +// GetPolicyStatistics godoc +// +// @Tags Dashboard Widgets +// @Summary Get policy violation log +// @Description Get policy violation log +// @Accept json +// @Produce json +// @Param organizationId path string true "Organization ID" +// @Success 200 {object} domain.GetDashboardPolicyStatisticsResponse +// @Router /organizations/{organizationId}/dashboards/policy-statistics [get] +// @Security JWT +func (h *DashboardHandler) GetPolicyStatistics(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + organizationId, ok := vars["organizationId"] + if !ok { + ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("invalid organizationId"), + "C_INVALID_ORGANIZATION_ID", "")) + return + } + + psr, err := h.policyUsecase.GetPolicyStatistics(r.Context(), organizationId) + if err != nil { + log.Errorf(r.Context(), "error is :%s(%T)", err.Error(), err) + if _, status := httpErrors.ErrorResponse(err); status == http.StatusNotFound { + ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "", "")) + return + } + + ErrorJSON(w, r, err) + return + } + out := domain.GetDashboardPolicyStatisticsResponse{PolicyStatisticsResponse: *psr} + + ResponseJSON(w, r, http.StatusOK, out) +} diff --git a/internal/route/route.go b/internal/route/route.go index 4ac45ede..b7aa0014 100644 --- a/internal/route/route.go +++ b/internal/route/route.go @@ -210,6 +210,7 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboards/policy-enforcement", customMiddleware.Handle(internalApi.GetPolicyEnforcementDashboard, http.HandlerFunc(dashboardHandler.GetPolicyEnforcement))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboards/policy-violation", customMiddleware.Handle(internalApi.GetPolicyViolationDashboard, http.HandlerFunc(dashboardHandler.GetPolicyViolation))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboards/policy-violation-log", customMiddleware.Handle(internalApi.GetPolicyViolationLogDashboard, http.HandlerFunc(dashboardHandler.GetPolicyViolationLog))).Methods(http.MethodGet) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboards/policy-statistics", customMiddleware.Handle(internalApi.GetPolicyStatisticsDashboard, http.HandlerFunc(dashboardHandler.GetPolicyStatistics))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboards", customMiddleware.Handle(internalApi.CreateDashboard, http.HandlerFunc(dashboardHandler.CreateDashboard))).Methods(http.MethodPost) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboards", customMiddleware.Handle(internalApi.GetDashboard, http.HandlerFunc(dashboardHandler.GetDashboard))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboards", customMiddleware.Handle(internalApi.UpdateDashboard, http.HandlerFunc(dashboardHandler.UpdateDashboard))).Methods(http.MethodPut) diff --git a/pkg/domain/dashboard.go b/pkg/domain/dashboard.go index abd8c152..52571d9a 100644 --- a/pkg/domain/dashboard.go +++ b/pkg/domain/dashboard.go @@ -214,6 +214,10 @@ type GetDashboardPolicyViolationLogResponse struct { // TODO implement me } +type GetDashboardPolicyStatisticsResponse struct { + PolicyStatisticsResponse +} + type BarChart struct { ChartType string `json:"chartType"` OrganizationId string `json:"organizationId"` From c4fd13f2b0eab9b8c36ce61b9979901e6e569f1d Mon Sep 17 00:00:00 2001 From: Seungkyu Ahn Date: Thu, 18 Apr 2024 09:36:39 +0900 Subject: [PATCH 2/3] policy statistics widget\n endpoint generate --- internal/delivery/api/generated_endpoints.go.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/delivery/api/generated_endpoints.go.go b/internal/delivery/api/generated_endpoints.go.go index 3d9408fe..210b5f5f 100644 --- a/internal/delivery/api/generated_endpoints.go.go +++ b/internal/delivery/api/generated_endpoints.go.go @@ -375,6 +375,10 @@ var ApiMap = map[Endpoint]EndpointInfo{ Name: "GetPolicyViolationLogDashboard", Group: "Dashboard", }, + GetPolicyStatisticsDashboard: { + Name: "GetPolicyStatisticsDashboard", + Group: "Dashboard", + }, Admin_CreateSystemNotificationTemplate: { Name: "Admin_CreateSystemNotificationTemplate", Group: "SystemNotificationTemplate", @@ -1096,6 +1100,8 @@ func (e Endpoint) String() string { return "GetPolicyViolationDashboard" case GetPolicyViolationLogDashboard: return "GetPolicyViolationLogDashboard" + case GetPolicyStatisticsDashboard: + return "GetPolicyStatisticsDashboard" case Admin_CreateSystemNotificationTemplate: return "Admin_CreateSystemNotificationTemplate" case Admin_UpdateSystemNotificationTemplate: @@ -1554,6 +1560,8 @@ func GetEndpoint(name string) Endpoint { return GetPolicyViolationDashboard case "GetPolicyViolationLogDashboard": return GetPolicyViolationLogDashboard + case "GetPolicyStatisticsDashboard": + return GetPolicyStatisticsDashboard case "Admin_CreateSystemNotificationTemplate": return Admin_CreateSystemNotificationTemplate case "Admin_UpdateSystemNotificationTemplate": From a37e475987dfb62f43d7c3a59d23a6635ffed4e7 Mon Sep 17 00:00:00 2001 From: Seungkyu Ahn Date: Thu, 18 Apr 2024 09:37:27 +0900 Subject: [PATCH 3/3] policy statistics widget\n swagger generate --- api/swagger/docs.go | 48 ++++++++++++++++++++++++++++++++++++++++ api/swagger/swagger.json | 48 ++++++++++++++++++++++++++++++++++++++++ api/swagger/swagger.yaml | 30 +++++++++++++++++++++++++ 3 files changed, 126 insertions(+) diff --git a/api/swagger/docs.go b/api/swagger/docs.go index 417ada03..76083cf8 100644 --- a/api/swagger/docs.go +++ b/api/swagger/docs.go @@ -3465,6 +3465,43 @@ const docTemplate = `{ } } }, + "/organizations/{organizationId}/dashboards/policy-statistics": { + "get": { + "security": [ + { + "JWT": [] + } + ], + "description": "Get policy violation log", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Dashboard Widgets" + ], + "summary": "Get policy violation log", + "parameters": [ + { + "type": "string", + "description": "Organization ID", + "name": "organizationId", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.GetDashboardPolicyStatisticsResponse" + } + } + } + } + }, "/organizations/{organizationId}/dashboards/policy-status": { "get": { "security": [ @@ -12498,6 +12535,17 @@ const docTemplate = `{ } } }, + "github_com_openinfradev_tks-api_pkg_domain.GetDashboardPolicyStatisticsResponse": { + "type": "object", + "properties": { + "policyCount": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.PolicyCount" + }, + "templateCount": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.TemplateCount" + } + } + }, "github_com_openinfradev_tks-api_pkg_domain.GetDashboardPolicyStatusResponse": { "type": "object", "properties": { diff --git a/api/swagger/swagger.json b/api/swagger/swagger.json index 562ac8b3..571fc537 100644 --- a/api/swagger/swagger.json +++ b/api/swagger/swagger.json @@ -3459,6 +3459,43 @@ } } }, + "/organizations/{organizationId}/dashboards/policy-statistics": { + "get": { + "security": [ + { + "JWT": [] + } + ], + "description": "Get policy violation log", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Dashboard Widgets" + ], + "summary": "Get policy violation log", + "parameters": [ + { + "type": "string", + "description": "Organization ID", + "name": "organizationId", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.GetDashboardPolicyStatisticsResponse" + } + } + } + } + }, "/organizations/{organizationId}/dashboards/policy-status": { "get": { "security": [ @@ -12492,6 +12529,17 @@ } } }, + "github_com_openinfradev_tks-api_pkg_domain.GetDashboardPolicyStatisticsResponse": { + "type": "object", + "properties": { + "policyCount": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.PolicyCount" + }, + "templateCount": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.TemplateCount" + } + } + }, "github_com_openinfradev_tks-api_pkg_domain.GetDashboardPolicyStatusResponse": { "type": "object", "properties": { diff --git a/api/swagger/swagger.yaml b/api/swagger/swagger.yaml index a287656b..ce6b4b7f 100644 --- a/api/swagger/swagger.yaml +++ b/api/swagger/swagger.yaml @@ -1702,6 +1702,13 @@ definitions: updatedAt: type: string type: object + github_com_openinfradev_tks-api_pkg_domain.GetDashboardPolicyStatisticsResponse: + properties: + policyCount: + $ref: '#/definitions/github_com_openinfradev_tks-api_pkg_domain.PolicyCount' + templateCount: + $ref: '#/definitions/github_com_openinfradev_tks-api_pkg_domain.TemplateCount' + type: object github_com_openinfradev_tks-api_pkg_domain.GetDashboardPolicyStatusResponse: properties: statuses: @@ -6436,6 +6443,29 @@ paths: summary: Get the number of policy enforcement tags: - Dashboard Widgets + /organizations/{organizationId}/dashboards/policy-statistics: + get: + consumes: + - application/json + description: Get policy violation log + parameters: + - description: Organization ID + in: path + name: organizationId + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/github_com_openinfradev_tks-api_pkg_domain.GetDashboardPolicyStatisticsResponse' + security: + - JWT: [] + summary: Get policy violation log + tags: + - Dashboard Widgets /organizations/{organizationId}/dashboards/policy-status: get: consumes: