Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 941132e

Browse files
committed
Added pagination on package listing
1 parent 9ad37fe commit 941132e

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

handler_apt_packages.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ import (
2828

2929
// AptListEvent sends a list of available packages and their status
3030
func (s *Status) AptListEvent(client mqtt.Client, msg mqtt.Message) {
31+
const ITEMS_PER_PAGE = 30
32+
3133
var params struct {
3234
Search string `json:"search"`
35+
Page int `json:"page"`
3336
}
3437
err := json.Unmarshal(msg.Payload(), &params)
3538
if err != nil {
@@ -47,17 +50,32 @@ func (s *Status) AptListEvent(client mqtt.Client, msg mqtt.Message) {
4750
return
4851
}
4952

50-
// On upgradable packages set the status to "upgradable"
51-
updates, err := apt.ListUpgradable()
53+
// Paginate data
54+
pages := (len(all)-1)/ITEMS_PER_PAGE + 1
55+
first := params.Page * ITEMS_PER_PAGE
56+
last := first + ITEMS_PER_PAGE
57+
if first >= len(all) {
58+
all = all[0:0]
59+
} else if last >= len(all) {
60+
all = all[first:]
61+
} else {
62+
all = all[first:last]
63+
}
64+
65+
// On upgradable packages set the status to "upgradable" and add the
66+
// available upgrade to the Updates list
67+
allUpdates, err := apt.ListUpgradable()
5268
if err != nil {
5369
s.Error("/apt/list/error", fmt.Errorf("Retrieving packages: %s", err))
5470
return
5571
}
5672

57-
for _, update := range updates {
73+
updates := []*apt.Package{}
74+
for _, update := range allUpdates {
5875
for i := range all {
5976
if update.Name == all[i].Name {
6077
all[i].Status = "upgradable"
78+
updates = append(updates, update)
6179
break
6280
}
6381
}
@@ -67,10 +85,14 @@ func (s *Status) AptListEvent(client mqtt.Client, msg mqtt.Message) {
6785
type response struct {
6886
Packages []*apt.Package `json:"packages"`
6987
Updates []*apt.Package `json:"updates"`
88+
Page int `json:"page"`
89+
Pages int `json:"pages"`
7090
}
7191
info := response{
7292
Packages: all,
7393
Updates: updates,
94+
Page: params.Page,
95+
Pages: pages,
7496
}
7597

7698
// Send result

0 commit comments

Comments
 (0)