|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "crowdsec-manager/internal/models" |
| 8 | + |
| 9 | + "github.com/gin-gonic/gin" |
| 10 | +) |
| 11 | + |
| 12 | +func parseStaleFilter(raw string) (*bool, error) { |
| 13 | + if raw == "" { |
| 14 | + return nil, nil |
| 15 | + } |
| 16 | + value, err := strconv.ParseBool(raw) |
| 17 | + if err != nil { |
| 18 | + return nil, err |
| 19 | + } |
| 20 | + return &value, nil |
| 21 | +} |
| 22 | + |
| 23 | +// GetHistoryConfig returns history retention config. |
| 24 | +func GetHistoryConfig() gin.HandlerFunc { |
| 25 | + return func(c *gin.Context) { |
| 26 | + if historyService == nil { |
| 27 | + c.JSON(http.StatusServiceUnavailable, models.Response{Success: false, Error: "history service unavailable"}) |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + cfg, err := historyService.GetHistoryConfig(c.Request.Context()) |
| 32 | + if err != nil { |
| 33 | + c.JSON(http.StatusInternalServerError, models.Response{Success: false, Error: "failed to read history config: " + err.Error()}) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + c.JSON(http.StatusOK, models.Response{Success: true, Data: cfg}) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// UpdateHistoryConfig updates history retention config. |
| 42 | +func UpdateHistoryConfig() gin.HandlerFunc { |
| 43 | + return func(c *gin.Context) { |
| 44 | + if historyService == nil { |
| 45 | + c.JSON(http.StatusServiceUnavailable, models.Response{Success: false, Error: "history service unavailable"}) |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + var req struct { |
| 50 | + RetentionDays int `json:"retention_days"` |
| 51 | + } |
| 52 | + if err := c.ShouldBindJSON(&req); err != nil { |
| 53 | + c.JSON(http.StatusBadRequest, models.Response{Success: false, Error: "invalid request: " + err.Error()}) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + if req.RetentionDays < 1 || req.RetentionDays > 365 { |
| 58 | + c.JSON(http.StatusBadRequest, models.Response{Success: false, Error: "retention_days must be between 1 and 365"}) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + cfg, err := historyService.UpdateRetentionDays(c.Request.Context(), req.RetentionDays) |
| 63 | + if err != nil { |
| 64 | + c.JSON(http.StatusInternalServerError, models.Response{Success: false, Error: "failed to update history config: " + err.Error()}) |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + c.JSON(http.StatusOK, models.Response{Success: true, Message: "History config updated", Data: cfg}) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// GetDecisionHistory returns persisted decision history entries. |
| 73 | +func GetDecisionHistory() gin.HandlerFunc { |
| 74 | + return func(c *gin.Context) { |
| 75 | + if historyService == nil { |
| 76 | + c.JSON(http.StatusServiceUnavailable, models.Response{Success: false, Error: "history service unavailable"}) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + stale, err := parseStaleFilter(c.Query("stale")) |
| 81 | + if err != nil { |
| 82 | + c.JSON(http.StatusBadRequest, models.Response{Success: false, Error: "invalid stale filter"}) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + limit, _ := strconv.Atoi(c.DefaultQuery("limit", "50")) |
| 87 | + offset, _ := strconv.Atoi(c.DefaultQuery("offset", "0")) |
| 88 | + filter := models.DecisionHistoryFilter{ |
| 89 | + Stale: stale, |
| 90 | + Value: c.Query("value"), |
| 91 | + Scenario: c.Query("scenario"), |
| 92 | + Since: c.Query("since"), |
| 93 | + Limit: limit, |
| 94 | + Offset: offset, |
| 95 | + } |
| 96 | + |
| 97 | + records, total, err := historyService.ListDecisionHistory(c.Request.Context(), filter) |
| 98 | + if err != nil { |
| 99 | + c.JSON(http.StatusInternalServerError, models.Response{Success: false, Error: "failed to load decision history: " + err.Error()}) |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + c.JSON(http.StatusOK, models.Response{ |
| 104 | + Success: true, |
| 105 | + Data: gin.H{ |
| 106 | + "decisions": records, |
| 107 | + "count": len(records), |
| 108 | + "total": total, |
| 109 | + }, |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// GetAlertHistory returns persisted alert history entries. |
| 115 | +func GetAlertHistory() gin.HandlerFunc { |
| 116 | + return func(c *gin.Context) { |
| 117 | + if historyService == nil { |
| 118 | + c.JSON(http.StatusServiceUnavailable, models.Response{Success: false, Error: "history service unavailable"}) |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + stale, err := parseStaleFilter(c.Query("stale")) |
| 123 | + if err != nil { |
| 124 | + c.JSON(http.StatusBadRequest, models.Response{Success: false, Error: "invalid stale filter"}) |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + limit, _ := strconv.Atoi(c.DefaultQuery("limit", "50")) |
| 129 | + offset, _ := strconv.Atoi(c.DefaultQuery("offset", "0")) |
| 130 | + filter := models.AlertHistoryFilter{ |
| 131 | + Stale: stale, |
| 132 | + Value: c.Query("value"), |
| 133 | + Scenario: c.Query("scenario"), |
| 134 | + Since: c.Query("since"), |
| 135 | + Limit: limit, |
| 136 | + Offset: offset, |
| 137 | + } |
| 138 | + |
| 139 | + records, total, err := historyService.ListAlertHistory(c.Request.Context(), filter) |
| 140 | + if err != nil { |
| 141 | + c.JSON(http.StatusInternalServerError, models.Response{Success: false, Error: "failed to load alert history: " + err.Error()}) |
| 142 | + return |
| 143 | + } |
| 144 | + |
| 145 | + c.JSON(http.StatusOK, models.Response{ |
| 146 | + Success: true, |
| 147 | + Data: gin.H{ |
| 148 | + "alerts": records, |
| 149 | + "count": len(records), |
| 150 | + "total": total, |
| 151 | + }, |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// GetRepeatedOffenders returns repeated offenders from persisted history. |
| 157 | +func GetRepeatedOffenders() gin.HandlerFunc { |
| 158 | + return func(c *gin.Context) { |
| 159 | + if historyService == nil { |
| 160 | + c.JSON(http.StatusServiceUnavailable, models.Response{Success: false, Error: "history service unavailable"}) |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + offenders, err := historyService.ListRepeatedOffenders(c.Request.Context()) |
| 165 | + if err != nil { |
| 166 | + c.JSON(http.StatusInternalServerError, models.Response{Success: false, Error: "failed to load repeated offenders: " + err.Error()}) |
| 167 | + return |
| 168 | + } |
| 169 | + |
| 170 | + c.JSON(http.StatusOK, models.Response{ |
| 171 | + Success: true, |
| 172 | + Data: gin.H{ |
| 173 | + "offenders": offenders, |
| 174 | + "count": len(offenders), |
| 175 | + }, |
| 176 | + }) |
| 177 | + } |
| 178 | +} |
0 commit comments