Skip to content

Commit

Permalink
feat: add pagination to rule and record pages (#61)
Browse files Browse the repository at this point in the history
* feat: sorted data for rule and record

* Update record.go

* Update rule.go

---------

Co-authored-by: Gucheng <[email protected]>
  • Loading branch information
chatiti and nomeguy authored Aug 9, 2024
1 parent b957f74 commit 935437d
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 22 deletions.
42 changes: 34 additions & 8 deletions controllers/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down
37 changes: 31 additions & 6 deletions controllers/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down
16 changes: 16 additions & 0 deletions object/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
16 changes: 16 additions & 0 deletions object/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 5 additions & 1 deletion web/src/RecordListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 7 additions & 3 deletions web/src/RuleListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions web/src/backend/RecordBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions web/src/backend/RuleBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 935437d

Please sign in to comment.