diff --git a/agent/app/repo/app.go b/agent/app/repo/app.go index de3a62738..67bf3105b 100644 --- a/agent/app/repo/app.go +++ b/agent/app/repo/app.go @@ -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 } diff --git a/agent/app/service/dashboard.go b/agent/app/service/dashboard.go index 6395f7ea8..3f745782e 100644 --- a/agent/app/service/dashboard.go +++ b/agent/app/service/dashboard.go @@ -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 }