mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-09-12 09:34:58 +08:00
perf: 优化应用查询接口的执行速度 (#5202)
Refs https://github.com/1Panel-dev/1Panel/issues/5008
This commit is contained in:
parent
28f2dddcf6
commit
9f3573bcb8
7 changed files with 46 additions and 14 deletions
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
Normal file
BIN
frontend/src/assets/images/spider.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 274 B |
|
@ -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…
Add table
Reference in a new issue