Files
EdgeAPI/internal/db/models/server_daily_stat_dao_test.go

110 lines
2.5 KiB
Go
Raw Normal View History

package models
import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/dbs"
2021-06-08 11:18:27 +08:00
"github.com/iwind/TeaGo/logs"
timeutil "github.com/iwind/TeaGo/utils/time"
"testing"
"time"
)
func TestServerDailyStatDAO_SaveStats(t *testing.T) {
2021-01-10 17:34:35 +08:00
var tx *dbs.Tx
stats := []*pb.ServerDailyStat{
{
ServerId: 1,
RegionId: 2,
Bytes: 1,
CreatedAt: 1607671488,
},
}
2021-01-10 17:34:35 +08:00
err := NewServerDailyStatDAO().SaveStats(tx, stats)
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}
func TestServerDailyStatDAO_SaveStats2(t *testing.T) {
2021-01-10 17:34:35 +08:00
var tx *dbs.Tx
stats := []*pb.ServerDailyStat{
{
ServerId: 1,
RegionId: 3,
Bytes: 1,
CreatedAt: 1607671488,
},
}
2021-01-10 17:34:35 +08:00
err := NewServerDailyStatDAO().SaveStats(tx, stats)
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}
func TestServerDailyStatDAO_SumUserMonthly(t *testing.T) {
dbs.NotifyReady()
2021-01-10 17:34:35 +08:00
var tx *dbs.Tx
2022-01-23 19:16:52 +08:00
bytes, err := NewServerDailyStatDAO().SumUserMonthly(tx, 1, timeutil.Format("Ym"))
if err != nil {
t.Fatal(err)
}
t.Log("bytes:", bytes)
}
2021-06-08 11:18:27 +08:00
func TestServerDailyStatDAO_SumHourlyRequests(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
2021-06-08 15:10:08 +08:00
stat, err := NewServerDailyStatDAO().SumHourlyStat(tx, 23, timeutil.Format("YmdH"))
2021-06-08 11:18:27 +08:00
if err != nil {
t.Fatal(err)
}
2021-06-08 15:10:08 +08:00
logs.PrintAsJSON(stat, t)
}
func TestServerDailyStatDAO_SumMinutelyRequests(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
2022-01-23 19:16:52 +08:00
stat, err := NewServerDailyStatDAO().SumMinutelyStat(tx, 23, timeutil.Format("Ymd")+"1435")
2021-06-08 15:10:08 +08:00
if err != nil {
t.Fatal(err)
}
logs.PrintAsJSON(stat, t)
2021-06-08 11:18:27 +08:00
}
2022-01-23 19:16:52 +08:00
func TestServerDailyStatDAO_FindDistinctPlanServerIdsBetweenDay(t *testing.T) {
var tx *dbs.Tx
serverIds, err := NewServerDailyStatDAO().FindDistinctServerIds(tx, timeutil.Format("Ym01"), timeutil.Format("Ymd"))
if err != nil {
t.Fatal(err)
}
t.Log(serverIds)
}
func TestServerDailyStatDAO_FindStatsBetweenDays(t *testing.T) {
var tx *dbs.Tx
2022-10-14 10:03:29 +08:00
stats, err := NewServerDailyStatDAO().FindStatsBetweenDays(tx, 1, 0, 0, timeutil.Format("Ymd", time.Now().AddDate(0, 0, -1)), timeutil.Format("Ymd"))
if err != nil {
t.Fatal(err)
}
for _, stat := range stats {
t.Log(stat.Day, stat.TimeFrom, stat.TimeTo, stat.Bytes)
}
}
2022-10-14 10:03:29 +08:00
func TestServerDailyStatDAO_FindStatsWithDay(t *testing.T) {
var dao = NewServerDailyStatDAO()
var tx *dbs.Tx
stats, err := dao.FindStatsWithDay(tx, 23, timeutil.Format("Ymd"), "000000", "235900")
if err != nil {
t.Fatal(err)
}
for _, stat := range stats {
t.Log(stat.TimeFrom, stat.TimeTo, stat.Bytes)
}
}