mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-10-05 21:14:40 +08:00
fix: Optimize application recommendation logic (#10128)
This commit is contained in:
parent
aad79077bf
commit
ebdc3f05de
2 changed files with 33 additions and 19 deletions
|
@ -33,6 +33,8 @@ type IAppRepo interface {
|
|||
BatchDelete(ctx context.Context, apps []model.App) error
|
||||
DeleteByIDs(ctx context.Context, ids []uint) error
|
||||
DeleteBy(opts ...DBOption) error
|
||||
|
||||
GetTopRecomment() ([]string, error)
|
||||
}
|
||||
|
||||
func NewIAppRepo() IAppRepo {
|
||||
|
@ -123,6 +125,21 @@ func (a AppRepo) GetBy(opts ...DBOption) ([]model.App, error) {
|
|||
return apps, nil
|
||||
}
|
||||
|
||||
func (a AppRepo) GetTopRecomment() ([]string, error) {
|
||||
var (
|
||||
apps []model.App
|
||||
names []string
|
||||
)
|
||||
db := getDb().Model(&model.App{})
|
||||
if err := db.Order("recommend asc").Limit(6).Find(&apps).Error; err != nil {
|
||||
return names, err
|
||||
}
|
||||
for _, item := range apps {
|
||||
names = append(names, item.Key)
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
func (a AppRepo) BatchCreate(ctx context.Context, apps []model.App) error {
|
||||
return getTx(ctx).Omit(clause.Associations).Create(&apps).Error
|
||||
}
|
||||
|
|
|
@ -268,10 +268,7 @@ func (u *DashboardService) LoadCurrentInfo(ioOption string, netOption string) *d
|
|||
}
|
||||
|
||||
func (u *DashboardService) LoadAppLauncher(ctx *gin.Context) ([]dto.AppLauncher, error) {
|
||||
var (
|
||||
data []dto.AppLauncher
|
||||
recommendList []dto.AppLauncher
|
||||
)
|
||||
var data []dto.AppLauncher
|
||||
appInstalls, err := appInstallRepo.ListBy(context.Background())
|
||||
if err != nil {
|
||||
return data, err
|
||||
|
@ -281,8 +278,11 @@ func (u *DashboardService) LoadAppLauncher(ctx *gin.Context) ([]dto.AppLauncher,
|
|||
return data, err
|
||||
}
|
||||
|
||||
showList, _ := launcherRepo.ListName()
|
||||
defaultList := []string{"openresty", "mysql", "halo", "redis", "maxkb", "wordpress"}
|
||||
showList, err := launcherRepo.ListName()
|
||||
defaultList, err := appRepo.GetTopRecomment()
|
||||
if err != nil {
|
||||
return data, nil
|
||||
}
|
||||
allList := common.RemoveRepeatStr(append(defaultList, showList...))
|
||||
for _, showItem := range allList {
|
||||
var itemData dto.AppLauncher
|
||||
|
@ -317,24 +317,21 @@ func (u *DashboardService) LoadAppLauncher(ctx *gin.Context) ([]dto.AppLauncher,
|
|||
})
|
||||
}
|
||||
}
|
||||
if ArryContains(defaultList, showItem) && len(itemData.Detail) == 0 {
|
||||
itemData.IsRecommend = true
|
||||
recommendList = append(recommendList, itemData)
|
||||
continue
|
||||
if (ArryContains(showList, showItem) && len(itemData.Detail) != 0) ||
|
||||
(ArryContains(defaultList, showItem) && len(itemData.Detail) == 0) {
|
||||
data = append(data, itemData)
|
||||
}
|
||||
if !ArryContains(showList, showItem) && len(itemData.Detail) != 0 {
|
||||
continue
|
||||
}
|
||||
data = append(data, itemData)
|
||||
}
|
||||
|
||||
sort.Slice(recommendList, func(i, j int) bool {
|
||||
return recommendList[i].Recommend < recommendList[j].Recommend
|
||||
})
|
||||
sort.Slice(data, func(i, j int) bool {
|
||||
return data[i].Name < data[j].Name
|
||||
if data[i].IsInstall != data[j].IsInstall {
|
||||
return data[i].IsInstall
|
||||
}
|
||||
if data[i].IsInstall && data[j].IsInstall {
|
||||
return data[i].Name < data[j].Name
|
||||
}
|
||||
return data[i].Recommend < data[j].Recommend
|
||||
})
|
||||
data = append(data, recommendList...)
|
||||
return data, nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue