增加若干个统计数据清理程序

This commit is contained in:
刘祥超
2021-12-04 11:31:07 +08:00
parent a3a9e726cb
commit ef70d3465d
6 changed files with 143 additions and 6 deletions

View File

@@ -3,12 +3,31 @@ package stats
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/rands"
timeutil "github.com/iwind/TeaGo/utils/time"
"time"
)
func init() {
dbs.OnReadyDone(func() {
// 清理数据任务
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
go func() {
for range ticker.C {
err := SharedServerRegionCityMonthlyStatDAO.Clean(nil)
if err != nil {
remotelogs.Error("SharedServerRegionCityMonthlyStatDAO", "clean expired data failed: "+err.Error())
}
}
}()
})
}
type ServerRegionCityMonthlyStatDAO dbs.DAO
func NewServerRegionCityMonthlyStatDAO() *ServerRegionCityMonthlyStatDAO {
@@ -30,7 +49,7 @@ func init() {
})
}
// 增加数量
// IncreaseMonthlyCount 增加数量
func (this *ServerRegionCityMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, cityId int64, month string, count int64) error {
if len(month) != 6 {
return errors.New("invalid month '" + month + "'")
@@ -51,7 +70,7 @@ func (this *ServerRegionCityMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, ser
return nil
}
// 查找单页数据
// ListStats 查找单页数据
func (this *ServerRegionCityMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, countryId int64, provinceId int64, offset int64, size int64) (result []*ServerRegionCityMonthlyStat, err error) {
query := this.Query(tx).
Attr("serverId", serverId).
@@ -71,3 +90,13 @@ func (this *ServerRegionCityMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64
_, err = query.FindAll()
return
}
// Clean 清理统计数据
func (this *ServerRegionCityMonthlyStatDAO) Clean(tx *dbs.Tx) error {
// 只保留两个月的
var month = timeutil.Format("Ym", time.Now().AddDate(0, -2, 0))
_, err := this.Query(tx).
Lte("month", month).
Delete()
return err
}

View File

@@ -2,4 +2,14 @@ package stats
import (
_ "github.com/go-sql-driver/mysql"
"testing"
)
func TestServerRegionCityMonthlyStatDAO_Clean(t *testing.T) {
var dao = NewServerRegionCityMonthlyStatDAO()
err := dao.Clean(nil)
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}

View File

@@ -2,12 +2,31 @@ package stats
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/rands"
timeutil "github.com/iwind/TeaGo/utils/time"
"time"
)
func init() {
dbs.OnReadyDone(func() {
// 清理数据任务
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
go func() {
for range ticker.C {
err := SharedServerRegionCountryMonthlyStatDAO.Clean(nil)
if err != nil {
remotelogs.Error("SharedServerRegionCountryMonthlyStatDAO", "clean expired data failed: "+err.Error())
}
}
}()
})
}
type ServerRegionCountryMonthlyStatDAO dbs.DAO
func NewServerRegionCountryMonthlyStatDAO() *ServerRegionCountryMonthlyStatDAO {
@@ -62,3 +81,13 @@ func (this *ServerRegionCountryMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId in
_, err = query.FindAll()
return
}
// Clean 清理统计数据
func (this *ServerRegionCountryMonthlyStatDAO) Clean(tx *dbs.Tx) error {
// 只保留两个月的
var month = timeutil.Format("Ym", time.Now().AddDate(0, -2, 0))
_, err := this.Query(tx).
Lte("month", month).
Delete()
return err
}

View File

@@ -2,12 +2,31 @@ package stats
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/rands"
timeutil "github.com/iwind/TeaGo/utils/time"
"time"
)
func init() {
dbs.OnReadyDone(func() {
// 清理数据任务
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
go func() {
for range ticker.C {
err := SharedServerRegionProviderMonthlyStatDAO.Clean(nil)
if err != nil {
remotelogs.Error("SharedServerRegionProviderMonthlyStatDAO", "clean expired data failed: "+err.Error())
}
}
}()
})
}
type ServerRegionProviderMonthlyStatDAO dbs.DAO
func NewServerRegionProviderMonthlyStatDAO() *ServerRegionProviderMonthlyStatDAO {
@@ -29,7 +48,7 @@ func init() {
})
}
// 增加数量
// IncreaseMonthlyCount 增加数量
func (this *ServerRegionProviderMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, providerId int64, month string, count int64) error {
if len(month) != 6 {
return errors.New("invalid month '" + month + "'")
@@ -50,7 +69,7 @@ func (this *ServerRegionProviderMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx,
return nil
}
// 查找单页数据
// ListStats 查找单页数据
func (this *ServerRegionProviderMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, offset int64, size int64) (result []*ServerRegionProviderMonthlyStat, err error) {
query := this.Query(tx).
Attr("serverId", serverId).
@@ -62,3 +81,13 @@ func (this *ServerRegionProviderMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId i
_, err = query.FindAll()
return
}
// Clean 清理统计数据
func (this *ServerRegionProviderMonthlyStatDAO) Clean(tx *dbs.Tx) error {
// 只保留两个月的
var month = timeutil.Format("Ym", time.Now().AddDate(0, -2, 0))
_, err := this.Query(tx).
Lte("month", month).
Delete()
return err
}

View File

@@ -2,4 +2,15 @@ package stats
import (
_ "github.com/go-sql-driver/mysql"
"testing"
)
func TestServerRegionProviderMonthlyStatDAO_Clean(t *testing.T) {
var dao = NewServerRegionProviderMonthlyStatDAO()
err := dao.Clean(nil)
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}

View File

@@ -3,12 +3,31 @@ package stats
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/rands"
timeutil "github.com/iwind/TeaGo/utils/time"
"time"
)
func init() {
dbs.OnReadyDone(func() {
// 清理数据任务
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
go func() {
for range ticker.C {
err := SharedServerRegionProvinceMonthlyStatDAO.Clean(nil)
if err != nil {
remotelogs.Error("SharedServerRegionProvinceMonthlyStatDAO", "clean expired data failed: "+err.Error())
}
}
}()
})
}
type ServerRegionProvinceMonthlyStatDAO dbs.DAO
func NewServerRegionProvinceMonthlyStatDAO() *ServerRegionProvinceMonthlyStatDAO {
@@ -30,7 +49,7 @@ func init() {
})
}
// 增加数量
// IncreaseMonthlyCount 增加数量
func (this *ServerRegionProvinceMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, provinceId int64, month string, count int64) error {
if len(month) != 6 {
return errors.New("invalid month '" + month + "'")
@@ -51,7 +70,7 @@ func (this *ServerRegionProvinceMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx,
return nil
}
// 查找单页数据
// ListStats 查找单页数据
func (this *ServerRegionProvinceMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, countryId int64, offset int64, size int64) (result []*ServerRegionProvinceMonthlyStat, err error) {
query := this.Query(tx).
Attr("serverId", serverId).
@@ -68,3 +87,13 @@ func (this *ServerRegionProvinceMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId i
_, err = query.FindAll()
return
}
// Clean 清理统计数据
func (this *ServerRegionProvinceMonthlyStatDAO) Clean(tx *dbs.Tx) error {
// 只保留两个月的
var month = timeutil.Format("Ym", time.Now().AddDate(0, -2, 0))
_, err := this.Query(tx).
Lte("month", month).
Delete()
return err
}