Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature. implemtation policy-violation-log #454

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions internal/delivery/http/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package http

import (
"fmt"
"net/http"
"strings"
"time"

"github.com/gorilla/mux"
"github.com/openinfradev/tks-api/internal/middleware/auth/request"
"github.com/openinfradev/tks-api/internal/model"
"github.com/openinfradev/tks-api/internal/pagination"
policytemplate "github.com/openinfradev/tks-api/internal/policy-template"
"github.com/openinfradev/tks-api/internal/serializer"
"github.com/openinfradev/tks-api/internal/usecase"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/httpErrors"
"github.com/openinfradev/tks-api/pkg/log"
"net/http"
"strings"
"time"
)

type IDashboardHandler interface {
Expand All @@ -35,16 +37,18 @@ type IDashboardHandler interface {
}

type DashboardHandler struct {
usecase usecase.IDashboardUsecase
organizationUsecase usecase.IOrganizationUsecase
policyUsecase usecase.IPolicyUsecase
usecase usecase.IDashboardUsecase
organizationUsecase usecase.IOrganizationUsecase
policyUsecase usecase.IPolicyUsecase
systemNotificationUsecase usecase.ISystemNotificationUsecase
}

func NewDashboardHandler(h usecase.Usecase) IDashboardHandler {
return &DashboardHandler{
usecase: h.Dashboard,
organizationUsecase: h.Organization,
policyUsecase: h.Policy,
usecase: h.Dashboard,
organizationUsecase: h.Organization,
policyUsecase: h.Policy,
systemNotificationUsecase: h.SystemNotification,
}
}

Expand Down Expand Up @@ -650,8 +654,13 @@ func (h *DashboardHandler) GetPolicyViolation(w http.ResponseWriter, r *http.Req
// @Description Get policy violation log
// @Accept json
// @Produce json
// @Param organizationId path string true "Organization ID"
// @Success 200 {object} domain.GetDashboardPolicyViolationLogResponse
// @Param organizationId path string true "organizationId"
// @Param pageSize query string false "pageSize"
// @Param pageNumber query string false "pageNumber"
// @Param soertColumn query string false "sortColumn"
// @Param sortOrder query string false "sortOrder"
// @Param filters query []string false "filters"
// @Success 200 {object} domain.GetPolicyNotificationsResponse
// @Router /organizations/{organizationId}/dashboards/widgets/policy-violation-log [get]
// @Security JWT
func (h *DashboardHandler) GetPolicyViolationLog(w http.ResponseWriter, r *http.Request) {
Expand All @@ -663,13 +672,27 @@ func (h *DashboardHandler) GetPolicyViolationLog(w http.ResponseWriter, r *http.
return
}

out, err := h.usecase.GetPolicyViolationLog(r.Context(), organizationId)
urlParams := r.URL.Query()
pg := pagination.NewPagination(&urlParams)

policyNotifications, err := h.systemNotificationUsecase.FetchPolicyNotifications(r.Context(), organizationId, pg)
if err != nil {
log.Error(r.Context(), "Failed to make policy violation log", err)
ErrorJSON(w, r, err)
return
}

var out domain.GetPolicyNotificationsResponse
out.PolicyNotifications = make([]domain.PolicyNotificationResponse, len(policyNotifications))
for i, policyNotification := range policyNotifications {
if err := serializer.Map(r.Context(), policyNotification, &out.PolicyNotifications[i]); err != nil {
log.Info(r.Context(), err)
}
}

if out.Pagination, err = pg.Response(r.Context()); err != nil {
log.Info(r.Context(), err)
}

ResponseJSON(w, r, http.StatusOK, out)
}

Expand Down
Loading