Skip to content

Commit

Permalink
Merge branch 'release/v0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
moooofly committed Jun 19, 2018
2 parents 8835290 + fe9cdc9 commit 919a7c3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# customize

.cookie.yaml
harbor-go-client
*.tar.gz

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.0
0.3.0
36 changes: 19 additions & 17 deletions utils/heapsort.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ type tagItem struct {
timestamp int64
}

type maxheap []*tagItem
type tagminheap []*tagItem

func (h maxheap) Len() int { return len(h) }
func (h tagminheap) Len() int { return len(h) }

func (h maxheap) Less(i, j int) bool {
return h[i].timestamp > h[j].timestamp
func (h tagminheap) Less(i, j int) bool {
return h[i].timestamp < h[j].timestamp
}

func (h maxheap) Swap(i, j int) {
func (h tagminheap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}

func (h *maxheap) Push(x interface{}) {
func (h *tagminheap) Push(x interface{}) {
it := x.(*tagItem)
*h = append(*h, it)
}

func (h *maxheap) Pop() interface{} {
func (h *tagminheap) Pop() interface{} {
old := *h
n := len(old)
it := old[n-1]
Expand All @@ -36,31 +36,33 @@ func (h *maxheap) Pop() interface{} {
}

// Sort on UNIX timestamp of tags
var maxh maxheap
var tagmh tagminheap

// -------------

type repoItem struct {
data *repoTop
score float32
}

type minheap []*repoItem
type repominheap []*repoItem

func (h minheap) Len() int { return len(h) }
func (h repominheap) Len() int { return len(h) }

func (h minheap) Less(i, j int) bool {
func (h repominheap) Less(i, j int) bool {
return h[i].score < h[j].score
}

func (h minheap) Swap(i, j int) {
func (h repominheap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}

func (h *minheap) Push(x interface{}) {
func (h *repominheap) Push(x interface{}) {
it := x.(*repoItem)
*h = append(*h, it)
}

func (h *minheap) Pop() interface{} {
func (h *repominheap) Pop() interface{} {
old := *h
n := len(old)
it := old[n-1]
Expand All @@ -69,12 +71,12 @@ func (h *minheap) Pop() interface{} {
}

// Sort on the score of repos
var minh = minheap{}
var mhBk = minheap{}
var minh = repominheap{}
var mhBk = repominheap{}

func example1() {

h := minheap{
h := repominheap{
&repoItem{
data: &repoTop{
ID: 31,
Expand Down
59 changes: 40 additions & 19 deletions utils/retention_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ func (x *reposRetentionPolicy) Execute(args []string) error {
}

type tagsRetentionPolicy struct {
Day int `short:"d" long:"day" description:"(REQUIRED) The tags of a repository created less than N days should not be deleted." required:"yes"`
Max int `long:"max" description:"(REQUIRED) The maximum quantity of tags created more than N days of a repository should keep untouched." required:"yes"`
Day int `short:"d" long:"day" description:"(REQUIRED) The tags of a repository created less than N days should not be deleted." required:"yes"`
Max int `short:"m" long:"max" description:"(REQUIRED) The maximum quantity of tags created more than N days of a repository should keep untouched." required:"yes"`
RepoName string `short:"n" long:"repo_name" description:"Repo name for specific target. If not set, rp_tags will do jobs on all repos." default:""`
}

var tagsRP tagsRetentionPolicy
Expand All @@ -117,6 +118,10 @@ func (x *tagsRetentionPolicy) Execute(args []string) error {
}

func tagAnalyseAndErase() error {
fmt.Println("=========================")
fmt.Println("== 开始 tags RP 分析 ==")
fmt.Println("=========================")
fmt.Println()

c, err := CookieLoad()
if err != nil {
Expand All @@ -126,7 +131,9 @@ func tagAnalyseAndErase() error {

// 基于 search 接口获取全部 projects 和 repositories 信息
// 设置 "q=" 可以获取全部信息
searchURL := URLGen("/api/search") + "?q="
// 设置 "q=xxx" 可以过滤指定信息,但是目前发现该功能有 bug ,故暂时无法基于该接口针对指定 repo 进行处理
searchURL := URLGen("/api/search") + "?q=" + tagsRP.RepoName
fmt.Println("--------------------")
fmt.Println("==> GET", searchURL)

_, _, errs := Request.Get(searchURL).
Expand All @@ -139,12 +146,21 @@ func tagAnalyseAndErase() error {
}
}

fmt.Printf("\n=== 开始 tags RP 分析 ===\n\n")
fmt.Printf("==> maximum untouched: %d\n", tagsRP.Max)
if tagsRP.RepoName == "" {
fmt.Printf("==> on all Repos, max-days-untouched: %d max-keep-num-after-Ndays: %d\n",
tagsRP.Day, tagsRP.Max)
} else {
fmt.Printf("==> only on Repo [%s], max-days-untouched: %d max-keep-num-after-Ndays: %d\n",
tagsRP.RepoName, tagsRP.Day, tagsRP.Max)
}
fmt.Println("--------------------")

// 遍历全部 repositories 信息
for _, r := range scRsp.Repository {
fmt.Printf("\n==> repo_name: %s tags_count: %d\n", r.RepositoryName, r.TagsCount)
fmt.Println("\n")
fmt.Println("------------------------------------------------------")
fmt.Printf("| repo_name: %s | tags_count: %d |\n", r.RepositoryName, r.TagsCount)
fmt.Println("------------------------------------------------------")

// 获取每个 repo 下的 tags 信息
tagsListURL := URLGen("/api/repositories") + "/" + r.RepositoryName + "/tags"
Expand All @@ -161,8 +177,8 @@ func tagAnalyseAndErase() error {
}

// 用于针对每个 repo 下的 tags 进行排序
maxh = maxheap{}
heap.Init(&maxh)
tagmh = tagminheap{}
heap.Init(&tagmh)
for _, t := range tlRsp {
//fmt.Printf("==> name: %s created: %s\n", t.Name, t.Created)

Expand All @@ -175,26 +191,31 @@ func tagAnalyseAndErase() error {
tagName: t.Name,
timestamp: tagC.Unix(),
}
// 超过 N 天的 tag 保存到 maxheap
//fmt.Printf("[PUSH] %s <==> create(%s) dayPast(%f)\n", it.tagName, t.Created, dayPast)
heap.Push(&maxh, it)
// 超过 N 天的 tag 保存到 minheap
fmt.Printf("[PUSH] %s <==> create: %s dayPast: %f\n", it.tagName, t.Created, dayPast)
heap.Push(&tagmh, it)
} else {
fmt.Printf("[noPUSH] %s <==> create: %s dayPast: %f\n", t.Name, t.Created, dayPast)
}

}
// b. 针对创建于 N 天之外的 tag ,每个 repo 最多保留 Max 个
gtNdays := maxh.Len()
fmt.Printf("==> less then (%d days): %d more than (%d days): %d\n",
gtNdays := tagmh.Len()
fmt.Println("---")
fmt.Printf("--> # of tags less than %d days: %d , # of tags more than %d days: %d\n",
tagsRP.Day, r.TagsCount-gtNdays, tagsRP.Day, gtNdays)
if gtNdays <= tagsRP.Max {
fmt.Printf("===> untouched quantity (%d) more than %d, so DO NOTHING.\n", tagsRP.Max, gtNdays)
fmt.Printf("--> max-keep-num-after-Ndays (%d) more than actual num (%d), so DO NOTHING.\n", tagsRP.Max, gtNdays)
} else {
fmt.Printf("===> untouched quantity (%d) less than %d, so START DELETing.\n", tagsRP.Max, gtNdays)
fmt.Printf("--> max-keep-num-after-Ndays (%d) less than actual num (%d), so START DELETING.\n", tagsRP.Max, gtNdays)
fmt.Println("---")
for gtNdays > tagsRP.Max {
it := heap.Pop(&maxh).(*tagItem)
//fmt.Printf("[POP] %s <==> %d\n", it.tagName, it.timestamp)
it := heap.Pop(&tagmh).(*tagItem)
fmt.Printf("[POP] %s <==> %d\n", it.tagName, it.timestamp)

// 如果 len(maxheap) > max 则删除 len(maxheap) - max 个 tag
// 如果 len(minheap) > max 则删除 len(minheap) - max 个 tag
targetURL := URLGen("/api/repositories") + "/" + r.RepositoryName + "/tags/" + it.tagName
fmt.Println("\n==> DELETE", targetURL)
fmt.Println("==> DELETE", targetURL)

Request.Delete(targetURL).
Set("Cookie", "harbor-lang=zh-cn; beegosessionID="+c.BeegosessionID).
Expand Down

0 comments on commit 919a7c3

Please sign in to comment.