brunel/handlers/build/queue.go

71 lines
1.7 KiB
Go
Raw Normal View History

2024-07-29 23:40:26 +02:00
package handlers_build
import (
"brunel/buildqueue"
"brunel/domain"
2024-07-30 03:07:55 +02:00
"sort"
2024-07-29 23:40:26 +02:00
"strings"
"github.com/gofiber/fiber/v2"
)
type QueueResponse struct {
2024-07-30 03:07:55 +02:00
Total int `json:"total"`
Packages []domain.BuildQueueItem `json:"packages"`
2024-07-29 23:40:26 +02:00
}
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
}
2024-07-30 00:49:30 +02:00
packs := buildqueue.GetQueue()
2024-07-30 03:07:55 +02:00
finalReturn := make([]domain.BuildQueueItem, 0)
2024-07-29 23:40:26 +02:00
2024-07-30 00:49:30 +02:00
packs.ForEach(func(k string, source domain.BuildQueueItem) bool {
2024-07-29 23:40:26 +02:00
matchesSearch := search == "" || strings.Contains(strings.ToLower(k), search)
matchesFilter := filter == "" || source.Status == domain.BuildStatus(filter)
2024-07-30 02:24:18 +02:00
source.Source.Packages.ForEach(func(key string, value domain.PackageInfo) bool {
2024-07-29 23:40:26 +02:00
if !matchesSearch && strings.Contains(strings.ToLower(key), search) {
matchesSearch = true
}
return !(matchesSearch)
})
if matchesFilter && matchesSearch {
2024-07-30 03:07:55 +02:00
finalReturn = append(finalReturn, source)
2024-07-29 23:40:26 +02:00
}
return true
})
2024-07-30 03:07:55 +02:00
sort.Slice(finalReturn, func(i, j int) bool {
return finalReturn[i].Source.Name < finalReturn[j].Source.Name
})
2024-07-30 03:22:38 +02:00
var result []domain.BuildQueueItem
startIndex := adjustedPageNum * pageSize
endIndex := (adjustedPageNum + 1) * pageSize
if startIndex >= len(finalReturn) {
result = []domain.BuildQueueItem{}
} else {
if endIndex > len(finalReturn) {
endIndex = len(finalReturn)
}
result = finalReturn[startIndex:endIndex]
}
2024-07-29 23:40:26 +02:00
response := QueueResponse{
2024-07-30 03:07:55 +02:00
Total: len(finalReturn),
2024-07-29 23:40:26 +02:00
Packages: result,
}
return c.Status(fiber.StatusOK).JSON(response)
}