Release / Publish tags (push) Successful in 18s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Successful in 1m47s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 28s
CI / Format and vet (push) Successful in 23s
CI / Tests (Linux, race detector) (push) Successful in 1m24s
CI / Dependency audit (push) Successful in 24s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 5s
Backend pagination with one pager for sessions, IPs, campaigns and events; network probe source (port-scan, T1046, second firewall reason port-scan); host kit logs inbound probes; urutau call on the report cover. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package server
|
|
|
|
import (
|
|
"net/url"
|
|
"strconv"
|
|
)
|
|
|
|
// Backend pagination shared by the list endpoints: `page` (1-based) and
|
|
// `per_page` (default 25, at most 100). Out-of-range values are clamped and a
|
|
// page past the end is an empty slice with the true total.
|
|
const (
|
|
defaultPerPage = 25
|
|
maxPerPage = 100
|
|
maxPageNo = 100000
|
|
)
|
|
|
|
// pageParams reads page and per_page ("per" is the older spelling the IP list
|
|
// used). Missing, zero or negative values fall back to page 1 / 25 per page.
|
|
func pageParams(q url.Values) (page, per int) {
|
|
page, _ = strconv.Atoi(q.Get("page"))
|
|
page = min(max(page, 1), maxPageNo)
|
|
v := q.Get("per_page")
|
|
if v == "" {
|
|
v = q.Get("per")
|
|
}
|
|
per, _ = strconv.Atoi(v)
|
|
if per < 1 {
|
|
per = defaultPerPage
|
|
}
|
|
return page, min(per, maxPerPage)
|
|
}
|
|
|
|
// pageMeta is the metadata block every paginated response carries.
|
|
type pageMeta struct {
|
|
Page int `json:"page"`
|
|
PerPage int `json:"per_page"`
|
|
Pages int `json:"pages"`
|
|
}
|
|
|
|
// pageTotal adds the exact (or, for the event log, estimated) item count.
|
|
type pageTotal struct {
|
|
pageMeta
|
|
Total int `json:"total"`
|
|
}
|
|
|
|
func metaOf(total, page, per int) pageMeta {
|
|
return pageMeta{Page: page, PerPage: per, Pages: max(1, (total+per-1)/per)}
|
|
}
|
|
|
|
// window returns the bounds of the page inside a list of n items.
|
|
func window(n, page, per int) (from, to int) {
|
|
from = min((page-1)*per, n)
|
|
return from, min(from+per, n)
|
|
}
|