mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-19 08:19:15 +08:00
feat: 监控增加数据采集间隔设置 (#1146)
This commit is contained in:
parent
d64e1713fb
commit
c1acd8f5f0
@ -30,6 +30,7 @@ type SettingInfo struct {
|
||||
MFASecret string `json:"mfaSecret"`
|
||||
|
||||
MonitorStatus string `json:"monitorStatus"`
|
||||
MonitorInterval string `json:"monitorInterval"`
|
||||
MonitorStoreDays string `json:"monitorStoreDays"`
|
||||
|
||||
MessageType string `json:"messageType"`
|
||||
|
@ -1,6 +1,8 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/1Panel-dev/1Panel/backend/app/model"
|
||||
"github.com/1Panel-dev/1Panel/backend/global"
|
||||
"gorm.io/gorm"
|
||||
@ -14,6 +16,13 @@ type ISettingRepo interface {
|
||||
Create(key, value string) error
|
||||
Update(key, value string) error
|
||||
WithByKey(key string) DBOption
|
||||
|
||||
CreateMonitorBase(model model.MonitorBase) error
|
||||
BatchCreateMonitorIO(ioList []model.MonitorIO) error
|
||||
BatchCreateMonitorNet(ioList []model.MonitorNetwork) error
|
||||
DelMonitorBase(timeForDelete time.Time) error
|
||||
DelMonitorIO(timeForDelete time.Time) error
|
||||
DelMonitorNet(timeForDelete time.Time) error
|
||||
}
|
||||
|
||||
func NewISettingRepo() ISettingRepo {
|
||||
@ -57,3 +66,22 @@ func (c *SettingRepo) WithByKey(key string) DBOption {
|
||||
func (u *SettingRepo) Update(key, value string) error {
|
||||
return global.DB.Model(&model.Setting{}).Where("key = ?", key).Updates(map[string]interface{}{"value": value}).Error
|
||||
}
|
||||
|
||||
func (u *SettingRepo) CreateMonitorBase(model model.MonitorBase) error {
|
||||
return global.DB.Create(&model).Error
|
||||
}
|
||||
func (u *SettingRepo) BatchCreateMonitorIO(ioList []model.MonitorIO) error {
|
||||
return global.DB.CreateInBatches(ioList, len(ioList)).Error
|
||||
}
|
||||
func (u *SettingRepo) BatchCreateMonitorNet(ioList []model.MonitorNetwork) error {
|
||||
return global.DB.CreateInBatches(ioList, len(ioList)).Error
|
||||
}
|
||||
func (u *SettingRepo) DelMonitorBase(timeForDelete time.Time) error {
|
||||
return global.DB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorBase{}).Error
|
||||
}
|
||||
func (u *SettingRepo) DelMonitorIO(timeForDelete time.Time) error {
|
||||
return global.DB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorIO{}).Error
|
||||
}
|
||||
func (u *SettingRepo) DelMonitorNet(timeForDelete time.Time) error {
|
||||
return global.DB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorNetwork{}).Error
|
||||
}
|
||||
|
@ -1,12 +1,13 @@
|
||||
package job
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/1Panel-dev/1Panel/backend/app/model"
|
||||
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
||||
"github.com/1Panel-dev/1Panel/backend/global"
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/shirou/gopsutil/v3/cpu"
|
||||
"github.com/shirou/gopsutil/v3/disk"
|
||||
"github.com/shirou/gopsutil/v3/load"
|
||||
@ -14,14 +15,18 @@ import (
|
||||
"github.com/shirou/gopsutil/v3/net"
|
||||
)
|
||||
|
||||
type monitor struct{}
|
||||
type MonitorService struct{}
|
||||
|
||||
func NewMonitorJob() *monitor {
|
||||
return &monitor{}
|
||||
type IMonitorService interface {
|
||||
Run()
|
||||
}
|
||||
|
||||
func (m *monitor) Run() {
|
||||
settingRepo := repo.NewISettingRepo()
|
||||
func NewIMonitorService() IMonitorService {
|
||||
return &MonitorService{}
|
||||
}
|
||||
|
||||
func (m *MonitorService) Run() {
|
||||
fmt.Printf("开始采集数据了啊我 %s \n", time.Now().Format("2006-01-02 15:04:05"))
|
||||
monitorStatus, _ := settingRepo.Get(settingRepo.WithByKey("MonitorStatus"))
|
||||
if monitorStatus.Value == "disable" {
|
||||
return
|
||||
@ -42,7 +47,7 @@ func (m *monitor) Run() {
|
||||
memoryInfo, _ := mem.VirtualMemory()
|
||||
itemModel.Memory = memoryInfo.UsedPercent
|
||||
|
||||
if err := global.DB.Create(&itemModel).Error; err != nil {
|
||||
if err := settingRepo.CreateMonitorBase(itemModel); err != nil {
|
||||
global.LOG.Errorf("Insert basic monitoring data failed, err: %v", err)
|
||||
}
|
||||
|
||||
@ -55,9 +60,9 @@ func (m *monitor) Run() {
|
||||
}
|
||||
storeDays, _ := strconv.Atoi(MonitorStoreDays.Value)
|
||||
timeForDelete := time.Now().AddDate(0, 0, -storeDays)
|
||||
_ = global.DB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorBase{}).Error
|
||||
_ = global.DB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorIO{}).Error
|
||||
_ = global.DB.Where("created_at < ?", timeForDelete).Delete(&model.MonitorNetwork{}).Error
|
||||
_ = settingRepo.DelMonitorBase(timeForDelete)
|
||||
_ = settingRepo.DelMonitorIO(timeForDelete)
|
||||
_ = settingRepo.DelMonitorNet(timeForDelete)
|
||||
}
|
||||
|
||||
func loadDiskIO() {
|
||||
@ -91,7 +96,7 @@ func loadDiskIO() {
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := global.DB.CreateInBatches(ioList, len(ioList)).Error; err != nil {
|
||||
if err := settingRepo.BatchCreateMonitorIO(ioList); err != nil {
|
||||
global.LOG.Errorf("Insert io monitoring data failed, err: %v", err)
|
||||
}
|
||||
}
|
||||
@ -133,7 +138,19 @@ func loadNetIO() {
|
||||
}
|
||||
}
|
||||
|
||||
if err := global.DB.CreateInBatches(netList, len(netList)).Error; err != nil {
|
||||
if err := settingRepo.BatchCreateMonitorNet(netList); err != nil {
|
||||
global.LOG.Errorf("Insert network monitoring data failed, err: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func StartMonitor(removeBefore bool, interval string) error {
|
||||
if removeBefore {
|
||||
global.Cron.Remove(cron.EntryID(global.MonitorCronID))
|
||||
}
|
||||
monitorID, err := global.Cron.AddJob(fmt.Sprintf("@every %sm", interval), NewIMonitorService())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
global.MonitorCronID = int(monitorID)
|
||||
return nil
|
||||
}
|
@ -22,6 +22,7 @@ import (
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/ntp"
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/ssl"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
type SettingService struct{}
|
||||
@ -72,9 +73,41 @@ func (u *SettingService) LoadTimeZone() ([]string, error) {
|
||||
}
|
||||
|
||||
func (u *SettingService) Update(key, value string) error {
|
||||
switch key {
|
||||
case "MonitorStatus":
|
||||
if value == "enable" && global.MonitorCronID == 0 {
|
||||
interval, err := settingRepo.Get(settingRepo.WithByKey("MonitorInterval"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := StartMonitor(false, interval.Value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if value == "disable" && global.MonitorCronID != 0 {
|
||||
global.Cron.Remove(cron.EntryID(global.MonitorCronID))
|
||||
global.MonitorCronID = 0
|
||||
}
|
||||
case "MonitorInterval":
|
||||
status, err := settingRepo.Get(settingRepo.WithByKey("MonitorStatus"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if status.Value == "enable" && global.MonitorCronID != 0 {
|
||||
if err := StartMonitor(true, value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case "TimeZone":
|
||||
if err := ntp.UpdateSystemTimeZone(value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := settingRepo.Update(key, value); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch key {
|
||||
case "ExpirationDays":
|
||||
timeout, _ := strconv.Atoi(value)
|
||||
@ -86,9 +119,6 @@ func (u *SettingService) Update(key, value string) error {
|
||||
case "AllowIPs":
|
||||
global.CONF.System.AllowIPs = value
|
||||
case "TimeZone":
|
||||
if err := ntp.UpdateSystemTimeZone(value); err != nil {
|
||||
return err
|
||||
}
|
||||
go func() {
|
||||
_, err := cmd.Exec("systemctl restart 1panel.service")
|
||||
if err != nil {
|
||||
|
@ -15,19 +15,31 @@ import (
|
||||
|
||||
func Run() {
|
||||
nyc, _ := time.LoadLocation(common.LoadTimeZone())
|
||||
Cron := cron.New(cron.WithLocation(nyc), cron.WithChain(cron.Recover(cron.DefaultLogger)), cron.WithChain(cron.DelayIfStillRunning(cron.DefaultLogger)))
|
||||
if _, err := Cron.AddJob("@every 5m", job.NewMonitorJob()); err != nil {
|
||||
global.Cron = cron.New(cron.WithLocation(nyc), cron.WithChain(cron.Recover(cron.DefaultLogger)), cron.WithChain(cron.DelayIfStillRunning(cron.DefaultLogger)))
|
||||
|
||||
var (
|
||||
interval model.Setting
|
||||
status model.Setting
|
||||
)
|
||||
if err := global.DB.Where("key = ?", "MonitorStatus").Find(&status).Error; err != nil {
|
||||
global.LOG.Errorf("load monitor status from db failed, err: %v", err)
|
||||
}
|
||||
if status.Value == "enable" {
|
||||
if err := global.DB.Where("key = ?", "MonitorInterval").Find(&interval).Error; err != nil {
|
||||
global.LOG.Errorf("load monitor interval from db failed, err: %v", err)
|
||||
}
|
||||
if err := service.StartMonitor(false, interval.Value); err != nil {
|
||||
global.LOG.Errorf("can not add monitor corn job: %s", err.Error())
|
||||
}
|
||||
if _, err := Cron.AddJob("@daily", job.NewWebsiteJob()); err != nil {
|
||||
}
|
||||
|
||||
if _, err := global.Cron.AddJob("@daily", job.NewWebsiteJob()); err != nil {
|
||||
global.LOG.Errorf("can not add website corn job: %s", err.Error())
|
||||
}
|
||||
if _, err := Cron.AddJob("@daily", job.NewSSLJob()); err != nil {
|
||||
if _, err := global.Cron.AddJob("@daily", job.NewSSLJob()); err != nil {
|
||||
global.LOG.Errorf("can not add ssl corn job: %s", err.Error())
|
||||
}
|
||||
Cron.Start()
|
||||
|
||||
global.Cron = Cron
|
||||
global.Cron.Start()
|
||||
|
||||
var cronJobs []model.Cronjob
|
||||
if err := global.DB.Where("status = ?", constant.StatusEnable).Find(&cronJobs).Error; err != nil {
|
||||
|
@ -21,4 +21,5 @@ var (
|
||||
Viper *viper.Viper
|
||||
|
||||
Cron *cron.Cron
|
||||
MonitorCronID int
|
||||
)
|
||||
|
@ -351,6 +351,9 @@ var AddBindAndAllowIPs = &gormigrate.Migration{
|
||||
if err := tx.Create(&model.Setting{Key: "NtpSite", Value: "pool.ntp.org"}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Create(&model.Setting{Key: "MonitorInterval", Value: "1"}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ export namespace Setting {
|
||||
mfaSecret: string;
|
||||
|
||||
monitorStatus: string;
|
||||
monitorInterval: number;
|
||||
monitorStoreDays: number;
|
||||
|
||||
messageType: string;
|
||||
|
@ -699,6 +699,7 @@ const message = {
|
||||
network: 'Network',
|
||||
up: 'Up',
|
||||
down: 'Down',
|
||||
interval: 'Interval(min)',
|
||||
},
|
||||
terminal: {
|
||||
conn: 'connection',
|
||||
|
@ -705,6 +705,7 @@ const message = {
|
||||
network: '网络',
|
||||
up: '上行',
|
||||
down: '下行',
|
||||
interval: '采集间隔(分钟)',
|
||||
},
|
||||
terminal: {
|
||||
conn: '连接',
|
||||
|
89
frontend/src/views/host/monitor/setting/days/index.vue
Normal file
89
frontend/src/views/host/monitor/setting/days/index.vue
Normal file
@ -0,0 +1,89 @@
|
||||
<template>
|
||||
<div>
|
||||
<el-drawer
|
||||
v-model="drawerVisiable"
|
||||
:destroy-on-close="true"
|
||||
@close="handleClose"
|
||||
:close-on-click-modal="false"
|
||||
size="30%"
|
||||
>
|
||||
<template #header>
|
||||
<DrawerHeader :header="$t('setting.storeDays')" :back="handleClose" />
|
||||
</template>
|
||||
<el-form ref="formRef" label-position="top" :model="form" @submit.prevent v-loading="loading">
|
||||
<el-row type="flex" justify="center">
|
||||
<el-col :span="22">
|
||||
<el-form-item
|
||||
:label="$t('setting.storeDays')"
|
||||
:rules="[Rules.integerNumber, checkNumberRange(1, 30)]"
|
||||
prop="monitorStoreDays"
|
||||
>
|
||||
<el-input clearable v-model.number="form.monitorStoreDays" />
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
</el-row>
|
||||
</el-form>
|
||||
<template #footer>
|
||||
<span class="dialog-footer">
|
||||
<el-button @click="drawerVisiable = false">{{ $t('commons.button.cancel') }}</el-button>
|
||||
<el-button :disabled="loading" type="primary" @click="onSave(formRef)">
|
||||
{{ $t('commons.button.confirm') }}
|
||||
</el-button>
|
||||
</span>
|
||||
</template>
|
||||
</el-drawer>
|
||||
</div>
|
||||
</template>
|
||||
<script lang="ts" setup>
|
||||
import { reactive, ref } from 'vue';
|
||||
import i18n from '@/lang';
|
||||
import { MsgSuccess } from '@/utils/message';
|
||||
import { FormInstance } from 'element-plus';
|
||||
import { Rules, checkNumberRange } from '@/global/form-rules';
|
||||
import { updateSetting } from '@/api/modules/setting';
|
||||
|
||||
const emit = defineEmits<{ (e: 'search'): void }>();
|
||||
|
||||
interface DialogProps {
|
||||
monitorStoreDays: number;
|
||||
}
|
||||
const drawerVisiable = ref();
|
||||
const loading = ref();
|
||||
|
||||
const form = reactive({
|
||||
monitorStoreDays: 30,
|
||||
});
|
||||
|
||||
const formRef = ref<FormInstance>();
|
||||
|
||||
const acceptParams = (params: DialogProps): void => {
|
||||
form.monitorStoreDays = params.monitorStoreDays;
|
||||
drawerVisiable.value = true;
|
||||
};
|
||||
|
||||
const onSave = async (formEl: FormInstance | undefined) => {
|
||||
if (!formEl) return;
|
||||
formEl.validate(async (valid) => {
|
||||
if (!valid) return;
|
||||
loading.value = true;
|
||||
await updateSetting({ key: 'MonitorStoreDays', value: form.monitorStoreDays + '' })
|
||||
.then(() => {
|
||||
loading.value = false;
|
||||
handleClose();
|
||||
emit('search');
|
||||
MsgSuccess(i18n.global.t('commons.msg.operationSuccess'));
|
||||
})
|
||||
.catch(() => {
|
||||
loading.value = false;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const handleClose = () => {
|
||||
drawerVisiable.value = false;
|
||||
};
|
||||
|
||||
defineExpose({
|
||||
acceptParams,
|
||||
});
|
||||
</script>
|
@ -8,30 +8,28 @@
|
||||
<el-row>
|
||||
<el-col :span="1"><br /></el-col>
|
||||
<el-col :span="12">
|
||||
<el-form-item
|
||||
:label="$t('setting.enableMonitor')"
|
||||
:rules="Rules.requiredInput"
|
||||
prop="monitorStatus"
|
||||
>
|
||||
<el-form-item :label="$t('setting.enableMonitor')" prop="monitorStatus">
|
||||
<el-switch
|
||||
@change="onSave(panelFormRef, 'MonitorStatus', form.monitorStatus)"
|
||||
@change="onSaveStatus"
|
||||
v-model="form.monitorStatus"
|
||||
active-value="enable"
|
||||
inactive-value="disable"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item
|
||||
:label="$t('setting.storeDays')"
|
||||
:rules="[Rules.integerNumber, checkNumberRange(1, 30)]"
|
||||
prop="monitorStoreDays"
|
||||
>
|
||||
<el-input clearable v-model.number="form.monitorStoreDays">
|
||||
<el-form-item :label="$t('setting.storeDays')" prop="monitorStoreDays">
|
||||
<el-input disabled v-model="form.monitorStoreDays">
|
||||
<template #append>
|
||||
<el-button
|
||||
@click="onSave(panelFormRef, 'MonitorStoreDays', form.monitorStoreDays)"
|
||||
icon="Collection"
|
||||
>
|
||||
{{ $t('commons.button.save') }}
|
||||
<el-button @click="onChangeStoreDays" icon="Setting">
|
||||
{{ $t('commons.button.set') }}
|
||||
</el-button>
|
||||
</template>
|
||||
</el-input>
|
||||
</el-form-item>
|
||||
<el-form-item :label="$t('monitor.interval')" prop="monitorInterval">
|
||||
<el-input disabled v-model="form.monitorInterval">
|
||||
<template #append>
|
||||
<el-button @click="onChangeInterval" icon="Setting">
|
||||
{{ $t('commons.button.set') }}
|
||||
</el-button>
|
||||
</template>
|
||||
</el-input>
|
||||
@ -44,16 +42,19 @@
|
||||
</el-form>
|
||||
</template>
|
||||
</LayoutContent>
|
||||
|
||||
<Interval ref="intervalRef" @search="search" />
|
||||
<StoreDays ref="daysRef" @search="search" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { onMounted, reactive, ref } from 'vue';
|
||||
import { FormInstance } from 'element-plus';
|
||||
import { ElMessageBox, FormInstance } from 'element-plus';
|
||||
import { cleanMonitors, getSettingInfo, getSystemAvailable, updateSetting } from '@/api/modules/setting';
|
||||
import MonitorRouter from '@/views/host/monitor/index.vue';
|
||||
import { useDeleteData } from '@/hooks/use-delete-data';
|
||||
import { Rules, checkNumberRange } from '@/global/form-rules';
|
||||
import Interval from '@/views/host/monitor/setting/interval/index.vue';
|
||||
import StoreDays from '@/views/host/monitor/setting/days/index.vue';
|
||||
import i18n from '@/lang';
|
||||
import { MsgSuccess } from '@/utils/message';
|
||||
|
||||
@ -61,32 +62,24 @@ const loading = ref();
|
||||
const form = reactive({
|
||||
monitorStatus: 'disable',
|
||||
monitorStoreDays: 30,
|
||||
monitorInterval: 1,
|
||||
});
|
||||
const panelFormRef = ref<FormInstance>();
|
||||
|
||||
const intervalRef = ref();
|
||||
const daysRef = ref();
|
||||
|
||||
const search = async () => {
|
||||
const res = await getSettingInfo();
|
||||
form.monitorStatus = res.data.monitorStatus;
|
||||
form.monitorInterval = Number(res.data.monitorInterval);
|
||||
form.monitorStoreDays = Number(res.data.monitorStoreDays);
|
||||
};
|
||||
|
||||
const onSave = async (formEl: FormInstance | undefined, key: string, val: any) => {
|
||||
if (!formEl) return;
|
||||
const result = await formEl.validateField(key.replace(key[0], key[0].toLowerCase()), callback);
|
||||
if (!result) {
|
||||
return;
|
||||
}
|
||||
if (val === '') {
|
||||
return;
|
||||
}
|
||||
switch (key) {
|
||||
case 'MonitorStoreDays':
|
||||
val = val + '';
|
||||
break;
|
||||
}
|
||||
const onSaveStatus = async () => {
|
||||
let param = {
|
||||
key: key,
|
||||
value: val + '',
|
||||
key: 'MonitorStatus',
|
||||
value: form.monitorStatus,
|
||||
};
|
||||
loading.value = true;
|
||||
await updateSetting(param)
|
||||
@ -98,16 +91,22 @@ const onSave = async (formEl: FormInstance | undefined, key: string, val: any) =
|
||||
loading.value = false;
|
||||
});
|
||||
};
|
||||
function callback(error: any) {
|
||||
if (error) {
|
||||
return error.message;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const onChangeStoreDays = () => {
|
||||
daysRef.value.acceptParams({ monitorStoreDays: form.monitorStoreDays });
|
||||
};
|
||||
const onChangeInterval = () => {
|
||||
intervalRef.value.acceptParams({ monitorInterval: form.monitorInterval });
|
||||
};
|
||||
|
||||
const onClean = async () => {
|
||||
await useDeleteData(cleanMonitors, {}, 'commons.msg.delete');
|
||||
ElMessageBox.confirm(i18n.global.t('commons.msg.clean'), i18n.global.t('setting.cleanMonitor'), {
|
||||
confirmButtonText: i18n.global.t('commons.button.confirm'),
|
||||
cancelButtonText: i18n.global.t('commons.button.cancel'),
|
||||
type: 'info',
|
||||
}).then(async () => {
|
||||
await cleanMonitors();
|
||||
});
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
|
89
frontend/src/views/host/monitor/setting/interval/index.vue
Normal file
89
frontend/src/views/host/monitor/setting/interval/index.vue
Normal file
@ -0,0 +1,89 @@
|
||||
<template>
|
||||
<div>
|
||||
<el-drawer
|
||||
v-model="drawerVisiable"
|
||||
:destroy-on-close="true"
|
||||
@close="handleClose"
|
||||
:close-on-click-modal="false"
|
||||
size="30%"
|
||||
>
|
||||
<template #header>
|
||||
<DrawerHeader :header="$t('monitor.interval')" :back="handleClose" />
|
||||
</template>
|
||||
<el-form ref="formRef" label-position="top" :model="form" @submit.prevent v-loading="loading">
|
||||
<el-row type="flex" justify="center">
|
||||
<el-col :span="22">
|
||||
<el-form-item
|
||||
:label="$t('monitor.interval')"
|
||||
:rules="[Rules.integerNumber, checkNumberRange(1, 60)]"
|
||||
prop="monitorInterval"
|
||||
>
|
||||
<el-input clearable v-model.number="form.monitorInterval" />
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
</el-row>
|
||||
</el-form>
|
||||
<template #footer>
|
||||
<span class="dialog-footer">
|
||||
<el-button @click="drawerVisiable = false">{{ $t('commons.button.cancel') }}</el-button>
|
||||
<el-button :disabled="loading" type="primary" @click="onSave(formRef)">
|
||||
{{ $t('commons.button.confirm') }}
|
||||
</el-button>
|
||||
</span>
|
||||
</template>
|
||||
</el-drawer>
|
||||
</div>
|
||||
</template>
|
||||
<script lang="ts" setup>
|
||||
import { reactive, ref } from 'vue';
|
||||
import i18n from '@/lang';
|
||||
import { MsgSuccess } from '@/utils/message';
|
||||
import { FormInstance } from 'element-plus';
|
||||
import { Rules, checkNumberRange } from '@/global/form-rules';
|
||||
import { updateSetting } from '@/api/modules/setting';
|
||||
|
||||
const emit = defineEmits<{ (e: 'search'): void }>();
|
||||
|
||||
interface DialogProps {
|
||||
monitorInterval: number;
|
||||
}
|
||||
const drawerVisiable = ref();
|
||||
const loading = ref();
|
||||
|
||||
const form = reactive({
|
||||
monitorInterval: 1,
|
||||
});
|
||||
|
||||
const formRef = ref<FormInstance>();
|
||||
|
||||
const acceptParams = (params: DialogProps): void => {
|
||||
form.monitorInterval = params.monitorInterval;
|
||||
drawerVisiable.value = true;
|
||||
};
|
||||
|
||||
const onSave = async (formEl: FormInstance | undefined) => {
|
||||
if (!formEl) return;
|
||||
formEl.validate(async (valid) => {
|
||||
if (!valid) return;
|
||||
loading.value = true;
|
||||
await updateSetting({ key: 'MonitorInterval', value: form.monitorInterval + '' })
|
||||
.then(() => {
|
||||
loading.value = false;
|
||||
handleClose();
|
||||
emit('search');
|
||||
MsgSuccess(i18n.global.t('commons.msg.operationSuccess'));
|
||||
})
|
||||
.catch(() => {
|
||||
loading.value = false;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const handleClose = () => {
|
||||
drawerVisiable.value = false;
|
||||
};
|
||||
|
||||
defineExpose({
|
||||
acceptParams,
|
||||
});
|
||||
</script>
|
Loading…
x
Reference in New Issue
Block a user