brunel/handlers/build/queue.go

58 lines
1.4 KiB
Go
Raw Normal View History

2024-07-29 23:40:26 +02:00
package handlers_build
import (
"brunel/buildqueue"
"brunel/domain"
"brunel/fastmap"
"strings"
"github.com/gofiber/fiber/v2"
)
type QueueResponse struct {
Total int `json:"total"`
Packages *fastmap.Fastmap[string, domain.BuildQueueItem] `json:"packages"`
}
func Queue(c *fiber.Ctx) error {
pageNum := c.QueryInt("page", 1)
pageSize := c.QueryInt("pageSize", 250)
search := strings.ToLower(c.Query("search"))
filter := c.Query("filter")
// Adjust pageNum to be 0-based for GetPage
adjustedPageNum := pageNum - 1
if adjustedPageNum < 0 {
adjustedPageNum = 0
}
packs := buildqueue.GetQueue()
packs.StableSortByKey()
finalReturn := fastmap.New[string, domain.BuildQueueItem]()
packs.Iter(func(k string, source domain.BuildQueueItem) bool {
matchesSearch := search == "" || strings.Contains(strings.ToLower(k), search)
matchesFilter := filter == "" || source.Status == domain.BuildStatus(filter)
source.Source.Packages.Iter(func(key string, value domain.PackageInfo) bool {
if !matchesSearch && strings.Contains(strings.ToLower(key), search) {
matchesSearch = true
}
return !(matchesSearch)
})
if matchesFilter && matchesSearch {
finalReturn.Set(k, source)
}
return true
})
result := finalReturn.GetPage(adjustedPageNum, pageSize)
response := QueueResponse{
Total: finalReturn.Len(),
Packages: result,
}
return c.Status(fiber.StatusOK).JSON(response)
}