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

View File

@@ -141,10 +141,8 @@ func (s *SysCacheController) ClearCacheKey(c *gin.Context) {
func (s *SysCacheController) ClearCacheSafe(c *gin.Context) { func (s *SysCacheController) ClearCacheSafe(c *gin.Context) {
language := ctx.AcceptLanguage(c) language := ctx.AcceptLanguage(c)
caches := []model.SysCache{ 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_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.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.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.rate_limit"), cachekey.RATE_LIMIT_KEY),
model.NewSysCacheNames(i18n.TKey(language, "cache.name.pwd_err_cnt"), cachekey.PWD_ERR_CNT_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) { func (s *SysJobController) Export(c *gin.Context) {
language := ctx.AcceptLanguage(c) language := ctx.AcceptLanguage(c)
// 查询结果,根据查询条件结果,单页最大值限制 // 查询结果,根据查询条件结果,单页最大值限制
// querys := ctx.BodyJSONMap(c) querys := ctx.BodyJSONMap(c)
// data := s.sysJobService.SelectJobPage(querys) querys["pageNum"] = 1
// if data["total"].(int64) == 0 { querys["pageSize"] = 10000
// // 导出数据记录为空 data := s.sysJobService.SelectJobPage(querys)
// c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty"))) if parse.Number(data["total"]) == 0 {
// return // 导出数据记录为空
// } c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
// rows := data["rows"].([]model.SysJob) return
}
rows := data["rows"].([]model.SysJob)
rows := s.sysJobService.SelectJobList(model.SysJob{}) // rows := s.sysJobService.SelectJobList(model.SysJob{})
if len(rows) <= 0 { if len(rows) <= 0 {
// 导出数据记录为空 // 导出数据记录为空
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty"))) 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) { func (s *SysJobLogController) Export(c *gin.Context) {
language := ctx.AcceptLanguage(c) language := ctx.AcceptLanguage(c)
// 查询结果,根据查询条件结果,单页最大值限制 // 查询结果,根据查询条件结果,单页最大值限制
// querys := ctx.BodyJSONMap(c) querys := ctx.BodyJSONMap(c)
// data := s.sysJobLogService.SelectJobLogPage(querys) querys["pageNum"] = 1
// if data["total"].(int64) == 0 { querys["pageSize"] = 10000
// c.JSON(200, result.ErrMsg("Export data record is empty")) data := s.sysJobLogService.SelectJobLogPage(querys)
// return if parse.Number(data["total"]) == 0 {
// } // 导出数据记录为空
// rows := data["rows"].([]model.SysJobLog) 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 { if len(rows) <= 0 {
// 导出数据记录为空 // 导出数据记录为空
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty"))) c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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