44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/company/ai-ops/internal/domain/repository"
|
|
"github.com/company/ai-ops/pkg/errors"
|
|
"github.com/company/ai-ops/pkg/response"
|
|
)
|
|
|
|
// AlertHandler 是告警事件 HTTP 处理器
|
|
type AlertHandler struct {
|
|
repo repository.AlertRepository
|
|
}
|
|
|
|
func NewAlertHandler(repo repository.AlertRepository) *AlertHandler {
|
|
return &AlertHandler{repo: repo}
|
|
}
|
|
|
|
func (h *AlertHandler) RegisterRoutes(mux *http.ServeMux) {
|
|
mux.HandleFunc("GET /api/v1/ai-ops/alerts", h.ListAlerts)
|
|
}
|
|
|
|
func (h *AlertHandler) ListAlerts(w http.ResponseWriter, r *http.Request) {
|
|
query := r.URL.Query()
|
|
status := query.Get("status")
|
|
page, _ := strconv.Atoi(query.Get("page"))
|
|
pageSize, _ := strconv.Atoi(query.Get("page_size"))
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if pageSize < 1 || pageSize > 100 {
|
|
pageSize = 20
|
|
}
|
|
|
|
events, total, err := h.repo.ListEvents(r.Context(), status, page, pageSize)
|
|
if err != nil {
|
|
response.Error(w, errors.Wrap(err, errors.ErrInternal))
|
|
return
|
|
}
|
|
response.Success(w, map[string]any{"items": events, "total": total, "page": page, "page_size": pageSize})
|
|
}
|