Skip to content

Commit 5ee392b

Browse files
authored
fix: Cancel synchronization between recommended nodes on dashboard (#8270)
1 parent 076f3f0 commit 5ee392b

18 files changed

Lines changed: 102 additions & 293 deletions

File tree

agent/app/api/v2/dashboard.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,25 @@ func (b *BaseApi) LoadAppLauncherOption(c *gin.Context) {
6161
helper.SuccessWithData(c, data)
6262
}
6363

64-
func (b *BaseApi) SyncAppLauncher(c *gin.Context) {
65-
var req dto.AppLauncherSync
64+
// @Tags Dashboard
65+
// @Summary Update app Launcher
66+
// @Accept json
67+
// @Param request body dto.SettingUpdate true "request"
68+
// @Success 200
69+
// @Security ApiKeyAuth
70+
// @Security Timestamp
71+
// @Router /dashboard/app/launcher/show [post]
72+
// @x-panel-log {"bodyKeys":["key", "value"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"首页应用 [key] => 显示:[value]","formatEN":"app launcher [key] => show: [value]"}
73+
func (b *BaseApi) UpdateAppLauncher(c *gin.Context) {
74+
var req dto.SettingUpdate
6675
if err := helper.CheckBindAndValidate(&req, c); err != nil {
6776
return
6877
}
69-
if err := dashboardService.Sync(req); err != nil {
70-
helper.BadRequest(c, err)
78+
79+
if err := dashboardService.ChangeShow(req); err != nil {
80+
helper.InternalServer(c, err)
7181
return
7282
}
73-
7483
helper.SuccessWithOutData(c)
7584
}
7685

agent/app/repo/app_launcher.go

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ type LauncherRepo struct{}
99

1010
type ILauncherRepo interface {
1111
Get(opts ...DBOption) (model.AppLauncher, error)
12-
List(opts ...DBOption) ([]model.AppLauncher, error)
12+
ListName(opts ...DBOption) ([]string, error)
1313
Create(launcher *model.AppLauncher) error
1414
Save(launcher *model.AppLauncher) error
1515
Delete(opts ...DBOption) error
16-
17-
SyncAll(data []model.AppLauncher) error
1816
}
1917

2018
func NewILauncherRepo() ILauncherRepo {
@@ -30,14 +28,18 @@ func (u *LauncherRepo) Get(opts ...DBOption) (model.AppLauncher, error) {
3028
err := db.First(&launcher).Error
3129
return launcher, err
3230
}
33-
func (u *LauncherRepo) List(opts ...DBOption) ([]model.AppLauncher, error) {
31+
func (u *LauncherRepo) ListName(opts ...DBOption) ([]string, error) {
3432
var ops []model.AppLauncher
3533
db := global.DB.Model(&model.AppLauncher{})
3634
for _, opt := range opts {
3735
db = opt(db)
3836
}
39-
err := db.Find(&ops).Error
40-
return ops, err
37+
_ = db.Find(&ops).Error
38+
var names []string
39+
for i := 0; i < len(ops); i++ {
40+
names = append(names, ops[i].Key)
41+
}
42+
return names, nil
4143
}
4244

4345
func (u *LauncherRepo) Create(launcher *model.AppLauncher) error {
@@ -55,21 +57,3 @@ func (u *LauncherRepo) Delete(opts ...DBOption) error {
5557
}
5658
return db.Delete(&model.AppLauncher{}).Error
5759
}
58-
59-
func (u *LauncherRepo) SyncAll(data []model.AppLauncher) error {
60-
tx := global.DB.Begin()
61-
if err := tx.Where("1 = 1").Delete(&model.AppLauncher{}).Error; err != nil {
62-
tx.Rollback()
63-
return err
64-
}
65-
if len(data) == 0 {
66-
tx.Commit()
67-
return nil
68-
}
69-
if err := tx.Model(model.AppLauncher{}).Save(&data).Error; err != nil {
70-
tx.Rollback()
71-
return err
72-
}
73-
tx.Commit()
74-
return nil
75-
}

agent/app/repo/common.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ func WithByName(name string) DBOption {
4242
}
4343
}
4444

45+
func WithByKey(key string) DBOption {
46+
return func(g *gorm.DB) *gorm.DB {
47+
return g.Where("key = ?", key)
48+
}
49+
}
50+
4551
func WithByLowerName(name string) DBOption {
4652
return func(g *gorm.DB) *gorm.DB {
4753
return g.Where("LOWER(name) = LOWER(?)", name)

agent/app/service/dashboard.go

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/1Panel-dev/1Panel/agent/app/dto"
1515
"github.com/1Panel-dev/1Panel/agent/app/model"
16+
"github.com/1Panel-dev/1Panel/agent/app/repo"
1617
"github.com/1Panel-dev/1Panel/agent/constant"
1718
"github.com/1Panel-dev/1Panel/agent/global"
1819
"github.com/1Panel-dev/1Panel/agent/utils/ai_tools/gpu"
@@ -31,14 +32,13 @@ import (
3132
type DashboardService struct{}
3233

3334
type IDashboardService interface {
34-
Sync(req dto.AppLauncherSync) error
35-
3635
LoadOsInfo() (*dto.OsInfo, error)
3736
LoadBaseInfo(ioOption string, netOption string) (*dto.DashboardBase, error)
3837
LoadCurrentInfoForNode() *dto.NodeCurrent
3938
LoadCurrentInfo(ioOption string, netOption string) *dto.DashboardCurrent
4039

4140
LoadAppLauncher() ([]dto.AppLauncher, error)
41+
ChangeShow(req dto.SettingUpdate) error
4242
ListLauncherOption(filter string) ([]dto.LauncherOption, error)
4343
Restart(operation string) error
4444
}
@@ -47,14 +47,6 @@ func NewIDashboardService() IDashboardService {
4747
return &DashboardService{}
4848
}
4949

50-
func (u *DashboardService) Sync(req dto.AppLauncherSync) error {
51-
var launchers []model.AppLauncher
52-
for _, item := range req.Keys {
53-
launchers = append(launchers, model.AppLauncher{Key: item})
54-
}
55-
return launcherRepo.SyncAll(launchers)
56-
}
57-
5850
func (u *DashboardService) Restart(operation string) error {
5951
if operation != "1panel" && operation != "system" && operation != "1panel-agent" {
6052
return fmt.Errorf("handle restart operation %s failed, err: nonsupport such operation", operation)
@@ -285,7 +277,7 @@ func (u *DashboardService) LoadAppLauncher() ([]dto.AppLauncher, error) {
285277
return data, err
286278
}
287279

288-
showList := loadShowList()
280+
showList, _ := launcherRepo.ListName()
289281
defaultList := []string{"openresty", "mysql", "halo", "redis", "maxkb", "wordpress"}
290282
allList := common.RemoveRepeatStr(append(defaultList, showList...))
291283
for _, showItem := range allList {
@@ -343,8 +335,23 @@ func (u *DashboardService) LoadAppLauncher() ([]dto.AppLauncher, error) {
343335
return data, nil
344336
}
345337

338+
func (u *DashboardService) ChangeShow(req dto.SettingUpdate) error {
339+
launcher, _ := launcherRepo.Get(repo.WithByKey(req.Key))
340+
if req.Value == constant.StatusEnable && launcher.ID == 0 {
341+
if err := launcherRepo.Create(&model.AppLauncher{Key: req.Key}); err != nil {
342+
return err
343+
}
344+
}
345+
if req.Value == constant.StatusDisable && launcher.ID != 0 {
346+
if err := launcherRepo.Delete(repo.WithByKey(req.Key)); err != nil {
347+
return err
348+
}
349+
}
350+
return nil
351+
}
352+
346353
func (u *DashboardService) ListLauncherOption(filter string) ([]dto.LauncherOption, error) {
347-
showList := loadShowList()
354+
showList, _ := launcherRepo.ListName()
348355
var data []dto.LauncherOption
349356
optionMap := make(map[string]bool)
350357
appInstalls, err := appInstallRepo.ListBy(context.Background())
@@ -500,25 +507,6 @@ func loadGPUInfo() []dto.GPUInfo {
500507
return data
501508
}
502509

503-
func loadShowList() []string {
504-
var data []string
505-
if global.IsMaster {
506-
var list []AppLauncher
507-
if err := global.CoreDB.Model(AppLauncher{}).Where("1 == 1").Find(&list).Error; err != nil {
508-
return []string{}
509-
}
510-
for _, item := range list {
511-
data = append(data, item.Key)
512-
}
513-
return data
514-
}
515-
list, _ := launcherRepo.List()
516-
for _, item := range list {
517-
data = append(data, item.Key)
518-
}
519-
return data
520-
}
521-
522510
type AppLauncher struct {
523511
Key string `json:"key"`
524512
}

agent/app/service/device_clean.go

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,23 @@ func (u *DeviceService) Scan() dto.CleanData {
6060

6161
upgradePath := path.Join(global.Dir.BaseDir, upgradePath)
6262
upgradeSize, _ := fileOp.GetDirSize(upgradePath)
63-
treeData = append(treeData, dto.CleanTree{
63+
upgradeTree := dto.CleanTree{
6464
ID: uuid.NewString(),
6565
Label: "upgrade",
6666
Size: uint64(upgradeSize),
6767
IsCheck: false,
6868
IsRecommend: true,
6969
Type: "upgrade",
7070
Children: loadTreeWithDir(true, "upgrade", upgradePath, fileOp),
71-
})
71+
}
72+
if len(upgradeTree.Children) != 0 {
73+
sort.Slice(upgradeTree.Children, func(i, j int) bool {
74+
return common.CompareVersion(upgradeTree.Children[i].Label, upgradeTree.Children[j].Label)
75+
})
76+
upgradeTree.Children[0].IsCheck = false
77+
upgradeTree.Children[0].IsRecommend = false
78+
}
79+
treeData = append(treeData, upgradeTree)
7280

7381
snapTree := loadSnapshotTree(fileOp)
7482
snapSize := uint64(0)
@@ -563,11 +571,6 @@ func loadTreeWithDir(isCheck bool, treeType, pathItem string, fileOp fileUtils.F
563571
if err != nil {
564572
return lists
565573
}
566-
sort.Slice(files, func(i, j int) bool {
567-
infoI, _ := files[i].Info()
568-
infoJ, _ := files[i].Info()
569-
return infoI.ModTime().Before(infoJ.ModTime())
570-
})
571574
for _, file := range files {
572575
if treeType == "old_upgrade" {
573576
continue
@@ -589,10 +592,6 @@ func loadTreeWithDir(isCheck bool, treeType, pathItem string, fileOp fileUtils.F
589592
IsCheck: isCheck,
590593
IsRecommend: isCheck,
591594
}
592-
if treeType == "upgrade" && len(lists) == 0 {
593-
item.IsCheck = false
594-
item.IsRecommend = false
595-
}
596595
lists = append(lists, item)
597596
}
598597
}
@@ -742,3 +741,31 @@ func scanFile(pathItem string, size *int64, count *int) {
742741
}
743742
}
744743
}
744+
745+
func loadRestorePath(upgradeDir string) (string, error) {
746+
if _, err := os.Stat(upgradeDir); err != nil && os.IsNotExist(err) {
747+
return "no such file", nil
748+
}
749+
files, err := os.ReadDir(upgradeDir)
750+
if err != nil {
751+
return "", err
752+
}
753+
type itemState struct {
754+
Name string
755+
CreateAt time.Time
756+
}
757+
var folders []itemState
758+
for _, file := range files {
759+
if file.IsDir() {
760+
info, _ := file.Info()
761+
folders = append(folders, itemState{Name: file.Name(), CreateAt: info.ModTime()})
762+
}
763+
}
764+
if len(folders) == 0 {
765+
return "no such file", nil
766+
}
767+
sort.Slice(folders, func(i, j int) bool {
768+
return folders[i].CreateAt.After(folders[j].CreateAt)
769+
})
770+
return folders[0].Name, nil
771+
}

agent/router/ro_dashboard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func (s *DashboardRouter) InitRouter(Router *gin.RouterGroup) {
1313
{
1414
cmdRouter.GET("/base/os", baseApi.LoadDashboardOsInfo)
1515
cmdRouter.GET("/app/launcher", baseApi.LoadAppLauncher)
16-
cmdRouter.POST("/app/launcher/sync", baseApi.SyncAppLauncher)
16+
cmdRouter.POST("/app/launcher/show", baseApi.UpdateAppLauncher)
1717
cmdRouter.POST("/app/launcher/option", baseApi.LoadAppLauncherOption)
1818
cmdRouter.GET("/base/:ioOption/:netOption", baseApi.LoadDashboardBaseInfo)
1919
cmdRouter.GET("/current/node", baseApi.LoadCurrentInfoForNode)

core/app/api/v2/app_launcher.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

core/app/api/v2/entry.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ type ApiGroup struct {
99
var ApiGroupApp = new(ApiGroup)
1010

1111
var (
12-
hostService = service.NewIHostService()
13-
authService = service.NewIAuthService()
14-
backupService = service.NewIBackupService()
15-
settingService = service.NewISettingService()
16-
logService = service.NewILogService()
17-
upgradeService = service.NewIUpgradeService()
18-
groupService = service.NewIGroupService()
19-
commandService = service.NewICommandService()
20-
appLauncherService = service.NewIAppLauncher()
21-
scriptService = service.NewIScriptService()
12+
hostService = service.NewIHostService()
13+
authService = service.NewIAuthService()
14+
backupService = service.NewIBackupService()
15+
settingService = service.NewISettingService()
16+
logService = service.NewILogService()
17+
upgradeService = service.NewIUpgradeService()
18+
groupService = service.NewIGroupService()
19+
commandService = service.NewICommandService()
20+
scriptService = service.NewIScriptService()
2221
)

core/app/model/app_launcher.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)