@@ -19,6 +19,40 @@ import (
1919 "github.com/gin-gonic/gin"
2020)
2121
22+ func parsePaginationParams (c * gin.Context , defaultLimit , maxLimit int ) (int , int ) {
23+ limit := defaultLimit
24+ if raw := c .Query ("limit" ); raw != "" {
25+ if parsed , err := strconv .Atoi (raw ); err == nil {
26+ limit = parsed
27+ }
28+ }
29+ if limit <= 0 || limit > maxLimit {
30+ limit = maxLimit
31+ }
32+
33+ offset := 0
34+ if raw := c .Query ("offset" ); raw != "" {
35+ if parsed , err := strconv .Atoi (raw ); err == nil && parsed > 0 {
36+ offset = parsed
37+ }
38+ }
39+
40+ return limit , offset
41+ }
42+
43+ func paginateDecisions (decisions []models.Decision , limit , offset int ) []models.Decision {
44+ if offset >= len (decisions ) {
45+ return []models.Decision {}
46+ }
47+
48+ end := offset + limit
49+ if end > len (decisions ) {
50+ end = len (decisions )
51+ }
52+
53+ return decisions [offset :end ]
54+ }
55+
2256// =============================================================================
2357// DASHBOARD & METRICS
2458// =============================================================================
@@ -46,8 +80,14 @@ func GetDecisions(dockerClient *docker.Client, cfg *config.Config, ttlCache ...*
4680
4781 logger .Info ("Getting CrowdSec decisions via cscli" )
4882
49- limit := config .EffectiveLimit (cfg .DecisionListLimit , constants .MaxListLimit )
50- cmd := []string {"cscli" , "decisions" , "list" , "-o" , "json" , "--limit" , strconv .Itoa (limit )}
83+ defaultLimit := config .EffectiveLimit (cfg .DecisionListLimit , constants .MaxListLimit )
84+ limit , offset := parsePaginationParams (c , defaultLimit , constants .MaxListLimit )
85+ fetchLimit := defaultLimit
86+ if c .Query ("limit" ) != "" || c .Query ("offset" ) != "" {
87+ fetchLimit = constants .MaxListLimit
88+ }
89+
90+ cmd := []string {"cscli" , "decisions" , "list" , "-o" , "json" , "--limit" , strconv .Itoa (fetchLimit )}
5191 output , err := dockerClient .ExecCommand (cfg .CrowdsecContainerName , cmd )
5292 if err != nil {
5393 c .JSON (http .StatusInternalServerError , models.Response {
@@ -61,7 +101,7 @@ func GetDecisions(dockerClient *docker.Client, cfg *config.Config, ttlCache ...*
61101 if output == "null" || output == "" || output == "[]" {
62102 c .JSON (http .StatusOK , models.Response {
63103 Success : true ,
64- Data : gin.H {"decisions" : []models.Decision {}, "count" : 0 },
104+ Data : gin.H {"decisions" : []models.Decision {}, "count" : 0 , "total" : 0 , "limit" : limit , "offset" : offset },
65105 })
66106 return
67107 }
@@ -106,7 +146,15 @@ func GetDecisions(dockerClient *docker.Client, cfg *config.Config, ttlCache ...*
106146 return
107147 }
108148
109- result := gin.H {"decisions" : decisions , "count" : len (decisions )}
149+ total := len (decisions )
150+ pagedDecisions := paginateDecisions (decisions , limit , offset )
151+ result := gin.H {
152+ "decisions" : pagedDecisions ,
153+ "count" : len (pagedDecisions ),
154+ "total" : total ,
155+ "limit" : limit ,
156+ "offset" : offset ,
157+ }
110158 if len (ttlCache ) > 0 && ttlCache [0 ] != nil {
111159 ttlCache [0 ].Set (cacheKey , result , 15 * time .Second )
112160 }
0 commit comments