|
6 | 6 | "errors"
|
7 | 7 | "fmt"
|
8 | 8 | "net/http"
|
| 9 | + "strconv" |
9 | 10 | "strings"
|
10 | 11 |
|
11 | 12 | "github.com/grafana/grafana-plugin-sdk-go/backend"
|
@@ -188,7 +189,7 @@ type InfinityDataOverride struct {
|
188 | 189 | Override string `json:"override"`
|
189 | 190 | }
|
190 | 191 |
|
191 |
| -func ApplyDefaultsToQuery(ctx context.Context, query Query, settings InfinitySettings) Query { |
| 192 | +func ApplyDefaultsToQuery(ctx context.Context, pCtx *backend.PluginContext, query Query, settings InfinitySettings) Query { |
192 | 193 | if query.Type == "" {
|
193 | 194 | query.Type = QueryTypeJSON
|
194 | 195 | if query.Source == "" {
|
@@ -254,12 +255,7 @@ func ApplyDefaultsToQuery(ctx context.Context, query Query, settings InfinitySet
|
254 | 255 | }
|
255 | 256 | if query.Parser == InfinityParserBackend && query.Source == "url" && !(query.PageMode == "" || query.PageMode == PaginationModeNone) {
|
256 | 257 | if query.PageMode != PaginationModeNone {
|
257 |
| - if query.PageMaxPages <= 0 { |
258 |
| - query.PageMaxPages = 1 |
259 |
| - } |
260 |
| - if query.PageMaxPages >= 5 { |
261 |
| - query.PageMaxPages = 5 |
262 |
| - } |
| 258 | + query.PageMaxPages = GetPaginationMaxPagesValue(ctx, pCtx, query) |
263 | 259 | if query.PageParamSizeFieldName == "" {
|
264 | 260 | query.PageParamSizeFieldName = "limit"
|
265 | 261 | }
|
@@ -315,10 +311,28 @@ func LoadQuery(ctx context.Context, backendQuery backend.DataQuery, pluginContex
|
315 | 311 | if err != nil {
|
316 | 312 | return query, backend.DownstreamError(fmt.Errorf("error while parsing the query json. %w", err))
|
317 | 313 | }
|
318 |
| - query = ApplyDefaultsToQuery(ctx, query, settings) |
| 314 | + query = ApplyDefaultsToQuery(ctx, &pluginContext, query, settings) |
319 | 315 | if query.PageMode == PaginationModeList && strings.TrimSpace(query.PageParamListFieldName) == "" {
|
320 | 316 | // Downstream error as user input is not correct
|
321 | 317 | return query, backend.DownstreamError(errors.New("pagination_param_list_field_name cannot be empty"))
|
322 | 318 | }
|
323 | 319 | return ApplyMacros(ctx, query, backendQuery.TimeRange, pluginContext)
|
324 | 320 | }
|
| 321 | + |
| 322 | +func GetPaginationMaxPagesValue(ctx context.Context, pCtx *backend.PluginContext, query Query) int { |
| 323 | + maxPages := 5 |
| 324 | + if query.PageMaxPages <= 0 { |
| 325 | + maxPages = 1 |
| 326 | + } |
| 327 | + if query.PageMaxPages >= 5 { |
| 328 | + maxPages = 5 |
| 329 | + } |
| 330 | + maxPageFromEnv := GetGrafanaConfig(ctx, pCtx, "pagination_max_pages") |
| 331 | + if maxPageFromEnvValue, err := strconv.Atoi(maxPageFromEnv); err == nil && maxPageFromEnvValue > 0 { |
| 332 | + maxPages = maxPageFromEnvValue |
| 333 | + } |
| 334 | + if query.PageMaxPages <= maxPages && query.PageMaxPages > 0 { |
| 335 | + return query.PageMaxPages |
| 336 | + } |
| 337 | + return maxPages |
| 338 | +} |
0 commit comments