对服务增加基础的数据统计/部分代码分Package

This commit is contained in:
GoEdgeLab
2021-01-25 16:40:03 +08:00
parent f1c325713d
commit 2f9fe99088
106 changed files with 2095 additions and 148 deletions

View File

@@ -0,0 +1,65 @@
package stats
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
)
type ServerClientBrowserMonthlyStatDAO dbs.DAO
func NewServerClientBrowserMonthlyStatDAO() *ServerClientBrowserMonthlyStatDAO {
return dbs.NewDAO(&ServerClientBrowserMonthlyStatDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeServerClientBrowserMonthlyStats",
Model: new(ServerClientBrowserMonthlyStat),
PkName: "id",
},
}).(*ServerClientBrowserMonthlyStatDAO)
}
var SharedServerClientBrowserMonthlyStatDAO *ServerClientBrowserMonthlyStatDAO
func init() {
dbs.OnReady(func() {
SharedServerClientBrowserMonthlyStatDAO = NewServerClientBrowserMonthlyStatDAO()
})
}
// 增加数量
func (this *ServerClientBrowserMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, browserId int64, version string, month string, count int64) error {
if len(month) != 6 {
return errors.New("invalid month '" + month + "'")
}
err := this.Query(tx).
Param("count", count).
InsertOrUpdateQuickly(maps.Map{
"serverId": serverId,
"browserId": browserId,
"version": version,
"month": month,
"count": count,
}, maps.Map{
"count": dbs.SQL("count+:count"),
})
if err != nil {
return err
}
return nil
}
// 查找单页数据
func (this *ServerClientBrowserMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, offset int64, size int64) (result []*ServerClientBrowserMonthlyStat, err error) {
query := this.Query(tx).
Attr("serverId", serverId).
Attr("month", month).
Offset(offset).
Limit(size).
Slice(&result).
Desc("count")
_, err = query.FindAll()
return
}

View File

@@ -0,0 +1,19 @@
package stats
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/dbs"
"testing"
)
func TestServerClientBrowserMonthlyStatDAO_IncreaseMonthlyCount(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
err := SharedServerClientBrowserMonthlyStatDAO.IncreaseMonthlyCount(tx, 1, 1, "202101", 1)
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}

View File

@@ -0,0 +1,24 @@
package stats
// 浏览器统计(按月)
type ServerClientBrowserMonthlyStat struct {
Id uint64 `field:"id"` // ID
ServerId uint32 `field:"serverId"` // 服务ID
BrowserId uint32 `field:"browserId"` // 浏览器ID
Month string `field:"month"` // YYYYMM
Version string `field:"version"` // 主版本号
Count uint64 `field:"count"` // 数量
}
type ServerClientBrowserMonthlyStatOperator struct {
Id interface{} // ID
ServerId interface{} // 服务ID
BrowserId interface{} // 浏览器ID
Month interface{} // YYYYMM
Version interface{} // 主版本号
Count interface{} // 数量
}
func NewServerClientBrowserMonthlyStatOperator() *ServerClientBrowserMonthlyStatOperator {
return &ServerClientBrowserMonthlyStatOperator{}
}

View File

@@ -0,0 +1 @@
package stats

View File

@@ -0,0 +1,65 @@
package stats
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
)
type ServerClientSystemMonthlyStatDAO dbs.DAO
func NewServerClientSystemMonthlyStatDAO() *ServerClientSystemMonthlyStatDAO {
return dbs.NewDAO(&ServerClientSystemMonthlyStatDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeServerClientSystemMonthlyStats",
Model: new(ServerClientSystemMonthlyStat),
PkName: "id",
},
}).(*ServerClientSystemMonthlyStatDAO)
}
var SharedServerClientSystemMonthlyStatDAO *ServerClientSystemMonthlyStatDAO
func init() {
dbs.OnReady(func() {
SharedServerClientSystemMonthlyStatDAO = NewServerClientSystemMonthlyStatDAO()
})
}
// 增加数量
func (this *ServerClientSystemMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, systemId int64, version string, month string, count int64) error {
if len(month) != 6 {
return errors.New("invalid month '" + month + "'")
}
err := this.Query(tx).
Param("count", count).
InsertOrUpdateQuickly(maps.Map{
"serverId": serverId,
"systemId": systemId,
"version": version,
"month": month,
"count": count,
}, maps.Map{
"count": dbs.SQL("count+:count"),
})
if err != nil {
return err
}
return nil
}
// 查找单页数据
func (this *ServerClientSystemMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, offset int64, size int64) (result []*ServerClientSystemMonthlyStat, err error) {
query := this.Query(tx).
Attr("serverId", serverId).
Attr("month", month).
Offset(offset).
Limit(size).
Slice(&result).
Desc("count")
_, err = query.FindAll()
return
}

View File

@@ -0,0 +1,5 @@
package stats
import (
_ "github.com/go-sql-driver/mysql"
)

View File

@@ -0,0 +1,24 @@
package stats
// 操作系统统计(按月)
type ServerClientSystemMonthlyStat struct {
Id uint64 `field:"id"` // ID
ServerId uint32 `field:"serverId"` // 服务ID
SystemId uint32 `field:"systemId"` // 系统ID
Version string `field:"version"` // 主版本号
Month string `field:"month"` // YYYYMM
Count uint64 `field:"count"` // 数量
}
type ServerClientSystemMonthlyStatOperator struct {
Id interface{} // ID
ServerId interface{} // 服务ID
SystemId interface{} // 系统ID
Version interface{} // 主版本号
Month interface{} // YYYYMM
Count interface{} // 数量
}
func NewServerClientSystemMonthlyStatOperator() *ServerClientSystemMonthlyStatOperator {
return &ServerClientSystemMonthlyStatOperator{}
}

View File

@@ -0,0 +1 @@
package stats

View File

@@ -0,0 +1,73 @@
package stats
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
)
type ServerRegionCityMonthlyStatDAO dbs.DAO
func NewServerRegionCityMonthlyStatDAO() *ServerRegionCityMonthlyStatDAO {
return dbs.NewDAO(&ServerRegionCityMonthlyStatDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeServerRegionCityMonthlyStats",
Model: new(ServerRegionCityMonthlyStat),
PkName: "id",
},
}).(*ServerRegionCityMonthlyStatDAO)
}
var SharedServerRegionCityMonthlyStatDAO *ServerRegionCityMonthlyStatDAO
func init() {
dbs.OnReady(func() {
SharedServerRegionCityMonthlyStatDAO = NewServerRegionCityMonthlyStatDAO()
})
}
// 增加数量
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 + "'")
}
err := this.Query(tx).
Param("count", count).
InsertOrUpdateQuickly(maps.Map{
"serverId": serverId,
"cityId": cityId,
"month": month,
"count": count,
}, maps.Map{
"count": dbs.SQL("count+:count"),
})
if err != nil {
return err
}
return nil
}
// 查找单页数据
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).
Attr("month", month).
Offset(offset).
Limit(size).
Slice(&result).
Desc("count")
if countryId > 0 {
query.Where("cityId IN (SELECT id FROM "+regions.SharedRegionCityDAO.Table+" WHERE provinceId IN (SELECT id FROM "+regions.SharedRegionProvinceDAO.Table+" WHERE countryId=:countryId AND state=1) AND state=1)").
Param("countryId", countryId)
}
if provinceId > 0 {
query.Where("cityId IN (SELECT id FROM "+regions.SharedRegionCityDAO.Table+" WHERE provinceId=:provinceId AND state=1)").
Param("provinceId", provinceId)
}
_, err = query.FindAll()
return
}

View File

@@ -0,0 +1,5 @@
package stats
import (
_ "github.com/go-sql-driver/mysql"
)

View File

@@ -0,0 +1,22 @@
package stats
// 服务用户省份分布统计(按天)
type ServerRegionCityMonthlyStat struct {
Id uint64 `field:"id"` // ID
ServerId uint32 `field:"serverId"` // 服务ID
CityId uint32 `field:"cityId"` // 城市ID
Month string `field:"month"` // 月份YYYYMM
Count uint64 `field:"count"` // 数量
}
type ServerRegionCityMonthlyStatOperator struct {
Id interface{} // ID
ServerId interface{} // 服务ID
CityId interface{} // 城市ID
Month interface{} // 月份YYYYMM
Count interface{} // 数量
}
func NewServerRegionCityMonthlyStatOperator() *ServerRegionCityMonthlyStatOperator {
return &ServerRegionCityMonthlyStatOperator{}
}

View File

@@ -0,0 +1 @@
package stats

View File

@@ -0,0 +1,64 @@
package stats
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
)
type ServerRegionCountryMonthlyStatDAO dbs.DAO
func NewServerRegionCountryMonthlyStatDAO() *ServerRegionCountryMonthlyStatDAO {
return dbs.NewDAO(&ServerRegionCountryMonthlyStatDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeServerRegionCountryMonthlyStats",
Model: new(ServerRegionCountryMonthlyStat),
PkName: "id",
},
}).(*ServerRegionCountryMonthlyStatDAO)
}
var SharedServerRegionCountryMonthlyStatDAO *ServerRegionCountryMonthlyStatDAO
func init() {
dbs.OnReady(func() {
SharedServerRegionCountryMonthlyStatDAO = NewServerRegionCountryMonthlyStatDAO()
})
}
// 增加数量
func (this *ServerRegionCountryMonthlyStatDAO) IncreaseMonthlyCount(tx *dbs.Tx, serverId int64, countryId int64, month string, count int64) error {
if len(month) != 6 {
return errors.New("invalid month '" + month + "'")
}
err := this.Query(tx).
Param("count", count).
InsertOrUpdateQuickly(maps.Map{
"serverId": serverId,
"countryId": countryId,
"month": month,
"count": count,
}, maps.Map{
"count": dbs.SQL("count+:count"),
})
if err != nil {
return err
}
return nil
}
// 查找单页数据
func (this *ServerRegionCountryMonthlyStatDAO) ListStats(tx *dbs.Tx, serverId int64, month string, offset int64, size int64) (result []*ServerRegionCountryMonthlyStat, err error) {
query := this.Query(tx).
Attr("serverId", serverId).
Attr("month", month).
Offset(offset).
Limit(size).
Slice(&result).
Desc("count")
_, err = query.FindAll()
return
}

View File

@@ -0,0 +1,5 @@
package stats
import (
_ "github.com/go-sql-driver/mysql"
)

View File

@@ -0,0 +1,22 @@
package stats
// 服务用户区域分布统计(按天)
type ServerRegionCountryMonthlyStat struct {
Id uint64 `field:"id"` // ID
ServerId uint32 `field:"serverId"` // 服务ID
CountryId uint32 `field:"countryId"` // 国家/区域ID
Month string `field:"month"` // 月份YYYYMM
Count uint64 `field:"count"` // 数量
}
type ServerRegionCountryMonthlyStatOperator struct {
Id interface{} // ID
ServerId interface{} // 服务ID
CountryId interface{} // 国家/区域ID
Month interface{} // 月份YYYYMM
Count interface{} // 数量
}
func NewServerRegionCountryMonthlyStatOperator() *ServerRegionCountryMonthlyStatOperator {
return &ServerRegionCountryMonthlyStatOperator{}
}

View File

@@ -0,0 +1 @@
package stats

View File

@@ -0,0 +1,64 @@
package stats
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
)
type ServerRegionProviderMonthlyStatDAO dbs.DAO
func NewServerRegionProviderMonthlyStatDAO() *ServerRegionProviderMonthlyStatDAO {
return dbs.NewDAO(&ServerRegionProviderMonthlyStatDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeServerRegionProviderMonthlyStats",
Model: new(ServerRegionProviderMonthlyStat),
PkName: "id",
},
}).(*ServerRegionProviderMonthlyStatDAO)
}
var SharedServerRegionProviderMonthlyStatDAO *ServerRegionProviderMonthlyStatDAO
func init() {
dbs.OnReady(func() {
SharedServerRegionProviderMonthlyStatDAO = NewServerRegionProviderMonthlyStatDAO()
})
}
// 增加数量
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 + "'")
}
err := this.Query(tx).
Param("count", count).
InsertOrUpdateQuickly(maps.Map{
"serverId": serverId,
"providerId": providerId,
"month": month,
"count": count,
}, maps.Map{
"count": dbs.SQL("count+:count"),
})
if err != nil {
return err
}
return nil
}
// 查找单页数据
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).
Attr("month", month).
Offset(offset).
Limit(size).
Slice(&result).
Desc("count")
_, err = query.FindAll()
return
}

View File

@@ -0,0 +1,5 @@
package stats
import (
_ "github.com/go-sql-driver/mysql"
)

View File

@@ -0,0 +1,22 @@
package stats
// 服务用户省份分布统计(按天)
type ServerRegionProviderMonthlyStat struct {
Id uint64 `field:"id"` // ID
ServerId uint32 `field:"serverId"` // 服务ID
ProviderId uint32 `field:"providerId"` // 运营商ID
Month string `field:"month"` // 月份YYYYMM
Count uint64 `field:"count"` // 数量
}
type ServerRegionProviderMonthlyStatOperator struct {
Id interface{} // ID
ServerId interface{} // 服务ID
ProviderId interface{} // 运营商ID
Month interface{} // 月份YYYYMM
Count interface{} // 数量
}
func NewServerRegionProviderMonthlyStatOperator() *ServerRegionProviderMonthlyStatOperator {
return &ServerRegionProviderMonthlyStatOperator{}
}

View File

@@ -0,0 +1 @@
package stats

View File

@@ -0,0 +1,70 @@
package stats
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
)
type ServerRegionProvinceMonthlyStatDAO dbs.DAO
func NewServerRegionProvinceMonthlyStatDAO() *ServerRegionProvinceMonthlyStatDAO {
return dbs.NewDAO(&ServerRegionProvinceMonthlyStatDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeServerRegionProvinceMonthlyStats",
Model: new(ServerRegionProvinceMonthlyStat),
PkName: "id",
},
}).(*ServerRegionProvinceMonthlyStatDAO)
}
var SharedServerRegionProvinceMonthlyStatDAO *ServerRegionProvinceMonthlyStatDAO
func init() {
dbs.OnReady(func() {
SharedServerRegionProvinceMonthlyStatDAO = NewServerRegionProvinceMonthlyStatDAO()
})
}
// 增加数量
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 + "'")
}
err := this.Query(tx).
Param("count", count).
InsertOrUpdateQuickly(maps.Map{
"serverId": serverId,
"provinceId": provinceId,
"month": month,
"count": count,
}, maps.Map{
"count": dbs.SQL("count+:count"),
})
if err != nil {
return err
}
return nil
}
// 查找单页数据
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).
Attr("month", month).
Offset(offset).
Limit(size).
Slice(&result).
Desc("count")
if countryId > 0 {
query.Where("id IN (SELECT id FROM "+regions.SharedRegionProvinceDAO.Table+" WHERE countryId=:countryId AND state=1)").
Param("countryId", countryId)
}
_, err = query.FindAll()
return
}

View File

@@ -0,0 +1,5 @@
package stats
import (
_ "github.com/go-sql-driver/mysql"
)

View File

@@ -0,0 +1,22 @@
package stats
// 服务用户省份分布统计(按天)
type ServerRegionProvinceMonthlyStat struct {
Id uint64 `field:"id"` // ID
ServerId uint32 `field:"serverId"` // 服务ID
ProvinceId uint32 `field:"provinceId"` // 省份ID
Month string `field:"month"` // 月份YYYYMM
Count uint64 `field:"count"` // 数量
}
type ServerRegionProvinceMonthlyStatOperator struct {
Id interface{} // ID
ServerId interface{} // 服务ID
ProvinceId interface{} // 省份ID
Month interface{} // 月份YYYYMM
Count interface{} // 数量
}
func NewServerRegionProvinceMonthlyStatOperator() *ServerRegionProvinceMonthlyStatOperator {
return &ServerRegionProvinceMonthlyStatOperator{}
}

View File

@@ -0,0 +1 @@
package stats