From 935437dcc11bc3cd023689d5c458bac6f029b500 Mon Sep 17 00:00:00 2001 From: Tengtao X Date: Fri, 9 Aug 2024 21:22:09 +0800 Subject: [PATCH] feat: add pagination to rule and record pages (#61) * feat: sorted data for rule and record * Update record.go * Update rule.go --------- Co-authored-by: Gucheng <85475922+nomeguy@users.noreply.github.com> --- controllers/record.go | 42 ++++++++++++++++++++++++++------ controllers/rule.go | 37 +++++++++++++++++++++++----- object/record.go | 16 ++++++++++++ object/rule.go | 16 ++++++++++++ web/src/RecordListPage.js | 6 ++++- web/src/RuleListPage.js | 10 +++++--- web/src/backend/RecordBackend.js | 4 +-- web/src/backend/RuleBackend.js | 4 +-- 8 files changed, 113 insertions(+), 22 deletions(-) diff --git a/controllers/record.go b/controllers/record.go index 0fa201f..11e6090 100644 --- a/controllers/record.go +++ b/controllers/record.go @@ -17,7 +17,9 @@ package controllers import ( "encoding/json" + "github.com/beego/beego/utils/pagination" "github.com/casbin/caswaf/object" + "github.com/casbin/caswaf/util" ) func (c *ApiController) GetRecords() { @@ -29,15 +31,39 @@ func (c *ApiController) GetRecords() { if owner == "admin" { owner = "" } - - sites, err := object.GetRecords(owner) - if err != nil { - c.ResponseError(err.Error()) - return + limit := c.Input().Get("pageSize") + page := c.Input().Get("p") + field := c.Input().Get("field") + value := c.Input().Get("value") + sortField := c.Input().Get("sortField") + sortOrder := c.Input().Get("sortOrder") + + if limit == "" || page == "" { + records, err := object.GetRecords(owner) + if err != nil { + c.ResponseError(err.Error()) + return + } + + // object.GetMaskedSites(sites, util.GetHostname()) + c.ResponseOk(records) + } else { + limit := util.ParseInt(limit) + count, err := object.GetRecordCount(owner, field, value) + if err != nil { + c.ResponseError(err.Error()) + return + } + + paginator := pagination.SetPaginator(c.Ctx, limit, count) + records, err := object.GetPaginationRecords(owner, paginator.Offset(), limit, field, value, sortField, sortOrder) + if err != nil { + c.ResponseError(err.Error()) + return + } + + c.ResponseOk(records, paginator.Nums()) } - - // object.GetMaskedSites(sites, util.GetHostname()) - c.ResponseOk(sites) } func (c *ApiController) DeleteRecord() { diff --git a/controllers/rule.go b/controllers/rule.go index d0d26f2..6a44669 100644 --- a/controllers/rule.go +++ b/controllers/rule.go @@ -20,6 +20,7 @@ import ( "net" "strings" + "github.com/beego/beego/utils/pagination" "github.com/casbin/caswaf/object" "github.com/casbin/caswaf/util" "github.com/hsluoyz/modsecurity-go/seclang/parser" @@ -33,14 +34,38 @@ func (c *ApiController) GetRules() { if owner == "admin" { owner = "" } + limit := c.Input().Get("pageSize") + page := c.Input().Get("p") + field := c.Input().Get("field") + value := c.Input().Get("value") + sortField := c.Input().Get("sortField") + sortOrder := c.Input().Get("sortOrder") - rules, err := object.GetRules(owner) - if err != nil { - c.ResponseError(err.Error()) - return - } + if limit == "" || page == "" { + rules, err := object.GetRules(owner) + if err != nil { + c.ResponseError(err.Error()) + return + } - c.ResponseOk(rules) + c.ResponseOk(rules) + } else { + limit := util.ParseInt(limit) + count, err := object.GetRuleCount(owner, field, value) + if err != nil { + c.ResponseError(err.Error()) + return + } + + paginator := pagination.SetPaginator(c.Ctx, limit, count) + rules, err := object.GetPaginationRules(owner, paginator.Offset(), limit, field, value, sortField, sortOrder) + if err != nil { + c.ResponseError(err.Error()) + return + } + + c.ResponseOk(rules, paginator.Nums()) + } } func (c *ApiController) GetRule() { diff --git a/object/record.go b/object/record.go index 6013b96..870f14c 100644 --- a/object/record.go +++ b/object/record.go @@ -143,3 +143,19 @@ func timeType2Format(timeType string) string { } return "%Y-%m-%d %H" } + +func GetRecordCount(owner, field, value string) (int64, error) { + session := GetSession(owner, -1, -1, field, value, "", "") + return session.Count(&Record{}) +} + +func GetPaginationRecords(owner string, offset, limit int, field, value, sortField, sortOrder string) ([]*Record, error) { + records := []*Record{} + session := GetSession(owner, offset, limit, field, value, sortField, sortOrder) + err := session.Where("owner = ? or owner = ?", "admin", owner).Find(&records) + if err != nil { + return records, err + } + + return records, nil +} diff --git a/object/rule.go b/object/rule.go index 7909cf2..1218d3e 100644 --- a/object/rule.go +++ b/object/rule.go @@ -114,3 +114,19 @@ func DeleteRule(rule *Rule) (bool, error) { func (rule *Rule) GetId() string { return fmt.Sprintf("%s/%s", rule.Owner, rule.Name) } + +func GetRuleCount(owner, field, value string) (int64, error) { + session := GetSession(owner, -1, -1, field, value, "", "") + return session.Count(&Rule{}) +} + +func GetPaginationRules(owner string, offset, limit int, field, value, sortField, sortOrder string) ([]*Rule, error) { + rules := []*Rule{} + session := GetSession(owner, offset, limit, field, value, sortField, sortOrder) + err := session.Where("owner = ? or owner = ?", "admin", owner).Find(&rules) + if err != nil { + return rules, err + } + + return rules, nil +} diff --git a/web/src/RecordListPage.js b/web/src/RecordListPage.js index c7fef48..847c35d 100644 --- a/web/src/RecordListPage.js +++ b/web/src/RecordListPage.js @@ -27,8 +27,12 @@ class RecordListPage extends BaseListPage { } fetch = (params = {}) => { + const sortField = params.sortField, sortOrder = params.sortOrder; + if (!params.pagination) { + params.pagination = {current: 1, pageSize: 10}; + } this.setState({loading: true}); - RecordBackend.getRecords(this.props.account.name) + RecordBackend.getRecords(this.props.account.name, params.pagination.current, params.pagination.pageSize, sortField, sortOrder) .then((res) => { this.setState({ loading: false, diff --git a/web/src/RuleListPage.js b/web/src/RuleListPage.js index cd27f2e..6f52e63 100644 --- a/web/src/RuleListPage.js +++ b/web/src/RuleListPage.js @@ -25,17 +25,21 @@ class RuleListPage extends BaseListPage { this.fetch(); } - fetch() { + fetch = (params = {}) => { + const sortField = params.sortField, sortOrder = params.sortOrder; + if (!params.pagination) { + params.pagination = {current: 1, pageSize: 10}; + } this.setState({ loading: true, }); - RuleBackend.getRules(this.props.account.name).then((res) => { + RuleBackend.getRules(this.props.account.name, params.pagination.current, params.pagination.pageSize, sortField, sortOrder).then((res) => { this.setState({ data: res.data, loading: false, }); }); - } + }; addRule() { const newRule = this.newRule(); diff --git a/web/src/backend/RecordBackend.js b/web/src/backend/RecordBackend.js index a05f87d..ca3f859 100644 --- a/web/src/backend/RecordBackend.js +++ b/web/src/backend/RecordBackend.js @@ -14,8 +14,8 @@ import * as Setting from "../Setting"; -export function getRecords(owner) { - return fetch(`${Setting.ServerUrl}/api/get-records?owner=${owner}`, { +export function getRecords(owner, page = "", pageSize = "", sortField = "", sortOrder = "") { + return fetch(`${Setting.ServerUrl}/api/get-records?owner=${owner}&p=${page}&pageSize=${pageSize}&sortField=${sortField}&sortOrder=${sortOrder}`, { method: "GET", credentials: "include", }).then(res => res.json()); diff --git a/web/src/backend/RuleBackend.js b/web/src/backend/RuleBackend.js index dc3b419..b2a097b 100644 --- a/web/src/backend/RuleBackend.js +++ b/web/src/backend/RuleBackend.js @@ -14,8 +14,8 @@ import * as Setting from "../Setting"; -export function getRules(owner) { - return fetch(`${Setting.ServerUrl}/api/get-rules?owner=${owner}`, { +export function getRules(owner, page = "", pageSize = "", sortField = "", sortOrder = "") { + return fetch(`${Setting.ServerUrl}/api/get-rules?owner=${owner}&p=${page}&pageSize=${pageSize}&sortField=${sortField}&sortOrder=${sortOrder}`, { method: "GET", credentials: "include", }).then(res => res.json());