fix: 监控数据查询数据为空时,重新渲染

This commit is contained in:
ssongliu 2022-12-27 18:57:11 +08:00 committed by ssongliu
parent df60a6ccde
commit ab5841e85b
12 changed files with 72 additions and 42 deletions

View file

@ -73,7 +73,6 @@ func (b *BaseApi) WsSsh(c *gin.Context) {
<-quitChan
global.LOG.Info("websocket finished")
if wshandleError(wsConn, err) {
return
}

View file

@ -23,8 +23,6 @@ type DashboardBase struct {
KernelArch string `json:"kernelArch"`
KernelVersion string `json:"kernelVersion"`
VirtualizationSystem string `json:"virtualizationSystem"`
Uptime string `json:"uptime"`
TimeSinceUptime string `json:"timeSinceUptime"`
CPUCores int `json:"cpuCores"`
CPULogicalCores int `json:"cpuLogicalCores"`
@ -34,6 +32,9 @@ type DashboardBase struct {
}
type DashboardCurrent struct {
Uptime uint64 `json:"uptime"`
TimeSinceUptime string `json:"timeSinceUptime"`
Procs uint64 `json:"procs"`
Load1 float64 `json:"load1"`

View file

@ -2,9 +2,6 @@ package service
import (
"encoding/json"
"fmt"
"os/exec"
"strings"
"time"
"github.com/1Panel-dev/1Panel/backend/app/dto"
@ -42,18 +39,6 @@ func (u *DashboardService) LoadBaseInfo(ioOption string, netOption string) (*dto
ss, _ := json.Marshal(hostInfo)
baseInfo.VirtualizationSystem = string(ss)
cmd := exec.Command("uptime", "-s")
stdout, err := cmd.CombinedOutput()
if err == nil {
baseInfo.Uptime = string(stdout)
uptime, err := time.Parse("2006-01-02 15:04:05", strings.ReplaceAll(string(stdout), "\n", ""))
if err == nil {
hours := int(time.Since(uptime).Hours())
minutes := int(time.Since(uptime).Minutes())
baseInfo.TimeSinceUptime = fmt.Sprintf("%ddays %dhours %dmimutes", hours/24, hours%24, minutes-hours*60)
}
}
apps, err := appRepo.GetBy()
if err != nil {
return nil, err
@ -111,6 +96,8 @@ func (u *DashboardService) LoadBaseInfo(ioOption string, netOption string) (*dto
func (u *DashboardService) LoadCurrentInfo(ioOption string, netOption string) *dto.DashboardCurrent {
var currentInfo dto.DashboardCurrent
hostInfo, _ := host.Info()
currentInfo.Uptime = hostInfo.Uptime
currentInfo.TimeSinceUptime = time.Now().Add(-time.Duration(hostInfo.Uptime) * time.Second).Format("2006-01-02 15:04:05")
currentInfo.Procs = hostInfo.Procs
currentInfo.CPUTotal, _ = cpu.Counts(true)

View file

@ -8,6 +8,7 @@ import (
"fmt"
"io"
"os"
"path"
"strings"
"time"
@ -165,8 +166,7 @@ func (u *ImageService) ImagePull(req dto.ImagePull) (string, error) {
return "", err
}
}
imageItemName := strings.ReplaceAll(req.ImageName, ":", "_")
imageItemName = strings.ReplaceAll(imageItemName, "/", "@")
imageItemName := strings.ReplaceAll(path.Base(req.ImageName), ":", "_")
pathItem := fmt.Sprintf("%s/image_pull_%s_%s.log", dockerLogDir, imageItemName, time.Now().Format("20060102150405"))
file, err := os.OpenFile(pathItem, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
@ -301,8 +301,7 @@ func (u *ImageService) ImagePush(req dto.ImagePush) (string, error) {
return "", err
}
}
imageItemName := strings.ReplaceAll(req.Name, ":", "_")
imageItemName = strings.ReplaceAll(imageItemName, "/", "@")
imageItemName := strings.ReplaceAll(path.Base(req.Name), ":", "_")
pathItem := fmt.Sprintf("%s/image_push_%s_%s.log", dockerLogDir, imageItemName, time.Now().Format("20060102150405"))
file, err := os.OpenFile(pathItem, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {

View file

@ -185,7 +185,6 @@ func (sws *LogicSshWsSession) sendComboOutput(exitCh chan bool) {
func (sws *LogicSshWsSession) Wait(quitChan chan bool) {
if err := sws.session.Wait(); err != nil {
global.LOG.Errorf("ssh session wait failed, err: %v", err)
setQuit(quitChan)
}
}

View file

@ -20,8 +20,6 @@ export namespace Dashboard {
kernelArch: string;
kernelVersion: string;
virtualizationSystem: string;
uptime: string;
timeSinceUptime: string;
cpuCores: number;
cpuLogicalCores: number;
@ -30,6 +28,8 @@ export namespace Dashboard {
currentInfo: CurrentInfo;
}
export interface CurrentInfo {
uptime: number;
timeSinceUptime: string;
procs: number;
load1: number;

View file

@ -191,6 +191,7 @@ export default {
Day: 'Days',
Hour: 'Hours',
Minute: 'Minutes',
Second: 'Seconds',
runSmoothly: 'Run smoothly',
runNormal: 'Run normal',
@ -596,6 +597,7 @@ export default {
apps: 'App',
containers: 'Container',
commands: 'Command',
groups: 'System Group',
backups: 'Backup Account',
settings: 'Panel Setting',
cronjobs: 'Cronjob',

View file

@ -194,8 +194,9 @@ export default {
uptime: '启动时间',
runningTime: '运行时间',
Day: '天',
Hour: '小时',
Minute: '分钟',
Hour: '时',
Minute: '分',
Second: '秒',
runSmoothly: '运行流畅',
runNormal: '运行正常',
@ -607,6 +608,7 @@ export default {
hosts: '主机',
apps: '应用',
containers: '容器',
groups: '系统组',
commands: '快捷命令',
backups: '备份账号',
settings: '面板设置',

View file

@ -100,8 +100,10 @@
{{ baseInfo.kernelVersion }}
</el-form-item>
<el-form-item :label="$t('home.kernelArch')">{{ baseInfo.kernelArch }}</el-form-item>
<el-form-item :label="$t('home.uptime')">{{ baseInfo.uptime }}</el-form-item>
<el-form-item :label="$t('home.runningTime')">{{ baseInfo.timeSinceUptime }}</el-form-item>
<el-form-item :label="$t('home.uptime')">{{ currentInfo.timeSinceUptime }}</el-form-item>
<el-form-item :label="$t('home.runningTime')">
{{ loadUpTime(currentInfo.uptime) }}
</el-form-item>
</el-form>
</el-card>
</el-col>
@ -227,8 +229,6 @@ const baseInfo = ref<Dashboard.BaseInfo>({
kernelArch: '',
kernelVersion: '',
virtualizationSystem: '',
uptime: '',
timeSinceUptime: '',
cpuCores: 0,
cpuLogicalCores: 0,
@ -236,6 +236,8 @@ const baseInfo = ref<Dashboard.BaseInfo>({
currentInfo: null,
});
const currentInfo = ref<Dashboard.CurrentInfo>({
uptime: 0,
timeSinceUptime: '',
procs: 0,
load1: 0,
@ -316,11 +318,6 @@ const onLoadBaseInfo = async (isInit: boolean, range: string) => {
const res = await loadBaseInfo(searchInfo.ioOption, searchInfo.netOption);
baseInfo.value = res.data;
currentInfo.value = baseInfo.value.currentInfo;
if (baseInfo.value.timeSinceUptime) {
baseInfo.value.timeSinceUptime.replaceAll('days', i18n.global.t('home.Day'));
baseInfo.value.timeSinceUptime.replaceAll('hours', i18n.global.t('home.Hour'));
baseInfo.value.timeSinceUptime.replaceAll('minutes', i18n.global.t('home.Minute'));
}
onLoadCurrentInfo();
statuRef.value.acceptParams(currentInfo.value, baseInfo.value);
appRef.value.acceptParams(baseInfo.value);
@ -334,6 +331,8 @@ const onLoadBaseInfo = async (isInit: boolean, range: string) => {
const onLoadCurrentInfo = async () => {
const res = await loadCurrentInfo(searchInfo.ioOption, searchInfo.netOption);
currentInfo.value.timeSinceUptime = res.data.timeSinceUptime;
currentChartInfo.netBytesSent = Number(
((res.data.netBytesSent - currentInfo.value.netBytesSent) / 1024 / 3).toFixed(2),
);
@ -378,6 +377,47 @@ const onLoadCurrentInfo = async () => {
statuRef.value.acceptParams(currentInfo.value, baseInfo.value);
};
function loadUpTime(uptime: number) {
if (uptime <= 0) {
return '-';
}
let days = Math.floor(uptime / 86400);
let hours = Math.floor((uptime % 86400) / 3600);
let minutes = Math.floor((uptime % 3600) / 60);
let seconds = uptime % 60;
if (days !== 0) {
return (
days +
i18n.global.t('home.Day') +
' ' +
hours +
i18n.global.t('home.Hour') +
' ' +
minutes +
i18n.global.t('home.Minute') +
' ' +
seconds +
i18n.global.t('home.Second')
);
}
if (hours !== 0) {
return (
hours +
i18n.global.t('home.Hour') +
' ' +
minutes +
i18n.global.t('home.Minute') +
' ' +
seconds +
i18n.global.t('home.Second')
);
}
if (minutes !== 0) {
return minutes + i18n.global.t('home.Minute') + ' ' + seconds + i18n.global.t('home.Second');
}
return seconds + i18n.global.t('home.Second');
}
const loadData = async () => {
if (chartOption.value === 'io') {
let ioReadYDatas = {

View file

@ -138,8 +138,6 @@ const baseInfo = ref<Dashboard.BaseInfo>({
databaseNumber: 0,
cronjobNumber: 0,
appInstalldNumber: 0,
uptime: '',
timeSinceUptime: '',
hostname: '',
os: '',
@ -156,6 +154,8 @@ const baseInfo = ref<Dashboard.BaseInfo>({
currentInfo: null,
});
const currentInfo = ref<Dashboard.CurrentInfo>({
uptime: 0,
timeSinceUptime: '',
procs: 0,
load1: 0,

View file

@ -202,11 +202,12 @@ const search = async (param: string) => {
searchInfo.endTime = searchTime.value[1];
}
const res = await loadMonitor(searchInfo);
if (res.data[0].value === null) {
return;
}
monitorBase.value = res.data;
for (const item of monitorBase.value) {
if (!item.value) {
item.value = [];
item.date = [];
}
switch (item.param) {
case 'base':
let baseDate = item.date.map(function (item: any) {

View file

@ -64,7 +64,7 @@ const panelFormRef = ref<FormInstance>();
const search = async () => {
const res = await getSettingInfo();
form.monitorStatus = res.data.monitorStatus;
form.monitorStoreDays = res.data.monitorStoreDays;
form.monitorStoreDays = Number(res.data.monitorStoreDays);
};
const onSave = async (formEl: FormInstance | undefined, key: string, val: any) => {