fix: 根据查询条件导出Excel

This commit is contained in:
TsMask
2024-08-31 14:32:09 +08:00
parent bf0dc1a1e9
commit da0cd8bb68
11 changed files with 94 additions and 113 deletions

View File

@@ -28,21 +28,14 @@ type MonitorController struct {
func (s *MonitorController) Load(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var querys struct {
// 数据类型all/load/cpu/memory/io/network
Type string `form:"type" binding:"required,oneof=all load cpu memory io network"`
// 开始时间
StartTime int64 `form:"startTime" binding:"required"`
// 结束时间
EndTime int64 `form:"endTime" binding:"required"`
// 网元类型
NeType string `form:"neType"`
// 网元ID
NeID string `form:"neId"`
// 名称networ和iok时有效
Name string `form:"name"`
Type string `form:"type" binding:"required,oneof=all load cpu memory io network"` // 数据类型all/load/cpu/memory/io/network
StartTime int64 `form:"startTime" binding:"required"` // 开始时间
EndTime int64 `form:"endTime" binding:"required"` // 结束时间
NeType string `form:"neType"` // 网元类型
NeID string `form:"neId"` // 网元ID
Name string `form:"name"` // 名称networ和io时有效
}
err := c.ShouldBindQuery(&querys)
if err != nil {
if err := c.ShouldBindQuery(&querys); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}

View File

@@ -141,10 +141,8 @@ func (s *SysCacheController) ClearCacheKey(c *gin.Context) {
func (s *SysCacheController) ClearCacheSafe(c *gin.Context) {
language := ctx.AcceptLanguage(c)
caches := []model.SysCache{
model.NewSysCacheNames(i18n.TKey(language, "cache.name.user"), cachekey.LOGIN_TOKEN_KEY),
model.NewSysCacheNames(i18n.TKey(language, "cache.name.sys_config"), cachekey.SYS_CONFIG_KEY),
model.NewSysCacheNames(i18n.TKey(language, "cache.name.sys_dict"), cachekey.SYS_DICT_KEY),
model.NewSysCacheNames(i18n.TKey(language, "cache.name.captcha_codes"), cachekey.CAPTCHA_CODE_KEY),
model.NewSysCacheNames(i18n.TKey(language, "cache.name.repeat_submit"), cachekey.REPEAT_SUBMIT_KEY),
model.NewSysCacheNames(i18n.TKey(language, "cache.name.rate_limit"), cachekey.RATE_LIMIT_KEY),
model.NewSysCacheNames(i18n.TKey(language, "cache.name.pwd_err_cnt"), cachekey.PWD_ERR_CNT_KEY),

View File

@@ -319,16 +319,18 @@ func (s *SysJobController) ResetQueueJob(c *gin.Context) {
func (s *SysJobController) Export(c *gin.Context) {
language := ctx.AcceptLanguage(c)
// 查询结果,根据查询条件结果,单页最大值限制
// querys := ctx.BodyJSONMap(c)
// data := s.sysJobService.SelectJobPage(querys)
// if data["total"].(int64) == 0 {
// // 导出数据记录为空
// c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
// return
// }
// rows := data["rows"].([]model.SysJob)
querys := ctx.BodyJSONMap(c)
querys["pageNum"] = 1
querys["pageSize"] = 10000
data := s.sysJobService.SelectJobPage(querys)
if parse.Number(data["total"]) == 0 {
// 导出数据记录为空
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
return
}
rows := data["rows"].([]model.SysJob)
rows := s.sysJobService.SelectJobList(model.SysJob{})
// rows := s.sysJobService.SelectJobList(model.SysJob{})
if len(rows) <= 0 {
// 导出数据记录为空
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))

View File

@@ -131,15 +131,18 @@ func (s *SysJobLogController) Clean(c *gin.Context) {
func (s *SysJobLogController) Export(c *gin.Context) {
language := ctx.AcceptLanguage(c)
// 查询结果,根据查询条件结果,单页最大值限制
// querys := ctx.BodyJSONMap(c)
// data := s.sysJobLogService.SelectJobLogPage(querys)
// if data["total"].(int64) == 0 {
// c.JSON(200, result.ErrMsg("Export data record is empty"))
// return
// }
// rows := data["rows"].([]model.SysJobLog)
querys := ctx.BodyJSONMap(c)
querys["pageNum"] = 1
querys["pageSize"] = 10000
data := s.sysJobLogService.SelectJobLogPage(querys)
if parse.Number(data["total"]) == 0 {
// 导出数据记录为空
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
return
}
rows := data["rows"].([]model.SysJobLog)
rows := s.sysJobLogService.SelectJobLogList(model.SysJobLog{})
// rows := s.sysJobLogService.SelectJobLogList(model.SysJobLog{})
if len(rows) <= 0 {
// 导出数据记录为空
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))

View File

@@ -24,12 +24,13 @@ type SystemInfoController struct {
//
// GET /
func (s *SystemInfoController) Info(c *gin.Context) {
c.JSON(200, result.OkData(map[string]any{
data := map[string]any{
"cpu": s.systemInfogService.CPUInfo(),
"memory": s.systemInfogService.MemoryInfo(),
"network": s.systemInfogService.NetworkInfo(),
"time": s.systemInfogService.TimeInfo(),
"system": s.systemInfogService.SystemInfo(),
"disk": s.systemInfogService.DiskInfo(),
}))
}
c.JSON(200, result.OkData(data))
}

View File

@@ -2,28 +2,19 @@ package model
// MonitorBase 监控_基本信息 monitor_base
type MonitorBase struct {
// id
ID int64 `json:"id" gorm:"primaryKey"`
// 创建时间
CreateTime int64 `json:"createTime"`
// cpu使用
CPU float64 `json:"cpu"`
// cpu平均使用率
LoadUsage float64 `json:"loadUsage"`
// cpu使用1分钟
CPULoad1 float64 `json:"cpuLoad1"`
// cpu使用5分钟
CPULoad5 float64 `json:"cpuLoad5"`
// cpu使用15分钟
CPULoad15 float64 `json:"cpuLoad15"`
// 内存使用率
Memory float64 `json:"memory"`
// 网元ID
NeType string `json:"neType"`
// 网元类型
NeID string `json:"neId"`
ID int64 `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
CreateTime int64 `json:"createTime" gorm:"create_time"` // 创建时间
CPU float64 `json:"cpu" gorm:"cpu"` // cpu使用率
LoadUsage float64 `json:"loadUsage" gorm:"load_usage"` // cpu平均使用率
CPULoad1 float64 `json:"cpuLoad1" gorm:"cpu_load1"` // cpu使用1分钟
CPULoad5 float64 `json:"cpuLoad5" gorm:"cpu_load5"` // cpu使用5分钟
CPULoad15 float64 `json:"cpuLoad15" gorm:"cpu_load15"` // cpu使用15分钟
Memory float64 `json:"memory" gorm:"memory"` // 内存使用率
NeType string `json:"neType" gorm:"ne_type"` // 网元类型
NeID string `json:"neId" gorm:"ne_id"` // 网元ID
}
func (MonitorBase) TableName() string {
// TableName 表名称
func (*MonitorBase) TableName() string {
return "monitor_base"
}

View File

@@ -2,26 +2,18 @@ package model
// MonitorIO 监控_磁盘IO monitor_io
type MonitorIO struct {
// id
ID int64 `json:"id" gorm:"primaryKey"`
// 创建时间
CreateTime int64 `json:"createTime"`
// 磁盘名
Name string `json:"name"`
// 读取K
Read int64 `json:"read"`
// 写入K
Write int64 `json:"write"`
// 次数
Count int64 `json:"count"`
// 耗时
Time int64 `json:"time"`
// 网元ID
NeType string `json:"neType"`
// 网元类型
NeID string `json:"neId"`
ID int64 `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
CreateTime int64 `json:"createTime" gorm:"create_time"` // 创建时间
Name string `json:"name" gorm:"name"` // 磁盘名
Read int64 `json:"read" gorm:"read"` // 读取K
Write int64 `json:"write" gorm:"write"` // 写入K
Count int64 `json:"count" gorm:"count"` // 读写次数
Time int64 `json:"time" gorm:"time"` // 读写延迟
NeType string `json:"neType" gorm:"ne_type"` // 网元类型
NeID string `json:"neId" gorm:"ne_id"` // 网元ID
}
func (MonitorIO) TableName() string {
// TableName 表名称
func (*MonitorIO) TableName() string {
return "monitor_io"
}

View File

@@ -2,22 +2,16 @@ package model
// MonitorNetwork 监控_网络IO monitor_network
type MonitorNetwork struct {
// id
ID int64 `json:"id" gorm:"primaryKey"`
// 创建时间
CreateTime int64 `json:"createTime"`
// 网卡名
Name string `json:"name"`
// 上行
Up float64 `json:"up"`
// 下行
Down float64 `json:"down"`
// 网元ID 本机#号
NeType string `json:"neType"`
// 网元类型 本机#号
NeID string `json:"neId"`
ID int64 `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
CreateTime int64 `json:"createTime" gorm:"create_time"` // 创建时间
Name string `json:"name" gorm:"name"` // 网卡名
Up float64 `json:"up" gorm:"up"` // 上行
Down float64 `json:"down" gorm:"down"` // 下行
NeType string `json:"neType" gorm:"ne_type"` // 网元类型
NeID string `json:"neId" gorm:"ne_id"` // 网元ID
}
func (MonitorNetwork) TableName() string {
// TableName 表名称
func (*MonitorNetwork) TableName() string {
return "monitor_network"
}

View File

@@ -10,11 +10,11 @@ import (
"nms_cxy/src/modules/monitor/repository"
systemService "nms_cxy/src/modules/system/service"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/load"
"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/v3/net"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/disk"
"github.com/shirou/gopsutil/v4/load"
"github.com/shirou/gopsutil/v4/mem"
"github.com/shirou/gopsutil/v4/net"
)
// 实例化服务层 MonitorImpl 结构体
@@ -42,17 +42,20 @@ func (s *MonitorImpl) RunMonitor() {
itemBase.CreateTime = time.Now().UnixMilli()
itemBase.NeType = "#"
itemBase.NeID = "#"
loadInfo, _ := load.Avg()
itemBase.CPULoad1 = loadInfo.Load1
itemBase.CPULoad5 = loadInfo.Load5
itemBase.CPULoad15 = loadInfo.Load15
totalPercent, _ := cpu.Percent(3*time.Second, false)
if len(totalPercent) == 1 {
itemBase.CPU = totalPercent[0]
}
cpuCount, _ := cpu.Counts(false)
loadInfo, _ := load.Avg()
itemBase.CPULoad1 = loadInfo.Load1
itemBase.CPULoad5 = loadInfo.Load5
itemBase.CPULoad15 = loadInfo.Load15
itemBase.LoadUsage = loadInfo.Load1 / (float64(cpuCount*2) * 0.75) * 100
cpuAvg := (float64(cpuCount*2) * 0.75) * 100
itemBase.LoadUsage = 0
if cpuAvg > 0 {
itemBase.LoadUsage = loadInfo.Load1 / cpuAvg
}
memoryInfo, _ := mem.VirtualMemory()
itemBase.Memory = memoryInfo.UsedPercent

View File

@@ -5,11 +5,11 @@ import (
"testing"
"time"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/load"
"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/v3/net"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/disk"
"github.com/shirou/gopsutil/v4/load"
"github.com/shirou/gopsutil/v4/mem"
"github.com/shirou/gopsutil/v4/net"
)
func init() {

View File

@@ -1,6 +1,7 @@
package service
import (
"context"
"fmt"
"os"
"runtime"
@@ -10,11 +11,11 @@ import (
"nms_cxy/src/framework/config"
"nms_cxy/src/framework/utils/parse"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/v3/net"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/disk"
"github.com/shirou/gopsutil/v4/host"
"github.com/shirou/gopsutil/v4/mem"
"github.com/shirou/gopsutil/v4/net"
)
// 实例化服务层 SystemInfoImpl 结构体
@@ -150,9 +151,12 @@ func (s *SystemInfoImpl) NetworkInfo() map[string]string {
// DiskInfo 磁盘信息
func (s *SystemInfoImpl) DiskInfo() []map[string]string {
disks := make([]map[string]string, 0)
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
partitions, err := disk.Partitions(false)
if err != nil {
partitions, err := disk.PartitionsWithContext(ctx, false)
if err != nil && err != context.DeadlineExceeded {
return disks
}