Browse Source

perf: 优化应用查询接口的执行速度 (#5202)

Refs https://github.com/1Panel-dev/1Panel/issues/5008
pull/5205/head
zhengkunwang 6 months ago committed by GitHub
parent
commit
9f3573bcb8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 1
      backend/app/dto/request/app.go
  2. 31
      backend/app/service/app_install.go
  3. 8
      backend/app/service/app_utils.go
  4. 2
      backend/utils/docker/docker.go
  5. 1
      frontend/src/api/interface/app.ts
  6. BIN
      frontend/src/assets/images/spider.png
  7. 17
      frontend/src/views/app-store/installed/index.vue

1
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 {

31
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

8
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{

2
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 {

1
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;

BIN
frontend/src/assets/images/spider.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 274 B

17
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(() => {

Loading…
Cancel
Save