Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: sort images with namespace and last_updated #130

Merged
merged 1 commit into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions registry/v2/extensions/catalog_detail.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package extensions

import (
"fmt"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -35,6 +36,7 @@ func (ext *extension) CatalogDetail(ctx echo.Context) error {
queryParamPageSize := ctx.QueryParam("n")
queryParamOffset := ctx.QueryParam("last")
namespace := ctx.QueryParam("ns")
sortBy := ctx.QueryParam("sort_by")
var pageSize int64
var offset int64
if queryParamPageSize != "" {
Expand Down Expand Up @@ -62,20 +64,32 @@ func (ext *extension) CatalogDetail(ctx echo.Context) error {
total, err := ext.store.GetCatalogCount(ctx.Request().Context())
if err != nil {
ext.logger.Log(ctx, err)
return ctx.JSON(http.StatusInternalServerError, echo.Map{
return ctx.JSON(http.StatusInternalServerError, echo.Map{"error": err.Error()})
}

switch sortBy {
case "last_updated":
sortBy = "updated_at desc"
case "namespace":
sortBy = "namespace asc"
case "":
sortBy = "namespace asc"
default:
err = fmt.Errorf("invalid choice of sort_by element")
ext.logger.Log(ctx, err)
return ctx.JSON(http.StatusBadRequest, echo.Map{
"error": err.Error(),
})
}

catalogWithDetail, err := ext.store.GetCatalogDetail(ctx.Request().Context(), namespace, pageSize, offset)
catalogWithDetail, err := ext.store.GetCatalogDetail(ctx.Request().Context(), namespace, pageSize, offset, sortBy)
if err != nil {
ext.logger.Log(ctx, err)
return ctx.JSON(http.StatusInternalServerError, echo.Map{
"error": err.Error(),
})
}

ext.logger.Log(ctx, nil)
return ctx.JSON(http.StatusOK, echo.Map{
"repositories": catalogWithDetail,
"total": total,
Expand Down
30 changes: 17 additions & 13 deletions store/postgres/container_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,27 +296,31 @@ func (p *pg) GetCatalog(ctx context.Context, ns string, pageSize, offset int64)
}

// GetCatalogDetail - ns -> Namespace; ps -> PageSize
func (p *pg) GetCatalogDetail(ctx context.Context, ns string, ps, offset int64) ([]*types.ImageManifestV2, error) {
func (p *pg) GetCatalogDetail(
ctx context.Context, ns string, ps, offset int64, sortBy string,
) ([]*types.ImageManifestV2, error) {
childCtx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

var rows pgx.Rows
var err error
pageSize := int64(10)
if ps > 0 {
pageSize = ps
}

if ps != 0 {
rows, err = p.conn.Query(childCtx, queries.GetCatalogDetailWithPagination, ps, offset)
if err != nil {
err = fmt.Errorf("ERR_CATALOG_WITH_PAGINATION: %w", err)
}
} else {
rows, err = p.conn.Query(childCtx, queries.GetCatalogDetailWithPagination, 10, 0)
if err != nil {
err = fmt.Errorf("ERR_CATALOG: %w", err)
}
q := fmt.Sprintf(queries.GetCatalogDetailWithPagination, sortBy)
if ns != "" {
q = fmt.Sprintf(queries.GetUserCatalogDetailWithPagination, sortBy)
}

rows, err = p.conn.Query(childCtx, q, pageSize, offset)
if err != nil {
err = fmt.Errorf("ERR_CATALOG_WITH_PAGINATION: %w", err)
}

if ns != "" {
rows, err = p.conn.Query(childCtx, queries.GetUserCatalogDetailWithPagination, ns+"/%", ps, offset)
rows, err = p.conn.Query(childCtx, q, ns+"/%", sortBy, ps, offset)
if err != nil {
err = fmt.Errorf("ERR_USER_CATALOG: %w", err)
}
Expand All @@ -327,8 +331,8 @@ func (p *pg) GetCatalogDetail(ctx context.Context, ns string, ps, offset int64)
}

defer rows.Close()
var catalog []*types.ImageManifestV2

var catalog []*types.ImageManifestV2
for i := 0; rows.Next(); i++ {
var mf types.ImageManifestV2

Expand Down
4 changes: 3 additions & 1 deletion store/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ type RegistryStore interface {
GetConfig(ctx context.Context, namespace string) ([]*types.ConfigV2, error)
GetImageTags(ctx context.Context, namespace string) ([]string, error)
GetCatalog(ctx context.Context, namespace string, pageSize int64, offset int64) ([]string, error)
GetCatalogDetail(ctx context.Context, namespace string, pageSize int64, offset int64) ([]*types.ImageManifestV2, error)
GetCatalogDetail(
ctx context.Context, namespace string, pageSize int64, offset int64, sortBy string,
) ([]*types.ImageManifestV2, error)
GetRepoDetail(ctx context.Context, namespace string, pageSize int64, offset int64) (*types.Repository, error)
GetCatalogCount(ctx context.Context) (int64, error)
GetImageNamespace(ctx context.Context, search string) ([]*types.ImageManifestV2, error)
Expand Down
15 changes: 9 additions & 6 deletions store/postgres/queries/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ var (
GetCatalog = `select namespace from image_manifest;`
GetCatalogWithPagination = `select namespace from image_manifest limit $1 offset $2;`
GetUserCatalogWithPagination = `select namespace from image_manifest where namespace like $1 limit $2 offset $3;`
GetImageNamespace = `select uuid,namespace,created_at::timestamptz,updated_at::timestamptz from image_manifest where substr(namespace, 1, 50) like $1;`
GetImageNamespace = `select uuid,namespace,created_at::timestamptz,updated_at::timestamptz from
image_manifest where substr(namespace, 1, 50) like $1;`

GetCatalogDetailWithPagination = `select namespace,created_at::timestamptz,updated_at::timestamptz from image_manifest limit $1 offset $2;`
GetUserCatalogDetailWithPagination = `select namespace,created_at::timestamptz,updated_at::timestamptz from image_manifest where namespace like $1 limit $2 offset $3;`

// select floor(sum(size::float4/1000000)) from layer where digest = ANY(ARRAY(select layers from config where namespace='johndoe/traefik'));
GetRepoDetailWithPagination = `select reference, digest, sky_link, (select sum(size) from layer where digest = ANY(layers)) as size, created_at::timestamptz, updated_at::timestamptz from config where namespace=$1 limit $2 offset $3;`
// be very careful using this one
GetCatalogDetailWithPagination = `select namespace,created_at::timestamptz,updated_at::timestamptz from image_manifest order by %s limit $1 offset $2;`
GetUserCatalogDetailWithPagination = `select namespace,created_at::timestamptz,updated_at::timestamptz from
image_manifest where namespace like $1 order by %s limit $3 offset $4;`
GetRepoDetailWithPagination = `select reference, digest, sky_link, (select sum(size) from layer where digest =
ANY(layers)) as size, created_at::timestamptz, updated_at::timestamptz from config where namespace=$1
limit $2 offset $3;`
)

// delete queries
Expand Down