Skip to content

Commit

Permalink
sync mirror component
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiling-J committed Dec 18, 2024
1 parent 001c56c commit 67007de
Show file tree
Hide file tree
Showing 7 changed files with 534 additions and 46 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion api/handler/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ func (h *MirrorHandler) Index(ctx *gin.Context) {
httpbase.UnauthorizedError(ctx, component.ErrUserNotFound)
return
}
repos, total, err := h.mc.Index(ctx, currentUser, per, page)
search := ctx.Query("search")
repos, total, err := h.mc.Index(ctx, currentUser, per, page, search)
if err != nil {
slog.Error("failed to get mirror repos", slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down
39 changes: 35 additions & 4 deletions builder/store/database/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database
import (
"context"
"fmt"
"strings"
"time"

"github.com/uptrace/bun"
Expand Down Expand Up @@ -31,7 +32,8 @@ type MirrorStore interface {
Finished(ctx context.Context) ([]Mirror, error)
ToSyncRepo(ctx context.Context) ([]Mirror, error)
ToSyncLfs(ctx context.Context) ([]Mirror, error)
IndexWithPagination(ctx context.Context, per, page int) (mirrors []Mirror, count int, err error)
IndexWithPagination(ctx context.Context, per, page int, search string) (mirrors []Mirror, count int, err error)
StatusCount(ctx context.Context) ([]MirrorStatusCount, error)
UpdateMirrorAndRepository(ctx context.Context, mirror *Mirror, repo *Repository) error
}

Expand Down Expand Up @@ -76,6 +78,11 @@ type Mirror struct {
times
}

type MirrorStatusCount struct {
Status types.MirrorTaskStatus `bun:"status"`
Count int `bun:"count"`
}

func (s *mirrorStoreImpl) IsExist(ctx context.Context, repoID int64) (exists bool, err error) {
var mirror Mirror
exists, err = s.db.Operator.Core.
Expand All @@ -90,7 +97,8 @@ func (s *mirrorStoreImpl) IsRepoExist(ctx context.Context, repoType types.Reposi
exists, err = s.db.Operator.Core.
NewSelect().
Model(&repo).
Where("git_path=?", fmt.Sprintf("%ss_%s/%s", repoType, namespace, name)).
Where("path=?", fmt.Sprintf("%s/%s", namespace, name)).
Where("repository_type=?", repoType).
Exists(ctx)
return
}
Expand Down Expand Up @@ -273,7 +281,13 @@ func (s *mirrorStoreImpl) ToSyncRepo(ctx context.Context) ([]Mirror, error) {
var mirrors []Mirror
err := s.db.Operator.Core.NewSelect().
Model(&mirrors).
Where("next_execution_timestamp < ? or status in (?,?,?)", time.Now(), types.MirrorIncomplete, types.MirrorFailed, types.MirrorWaiting).
Where(
"next_execution_timestamp < ? or status in (?,?,?,?)",
time.Now(),
types.MirrorIncomplete,
types.MirrorFailed,
types.MirrorWaiting,
types.MirrorRunning).
Scan(ctx)
if err != nil {
return nil, err
Expand All @@ -293,11 +307,17 @@ func (s *mirrorStoreImpl) ToSyncLfs(ctx context.Context) ([]Mirror, error) {
return mirrors, nil
}

func (s *mirrorStoreImpl) IndexWithPagination(ctx context.Context, per, page int) (mirrors []Mirror, count int, err error) {
func (s *mirrorStoreImpl) IndexWithPagination(ctx context.Context, per, page int, search string) (mirrors []Mirror, count int, err error) {
q := s.db.Operator.Core.NewSelect().
Model(&mirrors).
Relation("Repository").
Relation("MirrorSource")
if search != "" {
q = q.Where("LOWER(mirror.source_url) like ? or LOWER(mirror.local_repo_path) like ?",
fmt.Sprintf("%%%s%%", strings.ToLower(search)),
fmt.Sprintf("%%%s%%", strings.ToLower(search)),
)
}
count, err = q.Count(ctx)
if err != nil {
return
Expand All @@ -313,6 +333,17 @@ func (s *mirrorStoreImpl) IndexWithPagination(ctx context.Context, per, page int
return
}

func (s *mirrorStoreImpl) StatusCount(ctx context.Context) ([]MirrorStatusCount, error) {
var statusCounts []MirrorStatusCount
err := s.db.Operator.Core.NewSelect().
Model((*Mirror)(nil)).
Column("status").
ColumnExpr("COUNT(*) AS count").
Group("status").
Scan(ctx, &statusCounts)
return statusCounts, err
}

func (s *mirrorStoreImpl) UpdateMirrorAndRepository(ctx context.Context, mirror *Mirror, repo *Repository) error {
err := s.db.Operator.Core.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
_, err := tx.NewUpdate().Model(mirror).WherePK().Exec(ctx)
Expand Down
13 changes: 11 additions & 2 deletions builder/store/database/mirror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestMirrorStore_CRUD(t *testing.T) {
RepositoryType: types.ModelRepo,
GitPath: "models_ns/n",
Name: "repo",
Path: "ns/n",
}
err = db.Core.NewInsert().Model(repo).Scan(ctx, repo)
require.Nil(t, err)
Expand Down Expand Up @@ -194,7 +195,7 @@ func TestMirrorStore_ToSync(t *testing.T) {
for _, m := range ms {
names = append(names, m.Interval)
}
require.ElementsMatch(t, []string{"m1", "m3", "m6", "m7"}, names)
require.ElementsMatch(t, []string{"m1", "m3", "m5", "m6", "m7"}, names)

ms, err = store.ToSyncLfs(ctx)
require.Nil(t, err)
Expand Down Expand Up @@ -222,7 +223,7 @@ func TestMirrorStore_IndexWithPagination(t *testing.T) {
require.Nil(t, err)
}

ms, count, err := store.IndexWithPagination(ctx, 10, 1)
ms, count, err := store.IndexWithPagination(ctx, 10, 1, "foo")
require.Nil(t, err)
names := []string{}
for _, m := range ms {
Expand Down Expand Up @@ -250,4 +251,12 @@ func TestMirrorStore_StatusCount(t *testing.T) {
require.Nil(t, err)
}

cs, err := store.StatusCount(ctx)
require.Nil(t, err)
require.Equal(t, 2, len(cs))
require.ElementsMatch(t, []database.MirrorStatusCount{
{types.MirrorFailed, 2},
{types.MirrorFinished, 1},
}, cs)

}
5 changes: 5 additions & 0 deletions common/types/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,8 @@ type Mirror struct {
type MirrorSource struct {
SourceName string `json:"source_name"`
}

type MirrorStatusCount struct {
Status MirrorTaskStatus
Count int
}
Loading

0 comments on commit 67007de

Please sign in to comment.