首页图表可查看用户组下的在线数、网络吞吐量

This commit is contained in:
lanrenwo
2022-09-18 16:17:11 +08:00
parent 5586b27319
commit 48bebadc6b
7 changed files with 179 additions and 14 deletions

View File

@@ -32,6 +32,11 @@ type ValData struct {
Note string `json:"note"`
}
type GroupNameId struct {
Id int `json:"id"`
Name string `json:"name"`
}
// type Group struct {
// Id int `json:"id" xorm:"pk autoincr not null"`
// Name string `json:"name" xorm:"varchar(60) not null unique"`
@@ -64,6 +69,20 @@ func GetGroupNames() []string {
return names
}
func GetGroupNamesIds() []GroupNameId {
var datas []Group
err := Find(&datas, 0, 0)
if err != nil {
base.Error(err)
return nil
}
var names []GroupNameId
for _, v := range datas {
names = append(names, GroupNameId{Id: v.Id, Name: v.Name})
}
return names
}
func SetGroup(g *Group) error {
var err error
if g.Name == "" {

View File

@@ -63,4 +63,10 @@ func TestGetGroupNames(t *testing.T) {
for _, v := range gs {
ast.Equal(true, utils.InArrStr(gAll, v))
}
gni := GetGroupNamesIds()
for _, v := range gni {
ast.NotEqual(0, v.Id)
ast.Equal(true, utils.InArrStr(gAll, v.Name))
}
}

View File

@@ -0,0 +1,55 @@
package dbdata
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestStatsInfo(t *testing.T) {
ast := assert.New(t)
preIpData()
defer closeIpdata()
ast.True(StatsInfoIns.ValidAction("online"))
ast.False(StatsInfoIns.ValidAction("diskio"))
ast.True(StatsInfoIns.ValidScope("30d"))
ast.False(StatsInfoIns.ValidScope("60d"))
up := uint32(100)
down := uint32(300)
upGroups := map[int]uint32{1: up}
downGroups := map[int]uint32{1: down}
numGroups := map[int]int{1: 5}
// online
numData, _ := json.Marshal(numGroups)
so := &StatsOnline{Num: 1, NumGroups: string(numData)}
// network
upData, _ := json.Marshal(upGroups)
downData, _ := json.Marshal(downGroups)
sn := &StatsNetwork{Up: up, Down: down, UpGroups: string(upData), DownGroups: string(downData)}
// cpu
sc := &StatsCpu{Percent: 0.3}
// mem
sm := &StatsMem{Percent: 24.50}
StatsInfoIns.SetRealTime("online", so)
StatsInfoIns.GetRealTime("online")
StatsInfoIns.SaveStatsInfo(so, sn, sc, sm)
var err error
_, err = StatsInfoIns.GetData("online", "1h")
ast.Nil(err)
_, err = StatsInfoIns.GetData("network", "1h")
ast.Nil(err)
_, err = StatsInfoIns.GetData("cpu", "1h")
ast.Nil(err)
_, err = StatsInfoIns.GetData("mem", "1h")
ast.Nil(err)
}