Skip to content

Commit

Permalink
Enrich version model with metadata (#94)
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Shishkin <me@teran.dev>
  • Loading branch information
teran authored Aug 4, 2024
1 parent 8035778 commit ff78af3
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 74 deletions.
18 changes: 9 additions & 9 deletions gc/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (s *service) Run(ctx context.Context) error {
for _, version := range versions {
log.WithFields(log.Fields{
"container": container,
"version": version,
"version": version.Name,
}).Debugf("listing objects ...")

var (
Expand All @@ -69,9 +69,9 @@ func (s *service) Run(ctx context.Context) error {

var objects []string = []string{}

total, objects, err = s.cfg.MdRepo.ListObjects(ctx, container, version, offset, defaultLimit)
total, objects, err = s.cfg.MdRepo.ListObjects(ctx, container, version.Name, offset, defaultLimit)
if err != nil {
return errors.Wrapf(err, "error listing objects for container `%s`; version `%s`", container, version)
return errors.Wrapf(err, "error listing objects for container `%s`; version `%s`", container, version.Name)
}

if total == 0 {
Expand All @@ -81,29 +81,29 @@ func (s *service) Run(ctx context.Context) error {
if !s.cfg.DryRun {
log.WithFields(log.Fields{
"container": container,
"version": version,
"version": version.Name,
"amount": len(objects),
}).Info("Performing actual metadata deletion: objects")

err = s.cfg.MdRepo.DeleteObject(ctx, container, version, objects...)
err = s.cfg.MdRepo.DeleteObject(ctx, container, version.Name, objects...)
if err != nil {
return errors.Wrapf(err, "error removing object from `%s/%s (%d objects)`", container, version, len(objects))
return errors.Wrapf(err, "error removing object from `%s/%s (%d objects)`", container, version.Name, len(objects))
}
}
}

log.WithFields(log.Fields{
"container": container,
"version": version,
"version": version.Name,
}).Debug("deleting version ...")

if !s.cfg.DryRun {
log.WithFields(log.Fields{
"container": container,
"version": version,
"version": version.Name,
}).Info("Performing actual metadata deletion: version")

err = s.cfg.MdRepo.DeleteVersion(ctx, container, version)
err = s.cfg.MdRepo.DeleteVersion(ctx, container, version.Name)
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion gc/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"

"github.com/teran/archived/models"
repoMock "github.com/teran/archived/repositories/metadata/mock"
)

Expand All @@ -18,7 +19,9 @@ func init() {
func (s *serviceTestSuite) TestAll() {
call1 := s.repoMock.On("ListContainers").Return([]string{"container1"}, nil).Once()

call2 := s.repoMock.On("ListUnpublishedVersionsByContainer", "container1").Return([]string{"version1"}, nil).Once().NotBefore(call1)
call2 := s.repoMock.On("ListUnpublishedVersionsByContainer", "container1").Return([]models.Version{
{Name: "version1"},
}, nil).Once().NotBefore(call1)
call3 := s.repoMock.On("ListObjects", "container1", "version1", uint64(0), uint64(1000)).Return(uint64(3), []string{"obj1", "obj2", "obj3"}, nil).Once().NotBefore(call2)
call4 := s.repoMock.On("DeleteObject", "container1", "version1", []string{"obj1", "obj2", "obj3"}).Return(nil).Once().NotBefore(call3)
call5 := s.repoMock.On("ListObjects", "container1", "version1", uint64(0), uint64(1000)).Return(uint64(0), []string{}, nil).Once().NotBefore(call4)
Expand Down
9 changes: 9 additions & 0 deletions models/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package models

import "time"

type Version struct {
Name string
IsPublished bool
CreatedAt time.Time
}
7 changes: 6 additions & 1 deletion presenter/manager/grpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ func (h *handlers) ListVersions(ctx context.Context, in *v1.ListVersionsRequest)
return nil, err
}

versionNames := []string{}
for _, v := range versions {
versionNames = append(versionNames, v.Name)
}

return &v1.ListVersionsResponse{
Versions: versions,
Versions: versionNames,
}, nil
}

Expand Down
3 changes: 2 additions & 1 deletion presenter/publisher/html/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
echo "github.com/labstack/echo/v4"
log "github.com/sirupsen/logrus"

"github.com/teran/archived/models"
"github.com/teran/archived/service"
)

Expand Down Expand Up @@ -81,7 +82,7 @@ func (h *handlers) VersionIndex(c echo.Context) error {
CurrentPage uint64
PagesCount uint64
Container string
Versions []string
Versions []models.Version
}

return c.Render(http.StatusOK, "version-list.html", &data{
Expand Down
7 changes: 5 additions & 2 deletions presenter/publisher/html/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
echo "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/stretchr/testify/suite"
"github.com/teran/archived/models"
"github.com/teran/archived/service"
)

Expand All @@ -20,7 +21,9 @@ func (s *handlersTestSuite) TestContainerIndex() {
}

func (s *handlersTestSuite) TestVersionIndex() {
s.serviceMock.On("ListPublishedVersionsByPage", "test-container-1", uint64(1)).Return(uint64(100), []string{"20241011121314"}, nil).Once()
s.serviceMock.On("ListPublishedVersionsByPage", "test-container-1", uint64(1)).Return(uint64(100), []models.Version{
{Name: "20241011121314"},
}, nil).Once()

s.compareHTMLResponse(s.srv.URL+"/test-container-1/", "testdata/versions.html.sample")
}
Expand Down Expand Up @@ -51,7 +54,7 @@ func (s *handlersTestSuite) TestGetObject() {
}

func (s *handlersTestSuite) TestErrNotFound() {
s.serviceMock.On("ListPublishedVersionsByPage", "test-container-1", uint64(1)).Return(uint64(100), []string(nil), service.ErrNotFound).Once()
s.serviceMock.On("ListPublishedVersionsByPage", "test-container-1", uint64(1)).Return(uint64(100), []models.Version(nil), service.ErrNotFound).Once()
s.compareHTMLResponse(s.srv.URL+"/test-container-1/", "testdata/404.html.sample")

s.serviceMock.On("ListObjectsByPage", "test-container-1", "20240101010101", uint64(1)).Return(uint64(100), []string(nil), service.ErrNotFound).Once()
Expand Down
2 changes: 1 addition & 1 deletion presenter/publisher/html/templates/version-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{{ template "header" . }}
<a href="..">..</a><br>
{{- range $version := .Versions }}
<a href="/{{ $container }}/{{ $version }}/">{{ $version }}/</a><br>
<a href="/{{ $container }}/{{ $version.Name }}/">{{ $version.Name }}/</a><br>
{{- else }}
<p>No versions found</p>
{{- end }}
Expand Down
13 changes: 7 additions & 6 deletions repositories/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (

"github.com/pkg/errors"

"github.com/teran/archived/exporter/models"
emodels "github.com/teran/archived/exporter/models"
"github.com/teran/archived/models"
)

var ErrNotFound = errors.New("not found")
Expand All @@ -17,10 +18,10 @@ type Repository interface {

CreateVersion(ctx context.Context, container string) (string, error)
GetLatestPublishedVersionByContainer(ctx context.Context, container string) (string, error)
ListAllVersionsByContainer(ctx context.Context, container string) ([]string, error)
ListPublishedVersionsByContainer(ctx context.Context, container string) ([]string, error)
ListPublishedVersionsByContainerAndPage(ctx context.Context, container string, offset, limit uint64) (uint64, []string, error)
ListUnpublishedVersionsByContainer(ctx context.Context, container string) ([]string, error)
ListAllVersionsByContainer(ctx context.Context, container string) ([]models.Version, error)
ListPublishedVersionsByContainer(ctx context.Context, container string) ([]models.Version, error)
ListPublishedVersionsByContainerAndPage(ctx context.Context, container string, offset, limit uint64) (uint64, []models.Version, error)
ListUnpublishedVersionsByContainer(ctx context.Context, container string) ([]models.Version, error)
MarkVersionPublished(ctx context.Context, container, version string) error
DeleteVersion(ctx context.Context, container, version string) error

Expand All @@ -33,5 +34,5 @@ type Repository interface {
GetBlobKeyByObject(ctx context.Context, container, version, key string) (string, error)
EnsureBlobKey(ctx context.Context, key string, size uint64) error

CountStats(ctx context.Context) (*models.Stats, error)
CountStats(ctx context.Context) (*emodels.Stats, error)
}
23 changes: 12 additions & 11 deletions repositories/metadata/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"context"

"github.com/stretchr/testify/mock"
"github.com/teran/archived/exporter/models"
emodels "github.com/teran/archived/exporter/models"
"github.com/teran/archived/models"
"github.com/teran/archived/repositories/metadata"
)

Expand Down Expand Up @@ -43,24 +44,24 @@ func (m *Mock) GetLatestPublishedVersionByContainer(_ context.Context, container
return args.String(0), args.Error(1)
}

func (m *Mock) ListAllVersionsByContainer(_ context.Context, container string) ([]string, error) {
func (m *Mock) ListAllVersionsByContainer(_ context.Context, container string) ([]models.Version, error) {
args := m.Called(container)
return args.Get(0).([]string), args.Error(1)
return args.Get(0).([]models.Version), args.Error(1)
}

func (m *Mock) ListPublishedVersionsByContainer(_ context.Context, container string) ([]string, error) {
func (m *Mock) ListPublishedVersionsByContainer(_ context.Context, container string) ([]models.Version, error) {
args := m.Called(container)
return args.Get(0).([]string), args.Error(1)
return args.Get(0).([]models.Version), args.Error(1)
}

func (m *Mock) ListPublishedVersionsByContainerAndPage(_ context.Context, container string, offset, limit uint64) (uint64, []string, error) {
func (m *Mock) ListPublishedVersionsByContainerAndPage(_ context.Context, container string, offset, limit uint64) (uint64, []models.Version, error) {
args := m.Called(container, offset, limit)
return args.Get(0).(uint64), args.Get(1).([]string), args.Error(2)
return args.Get(0).(uint64), args.Get(1).([]models.Version), args.Error(2)
}

func (m *Mock) ListUnpublishedVersionsByContainer(_ context.Context, container string) ([]string, error) {
func (m *Mock) ListUnpublishedVersionsByContainer(_ context.Context, container string) ([]models.Version, error) {
args := m.Called(container)
return args.Get(0).([]string), args.Error(1)
return args.Get(0).([]models.Version), args.Error(1)
}

func (m *Mock) MarkVersionPublished(_ context.Context, container, version string) error {
Expand Down Expand Up @@ -108,7 +109,7 @@ func (m *Mock) EnsureBlobKey(_ context.Context, key string, size uint64) error {
return args.Error(0)
}

func (m *Mock) CountStats(ctx context.Context) (*models.Stats, error) {
func (m *Mock) CountStats(ctx context.Context) (*emodels.Stats, error) {
args := m.Called()
return args.Get(0).(*models.Stats), args.Error(1)
return args.Get(0).(*emodels.Stats), args.Error(1)
}
30 changes: 21 additions & 9 deletions repositories/metadata/postgresql/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package postgresql

import (
"context"
"time"

sq "github.com/Masterminds/squirrel"
"github.com/pkg/errors"
ptr "github.com/teran/go-ptr"

"github.com/teran/archived/models"
)

const defaultLimit uint64 = 1000
Expand Down Expand Up @@ -81,26 +84,26 @@ func (r *repository) GetLatestPublishedVersionByContainer(ctx context.Context, c
return versionName, nil
}

func (r *repository) ListPublishedVersionsByContainer(ctx context.Context, container string) ([]string, error) {
func (r *repository) ListPublishedVersionsByContainer(ctx context.Context, container string) ([]models.Version, error) {
_, versions, err := r.listVersionsByContainer(ctx, container, ptr.Bool(true), 0, 0)
return versions, err
}

func (r *repository) ListAllVersionsByContainer(ctx context.Context, container string) ([]string, error) {
func (r *repository) ListAllVersionsByContainer(ctx context.Context, container string) ([]models.Version, error) {
_, versions, err := r.listVersionsByContainer(ctx, container, nil, 0, 0)
return versions, err
}

func (r *repository) ListUnpublishedVersionsByContainer(ctx context.Context, container string) ([]string, error) {
func (r *repository) ListUnpublishedVersionsByContainer(ctx context.Context, container string) ([]models.Version, error) {
_, versions, err := r.listVersionsByContainer(ctx, container, ptr.Bool(false), 0, 0)
return versions, err
}

func (r *repository) ListPublishedVersionsByContainerAndPage(ctx context.Context, container string, offset, limit uint64) (uint64, []string, error) {
func (r *repository) ListPublishedVersionsByContainerAndPage(ctx context.Context, container string, offset, limit uint64) (uint64, []models.Version, error) {
return r.listVersionsByContainer(ctx, container, ptr.Bool(true), offset, limit)
}

func (r *repository) listVersionsByContainer(ctx context.Context, container string, isPublished *bool, offset, limit uint64) (uint64, []string, error) {
func (r *repository) listVersionsByContainer(ctx context.Context, container string, isPublished *bool, offset, limit uint64) (uint64, []models.Version, error) {
if limit == 0 {
limit = defaultLimit
}
Expand Down Expand Up @@ -140,7 +143,7 @@ func (r *repository) listVersionsByContainer(ctx context.Context, container stri
}

rows, err := selectQuery(ctx, r.db, psql.
Select("name").
Select("name", "is_published", "created_at").
From("versions").
Where(condition).
OrderBy("created_at DESC").
Expand All @@ -151,12 +154,21 @@ func (r *repository) listVersionsByContainer(ctx context.Context, container stri
}
defer rows.Close()

result := []string{}
result := []models.Version{}
for rows.Next() {
var r string
if err := rows.Scan(&r); err != nil {
var (
r models.Version
createdAt time.Time
)

if err := rows.Scan(&r.Name, &r.IsPublished, &createdAt); err != nil {
return 0, nil, errors.Wrap(err, "error decoding database result")
}
r.CreatedAt = time.Date(
createdAt.Year(), createdAt.Month(), createdAt.Day(),
createdAt.Hour(), createdAt.Minute(), createdAt.Second(), createdAt.Nanosecond(),
time.UTC,
)

result = append(result, r)
}
Expand Down
Loading

0 comments on commit ff78af3

Please sign in to comment.