brunel/handlers/build/queue.go
2024-07-30 01:24:18 +01:00

57 lines
1.4 KiB
Go

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()
finalReturn := fastmap.New[string, domain.BuildQueueItem]()
packs.ForEach(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.ForEach(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)
}