From 9f3573bcb8e3474a63894da1952098a661d36b29 Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Wed, 29 May 2024 18:41:13 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E6=8E=A5=E5=8F=A3=E7=9A=84=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E9=80=9F=E5=BA=A6=20(#5202)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs https://github.com/1Panel-dev/1Panel/issues/5008 --- backend/app/dto/request/app.go | 1 + backend/app/service/app_install.go | 31 ++++++++++++++---- backend/app/service/app_utils.go | 8 +++-- backend/utils/docker/docker.go | 2 +- frontend/src/api/interface/app.ts | 1 + frontend/src/assets/images/spider.png | Bin 0 -> 274 bytes .../src/views/app-store/installed/index.vue | 17 ++++++++-- 7 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 frontend/src/assets/images/spider.png diff --git a/backend/app/dto/request/app.go b/backend/app/dto/request/app.go index 7a7fa461e..b06cbf96e 100644 --- a/backend/app/dto/request/app.go +++ b/backend/app/dto/request/app.go @@ -43,6 +43,7 @@ type AppInstalledSearch struct { Update bool `json:"update"` Unused bool `json:"unused"` All bool `json:"all"` + Sync bool `json:"sync"` } type AppInstalledInfo struct { diff --git a/backend/app/service/app_install.go b/backend/app/service/app_install.go index 0069766c4..028b02768 100644 --- a/backend/app/service/app_install.go +++ b/backend/app/service/app_install.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/docker/docker/api/types" "math" "os" "path" @@ -11,6 +12,7 @@ import ( "sort" "strconv" "strings" + "time" "github.com/1Panel-dev/1Panel/backend/utils/files" "gopkg.in/yaml.v3" @@ -118,7 +120,7 @@ func (a *AppInstallService) Page(req request.AppInstalledSearch) (int64, []respo } } - installDTOs, err := handleInstalled(installs, req.Update) + installDTOs, err := handleInstalled(installs, req.Update, req.Sync) if err != nil { return 0, nil, err } @@ -220,7 +222,7 @@ func (a *AppInstallService) SearchForWebsite(req request.AppInstalledSearch) ([] } } - return handleInstalled(installs, false) + return handleInstalled(installs, false, true) } func (a *AppInstallService) Operate(req request.AppInstalledOperate) error { @@ -700,24 +702,39 @@ func (a *AppInstallService) GetParams(id uint) (*response.AppConfig, error) { return &res, nil } +func measureExecutionTime(name string, fn func() error) error { + start := time.Now() // 记录开始时间 + err := fn() // 执行函数 + elapsed := time.Since(start) // 计算执行时间 + + fmt.Printf("%s took %s\n", name, elapsed) // 输出函数名和执行时间 + return err +} + func syncAppInstallStatus(appInstall *model.AppInstall) error { if appInstall.Status == constant.Installing || appInstall.Status == constant.Rebuilding || appInstall.Status == constant.Upgrading { return nil } - containerNames, err := getContainerNames(*appInstall) - if err != nil { - return err + var err error + containerNames := []string{appInstall.ContainerName} + if appInstall.ContainerName == "" { + containerNames, err = getContainerNames(*appInstall) + if err != nil { + return err + } } + cli, err := docker.NewClient() if err != nil { return err } defer cli.Close() - containers, err := cli.ListContainersByName(containerNames) + + var containers []types.Container + containers, err = cli.ListContainersByName(containerNames) if err != nil { return err } - var ( runningCount int exitedCount int diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index 1ae1cc49e..43fb4553a 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -1117,14 +1117,16 @@ func handleErr(install model.AppInstall, err error, out string) error { return reErr } -func handleInstalled(appInstallList []model.AppInstall, updated bool) ([]response.AppInstalledDTO, error) { +func handleInstalled(appInstallList []model.AppInstall, updated bool, sync bool) ([]response.AppInstalledDTO, error) { var res []response.AppInstalledDTO for _, installed := range appInstallList { if updated && (installed.App.Type == "php" || installed.Status == constant.Installing || (installed.App.Key == constant.AppMysql && installed.Version == "5.6.51")) { continue } - if err := syncAppInstallStatus(&installed); err != nil { - global.LOG.Error("sync app install status error : ", err) + if sync { + if err := syncAppInstallStatus(&installed); err != nil { + global.LOG.Error("sync app install status error : ", err) + } } installDTO := response.AppInstalledDTO{ diff --git a/backend/utils/docker/docker.go b/backend/utils/docker/docker.go index 142de4260..ba9ff78ab 100644 --- a/backend/utils/docker/docker.go +++ b/backend/utils/docker/docker.go @@ -57,7 +57,7 @@ func (c Client) ListContainersByName(names []string) ([]types.Container, error) namesMap = make(map[string]bool) res []types.Container ) - options.All = true + options.All = false if len(names) > 0 { var array []filters.KeyValuePair for _, n := range names { diff --git a/frontend/src/api/interface/app.ts b/frontend/src/api/interface/app.ts index cb762e0af..a01c885d4 100644 --- a/frontend/src/api/interface/app.ts +++ b/frontend/src/api/interface/app.ts @@ -99,6 +99,7 @@ export namespace App { tags?: string[]; update?: boolean; unused?: boolean; + sync?: boolean; } export interface ChangePort { key: string; diff --git a/frontend/src/assets/images/spider.png b/frontend/src/assets/images/spider.png new file mode 100644 index 0000000000000000000000000000000000000000..63b0faaaafdf580379a25c424a49b3deaf080699 GIT binary patch literal 274 zcmV+t0qy>YP)Px#%t=H+R5(wilZy?)FboCV$$-hAlL3HGVe9mWyKfk5OKS#th zA}*8f)@{dTRs#1PI18};{P+$D7)bD`(=dnZtEdLfNB^lr8`zFOIfrbtlLb&;yHgQt zgs>S+9TkvYMNA~f2o1Tq!LGtKL$Ky?bN{S>Gk(JVv@Ua;~p?Nu(< Y-ygX>5eCvFxBvhE07*qoM6N<$f=bM8*Z=?k literal 0 HcmV?d00001 diff --git a/frontend/src/views/app-store/installed/index.vue b/frontend/src/views/app-store/installed/index.vue index d17d296ce..f89bc3606 100644 --- a/frontend/src/views/app-store/installed/index.vue +++ b/frontend/src/views/app-store/installed/index.vue @@ -353,6 +353,7 @@ const searchReq = reactive({ name: '', tags: [], update: false, + sync: false, }); const router = useRouter(); const activeName = ref(i18n.global.t('app.installed')); @@ -417,7 +418,6 @@ const search = () => { loading.value = true; searchReq.page = paginationConfig.currentPage; searchReq.pageSize = paginationConfig.pageSize; - localStorage.setItem('app-installed', searchReq.pageSize + ''); SearchAppInstalled(searchReq) .then((res) => { data.value = res.data.items; @@ -468,10 +468,17 @@ const operate = async () => { await InstalledOp(operateReq) .then(() => { MsgSuccess(i18n.global.t('commons.msg.operationSuccess')); + searchReq.sync = true; search(); setTimeout(() => { search(); - }, 1500); + }, 3000); + setTimeout(() => { + search(); + }, 5000); + setTimeout(() => { + search(); + }, 10000); }) .catch(() => { search(); @@ -612,9 +619,13 @@ onMounted(() => { searchReq.update = true; } search(); + setTimeout(() => { + searchReq.sync = true; + search(); + }, 1000); timer = setInterval(() => { search(); - }, 1000 * 60); + }, 1000 * 20); }); onUnmounted(() => {