Skip to content

Modern Docker Management, Designed for Everyone

fix: sort and filter properly using docker api (#593)

Kyle Mendell6c7386469509
diff --git a/.gitignore b/.gitignore
index 491a2574a..18d382736 100644
--- a/.gitignore
+++ b/.gitignore
@@ -37,12 +37,4 @@ backend/build-errors.log
frontend/bun.lock
tests/bun.lock
tests/package-lock.json
-/.idea/.gitignore
-/.idea/arcane.iml
-/.idea/copilot.data.migration.agent.xml
-/.idea/copilot.data.migration.ask.xml
-/.idea/copilot.data.migration.edit.xml
-/.idea/modules.xml
-/.idea/prettier.xml
-/.idea/inspectionProfiles/Project_Default.xml
-/.idea/vcs.xml
+/.idea/**
diff --git a/backend/internal/api/container_handler.go b/backend/internal/api/container_handler.go
index ee33a383d..25b6c4f42 100644
--- a/backend/internal/api/container_handler.go
+++ b/backend/internal/api/container_handler.go
@@ -17,7 +17,7 @@ import (
"github.com/ofkm/arcane-backend/internal/dto"
"github.com/ofkm/arcane-backend/internal/middleware"
"github.com/ofkm/arcane-backend/internal/services"
- "github.com/ofkm/arcane-backend/internal/utils"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
ws "github.com/ofkm/arcane-backend/internal/utils/ws"
)
@@ -185,25 +185,15 @@ func (h *ContainerHandler) PullImage(c *gin.Context) {
}
func (h *ContainerHandler) List(c *gin.Context) {
- var req utils.SortedPaginationRequest
- if err := c.ShouldBindQuery(&req); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "success": false,
- "data": gin.H{"error": "Invalid pagination or sort parameters: " + err.Error()},
- })
- return
- }
+ params := pagination.ExtractListModifiersQueryParams(c)
- if req.Pagination.Page == 0 {
- req.Pagination.Page = 1
- }
- if req.Pagination.Limit == 0 {
- req.Pagination.Limit = 20
+ if params.Limit == 0 {
+ params.Limit = 20
}
includeAll := true
- containers, pagination, err := h.containerService.ListContainersPaginated(c.Request.Context(), req, includeAll)
+ containers, paginationResp, err := h.containerService.ListContainersPaginated(c.Request.Context(), params, includeAll)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
@@ -212,10 +202,16 @@ func (h *ContainerHandler) List(c *gin.Context) {
return
}
+ pagination.ApplyFilterResultsHeaders(&c.Writer, pagination.FilterResult[dto.ContainerSummaryDto]{
+ Items: containers,
+ TotalCount: int(paginationResp.TotalItems),
+ TotalAvailable: int(paginationResp.GrandTotalItems),
+ })
+
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": containers,
- "pagination": pagination,
+ "pagination": paginationResp,
})
}
diff --git a/backend/internal/api/container_registry_handler.go b/backend/internal/api/container_registry_handler.go
index 00c48fe95..7fb78150b 100644
--- a/backend/internal/api/container_registry_handler.go
+++ b/backend/internal/api/container_registry_handler.go
@@ -12,6 +12,7 @@ import (
"github.com/ofkm/arcane-backend/internal/models"
"github.com/ofkm/arcane-backend/internal/services"
"github.com/ofkm/arcane-backend/internal/utils"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
registry "github.com/ofkm/arcane-backend/internal/utils/registry"
)
@@ -36,23 +37,9 @@ func NewContainerRegistryHandler(group *gin.RouterGroup, registryService *servic
}
func (h *ContainerRegistryHandler) GetRegistries(c *gin.Context) {
- var req utils.SortedPaginationRequest
- if err := c.ShouldBindQuery(&req); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "success": false,
- "data": gin.H{"error": "Invalid pagination or sort parameters: " + err.Error()},
- })
- return
- }
-
- if req.Pagination.Page == 0 {
- req.Pagination.Page = 1
- }
- if req.Pagination.Limit == 0 {
- req.Pagination.Limit = 20
- }
+ params := pagination.ExtractListModifiersQueryParams(c)
- registries, pagination, err := h.registryService.GetRegistriesPaginated(c.Request.Context(), req)
+ registries, paginationResp, err := h.registryService.GetRegistriesPaginated(c.Request.Context(), params)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
@@ -64,7 +51,7 @@ func (h *ContainerRegistryHandler) GetRegistries(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": registries,
- "pagination": pagination,
+ "pagination": paginationResp,
})
}
diff --git a/backend/internal/api/environment_handler.go b/backend/internal/api/environment_handler.go
index 8c829ca15..e8eb4bbba 100644
--- a/backend/internal/api/environment_handler.go
+++ b/backend/internal/api/environment_handler.go
@@ -10,6 +10,7 @@ import (
"github.com/ofkm/arcane-backend/internal/models"
"github.com/ofkm/arcane-backend/internal/services"
"github.com/ofkm/arcane-backend/internal/utils"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
)
const LOCAL_DOCKER_ENVIRONMENT_ID = "0"
@@ -123,19 +124,9 @@ func (h *EnvironmentHandler) CreateEnvironment(c *gin.Context) {
}
func (h *EnvironmentHandler) ListEnvironments(c *gin.Context) {
- var req utils.SortedPaginationRequest
- if err := c.ShouldBindQuery(&req); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"success": false, "data": gin.H{"error": "Invalid pagination or sort parameters: " + err.Error()}})
- return
- }
- if req.Pagination.Page == 0 {
- req.Pagination.Page = 1
- }
- if req.Pagination.Limit == 0 {
- req.Pagination.Limit = 20
- }
+ params := pagination.ExtractListModifiersQueryParams(c)
- envs, pagination, err := h.environmentService.ListEnvironmentsPaginated(c.Request.Context(), req)
+ envs, paginationResp, err := h.environmentService.ListEnvironmentsPaginated(c.Request.Context(), params)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "data": gin.H{"error": "Failed to fetch environments"}})
return
@@ -144,7 +135,7 @@ func (h *EnvironmentHandler) ListEnvironments(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": envs,
- "pagination": pagination,
+ "pagination": paginationResp,
})
}
diff --git a/backend/internal/api/event_handler.go b/backend/internal/api/event_handler.go
index d2dba52b3..9d2265f67 100644
--- a/backend/internal/api/event_handler.go
+++ b/backend/internal/api/event_handler.go
@@ -7,7 +7,7 @@ import (
"github.com/ofkm/arcane-backend/internal/dto"
"github.com/ofkm/arcane-backend/internal/middleware"
"github.com/ofkm/arcane-backend/internal/services"
- "github.com/ofkm/arcane-backend/internal/utils"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
)
type EventHandler struct {
@@ -28,23 +28,9 @@ func NewEventHandler(group *gin.RouterGroup, eventService *services.EventService
}
func (h *EventHandler) ListEvents(c *gin.Context) {
- var req utils.SortedPaginationRequest
- if err := c.ShouldBindQuery(&req); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "success": false,
- "data": gin.H{"error": "Invalid pagination or sort parameters: " + err.Error()},
- })
- return
- }
-
- if req.Pagination.Page == 0 {
- req.Pagination.Page = 1
- }
- if req.Pagination.Limit == 0 {
- req.Pagination.Limit = 20
- }
+ params := pagination.ExtractListModifiersQueryParams(c)
- events, pagination, err := h.eventService.ListEventsPaginated(c.Request.Context(), req)
+ events, paginationResp, err := h.eventService.ListEventsPaginated(c.Request.Context(), params)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
@@ -56,7 +42,7 @@ func (h *EventHandler) ListEvents(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": events,
- "pagination": pagination,
+ "pagination": paginationResp,
})
}
@@ -70,23 +56,9 @@ func (h *EventHandler) GetEventsByEnvironment(c *gin.Context) {
return
}
- var req utils.SortedPaginationRequest
- if err := c.ShouldBindQuery(&req); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "success": false,
- "data": gin.H{"error": "Invalid pagination or sort parameters: " + err.Error()},
- })
- return
- }
-
- if req.Pagination.Page == 0 {
- req.Pagination.Page = 1
- }
- if req.Pagination.Limit == 0 {
- req.Pagination.Limit = 20
- }
+ params := pagination.ExtractListModifiersQueryParams(c)
- events, pagination, err := h.eventService.GetEventsByEnvironmentPaginated(c.Request.Context(), environmentID, req)
+ events, paginationResp, err := h.eventService.GetEventsByEnvironmentPaginated(c.Request.Context(), environmentID, params)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
@@ -98,7 +70,7 @@ func (h *EventHandler) GetEventsByEnvironment(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": events,
- "pagination": pagination,
+ "pagination": paginationResp,
})
}
diff --git a/backend/internal/api/image_handler.go b/backend/internal/api/image_handler.go
index 753f9779c..d2d73ce91 100644
--- a/backend/internal/api/image_handler.go
+++ b/backend/internal/api/image_handler.go
@@ -10,7 +10,7 @@ import (
"github.com/ofkm/arcane-backend/internal/dto"
"github.com/ofkm/arcane-backend/internal/middleware"
"github.com/ofkm/arcane-backend/internal/services"
- "github.com/ofkm/arcane-backend/internal/utils"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
)
type ImageHandler struct {
@@ -35,23 +35,13 @@ func NewImageHandler(group *gin.RouterGroup, dockerService *services.DockerClien
}
func (h *ImageHandler) List(c *gin.Context) {
- var req utils.SortedPaginationRequest
- if err := c.ShouldBindQuery(&req); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "success": false,
- "data": dto.MessageDto{Message: "Invalid pagination or sort parameters: " + err.Error()},
- })
- return
- }
+ params := pagination.ExtractListModifiersQueryParams(c)
- if req.Pagination.Page == 0 {
- req.Pagination.Page = 1
- }
- if req.Pagination.Limit == 0 {
- req.Pagination.Limit = 20
+ if params.Limit == 0 {
+ params.Limit = 20
}
- images, pagination, err := h.imageService.ListImagesWithUpdatesPaginated(c.Request.Context(), req)
+ images, paginationResp, err := h.imageService.ListImagesPaginated(c.Request.Context(), params)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
@@ -60,10 +50,16 @@ func (h *ImageHandler) List(c *gin.Context) {
return
}
+ pagination.ApplyFilterResultsHeaders(&c.Writer, pagination.FilterResult[dto.ImageSummaryDto]{
+ Items: images,
+ TotalCount: int(paginationResp.TotalItems),
+ TotalAvailable: int(paginationResp.GrandTotalItems),
+ })
+
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": images,
- "pagination": pagination,
+ "pagination": paginationResp,
})
}
@@ -136,7 +132,7 @@ func (h *ImageHandler) Pull(c *gin.Context) {
return
}
- slog.InfoContext(ctx, "Image pull stream completed and database sync attempted",
+ slog.InfoContext(ctx, "Image pull stream completed",
slog.String("imageName", req.ImageName))
}
diff --git a/backend/internal/api/network_handler.go b/backend/internal/api/network_handler.go
index fd4bcd38d..5c0ea320e 100644
--- a/backend/internal/api/network_handler.go
+++ b/backend/internal/api/network_handler.go
@@ -8,7 +8,7 @@ import (
"github.com/ofkm/arcane-backend/internal/dto"
"github.com/ofkm/arcane-backend/internal/middleware"
"github.com/ofkm/arcane-backend/internal/services"
- "github.com/ofkm/arcane-backend/internal/utils"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
)
type NetworkHandler struct {
@@ -32,23 +32,13 @@ func NewNetworkHandler(group *gin.RouterGroup, dockerService *services.DockerCli
}
func (h *NetworkHandler) List(c *gin.Context) {
- var req utils.SortedPaginationRequest
- if err := c.ShouldBindQuery(&req); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "success": false,
- "data": dto.MessageDto{Message: "Invalid pagination or sort parameters: " + err.Error()},
- })
- return
- }
+ params := pagination.ExtractListModifiersQueryParams(c)
- if req.Pagination.Page == 0 {
- req.Pagination.Page = 1
- }
- if req.Pagination.Limit == 0 {
- req.Pagination.Limit = 20
+ if params.Limit == 0 {
+ params.Limit = 20
}
- networks, pagination, err := h.networkService.ListNetworksPaginated(c.Request.Context(), req)
+ networks, paginationResp, err := h.networkService.ListNetworksPaginated(c.Request.Context(), params)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
@@ -57,10 +47,16 @@ func (h *NetworkHandler) List(c *gin.Context) {
return
}
+ pagination.ApplyFilterResultsHeaders(&c.Writer, pagination.FilterResult[dto.NetworkSummaryDto]{
+ Items: networks,
+ TotalCount: int(paginationResp.TotalItems),
+ TotalAvailable: int(paginationResp.GrandTotalItems),
+ })
+
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": networks,
- "pagination": pagination,
+ "pagination": paginationResp,
})
}
diff --git a/backend/internal/api/project_handler.go b/backend/internal/api/project_handler.go
index 09a0d40d0..c258a802a 100644
--- a/backend/internal/api/project_handler.go
+++ b/backend/internal/api/project_handler.go
@@ -16,6 +16,7 @@ import (
"github.com/ofkm/arcane-backend/internal/middleware"
"github.com/ofkm/arcane-backend/internal/services"
"github.com/ofkm/arcane-backend/internal/utils"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
ws "github.com/ofkm/arcane-backend/internal/utils/ws"
)
@@ -46,11 +47,12 @@ func NewProjectHandler(group *gin.RouterGroup, projectService *services.ProjectS
apiGroup := group.Group("/environments/:id/projects")
apiGroup.Use(authMiddleware.WithAdminNotRequired().Add())
{
+
+ apiGroup.GET("", handler.ListProjects)
apiGroup.GET("/counts", handler.GetProjectStatusCounts)
- apiGroup.GET("/", handler.ListProjects)
apiGroup.POST("/:projectId/up", handler.DeployProject)
apiGroup.POST("/:projectId/down", handler.DownProject)
- apiGroup.POST("/", handler.CreateProject)
+ apiGroup.POST("", handler.CreateProject)
apiGroup.GET("/:projectId", handler.GetProject)
apiGroup.POST("/:projectId/pull", handler.PullProjectImages)
apiGroup.POST("/:projectId/redeploy", handler.RedeployProject)
@@ -63,16 +65,9 @@ func NewProjectHandler(group *gin.RouterGroup, projectService *services.ProjectS
}
func (h *ProjectHandler) ListProjects(c *gin.Context) {
- var req utils.SortedPaginationRequest
- if err := c.ShouldBindQuery(&req); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "success": false,
- "error": "Invalid pagination or sort parameters: " + err.Error(),
- })
- return
- }
+ params := pagination.ExtractListModifiersQueryParams(c)
- projectsResponse, pagination, err := h.projectService.ListProjects(c.Request.Context(), req)
+ projectsResponse, paginationResp, err := h.projectService.ListProjects(c.Request.Context(), params)
if err != nil {
if errors.Is(err, context.Canceled) {
c.JSON(http.StatusRequestTimeout, gin.H{
@@ -94,7 +89,7 @@ func (h *ProjectHandler) ListProjects(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": projectsResponse,
- "pagination": pagination,
+ "pagination": paginationResp,
})
}
diff --git a/backend/internal/api/user_handler.go b/backend/internal/api/user_handler.go
index 7186e2263..d70819c9a 100644
--- a/backend/internal/api/user_handler.go
+++ b/backend/internal/api/user_handler.go
@@ -9,7 +9,7 @@ import (
"github.com/ofkm/arcane-backend/internal/middleware"
"github.com/ofkm/arcane-backend/internal/models"
"github.com/ofkm/arcane-backend/internal/services"
- "github.com/ofkm/arcane-backend/internal/utils"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
)
type UserHandler struct {
@@ -32,49 +32,9 @@ func NewUserHandler(group *gin.RouterGroup, userService *services.UserService, a
}
func (h *UserHandler) ListUsers(c *gin.Context) {
- var req utils.SortedPaginationRequest
- if err := c.ShouldBindQuery(&req); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "success": false,
- "data": gin.H{"error": "Invalid pagination or sort parameters: " + err.Error()},
- })
- return
- }
-
- if req.Pagination.Page == 0 {
- req.Pagination.Page = 1
- }
- if req.Pagination.Limit == 0 {
- req.Pagination.Limit = 20
- }
-
- if req.Pagination.Page == 1 && req.Pagination.Limit == 20 && req.Search == "" && req.Sort.Column == "" {
- users, err := h.userService.ListUsers(c.Request.Context())
- if err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{
- "success": false,
- "data": gin.H{"error": "Failed to fetch users"},
- })
- return
- }
-
- userResponses, err := dto.MapSlice[models.User, dto.UserResponseDto](users)
- if err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{
- "success": false,
- "data": gin.H{"error": "Failed to map users"},
- })
- return
- }
-
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "data": userResponses,
- })
- return
- }
+ params := pagination.ExtractListModifiersQueryParams(c)
- users, pagination, err := h.userService.ListUsersPaginated(c.Request.Context(), req)
+ users, paginationResp, err := h.userService.ListUsersPaginated(c.Request.Context(), params)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
@@ -86,7 +46,7 @@ func (h *UserHandler) ListUsers(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": users,
- "pagination": pagination,
+ "pagination": paginationResp,
})
}
diff --git a/backend/internal/api/volume_handler.go b/backend/internal/api/volume_handler.go
index 277fc5163..baae82cef 100644
--- a/backend/internal/api/volume_handler.go
+++ b/backend/internal/api/volume_handler.go
@@ -8,7 +8,7 @@ import (
"github.com/ofkm/arcane-backend/internal/dto"
"github.com/ofkm/arcane-backend/internal/middleware"
"github.com/ofkm/arcane-backend/internal/services"
- "github.com/ofkm/arcane-backend/internal/utils"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
)
type VolumeHandler struct {
@@ -33,25 +33,13 @@ func NewVolumeHandler(group *gin.RouterGroup, dockerService *services.DockerClie
}
func (h *VolumeHandler) List(c *gin.Context) {
- var req utils.SortedPaginationRequest
- if err := c.ShouldBindQuery(&req); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "success": false,
- "data": gin.H{"error": "Invalid pagination or sort parameters: " + err.Error()},
- })
- return
- }
+ params := pagination.ExtractListModifiersQueryParams(c)
- if req.Pagination.Page == 0 {
- req.Pagination.Page = 1
+ if params.Limit == 0 {
+ params.Limit = 20
}
- if req.Pagination.Limit == 0 {
- req.Pagination.Limit = 20
- }
-
- driver := c.Query("driver")
- volumes, pagination, err := h.volumeService.ListVolumesPaginated(c.Request.Context(), req, driver)
+ volumes, paginationResp, err := h.volumeService.ListVolumesPaginated(c.Request.Context(), params)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
@@ -60,10 +48,16 @@ func (h *VolumeHandler) List(c *gin.Context) {
return
}
+ pagination.ApplyFilterResultsHeaders(&c.Writer, pagination.FilterResult[dto.VolumeDto]{
+ Items: volumes,
+ TotalCount: int(paginationResp.TotalItems),
+ TotalAvailable: int(paginationResp.GrandTotalItems),
+ })
+
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": volumes,
- "pagination": pagination,
+ "pagination": paginationResp,
})
}
diff --git a/backend/internal/bootstrap/bootstrap.go b/backend/internal/bootstrap/bootstrap.go
index cf64d7ca2..f2d5bd3a0 100644
--- a/backend/internal/bootstrap/bootstrap.go
+++ b/backend/internal/bootstrap/bootstrap.go
@@ -109,14 +109,6 @@ func InitializeApp() (*App, error) {
dockerClient.Close()
}
- slog.InfoContext(appCtx, "Performing initial Docker image synchronization with the database")
- if err := appServices.Image.SyncDockerImages(appCtx); err != nil {
- slog.WarnContext(appCtx, "Initial Docker image synchronization failed, image data may be stale",
- slog.String("error", err.Error()))
- } else {
- slog.InfoContext(appCtx, "Initial Docker image synchronization complete")
- }
-
if !cfg.AgentMode {
if err := appServices.User.CreateDefaultAdmin(); err != nil {
slog.WarnContext(appCtx, "Failed to create default admin user",
diff --git a/backend/internal/models/user.go b/backend/internal/models/user.go
index 51ebdd75e..2f7fb0400 100644
--- a/backend/internal/models/user.go
+++ b/backend/internal/models/user.go
@@ -5,13 +5,13 @@ import (
)
type User struct {
- Username string `json:"username" gorm:"uniqueIndex;not null"`
+ Username string `json:"username" gorm:"uniqueIndex;not null" sortable:"true"`
PasswordHash string `json:"-" gorm:"column:password_hash"`
- DisplayName *string `json:"displayName,omitempty" gorm:"column:display_name"`
- Email *string `json:"email,omitempty"`
+ DisplayName *string `json:"displayName,omitempty" gorm:"column:display_name" sortable:"true"`
+ Email *string `json:"email,omitempty" sortable:"true"`
Roles StringSlice `json:"roles" gorm:"type:text;not null;default:'[]'"`
OidcSubjectId *string `json:"oidcSubjectId,omitempty" gorm:"column:oidc_subject_id"`
- LastLogin *time.Time `json:"lastLogin,omitempty" gorm:"column:last_login"`
+ LastLogin *time.Time `json:"lastLogin,omitempty" gorm:"column:last_login" sortable:"true"`
Locale *string `json:"locale,omitempty" gorm:"column:locale"`
// OIDC provider tokens
diff --git a/backend/internal/services/container_registry_service.go b/backend/internal/services/container_registry_service.go
index 31ad4b318..f0c5893f5 100644
--- a/backend/internal/services/container_registry_service.go
+++ b/backend/internal/services/container_registry_service.go
@@ -10,6 +10,7 @@ import (
"github.com/ofkm/arcane-backend/internal/dto"
"github.com/ofkm/arcane-backend/internal/models"
"github.com/ofkm/arcane-backend/internal/utils"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
)
type ContainerRegistryService struct {
@@ -28,26 +29,47 @@ func (s *ContainerRegistryService) GetAllRegistries(ctx context.Context) ([]mode
return registries, nil
}
-func (s *ContainerRegistryService) GetRegistriesPaginated(ctx context.Context, req utils.SortedPaginationRequest) ([]dto.ContainerRegistryDto, utils.PaginationResponse, error) {
+func (s *ContainerRegistryService) GetRegistriesPaginated(ctx context.Context, params pagination.QueryParams) ([]dto.ContainerRegistryDto, pagination.Response, error) {
var registries []models.ContainerRegistry
- query := s.db.WithContext(ctx).Model(&models.ContainerRegistry{})
+ q := s.db.WithContext(ctx).Model(&models.ContainerRegistry{})
+
+ if term := strings.TrimSpace(params.Search); term != "" {
+ searchPattern := "%" + term + "%"
+ q = q.Where(
+ "url LIKE ? OR username LIKE ? OR COALESCE(description, '') LIKE ?",
+ searchPattern, searchPattern, searchPattern,
+ )
+ }
+
+ if enabled := params.Filters["enabled"]; enabled != "" {
+ switch enabled {
+ case "true", "1":
+ q = q.Where("enabled = ?", true)
+ case "false", "0":
+ q = q.Where("enabled = ?", false)
+ }
+ }
- if req.Search != "" {
- searchTerm := "%" + strings.ToLower(req.Search) + "%"
- query = query.Where("LOWER(url) LIKE ? OR LOWER(username) LIKE ? OR LOWER(description) LIKE ?", searchTerm, searchTerm, searchTerm)
+ if insecure := params.Filters["insecure"]; insecure != "" {
+ switch insecure {
+ case "true", "1":
+ q = q.Where("insecure = ?", true)
+ case "false", "0":
+ q = q.Where("insecure = ?", false)
+ }
}
- pagination, err := utils.PaginateAndSort(req, query, &registries)
+ paginationResp, err := pagination.PaginateAndSortDB(params, q, &registries)
if err != nil {
- return nil, utils.PaginationResponse{}, fmt.Errorf("failed to paginate container registries: %w", err)
+ return nil, pagination.Response{}, fmt.Errorf("failed to paginate container registries: %w", err)
}
out, mapErr := dto.MapSlice[models.ContainerRegistry, dto.ContainerRegistryDto](registries)
if mapErr != nil {
- return nil, utils.PaginationResponse{}, fmt.Errorf("failed to map registries: %w", mapErr)
+ return nil, pagination.Response{}, fmt.Errorf("failed to map registries: %w", mapErr)
}
- return out, pagination, nil
+ return out, paginationResp, nil
}
func (s *ContainerRegistryService) GetRegistryByID(ctx context.Context, id string) (*models.ContainerRegistry, error) {
diff --git a/backend/internal/services/container_service.go b/backend/internal/services/container_service.go
index 19007552b..ee8d35118 100644
--- a/backend/internal/services/container_service.go
+++ b/backend/internal/services/container_service.go
@@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
- "sort"
"strings"
"github.com/docker/docker/api/types/container"
@@ -17,7 +16,7 @@ import (
"github.com/ofkm/arcane-backend/internal/database"
"github.com/ofkm/arcane-backend/internal/dto"
"github.com/ofkm/arcane-backend/internal/models"
- "github.com/ofkm/arcane-backend/internal/utils"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
)
type ContainerService struct {
@@ -406,126 +405,102 @@ func (s *ContainerService) readAllLogs(logs io.ReadCloser, logsChan chan<- strin
return nil
}
-//nolint:gocognit
-func (s *ContainerService) ListContainersPaginated(ctx context.Context, req utils.SortedPaginationRequest, includeAll bool) ([]dto.ContainerSummaryDto, utils.PaginationResponse, error) {
+func (s *ContainerService) ListContainersPaginated(ctx context.Context, params pagination.QueryParams, includeAll bool) ([]dto.ContainerSummaryDto, pagination.Response, error) {
dockerClient, err := s.dockerService.CreateConnection(ctx)
if err != nil {
- return nil, utils.PaginationResponse{}, fmt.Errorf("failed to connect to Docker: %w", err)
+ return nil, pagination.Response{}, fmt.Errorf("failed to connect to Docker: %w", err)
}
defer dockerClient.Close()
dockerContainers, err := dockerClient.ContainerList(ctx, container.ListOptions{All: includeAll})
if err != nil {
- return nil, utils.PaginationResponse{}, fmt.Errorf("failed to list Docker containers: %w", err)
+ return nil, pagination.Response{}, fmt.Errorf("failed to list Docker containers: %w", err)
}
- grandTotal := len(dockerContainers)
-
- // Map to DTOs
- result := make([]dto.ContainerSummaryDto, 0, len(dockerContainers))
+ items := make([]dto.ContainerSummaryDto, 0, len(dockerContainers))
for _, dc := range dockerContainers {
- result = append(result, dto.NewContainerSummaryDto(dc))
- }
-
- // Search filter
- if req.Search != "" {
- searchLower := strings.ToLower(req.Search)
- filtered := make([]dto.ContainerSummaryDto, 0, len(result))
- for _, c := range result {
- matched := false
- for _, name := range c.Names {
- if strings.Contains(strings.ToLower(name), searchLower) {
- matched = true
- break
- }
- }
- if !matched && strings.Contains(strings.ToLower(c.Image), searchLower) {
- matched = true
- }
- if !matched && strings.Contains(strings.ToLower(c.State), searchLower) {
- matched = true
- }
- if matched {
- filtered = append(filtered, c)
- }
- }
- result = filtered
- }
-
- totalItems := len(result)
-
- // Sort
- if req.Sort.Column != "" {
- sortContainers(result, req.Sort.Column, req.Sort.Direction)
- }
-
- // Pagination window
- startIdx := (req.Pagination.Page - 1) * req.Pagination.Limit
- endIdx := startIdx + req.Pagination.Limit
- if startIdx > len(result) {
- startIdx = len(result)
- }
- if endIdx > len(result) {
- endIdx = len(result)
- }
- pageItems := []dto.ContainerSummaryDto{}
- if startIdx < endIdx {
- pageItems = result[startIdx:endIdx]
- }
-
- totalPages := (totalItems + req.Pagination.Limit - 1) / req.Pagination.Limit
- pagination := utils.PaginationResponse{
- TotalPages: int64(totalPages),
- TotalItems: int64(totalItems),
- CurrentPage: req.Pagination.Page,
- ItemsPerPage: req.Pagination.Limit,
- GrandTotalItems: int64(grandTotal),
+ items = append(items, dto.NewContainerSummaryDto(dc))
}
- return pageItems, pagination, nil
-}
-
-func sortContainers(items []dto.ContainerSummaryDto, field, direction string) {
- dir := utils.NormalizeSortDirection(direction)
- f := strings.ToLower(strings.TrimSpace(field))
-
- desc := dir == "desc"
- lessStr := func(a, b string) bool {
- if desc {
- return a > b
- }
- return a < b
- }
- lessInt := func(a, b int64) bool {
- if desc {
- return a > b
- }
- return a < b
- }
-
- sort.Slice(items, func(i, j int) bool {
- a, b := items[i], items[j]
- switch f {
- case "name", "names":
- var n1, n2 string
- if len(a.Names) > 0 {
- n1 = a.Names[0]
- }
- if len(b.Names) > 0 {
- n2 = b.Names[0]
- }
- return lessStr(n1, n2)
- case "image":
- return lessStr(a.Image, b.Image)
- case "state":
- return lessStr(a.State, b.State)
- case "status":
- return lessStr(a.Status, b.Status)
- case "created":
- return lessInt(a.Created, b.Created)
- default:
- // no-op sort keeps original order
- return false
- }
- })
+ config := pagination.Config[dto.ContainerSummaryDto]{
+ SearchAccessors: []pagination.SearchAccessor[dto.ContainerSummaryDto]{
+ func(c dto.ContainerSummaryDto) (string, error) {
+ if len(c.Names) > 0 {
+ return c.Names[0], nil
+ }
+ return "", nil
+ },
+ func(c dto.ContainerSummaryDto) (string, error) { return c.Image, nil },
+ func(c dto.ContainerSummaryDto) (string, error) { return c.State, nil },
+ func(c dto.ContainerSummaryDto) (string, error) { return c.Status, nil },
+ },
+ SortBindings: []pagination.SortBinding[dto.ContainerSummaryDto]{
+ {
+ Key: "name",
+ Fn: func(a, b dto.ContainerSummaryDto) int {
+ nameA := ""
+ if len(a.Names) > 0 {
+ nameA = a.Names[0]
+ }
+ nameB := ""
+ if len(b.Names) > 0 {
+ nameB = b.Names[0]
+ }
+ return strings.Compare(nameA, nameB)
+ },
+ },
+ {
+ Key: "image",
+ Fn: func(a, b dto.ContainerSummaryDto) int {
+ return strings.Compare(a.Image, b.Image)
+ },
+ },
+ {
+ Key: "state",
+ Fn: func(a, b dto.ContainerSummaryDto) int {
+ return strings.Compare(a.State, b.State)
+ },
+ },
+ {
+ Key: "status",
+ Fn: func(a, b dto.ContainerSummaryDto) int {
+ return strings.Compare(a.Status, b.Status)
+ },
+ },
+ {
+ Key: "created",
+ Fn: func(a, b dto.ContainerSummaryDto) int {
+ if a.Created < b.Created {
+ return -1
+ }
+ if a.Created > b.Created {
+ return 1
+ }
+ return 0
+ },
+ },
+ },
+ }
+
+ result := pagination.SearchOrderAndPaginate(items, params, config)
+
+ totalPages := int64(0)
+ if params.Limit > 0 {
+ totalPages = (int64(result.TotalCount) + int64(params.Limit) - 1) / int64(params.Limit)
+ }
+
+ page := 1
+ if params.Limit > 0 {
+ page = (params.Start / params.Limit) + 1
+ }
+
+ paginationResp := pagination.Response{
+ TotalPages: totalPages,
+ TotalItems: int64(result.TotalCount),
+ CurrentPage: page,
+ ItemsPerPage: params.Limit,
+ GrandTotalItems: int64(result.TotalAvailable),
+ }
+
+ return result.Items, paginationResp, nil
}
diff --git a/backend/internal/services/environment_service.go b/backend/internal/services/environment_service.go
index 4489be31c..001bd85eb 100644
--- a/backend/internal/services/environment_service.go
+++ b/backend/internal/services/environment_service.go
@@ -16,7 +16,7 @@ import (
"github.com/ofkm/arcane-backend/internal/database"
"github.com/ofkm/arcane-backend/internal/dto"
"github.com/ofkm/arcane-backend/internal/models"
- "github.com/ofkm/arcane-backend/internal/utils"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
"gorm.io/gorm"
)
@@ -57,45 +57,41 @@ func (s *EnvironmentService) GetEnvironmentByID(ctx context.Context, id string)
return &environment, nil
}
-func (s *EnvironmentService) ListEnvironmentsPaginated(ctx context.Context, req utils.SortedPaginationRequest) ([]dto.EnvironmentDto, utils.PaginationResponse, error) {
+func (s *EnvironmentService) ListEnvironmentsPaginated(ctx context.Context, params pagination.QueryParams) ([]dto.EnvironmentDto, pagination.Response, error) {
var envs []models.Environment
q := s.db.WithContext(ctx).Model(&models.Environment{})
- if req.Search != "" {
- like := "%" + req.Search + "%"
- q = q.Where("api_url ILIKE ?", like)
+ if term := strings.TrimSpace(params.Search); term != "" {
+ searchPattern := "%" + term + "%"
+ q = q.Where(
+ "name LIKE ? OR api_url LIKE ?",
+ searchPattern, searchPattern,
+ )
}
- if req.Filters != nil {
- if v, ok := req.Filters["status"]; ok && v != nil && v != "" {
- q = q.Where("status = ?", v)
- }
- if v, ok := req.Filters["enabled"]; ok && v != nil && v != "" {
- switch vv := v.(type) {
- case bool:
- q = q.Where("enabled = ?", vv)
- case string:
- switch vv {
- case "true", "1":
- q = q.Where("enabled = ?", true)
- case "false", "0":
- q = q.Where("enabled = ?", false)
- }
- }
+ if status := params.Filters["status"]; status != "" {
+ q = q.Where("status = ?", status)
+ }
+ if enabled := params.Filters["enabled"]; enabled != "" {
+ switch enabled {
+ case "true", "1":
+ q = q.Where("enabled = ?", true)
+ case "false", "0":
+ q = q.Where("enabled = ?", false)
}
}
- pagination, err := utils.PaginateAndSort(req, q, &envs)
+ paginationResp, err := pagination.PaginateAndSortDB(params, q, &envs)
if err != nil {
- return nil, utils.PaginationResponse{}, fmt.Errorf("failed to paginate environments: %w", err)
+ return nil, pagination.Response{}, fmt.Errorf("failed to paginate environments: %w", err)
}
out, mapErr := dto.MapSlice[models.Environment, dto.EnvironmentDto](envs)
if mapErr != nil {
- return nil, utils.PaginationResponse{}, fmt.Errorf("failed to map environments: %w", mapErr)
+ return nil, pagination.Response{}, fmt.Errorf("failed to map environments: %w", mapErr)
}
- return out, pagination, nil
+ return out, paginationResp, nil
}
func (s *EnvironmentService) UpdateEnvironment(ctx context.Context, id string, updates map[string]interface{}) (*models.Environment, error) {
diff --git a/backend/internal/services/event_service.go b/backend/internal/services/event_service.go
index 4420e5d43..c1fa7022a 100644
--- a/backend/internal/services/event_service.go
+++ b/backend/internal/services/event_service.go
@@ -3,12 +3,13 @@ package services
import (
"context"
"fmt"
+ "strings"
"time"
"github.com/ofkm/arcane-backend/internal/database"
"github.com/ofkm/arcane-backend/internal/dto"
"github.com/ofkm/arcane-backend/internal/models"
- "github.com/ofkm/arcane-backend/internal/utils"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
)
type EventService struct {
@@ -97,88 +98,83 @@ func (s *EventService) CreateEventFromDto(ctx context.Context, req dto.CreateEve
return s.toEventDto(event), nil
}
-func (s *EventService) ListEventsPaginated(ctx context.Context, req utils.SortedPaginationRequest) ([]dto.EventDto, utils.PaginationResponse, error) {
+func (s *EventService) ListEventsPaginated(ctx context.Context, params pagination.QueryParams) ([]dto.EventDto, pagination.Response, error) {
var events []models.Event
-
- query := s.db.WithContext(ctx).Model(&models.Event{})
-
- if req.Search != "" {
- searchQuery := "%" + req.Search + "%"
- query = query.Where("title LIKE ? OR description LIKE ? OR resource_name LIKE ? OR username LIKE ?",
- searchQuery, searchQuery, searchQuery, searchQuery)
+ q := s.db.WithContext(ctx).Model(&models.Event{})
+
+ if term := strings.TrimSpace(params.Search); term != "" {
+ searchPattern := "%" + term + "%"
+ q = q.Where(
+ "title LIKE ? OR description LIKE ? OR COALESCE(resource_name, '') LIKE ? OR COALESCE(username, '') LIKE ?",
+ searchPattern, searchPattern, searchPattern, searchPattern,
+ )
}
- if req.Filters != nil {
- for key, value := range req.Filters {
- if value != nil && value != "" {
- switch key {
- case "severity":
- query = query.Where("severity = ?", value)
- case "type":
- query = query.Where("type = ?", value)
- case "resourceType":
- query = query.Where("resource_type = ?", value)
- case "username":
- query = query.Where("username = ?", value)
- case "environmentId":
- query = query.Where("environment_id = ?", value)
- }
- }
- }
+ if severity := params.Filters["severity"]; severity != "" {
+ q = q.Where("severity = ?", severity)
+ }
+ if eventType := params.Filters["type"]; eventType != "" {
+ q = q.Where("type = ?", eventType)
+ }
+ if resourceType := params.Filters["resourceType"]; resourceType != "" {
+ q = q.Where("resource_type = ?", resourceType)
+ }
+ if username := params.Filters["username"]; username != "" {
+ q = q.Where("username = ?", username)
+ }
+ if environmentId := params.Filters["environmentId"]; environmentId != "" {
+ q = q.Where("environment_id = ?", environmentId)
}
- pagination, err := utils.PaginateAndSort(req, query, &events)
+ paginationResp, err := pagination.PaginateAndSortDB(params, q, &events)
if err != nil {
- return nil, utils.PaginationResponse{}, fmt.Errorf("failed to paginate events: %w", err)
+ return nil, pagination.Response{}, fmt.Errorf("failed to paginate events: %w", err)
}
eventDtos, mapErr := dto.MapSlice[models.Event, dto.EventDto](events)
if mapErr != nil {
- return nil, utils.PaginationResponse{}, fmt.Errorf("failed to map events: %w", mapErr)
+ return nil, pagination.Response{}, fmt.Errorf("failed to map events: %w", mapErr)
}
- return eventDtos, pagination, nil
+ return eventDtos, paginationResp, nil
}
-func (s *EventService) GetEventsByEnvironmentPaginated(ctx context.Context, environmentID string, req utils.SortedPaginationRequest) ([]dto.EventDto, utils.PaginationResponse, error) {
+func (s *EventService) GetEventsByEnvironmentPaginated(ctx context.Context, environmentID string, params pagination.QueryParams) ([]dto.EventDto, pagination.Response, error) {
var events []models.Event
-
- query := s.db.WithContext(ctx).Model(&models.Event{}).Where("environment_id = ?", environmentID)
-
- if req.Search != "" {
- searchQuery := "%" + req.Search + "%"
- query = query.Where("title LIKE ? OR description LIKE ? OR resource_name LIKE ? OR username LIKE ?",
- searchQuery, searchQuery, searchQuery, searchQuery)
+ q := s.db.WithContext(ctx).Model(&models.Event{}).Where("environment_id = ?", environmentID)
+
+ if term := strings.TrimSpace(params.Search); term != "" {
+ searchPattern := "%" + term + "%"
+ q = q.Where(
+ "title LIKE ? OR description LIKE ? OR COALESCE(resource_name, '') LIKE ? OR COALESCE(username, '') LIKE ?",
+ searchPattern, searchPattern, searchPattern, searchPattern,
+ )
}
- if req.Filters != nil {
- for key, value := range req.Filters {
- if value != nil && value != "" {
- switch key {
- case "severity":
- query = query.Where("severity = ?", value)
- case "type":
- query = query.Where("type = ?", value)
- case "resourceType":
- query = query.Where("resource_type = ?", value)
- case "username":
- query = query.Where("username = ?", value)
- }
- }
- }
+ if severity := params.Filters["severity"]; severity != "" {
+ q = q.Where("severity = ?", severity)
+ }
+ if eventType := params.Filters["type"]; eventType != "" {
+ q = q.Where("type = ?", eventType)
+ }
+ if resourceType := params.Filters["resourceType"]; resourceType != "" {
+ q = q.Where("resource_type = ?", resourceType)
+ }
+ if username := params.Filters["username"]; username != "" {
+ q = q.Where("username = ?", username)
}
- pagination, err := utils.PaginateAndSort(req, query, &events)
+ paginationResp, err := pagination.PaginateAndSortDB(params, q, &events)
if err != nil {
- return nil, utils.PaginationResponse{}, fmt.Errorf("failed to paginate events: %w", err)
+ return nil, pagination.Response{}, fmt.Errorf("failed to paginate events: %w", err)
}
eventDtos, mapErr := dto.MapSlice[models.Event, dto.EventDto](events)
if mapErr != nil {
- return nil, utils.PaginationResponse{}, fmt.Errorf("failed to map events: %w", mapErr)
+ return nil, pagination.Response{}, fmt.Errorf("failed to map events: %w", mapErr)
}
- return eventDtos, pagination, nil
+ return eventDtos, paginationResp, nil
}
func (s *EventService) DeleteEvent(ctx context.Context, eventID string) error {
diff --git a/backend/internal/services/image_service.go b/backend/internal/services/image_service.go
index fb9206a9b..abf45239d 100644
--- a/backend/internal/services/image_service.go
+++ b/backend/internal/services/image_service.go
@@ -10,7 +10,6 @@ import (
"io"
"net/http"
"strings"
- "time"
"log/slog"
@@ -18,12 +17,10 @@ import (
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/registry"
- "github.com/docker/docker/client"
"github.com/ofkm/arcane-backend/internal/database"
"github.com/ofkm/arcane-backend/internal/dto"
"github.com/ofkm/arcane-backend/internal/models"
- "github.com/ofkm/arcane-backend/internal/utils"
- "gorm.io/gorm/clause"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
)
type ImageService struct {
@@ -44,24 +41,6 @@ func NewImageService(db *database.DB, dockerService *DockerClientService, regist
}
}
-func (s *ImageService) SyncDockerImages(ctx context.Context) error {
- dockerClient, err := s.dockerService.CreateConnection(ctx)
- if err != nil {
- return fmt.Errorf("failed to connect to Docker: %w", err)
- }
- defer dockerClient.Close()
-
- images, err := dockerClient.ImageList(ctx, image.ListOptions{})
- if err != nil {
- return fmt.Errorf("failed to list Docker images: %w", err)
- }
-
- if err := s.syncImagesToDatabase(ctx, images, dockerClient); err != nil {
- return fmt.Errorf("error during image synchronization: %w", err)
- }
- return nil
-}
-
func (s *ImageService) GetImageByID(ctx context.Context, id string) (*image.InspectResponse, error) {
dockerClient, err := s.dockerService.CreateConnection(ctx)
if err != nil {
@@ -69,12 +48,12 @@ func (s *ImageService) GetImageByID(ctx context.Context, id string) (*image.Insp
}
defer dockerClient.Close()
- image, err := dockerClient.ImageInspect(ctx, id)
+ inspect, err := dockerClient.ImageInspect(ctx, id)
if err != nil {
- return nil, fmt.Errorf("image not found: %w", err)
+ return nil, fmt.Errorf("inspect not found: %w", err)
}
- return &image, nil
+ return &inspect, nil
}
func (s *ImageService) RemoveImage(ctx context.Context, id string, force bool, user models.User) error {
@@ -102,7 +81,9 @@ func (s *ImageService) RemoveImage(ctx context.Context, id string, force bool, u
return fmt.Errorf("failed to remove image: %w", err)
}
- s.db.WithContext(ctx).Delete(&models.Image{}, "id = ?", id)
+ if s.db != nil {
+ s.db.WithContext(ctx).Delete(&models.ImageUpdateRecord{}, "id = ?", id)
+ }
metadata := models.JSON{
"action": "delete",
@@ -161,20 +142,8 @@ func (s *ImageService) PullImage(ctx context.Context, imageName string, progress
return fmt.Errorf("error reading image pull stream for %s: %w", imageName, scanErr)
}
- slog.Debug("image pull stream completed, attempting to sync database", slog.String("image", imageName))
-
- latestImages, listErr := dockerClient.ImageList(ctx, image.ListOptions{})
- if listErr != nil {
- slog.Warn("failed to list images after pull for sync", slog.Any("err", listErr))
- } else {
- if syncErr := s.syncImagesToDatabase(ctx, latestImages, dockerClient); syncErr != nil {
- slog.Warn("error during image synchronization after pull", slog.Any("err", syncErr))
- } else {
- slog.Debug("database synchronized successfully after pulling image", slog.String("image", imageName))
- }
- }
+ slog.Debug("image pull stream completed", slog.String("image", imageName))
- // Log image pull event
metadata := models.JSON{
"action": "pull",
"imageName": imageName,
@@ -227,19 +196,15 @@ func (s *ImageService) getPullOptionsWithAuth(ctx context.Context, imageRef stri
}
func (s *ImageService) extractRegistryHost(imageRef string) string {
- // strip digest if present
if i := strings.IndexByte(imageRef, '@'); i != -1 {
imageRef = imageRef[:i]
}
- // split on first slash; if no slash, it's a Docker Hub shorthand
hostCandidate, _, found := strings.Cut(imageRef, "/")
if !found {
return "docker.io"
}
- // first segment is either a registry host[:port] or a namespace
- // if it doesn't look like a host (no dot/colon), default to docker.io
if !strings.Contains(hostCandidate, ".") && !strings.Contains(hostCandidate, ":") {
return "docker.io"
}
@@ -258,7 +223,6 @@ func (s *ImageService) normalizeRegistryForComparison(url string) string {
url = strings.TrimPrefix(url, "http://")
url = strings.TrimSuffix(url, "/")
- // keep only host part (drop any path like ghcr.io/org or /v2/)
if slash := strings.Index(url, "/"); slash != -1 {
url = url[:slash]
}
@@ -314,186 +278,251 @@ func (s *ImageService) PruneImages(ctx context.Context, dangling bool) (*image.P
return &report, nil
}
-func (s *ImageService) syncImagesToDatabase(ctx context.Context, dockerImages []image.Summary, dockerClient *client.Client) error {
- inUseImageIDs := s.getInUseImageIDs(ctx, dockerClient)
- currentDockerImageIDs := make([]string, 0, len(dockerImages))
- var lastErr error
-
- for _, di := range dockerImages {
- currentDockerImageIDs = append(currentDockerImageIDs, di.ID)
- if err := s.syncSingleImage(ctx, di, inUseImageIDs); err != nil {
- slog.Warn("error syncing image to database", slog.String("image_id", di.ID), slog.Any("err", err))
- lastErr = err
- }
- }
-
- if err := s.cleanupStaleImages(ctx, currentDockerImageIDs); err != nil {
- lastErr = err
+func (s *ImageService) ListImagesPaginated(ctx context.Context, params pagination.QueryParams) ([]dto.ImageSummaryDto, pagination.Response, error) {
+ dockerClient, err := s.dockerService.CreateConnection(ctx)
+ if err != nil {
+ return nil, pagination.Response{}, fmt.Errorf("failed to connect to Docker: %w", err)
}
+ defer dockerClient.Close()
- if err := s.imageUpdateService.CleanupOrphanedRecords(ctx); err != nil {
- slog.Warn("failed to cleanup orphaned image update records", slog.Any("err", err))
- if lastErr == nil {
- lastErr = err
- }
+ dockerImages, err := dockerClient.ImageList(ctx, image.ListOptions{})
+ if err != nil {
+ return nil, pagination.Response{}, fmt.Errorf("failed to list Docker images: %w", err)
}
- return lastErr
-}
-
-func (s *ImageService) getInUseImageIDs(ctx context.Context, dockerClient *client.Client) map[string]bool {
- inUseImageIDs := make(map[string]bool)
containers, err := dockerClient.ContainerList(ctx, container.ListOptions{All: true})
if err != nil {
- slog.Warn("error listing containers for in-use check; in-use status may be inaccurate", slog.Any("err", err))
- return inUseImageIDs
- }
-
- for _, cont := range containers {
- inUseImageIDs[cont.ImageID] = true
- }
- return inUseImageIDs
-}
-
-func (s *ImageService) syncSingleImage(ctx context.Context, di image.Summary, inUseImageIDs map[string]bool) error {
- _, isInUse := inUseImageIDs[di.ID]
-
- imageModel := s.buildImageModel(di, isInUse)
-
- return s.db.WithContext(ctx).Clauses(clause.OnConflict{
- Columns: []clause.Column{{Name: "id"}},
- DoUpdates: clause.AssignmentColumns([]string{"repo_tags", "repo_digests", "size", "virtual_size", "created", "labels", "repo", "tag", "in_use"}),
- }).Create(&imageModel).Error
+ return nil, pagination.Response{}, fmt.Errorf("failed to list containers: %w", err)
+ }
+
+ inUseMap := buildInUseMap(containers)
+
+ var updateRecords []models.ImageUpdateRecord
+ if s.db != nil {
+ s.db.WithContext(ctx).Find(&updateRecords)
+ }
+ updateMap := buildUpdateMap(updateRecords)
+
+ items := mapDockerImagesToDTOs(dockerImages, inUseMap, updateMap)
+
+ config := pagination.Config[dto.ImageSummaryDto]{
+ SearchAccessors: []pagination.SearchAccessor[dto.ImageSummaryDto]{
+ func(i dto.ImageSummaryDto) (string, error) { return i.Repo, nil },
+ func(i dto.ImageSummaryDto) (string, error) { return i.Tag, nil },
+ func(i dto.ImageSummaryDto) (string, error) { return i.ID, nil },
+ func(i dto.ImageSummaryDto) (string, error) {
+ if len(i.RepoTags) > 0 {
+ return i.RepoTags[0], nil
+ }
+ return "", nil
+ },
+ },
+ SortBindings: []pagination.SortBinding[dto.ImageSummaryDto]{
+ {
+ Key: "repo",
+ Fn: func(a, b dto.ImageSummaryDto) int {
+ return strings.Compare(a.Repo, b.Repo)
+ },
+ },
+ {
+ Key: "tag",
+ Fn: func(a, b dto.ImageSummaryDto) int {
+ return strings.Compare(a.Tag, b.Tag)
+ },
+ },
+ {
+ Key: "size",
+ Fn: func(a, b dto.ImageSummaryDto) int {
+ if a.Size < b.Size {
+ return -1
+ }
+ if a.Size > b.Size {
+ return 1
+ }
+ return 0
+ },
+ },
+ {
+ Key: "created",
+ Fn: func(a, b dto.ImageSummaryDto) int {
+ if a.Created < b.Created {
+ return -1
+ }
+ if a.Created > b.Created {
+ return 1
+ }
+ return 0
+ },
+ },
+ {
+ Key: "inUse",
+ Fn: func(a, b dto.ImageSummaryDto) int {
+ if a.InUse == b.InUse {
+ return 0
+ }
+ if a.InUse {
+ return -1
+ }
+ return 1
+ },
+ },
+ },
+ FilterAccessors: []pagination.FilterAccessor[dto.ImageSummaryDto]{
+ {
+ Key: "inUse",
+ Fn: func(i dto.ImageSummaryDto, filterValue string) bool {
+ if filterValue == "true" {
+ return i.InUse
+ }
+ if filterValue == "false" {
+ return !i.InUse
+ }
+ return true
+ },
+ },
+ {
+ Key: "updates",
+ Fn: func(i dto.ImageSummaryDto, filterValue string) bool {
+ hasUpdate := i.UpdateInfo != nil && i.UpdateInfo.HasUpdate
+ if filterValue == "true" {
+ return hasUpdate
+ }
+ if filterValue == "false" {
+ return !hasUpdate
+ }
+ return true
+ },
+ },
+ },
+ }
+
+ result := pagination.SearchOrderAndPaginate(items, params, config)
+
+ totalPages := int64(0)
+ if params.Limit > 0 {
+ totalPages = (int64(result.TotalCount) + int64(params.Limit) - 1) / int64(params.Limit)
+ }
+
+ page := 1
+ if params.Limit > 0 {
+ page = (params.Start / params.Limit) + 1
+ }
+
+ paginationResp := pagination.Response{
+ TotalPages: totalPages,
+ TotalItems: int64(result.TotalCount),
+ CurrentPage: page,
+ ItemsPerPage: params.Limit,
+ GrandTotalItems: int64(result.TotalAvailable),
+ }
+
+ return result.Items, paginationResp, nil
}
-func (s *ImageService) buildImageModel(di image.Summary, isInUse bool) models.Image {
- imageModel := models.Image{
- RepoTags: models.StringSlice(di.RepoTags),
- RepoDigests: models.StringSlice(di.RepoDigests),
- Size: di.Size,
- Created: time.Unix(di.Created, 0),
- InUse: isInUse,
+func convertLabels(labels map[string]string) map[string]interface{} {
+ if labels == nil {
+ return nil
}
- imageModel.ID = di.ID
-
- if di.Labels != nil {
- labelsJSON := make(map[string]interface{})
- for k, v := range di.Labels {
- labelsJSON[k] = v
- }
- imageModel.Labels = models.JSON(labelsJSON)
+ result := make(map[string]interface{}, len(labels))
+ for k, v := range labels {
+ result[k] = v
}
-
- s.setRepoAndTag(&imageModel, di.RepoTags)
- return imageModel
+ return result
}
-func (s *ImageService) setRepoAndTag(imageModel *models.Image, repoTags []string) {
- var repoTag string
- for _, t := range repoTags {
- if t != "" && !strings.Contains(t, "<none>") {
- repoTag = t
- break
- }
- }
- if repoTag == "" && len(repoTags) > 0 {
- repoTag = repoTags[0]
+func (s *ImageService) GetTotalImageSize(ctx context.Context) (int64, error) {
+ dockerClient, err := s.dockerService.CreateConnection(ctx)
+ if err != nil {
+ return 0, fmt.Errorf("failed to connect to Docker: %w", err)
}
+ defer dockerClient.Close()
- if repoTag != "" {
- if strings.Contains(repoTag, ":") {
- parts := strings.SplitN(repoTag, ":", 2)
- imageModel.Repo = parts[0]
- imageModel.Tag = parts[1]
- } else {
- imageModel.Repo = repoTag
- imageModel.Tag = "latest"
- }
- } else {
- imageModel.Repo = "<none>"
- imageModel.Tag = "<none>"
+ images, err := dockerClient.ImageList(ctx, image.ListOptions{})
+ if err != nil {
+ return 0, fmt.Errorf("failed to list images: %w", err)
}
-}
-func (s *ImageService) cleanupStaleImages(ctx context.Context, currentDockerImageIDs []string) error {
- if len(currentDockerImageIDs) > 0 {
- return s.deleteStaleImages(ctx, currentDockerImageIDs)
+ var total int64
+ for _, img := range images {
+ total += img.Size
}
- return s.deleteAllImages(ctx)
+
+ return total, nil
}
-func (s *ImageService) deleteStaleImages(ctx context.Context, currentDockerImageIDs []string) error {
- deleteResult := s.db.WithContext(ctx).Where("id NOT IN ?", currentDockerImageIDs).Delete(&models.Image{})
- if deleteResult.Error != nil {
- slog.Warn("error deleting stale images from database", slog.Any("err", deleteResult.Error))
- return deleteResult.Error
+func buildInUseMap(containers []container.Summary) map[string]bool {
+ inUseMap := make(map[string]bool)
+ for _, c := range containers {
+ inUseMap[c.ImageID] = true
}
- slog.Debug("stale image records deleted from database", slog.Int64("rows_affected", deleteResult.RowsAffected))
- return nil
+ return inUseMap
}
-func (s *ImageService) deleteAllImages(ctx context.Context) error {
- slog.Debug("no images found in Docker daemon, attempting to delete all image records from database")
- deleteAllResult := s.db.WithContext(ctx).Delete(&models.Image{}, "1 = 1")
- if deleteAllResult.Error != nil {
- slog.Warn("error deleting all image records from database when Docker is empty", slog.Any("err", deleteAllResult.Error))
- return deleteAllResult.Error
+func buildUpdateMap(records []models.ImageUpdateRecord) map[string]*models.ImageUpdateRecord {
+ updateMap := make(map[string]*models.ImageUpdateRecord, len(records))
+ for i := range records {
+ updateMap[records[i].ID] = &records[i]
}
- slog.Debug("all image records deleted from database as Docker reported no images", slog.Int64("rows_affected", deleteAllResult.RowsAffected))
- return nil
+ return updateMap
}
-func (s *ImageService) DeleteImageByDockerID(ctx context.Context, dockerImageID string) error {
- if dockerImageID == "" {
- return fmt.Errorf("docker image ID cannot be empty")
- }
+func mapDockerImagesToDTOs(dockerImages []image.Summary, inUseMap map[string]bool, updateMap map[string]*models.ImageUpdateRecord) []dto.ImageSummaryDto {
+ items := make([]dto.ImageSummaryDto, 0, len(dockerImages))
+ for _, di := range dockerImages {
+ inUse := inUseMap[di.ID]
+
+ imageDto := dto.ImageSummaryDto{
+ ID: di.ID,
+ RepoTags: di.RepoTags,
+ RepoDigests: di.RepoDigests,
+ Created: di.Created,
+ Size: di.Size,
+ VirtualSize: di.SharedSize,
+ Labels: convertLabels(di.Labels),
+ InUse: inUse,
+ }
- result := s.db.WithContext(ctx).Where("id = ?", dockerImageID).Delete(&models.Image{})
- if result.Error != nil {
- return fmt.Errorf("failed to delete image %s from database: %w", dockerImageID, result.Error)
- }
+ if len(di.RepoTags) > 0 {
+ repoTag := di.RepoTags[0]
+ if strings.Contains(repoTag, ":") {
+ parts := strings.SplitN(repoTag, ":", 2)
+ imageDto.Repo = parts[0]
+ imageDto.Tag = parts[1]
+ } else {
+ imageDto.Repo = repoTag
+ imageDto.Tag = "latest"
+ }
+ } else {
+ imageDto.Repo = "<none>"
+ imageDto.Tag = "<none>"
+ }
- if result.RowsAffected == 0 {
- } else {
- slog.Debug("successfully deleted image from database", slog.String("image_id", dockerImageID))
- }
- return nil
-}
+ if updateRecord, exists := updateMap[di.ID]; exists {
+ sp := func(p *string) string {
+ if p == nil {
+ return ""
+ }
+ return *p
+ }
-func (s *ImageService) ListImagesWithUpdatesPaginated(ctx context.Context, req utils.SortedPaginationRequest) ([]dto.ImageSummaryDto, utils.PaginationResponse, error) {
- err := s.SyncDockerImages(ctx)
- if err != nil {
- return nil, utils.PaginationResponse{}, fmt.Errorf("failed to list and sync Docker images: %w", err)
- }
- var images []models.Image
- query := s.db.WithContext(ctx).Model(&models.Image{}).Preload("UpdateRecord")
- if term := strings.TrimSpace(req.Search); term != "" {
- like := "%" + strings.ToLower(term) + "%"
- query = query.Where(`
- LOWER(repo) LIKE ? OR
- LOWER(tag) LIKE ? OR
- LOWER(id) LIKE ? OR
- LOWER(repo || ':' || tag) LIKE ?
- `, like, like, like, like)
- }
- pagination, err := utils.PaginateAndSort(req, query, &images)
- if err != nil {
- return nil, utils.PaginationResponse{}, fmt.Errorf("failed to paginate images: %w", err)
- }
- result := make([]dto.ImageSummaryDto, 0, len(images))
- for i := range images {
- result = append(result, dto.NewImageSummaryDto(&images[i]))
- }
- return result, pagination, nil
-}
+ imageDto.UpdateInfo = &dto.ImageUpdateInfoDto{
+ HasUpdate: updateRecord.HasUpdate,
+ UpdateType: updateRecord.UpdateType,
+ CurrentVersion: updateRecord.CurrentVersion,
+ LatestVersion: sp(updateRecord.LatestVersion),
+ CurrentDigest: sp(updateRecord.CurrentDigest),
+ LatestDigest: sp(updateRecord.LatestDigest),
+ CheckTime: updateRecord.CheckTime,
+ ResponseTimeMs: updateRecord.ResponseTimeMs,
+ Error: sp(updateRecord.LastError),
+ AuthMethod: sp(updateRecord.AuthMethod),
+ AuthUsername: sp(updateRecord.AuthUsername),
+ AuthRegistry: sp(updateRecord.AuthRegistry),
+ UsedCredential: updateRecord.UsedCredential,
+ }
+ }
-func (s *ImageService) GetTotalImageSize(ctx context.Context) (int64, error) {
- if err := s.SyncDockerImages(ctx); err != nil {
- return 0, fmt.Errorf("failed to sync images: %w", err)
+ items = append(items, imageDto)
}
- var total int64
- if err := s.db.WithContext(ctx).Model(&models.Image{}).Select("COALESCE(SUM(size), 0)").Scan(&total).Error; err != nil {
- return 0, fmt.Errorf("failed to sum image sizes: %w", err)
- }
- return total, nil
+ return items
}
diff --git a/backend/internal/services/image_update_service.go b/backend/internal/services/image_update_service.go
index 6d058e930..ae8ae194e 100644
--- a/backend/internal/services/image_update_service.go
+++ b/backend/internal/services/image_update_service.go
@@ -1110,10 +1110,47 @@ func (s *ImageUpdateService) CheckAllImages(ctx context.Context, limit int) (map
}
func (s *ImageUpdateService) CleanupOrphanedRecords(ctx context.Context) error {
- subQuery := s.db.WithContext(ctx).Model(&models.Image{}).Select("id")
- return s.db.WithContext(ctx).
- Where("id NOT IN (?)", subQuery).
- Delete(&models.ImageUpdateRecord{}).Error
+ dockerClient, err := s.dockerService.CreateConnection(ctx)
+ if err != nil {
+ return fmt.Errorf("failed to connect to Docker: %w", err)
+ }
+ defer dockerClient.Close()
+
+ // Get all image IDs from Docker
+ dockerImages, err := dockerClient.ImageList(ctx, image.ListOptions{})
+ if err != nil {
+ return fmt.Errorf("failed to list Docker images: %w", err)
+ }
+
+ dockerImageIDs := make(map[string]bool)
+ for _, img := range dockerImages {
+ dockerImageIDs[img.ID] = true
+ }
+
+ // Get all update records from database
+ var updateRecords []models.ImageUpdateRecord
+ if err := s.db.WithContext(ctx).Find(&updateRecords).Error; err != nil {
+ return fmt.Errorf("failed to query update records: %w", err)
+ }
+
+ // Delete records for images that no longer exist in Docker
+ orphanedCount := 0
+ for _, record := range updateRecords {
+ if !dockerImageIDs[record.ID] {
+ if err := s.db.WithContext(ctx).Delete(&models.ImageUpdateRecord{}, "id = ?", record.ID).Error; err != nil {
+ slog.WarnContext(ctx, "Failed to delete orphaned update record",
+ slog.String("imageId", record.ID),
+ slog.String("error", err.Error()))
+ } else {
+ orphanedCount++
+ }
+ }
+ }
+
+ slog.InfoContext(ctx, "Cleaned up orphaned image update records",
+ slog.Int("deletedCount", orphanedCount))
+
+ return nil
}
func (s *ImageUpdateService) GetUpdateSummary(ctx context.Context) (*dto.ImageUpdateSummaryResponse, error) {
diff --git a/backend/internal/services/network_service.go b/backend/internal/services/network_service.go
index 7c94a878d..2709dee90 100644
--- a/backend/internal/services/network_service.go
+++ b/backend/internal/services/network_service.go
@@ -3,9 +3,7 @@ package services
import (
"context"
"fmt"
- "sort"
"strings"
- "time"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
@@ -13,7 +11,7 @@ import (
"github.com/ofkm/arcane-backend/internal/database"
"github.com/ofkm/arcane-backend/internal/dto"
"github.com/ofkm/arcane-backend/internal/models"
- "github.com/ofkm/arcane-backend/internal/utils"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
)
type NetworkService struct {
@@ -124,116 +122,130 @@ func (s *NetworkService) PruneNetworks(ctx context.Context) (*network.PruneRepor
return &report, nil
}
-//nolint:gocognit
-func (s *NetworkService) ListNetworksPaginated(ctx context.Context, req utils.SortedPaginationRequest) ([]dto.NetworkSummaryDto, utils.PaginationResponse, error) {
- nets, _, _, _, err := s.dockerService.GetAllNetworks(ctx)
+func (s *NetworkService) ListNetworksPaginated(ctx context.Context, params pagination.QueryParams) ([]dto.NetworkSummaryDto, pagination.Response, error) {
+ dockerClient, err := s.dockerService.CreateConnection(ctx)
if err != nil {
- return nil, utils.PaginationResponse{}, fmt.Errorf("failed to list Docker networks: %w", err)
- }
-
- inUseByID := map[string]bool{}
- inUseByName := map[string]bool{}
- {
- dockerClient, derr := s.dockerService.CreateConnection(ctx)
- if derr == nil {
- defer dockerClient.Close()
- containers, lerr := dockerClient.ContainerList(ctx, container.ListOptions{All: true})
- if lerr == nil {
- for _, c := range containers {
- if c.NetworkSettings == nil || c.NetworkSettings.Networks == nil {
- continue
- }
- for netName, es := range c.NetworkSettings.Networks {
- if es.NetworkID != "" {
- inUseByID[es.NetworkID] = true
- }
- inUseByName[netName] = true
- }
- }
- }
- }
+ return nil, pagination.Response{}, fmt.Errorf("failed to connect to Docker: %w", err)
}
+ defer dockerClient.Close()
- // Map to DTOs
- items := make([]dto.NetworkSummaryDto, 0, len(nets))
- for _, n := range nets {
- d := dto.NewNetworkSummaryDto(n)
- if inUseByID[n.ID] || inUseByName[n.Name] {
- d.InUse = true
- }
- items = append(items, d)
- }
-
- // Search filter
- if req.Search != "" {
- search := strings.ToLower(req.Search)
- filtered := make([]dto.NetworkSummaryDto, 0, len(items))
- for _, n := range items {
- if strings.Contains(strings.ToLower(n.Name), search) ||
- strings.Contains(strings.ToLower(n.Driver), search) ||
- strings.Contains(strings.ToLower(n.Scope), search) {
- filtered = append(filtered, n)
- }
- }
- items = filtered
+ containers, err := dockerClient.ContainerList(ctx, container.ListOptions{All: true})
+ if err != nil {
+ return nil, pagination.Response{}, fmt.Errorf("failed to list containers: %w", err)
}
- totalItems := len(items)
-
- // Sort
- if col := strings.TrimSpace(strings.ToLower(req.Sort.Column)); col != "" {
- dir := utils.NormalizeSortDirection(req.Sort.Direction)
- desc := dir == "desc"
- lessStr := func(a, b string) bool {
- if desc {
- return a > b
- }
- return a < b
+ inUseByID := make(map[string]bool)
+ inUseByName := make(map[string]bool)
+ for _, c := range containers {
+ if c.NetworkSettings == nil || c.NetworkSettings.Networks == nil {
+ continue
}
- lessTime := func(a, b time.Time) bool {
- if desc {
- return a.After(b)
+ for netName, es := range c.NetworkSettings.Networks {
+ if es.NetworkID != "" {
+ inUseByID[es.NetworkID] = true
}
- return a.Before(b)
+ inUseByName[netName] = true
}
-
- sort.Slice(items, func(i, j int) bool {
- a, b := items[i], items[j]
- switch col {
- case "name":
- return lessStr(a.Name, b.Name)
- case "driver":
- return lessStr(a.Driver, b.Driver)
- case "scope":
- return lessStr(a.Scope, b.Scope)
- case "created":
- return lessTime(a.Created, b.Created)
- default:
- return false
- }
- })
}
- startIdx := (req.Pagination.Page - 1) * req.Pagination.Limit
- endIdx := startIdx + req.Pagination.Limit
- if startIdx > len(items) {
- startIdx = len(items)
+ rawNets, err := dockerClient.NetworkList(ctx, network.ListOptions{})
+ if err != nil {
+ return nil, pagination.Response{}, fmt.Errorf("failed to list Docker networks: %w", err)
+ }
+
+ items := make([]dto.NetworkSummaryDto, 0, len(rawNets))
+ for _, n := range rawNets {
+ netDto := dto.NewNetworkSummaryDto(n)
+ netDto.InUse = inUseByID[netDto.ID] || inUseByName[netDto.Name]
+ items = append(items, netDto)
+ }
+
+ config := pagination.Config[dto.NetworkSummaryDto]{
+ SearchAccessors: []pagination.SearchAccessor[dto.NetworkSummaryDto]{
+ func(n dto.NetworkSummaryDto) (string, error) { return n.Name, nil },
+ func(n dto.NetworkSummaryDto) (string, error) { return n.Driver, nil },
+ func(n dto.NetworkSummaryDto) (string, error) { return n.Scope, nil },
+ func(n dto.NetworkSummaryDto) (string, error) { return n.ID, nil },
+ },
+ SortBindings: []pagination.SortBinding[dto.NetworkSummaryDto]{
+ {
+ Key: "name",
+ Fn: func(a, b dto.NetworkSummaryDto) int {
+ return strings.Compare(a.Name, b.Name)
+ },
+ },
+ {
+ Key: "driver",
+ Fn: func(a, b dto.NetworkSummaryDto) int {
+ return strings.Compare(a.Driver, b.Driver)
+ },
+ },
+ {
+ Key: "scope",
+ Fn: func(a, b dto.NetworkSummaryDto) int {
+ return strings.Compare(a.Scope, b.Scope)
+ },
+ },
+ {
+ Key: "created",
+ Fn: func(a, b dto.NetworkSummaryDto) int {
+ if a.Created.Before(b.Created) {
+ return -1
+ }
+ if a.Created.After(b.Created) {
+ return 1
+ }
+ return 0
+ },
+ },
+ {
+ Key: "inUse",
+ Fn: func(a, b dto.NetworkSummaryDto) int {
+ if a.InUse == b.InUse {
+ return 0
+ }
+ if a.InUse {
+ return -1
+ }
+ return 1
+ },
+ },
+ },
+ FilterAccessors: []pagination.FilterAccessor[dto.NetworkSummaryDto]{
+ {
+ Key: "inUse",
+ Fn: func(n dto.NetworkSummaryDto, filterValue string) bool {
+ if filterValue == "true" {
+ return n.InUse
+ }
+ if filterValue == "false" {
+ return !n.InUse
+ }
+ return true
+ },
+ },
+ },
}
- if endIdx > len(items) {
- endIdx = len(items)
+
+ result := pagination.SearchOrderAndPaginate(items, params, config)
+
+ totalPages := int64(0)
+ if params.Limit > 0 {
+ totalPages = (int64(result.TotalCount) + int64(params.Limit) - 1) / int64(params.Limit)
}
- pageItems := []dto.NetworkSummaryDto{}
- if startIdx < endIdx {
- pageItems = items[startIdx:endIdx]
+
+ page := 1
+ if params.Limit > 0 {
+ page = (params.Start / params.Limit) + 1
}
- totalPages := (totalItems + req.Pagination.Limit - 1) / req.Pagination.Limit
- pagination := utils.PaginationResponse{
- TotalPages: int64(totalPages),
- TotalItems: int64(totalItems),
- CurrentPage: req.Pagination.Page,
- ItemsPerPage: req.Pagination.Limit,
+ paginationResp := pagination.Response{
+ TotalPages: totalPages,
+ TotalItems: int64(result.TotalCount),
+ CurrentPage: page,
+ ItemsPerPage: params.Limit,
+ GrandTotalItems: int64(result.TotalAvailable),
}
- return pageItems, pagination, nil
+ return result.Items, paginationResp, nil
}
diff --git a/backend/internal/services/project_service.go b/backend/internal/services/project_service.go
index 4f3b44386..585cd2552 100644
--- a/backend/internal/services/project_service.go
+++ b/backend/internal/services/project_service.go
@@ -18,6 +18,7 @@ import (
"github.com/ofkm/arcane-backend/internal/models"
"github.com/ofkm/arcane-backend/internal/utils"
"github.com/ofkm/arcane-backend/internal/utils/fs"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
"github.com/ofkm/arcane-backend/internal/utils/projects"
"gorm.io/gorm"
)
@@ -737,11 +738,11 @@ func (s *ProjectService) StreamProjectLogs(ctx context.Context, projectID string
// Table Functions
-func (s *ProjectService) ListProjects(ctx context.Context, req utils.SortedPaginationRequest) ([]dto.ProjectDetailsDto, utils.PaginationResponse, error) {
+func (s *ProjectService) ListProjects(ctx context.Context, params pagination.QueryParams) ([]dto.ProjectDetailsDto, pagination.Response, error) {
var projectsArray []models.Project
query := s.db.WithContext(ctx).Model(&models.Project{})
- if term := strings.TrimSpace(req.Search); term != "" {
+ if term := strings.TrimSpace(params.Search); term != "" {
searchPattern := "%" + term + "%"
query = query.Where(
"name LIKE ? OR path LIKE ? OR status LIKE ? OR COALESCE(dir_name, '') LIKE ?",
@@ -749,9 +750,9 @@ func (s *ProjectService) ListProjects(ctx context.Context, req utils.SortedPagin
)
}
- pagination, err := utils.PaginateAndSort(req, query, &projectsArray)
+ paginationResp, err := pagination.PaginateAndSortDB(params, query, &projectsArray)
if err != nil {
- return nil, utils.PaginationResponse{}, fmt.Errorf("failed to paginate projects: %w", err)
+ return nil, pagination.Response{}, fmt.Errorf("failed to paginate projects: %w", err)
}
var result []dto.ProjectDetailsDto
@@ -777,7 +778,7 @@ func (s *ProjectService) ListProjects(ctx context.Context, req utils.SortedPagin
UpdatedAt: project.UpdatedAt.Format(time.RFC3339),
})
}
- return result, pagination, nil
+ return result, paginationResp, nil
}
// End Table Functions
diff --git a/backend/internal/services/system_service.go b/backend/internal/services/system_service.go
index 4397a6a61..eacc1d274 100644
--- a/backend/internal/services/system_service.go
+++ b/backend/internal/services/system_service.go
@@ -258,24 +258,33 @@ func (s *SystemService) pruneImages(ctx context.Context, danglingOnly bool, resu
slog.Int("images_deleted", len(report.ImagesDeleted)),
slog.Uint64("bytes_reclaimed", report.SpaceReclaimed))
+ // Clean up update records for pruned images
for _, imgReport := range report.ImagesDeleted {
var prunedDockerID string
if imgReport.Deleted != "" {
prunedDockerID = imgReport.Deleted
- result.ImagesDeleted = append(result.ImagesDeleted, prunedDockerID)
+ } else if imgReport.Untagged != "" {
+ prunedDockerID = imgReport.Untagged
}
- if prunedDockerID != "" {
- slog.DebugContext(ctx, "Attempting to delete image from database", slog.String("docker_id", prunedDockerID))
- if dbErr := s.imageService.DeleteImageByDockerID(ctx, prunedDockerID); dbErr != nil {
- errMsg := fmt.Sprintf("Failed to delete image %s from database: %v", prunedDockerID, dbErr)
- result.Errors = append(result.Errors, errMsg)
- slog.ErrorContext(ctx, "Failed to delete image from database",
- slog.String("docker_id", prunedDockerID),
- slog.String("error", dbErr.Error()))
+ if prunedDockerID != "" && s.db != nil {
+ // Only delete the update record
+ if err := s.db.WithContext(ctx).Delete(&models.ImageUpdateRecord{}, "id = ?", prunedDockerID).Error; err != nil {
+ slog.WarnContext(ctx, "Failed to delete image update record",
+ slog.String("imageId", prunedDockerID),
+ slog.String("error", err.Error()))
}
}
}
+
+ result.ImagesDeleted = make([]string, 0, len(report.ImagesDeleted))
+ for _, img := range report.ImagesDeleted {
+ if img.Deleted != "" {
+ result.ImagesDeleted = append(result.ImagesDeleted, img.Deleted)
+ } else if img.Untagged != "" {
+ result.ImagesDeleted = append(result.ImagesDeleted, img.Untagged)
+ }
+ }
result.SpaceReclaimed += report.SpaceReclaimed
return nil
}
diff --git a/backend/internal/services/user_service.go b/backend/internal/services/user_service.go
index 5c0a227a7..55d760341 100644
--- a/backend/internal/services/user_service.go
+++ b/backend/internal/services/user_service.go
@@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"log/slog"
- "math"
"strings"
"golang.org/x/crypto/argon2"
@@ -19,7 +18,7 @@ import (
"github.com/ofkm/arcane-backend/internal/database"
"github.com/ofkm/arcane-backend/internal/dto"
"github.com/ofkm/arcane-backend/internal/models"
- "github.com/ofkm/arcane-backend/internal/utils"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
)
type Argon2Params struct {
@@ -264,57 +263,29 @@ func (s *UserService) UpgradePasswordHash(ctx context.Context, userID, password
Update("password_hash", newHash).Error
}
-func (s *UserService) ListUsersPaginated(ctx context.Context, req utils.SortedPaginationRequest) ([]dto.UserResponseDto, utils.PaginationResponse, error) {
- users, err := s.ListUsers(ctx)
- if err != nil {
- return nil, utils.PaginationResponse{}, fmt.Errorf("failed to list users: %w", err)
- }
-
- var result []dto.UserResponseDto
- for _, user := range users {
- result = append(result, toUserResponseDto(user))
- }
-
- // Apply search filter if provided
- if req.Search != "" {
- var filtered []dto.UserResponseDto
- searchLower := strings.ToLower(req.Search)
- for _, u := range result {
- if matchesSearch(u, searchLower) {
- filtered = append(filtered, u)
- }
- }
- result = filtered
- }
-
- totalItems := len(result)
+func (s *UserService) ListUsersPaginated(ctx context.Context, params pagination.QueryParams) ([]dto.UserResponseDto, pagination.Response, error) {
+ var users []models.User
+ query := s.db.WithContext(ctx).Model(&models.User{})
- // Apply sorting if specified
- if req.Sort.Column != "" && req.Sort.Direction != "" {
- utils.SortUserResponses(result, req.Sort.Column, req.Sort.Direction)
+ if term := strings.TrimSpace(params.Search); term != "" {
+ searchPattern := "%" + term + "%"
+ query = query.Where(
+ "username LIKE ? OR COALESCE(email, '') LIKE ? OR COALESCE(display_name, '') LIKE ?",
+ searchPattern, searchPattern, searchPattern,
+ )
}
- // Apply pagination
- start := (req.Pagination.Page - 1) * req.Pagination.Limit
- end := start + req.Pagination.Limit
-
- if start > len(result) {
- result = []dto.UserResponseDto{}
- } else {
- if end > len(result) {
- end = len(result)
- }
- result = result[start:end]
+ paginationResp, err := pagination.PaginateAndSortDB(params, query, &users)
+ if err != nil {
+ return nil, pagination.Response{}, fmt.Errorf("failed to paginate users: %w", err)
}
- pagination := utils.PaginationResponse{
- TotalPages: int64(math.Ceil(float64(totalItems) / float64(req.Pagination.Limit))),
- TotalItems: int64(totalItems),
- CurrentPage: req.Pagination.Page,
- ItemsPerPage: req.Pagination.Limit,
+ result := make([]dto.UserResponseDto, len(users))
+ for i, user := range users {
+ result[i] = toUserResponseDto(user)
}
- return result, pagination, nil
+ return result, paginationResp, nil
}
func toUserResponseDto(user models.User) dto.UserResponseDto {
@@ -331,12 +302,6 @@ func toUserResponseDto(user models.User) dto.UserResponseDto {
}
}
-func matchesSearch(u dto.UserResponseDto, searchLower string) bool {
- return strings.Contains(strings.ToLower(u.Username), searchLower) ||
- (u.Email != nil && strings.Contains(strings.ToLower(*u.Email), searchLower)) ||
- (u.DisplayName != nil && strings.Contains(strings.ToLower(*u.DisplayName), searchLower))
-}
-
func (s *UserService) GetUser(ctx context.Context, userID string) (*models.User, error) {
slog.Debug("GetUser called", "user_id", userID)
return s.getUserInternal(ctx, userID, s.db.DB)
diff --git a/backend/internal/services/volume_service.go b/backend/internal/services/volume_service.go
index b2cd7e18a..4a96f7bc0 100644
--- a/backend/internal/services/volume_service.go
+++ b/backend/internal/services/volume_service.go
@@ -3,7 +3,6 @@ package services
import (
"context"
"fmt"
- "sort"
"strings"
"time"
@@ -14,7 +13,7 @@ import (
"github.com/ofkm/arcane-backend/internal/database"
"github.com/ofkm/arcane-backend/internal/dto"
"github.com/ofkm/arcane-backend/internal/models"
- "github.com/ofkm/arcane-backend/internal/utils"
+ "github.com/ofkm/arcane-backend/internal/utils/pagination"
)
type VolumeService struct {
@@ -31,27 +30,6 @@ func NewVolumeService(db *database.DB, dockerService *DockerClientService, event
}
}
-func (s *VolumeService) buildVolumeUsageMap(ctx context.Context, dockerClient *client.Client) (map[string]bool, error) {
- containers, err := dockerClient.ContainerList(ctx, container.ListOptions{All: true})
- if err != nil {
- return nil, fmt.Errorf("failed to list containers: %w", err)
- }
-
- usage := make(map[string]bool)
- for _, c := range containers {
- info, err := dockerClient.ContainerInspect(ctx, c.ID)
- if err != nil {
- continue
- }
- for _, m := range info.Mounts {
- if m.Type == "volume" && m.Name != "" {
- usage[m.Name] = true
- }
- }
- }
- return usage, nil
-}
-
func (s *VolumeService) containersUsingVolume(ctx context.Context, dockerClient *client.Client, name string) (bool, []string, error) {
containers, err := dockerClient.ContainerList(ctx, container.ListOptions{All: true})
if err != nil {
@@ -238,133 +216,134 @@ func (s *VolumeService) GetVolumeUsage(ctx context.Context, name string) (bool,
return inUse, usingContainers, nil
}
-//nolint:gocognit
-func (s *VolumeService) ListVolumesPaginated(ctx context.Context, req utils.SortedPaginationRequest, driver string) ([]dto.VolumeDto, utils.PaginationResponse, error) {
- volumes, err := s.ListVolumes(ctx)
- if err != nil {
- return nil, utils.PaginationResponse{}, fmt.Errorf("failed to list Docker volumes: %w", err)
- }
-
+func (s *VolumeService) ListVolumesPaginated(ctx context.Context, params pagination.QueryParams) ([]dto.VolumeDto, pagination.Response, error) {
dockerClient, err := s.dockerService.CreateConnection(ctx)
if err != nil {
- return nil, utils.PaginationResponse{}, fmt.Errorf("failed to connect to Docker: %w", err)
+ return nil, pagination.Response{}, fmt.Errorf("failed to connect to Docker: %w", err)
}
defer dockerClient.Close()
- volumeUsageMap, err := s.buildVolumeUsageMap(ctx, dockerClient)
+ // Get all containers ONCE
+ containers, err := dockerClient.ContainerList(ctx, container.ListOptions{All: true})
if err != nil {
- return nil, utils.PaginationResponse{}, err
- }
-
- result := make([]dto.VolumeDto, 0, len(volumes))
- for _, v := range volumes {
- if driver != "" && v.Driver != driver {
- continue
- }
- result = append(result, dto.NewVolumeDto(v, volumeUsageMap[v.Name]))
+ return nil, pagination.Response{}, fmt.Errorf("failed to list containers: %w", err)
}
- if req.Search != "" {
- filtered := make([]dto.VolumeDto, 0, len(result))
- searchLower := strings.ToLower(req.Search)
- for _, vol := range result {
- if strings.Contains(strings.ToLower(vol.Name), searchLower) ||
- strings.Contains(strings.ToLower(vol.Driver), searchLower) {
- filtered = append(filtered, vol)
+ // Build inUse map in a single pass
+ inUseMap := make(map[string]bool)
+ for _, c := range containers {
+ for _, m := range c.Mounts {
+ if m.Type == "volume" && m.Name != "" {
+ inUseMap[m.Name] = true
}
}
- result = filtered
- }
-
- totalItems := len(result)
-
- if req.Sort.Column != "" {
- sortVolumes(result, req.Sort.Column, req.Sort.Direction)
- }
-
- startIdx := (req.Pagination.Page - 1) * req.Pagination.Limit
- endIdx := startIdx + req.Pagination.Limit
-
- if startIdx > len(result) {
- startIdx = len(result)
- }
- if endIdx > len(result) {
- endIdx = len(result)
}
- pageItems := []dto.VolumeDto{}
- if startIdx < endIdx {
- pageItems = result[startIdx:endIdx]
- }
-
- totalPages := (totalItems + req.Pagination.Limit - 1) / req.Pagination.Limit
- pagination := utils.PaginationResponse{
- TotalPages: int64(totalPages),
- TotalItems: int64(totalItems),
- CurrentPage: req.Pagination.Page,
- ItemsPerPage: req.Pagination.Limit,
+ // List all volumes
+ volListBody, err := dockerClient.VolumeList(ctx, volume.ListOptions{})
+ if err != nil {
+ return nil, pagination.Response{}, fmt.Errorf("failed to list Docker volumes: %w", err)
}
- return pageItems, pagination, nil
-}
-
-func sortVolumes(items []dto.VolumeDto, field, direction string) {
- dir := utils.NormalizeSortDirection(direction)
- f := strings.ToLower(strings.TrimSpace(field))
- desc := dir == "desc"
-
- lessStr := func(a, b string) bool {
- if desc {
- return a > b
- }
- return a < b
- }
- lessBool := func(a, b bool) bool {
- if desc {
- return a && !b
+ volumes := make([]volume.Volume, 0, len(volListBody.Volumes))
+ for _, v := range volListBody.Volumes {
+ if v != nil {
+ volumes = append(volumes, *v)
}
- return !a && b
}
- parseTime := func(s string) (time.Time, bool) {
- if s == "" {
- return time.Time{}, false
- }
- if t, err := time.Parse(time.RFC3339, s); err == nil {
- return t, true
- }
- return time.Time{}, false
- }
- lessTime := func(aS, bS string) bool {
- aT, aOk := parseTime(aS)
- bT, bOk := parseTime(bS)
- if aOk && bOk {
- if desc {
- return aT.After(bT)
- }
- return aT.Before(bT)
- }
- return lessStr(aS, bS)
- }
-
- sort.Slice(items, func(i, j int) bool {
- a, b := items[i], items[j]
- switch f {
- case "name":
- return lessStr(a.Name, b.Name)
- case "driver":
- return lessStr(a.Driver, b.Driver)
- case "mountpoint":
- return lessStr(a.Mountpoint, b.Mountpoint)
- case "scope":
- return lessStr(a.Scope, b.Scope)
- case "created", "createdat":
- return lessTime(a.CreatedAt, b.CreatedAt)
- case "inuse":
- return lessBool(a.InUse, b.InUse)
- default:
- // no-op keeps original order
- return false
- }
- })
+ // Build DTOs using the inUse map (O(1) lookup)
+ items := make([]dto.VolumeDto, 0, len(volumes))
+ for _, v := range volumes {
+ items = append(items, dto.NewVolumeDto(v, inUseMap[v.Name]))
+ }
+
+ config := pagination.Config[dto.VolumeDto]{
+ SearchAccessors: []pagination.SearchAccessor[dto.VolumeDto]{
+ func(v dto.VolumeDto) (string, error) { return v.Name, nil },
+ func(v dto.VolumeDto) (string, error) { return v.Driver, nil },
+ func(v dto.VolumeDto) (string, error) { return v.Mountpoint, nil },
+ func(v dto.VolumeDto) (string, error) { return v.Scope, nil },
+ },
+ SortBindings: []pagination.SortBinding[dto.VolumeDto]{
+ {
+ Key: "name",
+ Fn: func(a, b dto.VolumeDto) int {
+ return strings.Compare(a.Name, b.Name)
+ },
+ },
+ {
+ Key: "driver",
+ Fn: func(a, b dto.VolumeDto) int {
+ return strings.Compare(a.Driver, b.Driver)
+ },
+ },
+ {
+ Key: "mountpoint",
+ Fn: func(a, b dto.VolumeDto) int {
+ return strings.Compare(a.Mountpoint, b.Mountpoint)
+ },
+ },
+ {
+ Key: "scope",
+ Fn: func(a, b dto.VolumeDto) int {
+ return strings.Compare(a.Scope, b.Scope)
+ },
+ },
+ {
+ Key: "created",
+ Fn: func(a, b dto.VolumeDto) int {
+ return strings.Compare(a.CreatedAt, b.CreatedAt)
+ },
+ },
+ {
+ Key: "inUse",
+ Fn: func(a, b dto.VolumeDto) int {
+ if a.InUse == b.InUse {
+ return 0
+ }
+ if a.InUse {
+ return -1
+ }
+ return 1
+ },
+ },
+ },
+ FilterAccessors: []pagination.FilterAccessor[dto.VolumeDto]{
+ {
+ Key: "inUse",
+ Fn: func(v dto.VolumeDto, filterValue string) bool {
+ if filterValue == "true" {
+ return v.InUse
+ }
+ if filterValue == "false" {
+ return !v.InUse
+ }
+ return true
+ },
+ },
+ },
+ }
+
+ result := pagination.SearchOrderAndPaginate(items, params, config)
+
+ totalPages := int64(0)
+ if params.Limit > 0 {
+ totalPages = (int64(result.TotalCount) + int64(params.Limit) - 1) / int64(params.Limit)
+ }
+
+ page := 1
+ if params.Limit > 0 {
+ page = (params.Start / params.Limit) + 1
+ }
+
+ paginationResp := pagination.Response{
+ TotalPages: totalPages,
+ TotalItems: int64(result.TotalCount),
+ CurrentPage: page,
+ ItemsPerPage: params.Limit,
+ GrandTotalItems: int64(result.TotalAvailable),
+ }
+
+ return result.Items, paginationResp, nil
}
diff --git a/backend/internal/utils/filter_utils.go b/backend/internal/utils/filter_utils.go
new file mode 100644
index 000000000..969f895f2
--- /dev/null
+++ b/backend/internal/utils/filter_utils.go
@@ -0,0 +1,151 @@
+package utils
+
+import (
+ "fmt"
+ "net/url"
+ "strconv"
+ "strings"
+
+ "github.com/docker/docker/api/types/filters"
+)
+
+// NormalizeQueryToFilters converts raw URL query into a flat filters map.
+// Supports: filters[foo]=bar, filters.foo=bar, foo=bar.
+func NormalizeQueryToFilters(q url.Values) map[string]interface{} {
+ if q == nil {
+ return nil
+ }
+ out := map[string]interface{}{}
+ for k, vals := range q {
+ // skip reserved
+ if strings.HasPrefix(k, "pagination[") || strings.HasPrefix(k, "sort[") || k == "search" {
+ continue
+ }
+ key := k
+ if strings.HasPrefix(key, "filters[") && strings.HasSuffix(key, "]") {
+ key = key[len("filters[") : len(key)-1]
+ } else if strings.HasPrefix(key, "filters.") {
+ key = key[len("filters."):]
+ }
+ if len(vals) == 1 {
+ out[key] = vals[0]
+ } else if len(vals) > 1 {
+ out[key] = vals
+ }
+ }
+ return out
+}
+
+// NormalizeFilterKeys flattens "filters[foo]" / "filters.foo" -> "foo".
+func NormalizeFilterKeys(filtersMap map[string]interface{}) map[string]interface{} {
+ if filtersMap == nil {
+ return nil
+ }
+ out := make(map[string]interface{}, len(filtersMap))
+ for k, v := range filtersMap {
+ key := strings.TrimSpace(k)
+ if strings.HasPrefix(key, "filters[") && strings.HasSuffix(key, "]") {
+ key = key[len("filters[") : len(key)-1]
+ } else if strings.HasPrefix(key, "filters.") {
+ key = key[len("filters."):]
+ }
+ out[key] = v
+ }
+ return out
+}
+
+// ParseBoolAny interprets many input types as boolean: true/false, 1/0, "true"/"false", etc.
+func ParseBoolAny(v interface{}) (bool, bool) {
+ switch t := v.(type) {
+ case bool:
+ return t, true
+ case string:
+ s := strings.ToLower(strings.TrimSpace(t))
+ if s == "true" || s == "1" {
+ return true, true
+ }
+ if s == "false" || s == "0" {
+ return false, true
+ }
+ if b, err := strconv.ParseBool(s); err == nil {
+ return b, true
+ }
+ case []string:
+ if len(t) > 0 {
+ return ParseBoolAny(t[0])
+ }
+ case []interface{}:
+ if len(t) > 0 {
+ return ParseBoolAny(t[0])
+ }
+ default:
+ s := fmt.Sprintf("%v", t)
+ return ParseBoolAny(s)
+ }
+ return false, false
+}
+
+// BuildDockerFiltersFromMap builds Docker filters.Args from a flat filters map and a search string.
+// Only allowedKeys are forwarded to the daemon. Search is mapped to name filter when provided.
+func BuildDockerFiltersFromMap(filtersMap map[string]interface{}, search string, allowedKeys []string) filters.Args {
+ f := filters.NewArgs()
+ if strings.TrimSpace(search) != "" {
+ f.Add("name", search)
+ }
+ allowed := map[string]bool{}
+ for _, k := range allowedKeys {
+ allowed[k] = true
+ }
+ for k, v := range filtersMap {
+ if !allowed[k] {
+ continue
+ }
+ switch t := v.(type) {
+ case string:
+ f.Add(k, t)
+ case []string:
+ for _, s := range t {
+ f.Add(k, s)
+ }
+ case []interface{}:
+ for _, it := range t {
+ f.Add(k, fmt.Sprintf("%v", it))
+ }
+ default:
+ f.Add(k, fmt.Sprintf("%v", t))
+ }
+ }
+ return f
+}
+
+// PaginateSlice returns the page of items and a PaginationResponse.
+func PaginateSlice[T any](items []T, page, limit int) ([]T, PaginationResponse) {
+ if limit <= 0 {
+ limit = len(items)
+ }
+ if page <= 0 {
+ page = 1
+ }
+ totalItems := len(items)
+ startIdx := (page - 1) * limit
+ if startIdx > totalItems {
+ startIdx = totalItems
+ }
+ endIdx := startIdx + limit
+ if endIdx > totalItems {
+ endIdx = totalItems
+ }
+ var pageItems []T
+ if startIdx < endIdx {
+ pageItems = items[startIdx:endIdx]
+ } else {
+ pageItems = []T{}
+ }
+ totalPages := (totalItems + limit - 1) / limit
+ return pageItems, PaginationResponse{
+ TotalPages: int64(totalPages),
+ TotalItems: int64(totalItems),
+ CurrentPage: page,
+ ItemsPerPage: limit,
+ }
+}
diff --git a/backend/internal/utils/http/request.go b/backend/internal/utils/http/request.go
new file mode 100644
index 000000000..5519cc492
--- /dev/null
+++ b/backend/internal/utils/http/request.go
@@ -0,0 +1,38 @@
+package http
+
+import (
+ "fmt"
+ "strconv"
+
+ "github.com/gin-gonic/gin"
+)
+
+// GetQueryParam reads a string query parameter from the Gin context.
+// If `required` is true and the parameter is missing or empty, an error is returned.
+func GetQueryParam(c *gin.Context, name string, required bool) (string, error) {
+ v, ok := c.GetQuery(name)
+ if !ok || v == "" {
+ if required {
+ return "", fmt.Errorf("missing query parameter %s", name)
+ }
+ return "", nil
+ }
+ return v, nil
+}
+
+// GetIntQueryParam reads and parses an integer query parameter from the Gin context.
+// If `required` is true and the parameter is missing, or if parsing fails, an error is returned.
+func GetIntQueryParam(c *gin.Context, name string, required bool) (int, error) {
+ v, ok := c.GetQuery(name)
+ if !ok || v == "" {
+ if required {
+ return 0, fmt.Errorf("missing numeric query parameter %s", name)
+ }
+ return 0, nil
+ }
+ n, err := strconv.Atoi(v)
+ if err != nil {
+ return 0, fmt.Errorf("invalid numeric query parameter %s: %w", name, err)
+ }
+ return n, nil
+}
diff --git a/backend/internal/utils/pagination/filters.go b/backend/internal/utils/pagination/filters.go
new file mode 100644
index 000000000..39f1c0e60
--- /dev/null
+++ b/backend/internal/utils/pagination/filters.go
@@ -0,0 +1,79 @@
+package pagination
+
+import (
+ "strconv"
+
+ "github.com/gin-gonic/gin"
+)
+
+type FilterResult[T any] struct {
+ Items []T
+ TotalCount int
+ TotalAvailable int
+}
+
+type FilterAccessor[T any] struct {
+ Key string
+ Fn func(item T, filterValue string) bool
+}
+
+type Config[T any] struct {
+ SearchAccessors []SearchAccessor[T]
+ SortBindings []SortBinding[T]
+ FilterAccessors []FilterAccessor[T]
+}
+
+func SearchOrderAndPaginate[T any](items []T, params QueryParams, searchConfig Config[T]) FilterResult[T] {
+ totalAvailable := len(items)
+
+ items = searchFn(items, params.SearchQuery, searchConfig.SearchAccessors)
+ items = filterFn(items, params.Filters, searchConfig.FilterAccessors)
+ items = sortFunction(items, params.SortParams, searchConfig.SortBindings)
+
+ totalCount := len(items)
+ items = paginateItemsFunction(items, params.PaginationParams)
+
+ return FilterResult[T]{
+ Items: items,
+ TotalCount: totalCount,
+ TotalAvailable: totalAvailable,
+ }
+}
+
+func filterFn[T any](items []T, filters map[string]string, accessors []FilterAccessor[T]) []T {
+ if len(filters) == 0 {
+ return items
+ }
+
+ results := []T{}
+ for _, item := range items {
+ matches := true
+ for key, value := range filters {
+ found := false
+ for _, accessor := range accessors {
+ if accessor.Key == key {
+ if !accessor.Fn(item, value) {
+ matches = false
+ }
+ found = true
+ break
+ }
+ }
+ if !found {
+ matches = false
+ }
+ if !matches {
+ break
+ }
+ }
+ if matches {
+ results = append(results, item)
+ }
+ }
+ return results
+}
+
+func ApplyFilterResultsHeaders[T any](w *gin.ResponseWriter, result FilterResult[T]) {
+ (*w).Header().Set("X-Arcane-Total-Items", strconv.Itoa(result.TotalCount))
+ (*w).Header().Set("X-Arcane-Total-Available", strconv.Itoa(result.TotalAvailable))
+}
diff --git a/backend/internal/utils/pagination/helpers.go b/backend/internal/utils/pagination/helpers.go
new file mode 100644
index 000000000..b93422994
--- /dev/null
+++ b/backend/internal/utils/pagination/helpers.go
@@ -0,0 +1,29 @@
+package pagination
+
+func ConvertQueryParamsToSortedRequest(q QueryParams) SortedPaginationRequest {
+ limit := q.Limit
+ if limit <= 0 {
+ limit = 20
+ }
+
+ page := 1
+ if limit > 0 {
+ page = (q.Start / limit) + 1
+ }
+
+ var r SortedPaginationRequest
+ r.Search = q.Search
+ r.Sort.Column = q.sort
+
+ // Convert SortOrder to string and default to asc when empty
+ order := q.order
+ if order == "" {
+ order = SortAsc
+ }
+ r.Sort.Direction = string(order)
+
+ r.Pagination.Page = page
+ r.Pagination.Limit = limit
+
+ return r
+}
diff --git a/backend/internal/utils/pagination/pagination.go b/backend/internal/utils/pagination/pagination.go
new file mode 100644
index 000000000..503da8590
--- /dev/null
+++ b/backend/internal/utils/pagination/pagination.go
@@ -0,0 +1,20 @@
+package pagination
+
+type PaginationParams struct {
+ Start int
+ Limit int
+}
+
+func paginateItemsFunction[T any](items []T, params PaginationParams) []T {
+ if params.Limit <= 0 {
+ return items
+ }
+
+ itemsCount := len(items)
+
+ start := min(max(params.Start, 0), itemsCount)
+
+ end := min(start+params.Limit, itemsCount)
+
+ return items[start:end]
+}
diff --git a/backend/internal/utils/pagination/params.go b/backend/internal/utils/pagination/params.go
new file mode 100644
index 000000000..7fe8313db
--- /dev/null
+++ b/backend/internal/utils/pagination/params.go
@@ -0,0 +1,48 @@
+package pagination
+
+import (
+ "github.com/gin-gonic/gin"
+ arcanehttp "github.com/ofkm/arcane-backend/internal/utils/http"
+)
+
+type QueryParams struct {
+ SearchQuery
+ SortParams
+ PaginationParams
+ Filters map[string]string
+}
+
+func ExtractListModifiersQueryParams(c *gin.Context) QueryParams {
+ // search
+ search, _ := arcanehttp.GetQueryParam(c, "search", true)
+ // sorting
+ sortField, _ := arcanehttp.GetQueryParam(c, "sort", true)
+ sortOrder, _ := arcanehttp.GetQueryParam(c, "order", true)
+ // pagination
+ start, _ := arcanehttp.GetIntQueryParam(c, "start", true)
+ limit, _ := arcanehttp.GetIntQueryParam(c, "limit", true)
+
+ filters := make(map[string]string)
+ for key, values := range c.Request.URL.Query() {
+ if key != "search" && key != "sort" && key != "order" && key != "start" && key != "limit" {
+ if len(values) > 0 {
+ filters[key] = values[0]
+ }
+ }
+ }
+
+ return QueryParams{
+ SearchQuery{
+ Search: search,
+ },
+ SortParams{
+ sort: sortField,
+ order: SortOrder(sortOrder),
+ },
+ PaginationParams{
+ Start: start,
+ Limit: limit,
+ },
+ filters,
+ }
+}
diff --git a/backend/internal/utils/pagination/reflect.go b/backend/internal/utils/pagination/reflect.go
new file mode 100644
index 000000000..ea0822ecb
--- /dev/null
+++ b/backend/internal/utils/pagination/reflect.go
@@ -0,0 +1,105 @@
+package pagination
+
+import (
+ "reflect"
+ "strconv"
+
+ "gorm.io/gorm"
+ "gorm.io/gorm/clause"
+)
+
+func PaginateAndSortDB(params QueryParams, query *gorm.DB, result interface{}) (Response, error) {
+ sortColumn := params.sort
+ sortDirection := string(params.order)
+
+ if sortDirection == "" {
+ sortDirection = "asc"
+ }
+
+ capitalizedSortColumn := capitalizeFirstLetter(sortColumn)
+ sortField, sortFieldFound := reflect.TypeOf(result).Elem().Elem().FieldByName(capitalizedSortColumn)
+ isSortable, _ := strconv.ParseBool(sortField.Tag.Get("sortable"))
+
+ sortDirection = normalizeSortDirection(sortDirection)
+
+ if sortFieldFound && isSortable {
+ columnName := camelCaseToSnakeCase(sortColumn)
+ query = query.Clauses(clause.OrderBy{
+ Columns: []clause.OrderByColumn{
+ {Column: clause.Column{Name: columnName}, Desc: sortDirection == "desc"},
+ },
+ })
+ }
+
+ limit := params.Limit
+ if limit <= 0 {
+ limit = 20
+ } else if limit > 100 {
+ limit = 100
+ }
+
+ page := 1
+ if limit > 0 {
+ page = (params.Start / limit) + 1
+ }
+
+ return paginateDB(page, limit, query, result)
+}
+
+func paginateDB(page int, pageSize int, query *gorm.DB, result interface{}) (Response, error) {
+ if page < 1 {
+ page = 1
+ }
+
+ offset := (page - 1) * pageSize
+
+ var totalItems int64
+ if err := query.Count(&totalItems).Error; err != nil {
+ return Response{}, err
+ }
+
+ if err := query.Offset(offset).Limit(pageSize).Find(result).Error; err != nil {
+ return Response{}, err
+ }
+
+ totalPages := (totalItems + int64(pageSize) - 1) / int64(pageSize)
+ if totalItems == 0 {
+ totalPages = 1
+ }
+
+ return Response{
+ TotalPages: totalPages,
+ TotalItems: totalItems,
+ CurrentPage: page,
+ ItemsPerPage: pageSize,
+ }, nil
+}
+
+func normalizeSortDirection(direction string) string {
+ if direction != "asc" && direction != "desc" {
+ return "asc"
+ }
+ return direction
+}
+
+func capitalizeFirstLetter(str string) string {
+ if len(str) == 0 {
+ return str
+ }
+ return string(str[0]-32) + str[1:]
+}
+
+func camelCaseToSnakeCase(str string) string {
+ var result []rune
+ for i, r := range str {
+ if i > 0 && r >= 'A' && r <= 'Z' {
+ result = append(result, '_')
+ }
+ if r >= 'A' && r <= 'Z' {
+ result = append(result, r+32)
+ } else {
+ result = append(result, r)
+ }
+ }
+ return string(result)
+}
diff --git a/backend/internal/utils/pagination/search.go b/backend/internal/utils/pagination/search.go
new file mode 100644
index 000000000..3b9ba0fa2
--- /dev/null
+++ b/backend/internal/utils/pagination/search.go
@@ -0,0 +1,36 @@
+package pagination
+
+import (
+ "strings"
+)
+
+// Return any error to skip the field (for when matching an unknown state on an enum)
+//
+// Note: returning ("", nil) will match!
+type SearchAccessor[T any] = func(T) (string, error)
+
+type SearchQuery struct {
+ Search string
+}
+
+func searchFn[T any](items []T, params SearchQuery, accessors []SearchAccessor[T]) []T {
+ search := strings.TrimSpace(params.Search)
+
+ if search == "" {
+ return items
+ }
+
+ results := []T{}
+
+ for iIdx := range items {
+ for aIdx := range accessors {
+ value, err := accessors[aIdx](items[iIdx])
+ if err == nil && strings.Contains(strings.ToLower(value), strings.ToLower(search)) {
+ results = append(results, items[iIdx])
+ break
+ }
+ }
+ }
+
+ return results
+}
diff --git a/backend/internal/utils/pagination/sort.go b/backend/internal/utils/pagination/sort.go
new file mode 100644
index 000000000..64e1e7d1f
--- /dev/null
+++ b/backend/internal/utils/pagination/sort.go
@@ -0,0 +1,40 @@
+package pagination
+
+import "slices"
+
+type SortOrder string
+
+const (
+ SortAsc SortOrder = "asc"
+ SortDesc SortOrder = "desc"
+)
+
+type SortParams struct {
+ sort string
+ order SortOrder
+}
+
+type SortOption[T any] func(a, b T) int
+type SortBinding[T any] struct {
+ Key string
+ Fn SortOption[T]
+}
+
+func sortFunction[T any](items []T, params SortParams, sorts []SortBinding[T]) []T {
+ for _, sort := range sorts {
+ if sort.Key == params.sort {
+ fn := sort.Fn
+ if params.order == SortDesc {
+ fn = reverSortFn(fn)
+ }
+ slices.SortStableFunc(items, fn)
+ }
+ }
+ return items
+}
+
+func reverSortFn[T any](fn SortOption[T]) SortOption[T] {
+ return func(a, b T) int {
+ return -1 * fn(a, b)
+ }
+}
diff --git a/backend/internal/utils/pagination/types.go b/backend/internal/utils/pagination/types.go
new file mode 100644
index 000000000..0be080c5f
--- /dev/null
+++ b/backend/internal/utils/pagination/types.go
@@ -0,0 +1,22 @@
+package pagination
+
+type Response struct {
+ TotalPages int64 `json:"totalPages"`
+ TotalItems int64 `json:"totalItems"`
+ CurrentPage int `json:"currentPage"`
+ ItemsPerPage int `json:"itemsPerPage"`
+ GrandTotalItems int64 `json:"grandTotalItems,omitempty"`
+}
+
+type SortedPaginationRequest struct {
+ Pagination struct {
+ Page int `form:"pagination[page]"`
+ Limit int `form:"pagination[limit]"`
+ } `form:"pagination"`
+ Sort struct {
+ Column string `form:"sort[column]"`
+ Direction string `form:"sort[direction]"`
+ } `form:"sort"`
+ Search string `form:"search"`
+ Filters map[string]interface{} `form:"filters"`
+}
diff --git a/backend/resources/migrations/postgres/016_drop_image_table.down.sql b/backend/resources/migrations/postgres/016_drop_image_table.down.sql
new file mode 100644
index 000000000..c14836e20
--- /dev/null
+++ b/backend/resources/migrations/postgres/016_drop_image_table.down.sql
@@ -0,0 +1,14 @@
+CREATE TABLE IF NOT EXISTS images_table (
+ id TEXT PRIMARY KEY,
+ repo_tags TEXT,
+ repo_digests TEXT,
+ size BIGINT NOT NULL,
+ virtual_size BIGINT NOT NULL DEFAULT 0,
+ labels TEXT,
+ created TIMESTAMP NOT NULL,
+ repo TEXT,
+ tag TEXT,
+ in_use BOOLEAN NOT NULL DEFAULT false,
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ updated_at TIMESTAMP
+);
\ No newline at end of file
diff --git a/backend/resources/migrations/postgres/016_drop_image_table.up.sql b/backend/resources/migrations/postgres/016_drop_image_table.up.sql
new file mode 100644
index 000000000..39d4074ea
--- /dev/null
+++ b/backend/resources/migrations/postgres/016_drop_image_table.up.sql
@@ -0,0 +1 @@
+DROP TABLE IF EXISTS images_table;
\ No newline at end of file
diff --git a/backend/resources/migrations/sqlite/016_drop_image_table.down.sql b/backend/resources/migrations/sqlite/016_drop_image_table.down.sql
new file mode 100644
index 000000000..4b92fe99e
--- /dev/null
+++ b/backend/resources/migrations/sqlite/016_drop_image_table.down.sql
@@ -0,0 +1,14 @@
+CREATE TABLE IF NOT EXISTS images_table (
+ id TEXT PRIMARY KEY,
+ repo_tags TEXT,
+ repo_digests TEXT,
+ size INTEGER NOT NULL,
+ virtual_size INTEGER NOT NULL DEFAULT 0,
+ labels TEXT,
+ created DATETIME NOT NULL,
+ repo TEXT,
+ tag TEXT,
+ in_use BOOLEAN NOT NULL DEFAULT false,
+ created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ updated_at DATETIME
+);
\ No newline at end of file
diff --git a/backend/resources/migrations/sqlite/016_drop_image_table.up.sql b/backend/resources/migrations/sqlite/016_drop_image_table.up.sql
new file mode 100644
index 000000000..39d4074ea
--- /dev/null
+++ b/backend/resources/migrations/sqlite/016_drop_image_table.up.sql
@@ -0,0 +1 @@
+DROP TABLE IF EXISTS images_table;
\ No newline at end of file
diff --git a/frontend/src/lib/components/arcane-table/arcane-table-filter.svelte b/frontend/src/lib/components/arcane-table/arcane-table-filter.svelte
index d53f9839d..a340d2ba0 100644
--- a/frontend/src/lib/components/arcane-table/arcane-table-filter.svelte
+++ b/frontend/src/lib/components/arcane-table/arcane-table-filter.svelte
@@ -28,7 +28,6 @@
showCheckboxes?: boolean;
} = $props();
- const facets = $derived(column?.getFacetedUniqueValues());
const selectedValues = $derived(new SvelteSet(column?.getFilterValue() as any[]));
</script>
@@ -99,18 +98,13 @@
{/if}
<span>{option.label}</span>
- {#if facets?.get(option.value)}
- <span class="ml-auto flex size-4 items-center justify-center font-mono text-xs">
- {facets.get(option.value)}
- </span>
- {/if}
</Command.Item>
{/each}
</Command.Group>
{#if selectedValues.size > 0}
<Command.Separator />
<Command.Group>
- <Command.Item onSelect={() => column?.setFilterValue(undefined)} class="justify-center text-center">
+ <Command.Item onselect={() => column?.setFilterValue(undefined)} class="justify-center text-center">
{m.common_clear_filters()}
</Command.Item>
</Command.Group>
diff --git a/frontend/src/lib/components/arcane-table/arcane-table.svelte b/frontend/src/lib/components/arcane-table/arcane-table.svelte
index b086c6f2f..fadbedfac 100644
--- a/frontend/src/lib/components/arcane-table/arcane-table.svelte
+++ b/frontend/src/lib/components/arcane-table/arcane-table.svelte
@@ -8,10 +8,7 @@
type SortingState,
type VisibilityState,
type Table as TableType,
- getCoreRowModel,
- getFacetedRowModel,
- getFacetedUniqueValues,
- getFilteredRowModel
+ getCoreRowModel
} from '@tanstack/table-core';
import DataTableToolbar from './arcane-table-toolbar.svelte';
import { createSvelteTable } from '$lib/components/ui/data-table/data-table.svelte.js';
@@ -31,7 +28,7 @@
import * as Select from '$lib/components/ui/select/index.js';
import type { HTMLAttributes } from 'svelte/elements';
import { cn } from '$lib/utils.js';
- import type { Paginated, SearchPaginationSortRequest } from '$lib/types/pagination.type';
+ import type { Paginated, SearchPaginationSortRequest, FilterMap } from '$lib/types/pagination.type';
import type { Snippet } from 'svelte';
import type { ColumnSpec } from './arcane-table.types.svelte';
import TableCheckbox from './arcane-table-checkbox.svelte';
@@ -224,8 +221,7 @@
return renderSnippet(TextCell, { value });
},
enableSorting: !!spec.sortable,
- enableHiding: true,
- filterFn: spec.filterFn
+ enableHiding: true
});
if (spec.hidden) {
@@ -299,10 +295,18 @@
},
onColumnFiltersChange: (updater) => {
columnFilters = typeof updater === 'function' ? updater(columnFilters) : updater;
- // Persist column filters
if (enablePersist && prefs) {
prefs.current = { ...prefs.current, f: encodeFilters(columnFilters) };
}
+ requestOptions = {
+ ...requestOptions,
+ filters: toFilterMap(columnFilters),
+ pagination: {
+ page: 1,
+ limit: requestOptions?.pagination?.limit ?? items?.pagination?.itemsPerPage ?? 10
+ }
+ };
+ onRefresh(requestOptions);
},
onColumnVisibilityChange: (updater) => {
columnVisibility = typeof updater === 'function' ? updater(columnVisibility) : updater;
@@ -325,12 +329,29 @@
}
onRefresh(requestOptions);
},
- getCoreRowModel: getCoreRowModel(),
- getFilteredRowModel: getFilteredRowModel(),
- getFacetedRowModel: getFacetedRowModel(),
- getFacetedUniqueValues: getFacetedUniqueValues()
+ getCoreRowModel: getCoreRowModel()
});
+ function toFilterMap(filters: ColumnFiltersState): FilterMap {
+ const out: FilterMap = {};
+ for (const f of filters ?? []) {
+ const id = f.id;
+ let v: unknown = (f as any).value;
+ if (Array.isArray(v)) {
+ if (v.length === 0) continue;
+ v = v[0];
+ } else if (v && typeof v === 'object' && v instanceof Set) {
+ const first = (v as Set<unknown>).values().next().value;
+ if (first === undefined) continue;
+ v = first;
+ }
+ if (v !== undefined && v !== null && String(v).trim() !== '') {
+ out[id] = v as any; // scalar only
+ }
+ }
+ return out;
+ }
+
$effect(() => {
const s = requestOptions?.sort;
if (!s) {
@@ -355,7 +376,7 @@
{#snippet Pagination({ table }: { table: TableType<TData> })}
<div class="flex items-center justify-between px-2">
<div class="text-muted-foreground flex-1 text-sm">
- {m.common_showing_of_total({ shown: table.getFilteredRowModel().rows.length, total: totalItems })}
+ {m.common_showing_of_total({ shown: items.data.length, total: totalItems })}
</div>
<div class="flex items-center space-x-6 lg:space-x-8">
<div class="flex items-center space-x-2">
diff --git a/frontend/src/lib/services/container-registry-service.ts b/frontend/src/lib/services/container-registry-service.ts
index b84af9662..b72ea8ce3 100644
--- a/frontend/src/lib/services/container-registry-service.ts
+++ b/frontend/src/lib/services/container-registry-service.ts
@@ -2,10 +2,12 @@ import BaseAPIService from './api-service';
import type { ContainerRegistryCreateDto, ContainerRegistryUpdateDto } from '$lib/types/container-registry.type';
import type { ContainerRegistry } from '$lib/types/container-registry.type';
import type { Paginated, SearchPaginationSortRequest } from '$lib/types/pagination.type';
+import { transformPaginationParams } from '$lib/utils/params.util';
export default class ContainerRegistryService extends BaseAPIService {
async getRegistries(options?: SearchPaginationSortRequest): Promise<Paginated<ContainerRegistry>> {
- const res = await this.api.get('/container-registries', { params: options });
+ const params = transformPaginationParams(options);
+ const res = await this.api.get('/container-registries', { params });
return res.data;
}
diff --git a/frontend/src/lib/services/container-service.ts b/frontend/src/lib/services/container-service.ts
index 997dab2c7..6bd6fa88a 100644
--- a/frontend/src/lib/services/container-service.ts
+++ b/frontend/src/lib/services/container-service.ts
@@ -2,6 +2,7 @@ import BaseAPIService from './api-service';
import { environmentStore } from '$lib/stores/environment.store';
import type { ContainerStatusCounts, ContainerSummaryDto } from '$lib/types/container.type';
import type { SearchPaginationSortRequest, Paginated } from '$lib/types/pagination.type';
+import { transformPaginationParams } from '$lib/utils/params.util';
// Would like to get rid of dockerode
import type { ContainerCreateOptions, ContainerStats } from 'dockerode';
@@ -9,8 +10,8 @@ import type { ContainerCreateOptions, ContainerStats } from 'dockerode';
export class ContainerService extends BaseAPIService {
async getContainers(options?: SearchPaginationSortRequest): Promise<Paginated<ContainerSummaryDto>> {
const envId = await environmentStore.getCurrentEnvironmentId();
-
- const res = await this.api.get(`/environments/${envId}/containers`, { params: options });
+ const params = transformPaginationParams(options);
+ const res = await this.api.get(`/environments/${envId}/containers`, { params });
return res.data;
}
diff --git a/frontend/src/lib/services/env-mgmt-service.ts b/frontend/src/lib/services/env-mgmt-service.ts
index 9cc0b390d..6edb143fe 100644
--- a/frontend/src/lib/services/env-mgmt-service.ts
+++ b/frontend/src/lib/services/env-mgmt-service.ts
@@ -2,6 +2,7 @@ import BaseAPIService from './api-service';
import type { Environment } from '$lib/types/environment.type';
import type { CreateEnvironmentDTO, UpdateEnvironmentDTO } from '$lib/types/environment.type';
import type { Paginated, SearchPaginationSortRequest } from '$lib/types/pagination.type';
+import { transformPaginationParams } from '$lib/utils/params.util';
export default class EnvironmentManagementService extends BaseAPIService {
async create(dto: CreateEnvironmentDTO): Promise<Environment> {
@@ -10,7 +11,8 @@ export default class EnvironmentManagementService extends BaseAPIService {
}
async getEnvironments(options: SearchPaginationSortRequest): Promise<Paginated<Environment>> {
- const res = await this.api.get('/environments', { params: options });
+ const params = transformPaginationParams(options);
+ const res = await this.api.get('/environments', { params });
return res.data;
}
diff --git a/frontend/src/lib/services/event-service.ts b/frontend/src/lib/services/event-service.ts
index 2cbf3e81a..bbe8ea99f 100644
--- a/frontend/src/lib/services/event-service.ts
+++ b/frontend/src/lib/services/event-service.ts
@@ -1,10 +1,12 @@
import BaseAPIService from './api-service';
import type { SearchPaginationSortRequest, Paginated } from '$lib/types/pagination.type';
import type { Event } from '$lib/types/event.type';
+import { transformPaginationParams } from '$lib/utils/params.util';
export default class EventService extends BaseAPIService {
async getEvents(options?: SearchPaginationSortRequest): Promise<Paginated<Event>> {
- const res = await this.api.get('/events', { params: options });
+ const params = transformPaginationParams(options);
+ const res = await this.api.get('/events', { params });
return res.data;
}
diff --git a/frontend/src/lib/services/image-service.ts b/frontend/src/lib/services/image-service.ts
index 79367eb2a..c8aacf2f1 100644
--- a/frontend/src/lib/services/image-service.ts
+++ b/frontend/src/lib/services/image-service.ts
@@ -3,18 +3,18 @@ import { environmentStore } from '$lib/stores/environment.store';
import type { ImageSummaryDto, ImageUsageCounts, ImageUpdateInfoDto } from '$lib/types/image.type';
import type { SearchPaginationSortRequest, Paginated } from '$lib/types/pagination.type';
import type { AutoUpdateCheck, AutoUpdateResult } from '$lib/types/auto-update.type';
+import { transformPaginationParams } from '$lib/utils/params.util';
export class ImageService extends BaseAPIService {
async getImages(options?: SearchPaginationSortRequest): Promise<Paginated<ImageSummaryDto>> {
const envId = await environmentStore.getCurrentEnvironmentId();
-
- const res = await this.api.get(`/environments/${envId}/images`, { params: options });
+ const params = transformPaginationParams(options);
+ const res = await this.api.get(`/environments/${envId}/images`, { params });
return res.data;
}
async getImageUsageCounts(): Promise<ImageUsageCounts> {
const envId = await environmentStore.getCurrentEnvironmentId();
-
const res = await this.api.get(`/environments/${envId}/images/counts`);
return res.data.data;
}
diff --git a/frontend/src/lib/services/network-service.ts b/frontend/src/lib/services/network-service.ts
index 596c9718a..1c1097104 100644
--- a/frontend/src/lib/services/network-service.ts
+++ b/frontend/src/lib/services/network-service.ts
@@ -3,18 +3,18 @@ import { environmentStore } from '$lib/stores/environment.store';
import type { NetworkSummaryDto, NetworkUsageCounts } from '$lib/types/network.type';
import type { SearchPaginationSortRequest, Paginated } from '$lib/types/pagination.type';
import type { NetworkCreateOptions } from 'dockerode';
+import { transformPaginationParams } from '$lib/utils/params.util';
export class NetworkService extends BaseAPIService {
async getNetworks(options?: SearchPaginationSortRequest): Promise<Paginated<NetworkSummaryDto>> {
const envId = await environmentStore.getCurrentEnvironmentId();
-
- const res = await this.api.get(`/environments/${envId}/networks`, { params: options });
+ const params = transformPaginationParams(options);
+ const res = await this.api.get(`/environments/${envId}/networks`, { params });
return res.data;
}
async getNetworkUsageCounts(): Promise<NetworkUsageCounts> {
const envId = await environmentStore.getCurrentEnvironmentId();
-
const res = await this.api.get(`/environments/${envId}/networks/counts`);
return res.data.data;
}
diff --git a/frontend/src/lib/services/project-service.ts b/frontend/src/lib/services/project-service.ts
index 68c54ec2b..f87513662 100644
--- a/frontend/src/lib/services/project-service.ts
+++ b/frontend/src/lib/services/project-service.ts
@@ -2,12 +2,13 @@ import BaseAPIService from './api-service';
import { environmentStore } from '$lib/stores/environment.store';
import type { Project, ProjectStatusCounts } from '$lib/types/project.type';
import type { SearchPaginationSortRequest, Paginated } from '$lib/types/pagination.type';
+import { transformPaginationParams } from '$lib/utils/params.util';
export class ProjectService extends BaseAPIService {
async getProjects(options?: SearchPaginationSortRequest): Promise<Paginated<Project>> {
const envId = await environmentStore.getCurrentEnvironmentId();
-
- const res = await this.api.get(`/environments/${envId}/projects`, { params: options });
+ const params = transformPaginationParams(options);
+ const res = await this.api.get(`/environments/${envId}/projects`, { params });
return res.data;
}
diff --git a/frontend/src/lib/services/user-service.ts b/frontend/src/lib/services/user-service.ts
index 63e93239d..fd63123c7 100644
--- a/frontend/src/lib/services/user-service.ts
+++ b/frontend/src/lib/services/user-service.ts
@@ -1,10 +1,12 @@
import BaseAPIService from './api-service';
import type { User, CreateUser } from '$lib/types/user.type';
import type { Paginated, SearchPaginationSortRequest } from '$lib/types/pagination.type';
+import { transformPaginationParams } from '$lib/utils/params.util';
export default class UserAPIService extends BaseAPIService {
async getUsers(options?: SearchPaginationSortRequest): Promise<Paginated<User>> {
- const res = await this.api.get('/users', { params: options });
+ const params = transformPaginationParams(options);
+ const res = await this.api.get('/users', { params });
return res.data;
}
diff --git a/frontend/src/lib/services/volume-service.ts b/frontend/src/lib/services/volume-service.ts
index dd0e2254a..8e54323d9 100644
--- a/frontend/src/lib/services/volume-service.ts
+++ b/frontend/src/lib/services/volume-service.ts
@@ -3,12 +3,13 @@ import { environmentStore } from '$lib/stores/environment.store';
import type { VolumeSummaryDto, VolumeDetailDto, VolumeUsageDto, VolumeUsageCounts } from '$lib/types/volume.type';
import type { SearchPaginationSortRequest, Paginated } from '$lib/types/pagination.type';
import type { VolumeCreateOptions } from 'dockerode';
+import { transformPaginationParams } from '$lib/utils/params.util';
export class VolumeService extends BaseAPIService {
async getVolumes(options?: SearchPaginationSortRequest): Promise<Paginated<VolumeSummaryDto>> {
const envId = await environmentStore.getCurrentEnvironmentId();
-
- const res = await this.api.get(`/environments/${envId}/volumes`, { params: options });
+ const params = transformPaginationParams(options);
+ const res = await this.api.get(`/environments/${envId}/volumes`, { params });
return res.data;
}
@@ -24,7 +25,6 @@ export class VolumeService extends BaseAPIService {
async getVolumeUsageCounts(): Promise<VolumeUsageCounts> {
const envId = await environmentStore.getCurrentEnvironmentId();
-
const res = await this.api.get(`/environments/${envId}/volumes/counts`);
return res.data.data;
}
diff --git a/frontend/src/lib/types/pagination.type.ts b/frontend/src/lib/types/pagination.type.ts
index eb7e2549f..e935d4914 100644
--- a/frontend/src/lib/types/pagination.type.ts
+++ b/frontend/src/lib/types/pagination.type.ts
@@ -8,7 +8,8 @@ export type SortRequest = {
direction: 'asc' | 'desc';
};
-export type FilterMap = Record<string, string>;
+export type FilterValue = string | number | boolean | (string | number | boolean)[];
+export type FilterMap = Record<string, FilterValue>;
export type SearchPaginationSortRequest = {
search?: string;
diff --git a/frontend/src/lib/utils/params.util.ts b/frontend/src/lib/utils/params.util.ts
new file mode 100644
index 000000000..3d67be0c4
--- /dev/null
+++ b/frontend/src/lib/utils/params.util.ts
@@ -0,0 +1,34 @@
+import type { SearchPaginationSortRequest } from '$lib/types/pagination.type';
+
+export function transformPaginationParams(options?: SearchPaginationSortRequest): Record<string, any> {
+ const params: Record<string, any> = {};
+
+ if (!options) return params;
+
+ if (options.search) {
+ params.search = options.search;
+ }
+
+ if (options.pagination) {
+ const { page, limit } = options.pagination;
+ params.start = Math.max(0, (page - 1) * limit);
+ params.limit = limit;
+ }
+
+ if (options.sort) {
+ params.sort = options.sort.column;
+ params.order = options.sort.direction;
+ }
+
+ if (options.filters) {
+ Object.entries(options.filters).forEach(([key, value]) => {
+ if (Array.isArray(value)) {
+ params[key] = value.join(',');
+ } else {
+ params[key] = String(value);
+ }
+ });
+ }
+
+ return params;
+}
diff --git a/frontend/src/routes/networks/network-table.svelte b/frontend/src/routes/networks/network-table.svelte
index 1a422df71..baa591d4d 100644
--- a/frontend/src/routes/networks/network-table.svelte
+++ b/frontend/src/routes/networks/network-table.svelte
@@ -10,12 +10,11 @@
import { toast } from 'svelte-sonner';
import * as DropdownMenu from '$lib/components/ui/dropdown-menu/index.js';
import { openConfirmDialog } from '$lib/components/confirm-dialog';
+ import StatusBadge from '$lib/components/badges/status-badge.svelte';
import { handleApiResultWithCallbacks } from '$lib/utils/api.util';
import { tryCatch } from '$lib/utils/try-catch';
import { DEFAULT_NETWORK_NAMES } from '$lib/constants';
import type { SearchPaginationSortRequest, Paginated } from '$lib/types/pagination.type';
- import StatusBadge from '$lib/components/badges/status-badge.svelte';
- import NetworkIcon from '@lucide/svelte/icons/network';
import { capitalizeFirstLetter } from '$lib/utils/string.utils';
import type { ColumnSpec } from '$lib/components/arcane-table';
import { m } from '$lib/paraglide/messages';
@@ -127,13 +126,7 @@
accessorKey: 'inUse',
title: m.common_status(),
sortable: true,
- cell: StatusCell,
- filterFn: (row, columnId, filterValue) => {
- const selected = Array.isArray(filterValue) ? (filterValue as boolean[]) : [];
- if (selected.length === 0) return true;
- const value = Boolean(row.getValue<boolean>(columnId));
- return selected.includes(value);
- }
+ cell: StatusCell
},
{
accessorKey: 'driver',
@@ -201,40 +194,31 @@
<ScanSearchIcon class="size-4" />
{m.common_inspect()}
</DropdownMenu.Item>
- {#if !DEFAULT_NETWORK_NAMES.has(item.name)}
- <DropdownMenu.Item
- variant="destructive"
- onclick={() => handleDeleteNetwork(item.id, item.name)}
- disabled={DEFAULT_NETWORK_NAMES.has(item.name) || isAnyLoading}
- >
- <Trash2Icon class="size-4" />
- {m.common_remove()}
- </DropdownMenu.Item>
- {/if}
+ <DropdownMenu.Separator />
+ <DropdownMenu.Item
+ variant="destructive"
+ onclick={() => handleDeleteNetwork(item.id, item.name)}
+ disabled={isAnyLoading || DEFAULT_NETWORK_NAMES.has(item.name)}
+ >
+ <Trash2Icon class="size-4" />
+ {m.common_delete()}
+ </DropdownMenu.Item>
</DropdownMenu.Group>
</DropdownMenu.Content>
</DropdownMenu.Root>
{/snippet}
-{#if hasNetworks}
- <Card.Root>
- <Card.Content class="py-5">
- <ArcaneTable
- persistKey="arcane-networks-table"
- items={networks}
- bind:requestOptions
- bind:selectedIds
- onRemoveSelected={(ids) => handleDeleteSelectedNetworks(ids)}
- onRefresh={async (options) => (networks = await networkService.getNetworks(options))}
- {columns}
- rowActions={RowActions}
- />
- </Card.Content>
- </Card.Root>
-{:else}
- <div class="flex flex-col items-center justify-center px-6 py-12 text-center">
- <NetworkIcon class="text-muted-foreground mb-4 size-12 opacity-40" />
- <p class="text-lg font-medium">{m.networks_empty_title()}</p>
- <p class="text-muted-foreground mt-1 text-sm">{m.networks_empty_description()}</p>
- </div>
-{/if}
+<Card.Root>
+ <Card.Content class="py-5">
+ <ArcaneTable
+ persistKey="arcane-networks-table"
+ items={networks}
+ bind:requestOptions
+ bind:selectedIds
+ onRemoveSelected={(ids) => handleDeleteSelectedNetworks(ids)}
+ onRefresh={async (options) => (networks = await networkService.getNetworks(options))}
+ {columns}
+ rowActions={RowActions}
+ />
+ </Card.Content>
+</Card.Root>
diff --git a/frontend/src/routes/volumes/volume-table.svelte b/frontend/src/routes/volumes/volume-table.svelte
index 82c69fd09..a24f56ada 100644
--- a/frontend/src/routes/volumes/volume-table.svelte
+++ b/frontend/src/routes/volumes/volume-table.svelte
@@ -103,13 +103,7 @@
accessorKey: 'inUse',
title: m.common_status(),
sortable: true,
- cell: StatusCell,
- filterFn: (row, columnId, filterValue) => {
- const selected = Array.isArray(filterValue) ? (filterValue as boolean[]) : [];
- if (selected.length === 0) return true;
- const value = Boolean(row.getValue<boolean>(columnId));
- return selected.includes(value);
- }
+ cell: StatusCell
},
{ accessorKey: 'driver', title: m.common_driver(), sortable: true },
{ accessorKey: 'createdAt', title: m.common_created(), sortable: true, cell: CreatedCell }
diff --git a/tests/setup/compose-postgres.yaml b/tests/setup/compose-postgres.yaml
index 916f9436c..05eee643e 100644
--- a/tests/setup/compose-postgres.yaml
+++ b/tests/setup/compose-postgres.yaml
@@ -1,6 +1,6 @@
services:
postgres:
- image: postgres:17-alpine
+ image: postgres:18-alpine
environment:
POSTGRES_DB: arcane_test
POSTGRES_USER: arcane
diff --git a/tests/spec/network.spec.ts b/tests/spec/network.spec.ts
index 690abe93a..23088e4d8 100644
--- a/tests/spec/network.spec.ts
+++ b/tests/spec/network.spec.ts
@@ -79,7 +79,7 @@ test.describe('Networks Page', () => {
await expect(row).toBeVisible();
await row.getByRole('button', { name: 'Open menu' }).click();
- await page.getByRole('menuitem', { name: 'Remove' }).click();
+ await page.getByRole('menuitem', { name: 'Delete' }).click();
await expect(page.getByRole('heading', { name: 'Delete Network' })).toBeVisible();
await page.locator('button:has-text("Delete")').click();