fix: [AH-578]: Fixed Sort By DownloadCount | Gitness (#3217)

* fix: [AH-578]: Fixed Sort By DownloadCount
* fix: [AH-578]: Fixed Sort By DownloadCount
* fix: [AH-578]: Fixed Sort By DownloadCount
BT-10437
Ritek Rounak 2025-01-03 21:12:48 +00:00 committed by Harness
parent 03a064bdfb
commit dec251e385
2 changed files with 18 additions and 7 deletions

View File

@ -72,7 +72,7 @@ var artifactSortMap = map[string]string{
"repoKey": "name", "repoKey": "name",
"lastModified": "updated_at", "lastModified": "updated_at",
"name": "image_name", "name": "image_name",
"downloadsCount": "image_name", "downloadsCount": "download_count",
"createdAt": "created_at", "createdAt": "created_at",
} }
@ -88,7 +88,7 @@ var artifactVersionSortMap = map[string]string{
"name": "name", "name": "name",
"size": "name", "size": "name",
"pullCommand": "name", "pullCommand": "name",
"downloadsCount": "name", "downloadsCount": "download_count",
"lastModified": "updated_at", "lastModified": "updated_at",
"createdAt": "created_at", "createdAt": "created_at",
} }

View File

@ -44,6 +44,7 @@ const (
greaterThan string = ">" greaterThan string = ">"
labelSeparatorStart string = "%^_" labelSeparatorStart string = "%^_"
labelSeparatorEnd string = "^_%" labelSeparatorEnd string = "^_%"
downloadCount string = "download_count"
) )
type tagDao struct { type tagDao struct {
@ -399,8 +400,11 @@ func (t tagDao) GetAllArtifactsByParentID(
if search != "" { if search != "" {
q = q.Where("tag_image_name LIKE ?", sqlPartialMatch(search)) q = q.Where("tag_image_name LIKE ?", sqlPartialMatch(search))
} }
sortField := "tag_" + sortByField
q = q.OrderBy("tag_" + sortByField + " " + sortByOrder).Limit(uint64(limit)).Offset(uint64(offset)) if sortByField == downloadCount {
sortField = downloadCount
}
q = q.OrderBy(sortField + " " + sortByOrder).Limit(uint64(limit)).Offset(uint64(offset))
sql, args, err := q.ToSql() sql, args, err := q.ToSql()
if err != nil { if err != nil {
@ -754,7 +758,11 @@ func (t tagDao) GetAllArtifactsByRepo(
q = q.Where("'^_' || ar.image_labels || '^_' LIKE ?", labelsVal) q = q.Where("'^_' || ar.image_labels || '^_' LIKE ?", labelsVal)
} }
q = q.OrderBy("t.tag_" + sortByField + " " + sortByOrder).Limit(uint64(limit)).Offset(uint64(offset)) sortField := "t.tag_" + sortByField
if sortByField == downloadCount {
sortField = downloadCount
}
q = q.OrderBy(sortField + " " + sortByOrder).Limit(uint64(limit)).Offset(uint64(offset))
sql, args, err := q.ToSql() sql, args, err := q.ToSql()
if err != nil { if err != nil {
@ -860,8 +868,11 @@ func (t tagDao) GetAllTagsByRepoAndImage(
if search != "" { if search != "" {
q = q.Where("tag_name LIKE ?", sqlPartialMatch(search)) q = q.Where("tag_name LIKE ?", sqlPartialMatch(search))
} }
sortField := "tag_" + sortByField
q = q.OrderBy("tag_" + sortByField + " " + sortByOrder).Limit(uint64(limit)).Offset(uint64(offset)) if sortByField == downloadCount {
sortField = downloadCount
}
q = q.OrderBy(sortField + " " + sortByOrder).Limit(uint64(limit)).Offset(uint64(offset))
sql, args, err := q.ToSql() sql, args, err := q.ToSql()
if err != nil { if err != nil {