对服务增加基础的数据统计/部分代码分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

@@ -1,4 +1,4 @@
package models
package acme
import (
_ "github.com/go-sql-driver/mysql"

View File

@@ -1,4 +1,4 @@
package models
package acme
import (
_ "github.com/go-sql-driver/mysql"

View File

@@ -1,4 +1,4 @@
package models
package acme
// ACME认证
type ACMEAuthentication struct {

View File

@@ -0,0 +1 @@
package acme

View File

@@ -1,8 +1,11 @@
package models
package acme
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/acme"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
@@ -97,14 +100,14 @@ func (this *ACMETaskDAO) DisableAllTasksWithCertId(tx *dbs.Tx, certId int64) err
// 计算所有任务数量
func (this *ACMETaskDAO) CountAllEnabledACMETasks(tx *dbs.Tx, adminId int64, userId int64) (int64, error) {
return NewQuery(tx, this, adminId, userId).
return dbutils.NewQuery(tx, this, adminId, userId).
State(ACMETaskStateEnabled).
Count()
}
// 列出单页任务
func (this *ACMETaskDAO) ListEnabledACMETasks(tx *dbs.Tx, adminId int64, userId int64, offset int64, size int64) (result []*ACMETask, err error) {
_, err = NewQuery(tx, this, adminId, userId).
_, err = dbutils.NewQuery(tx, this, adminId, userId).
State(ACMETaskStateEnabled).
DescPk().
Offset(offset).
@@ -173,7 +176,7 @@ func (this *ACMETaskDAO) UpdateACMETask(tx *dbs.Tx, acmeTaskId int64, acmeUserId
// 检查权限
func (this *ACMETaskDAO) CheckACMETask(tx *dbs.Tx, adminId int64, userId int64, acmeTaskId int64) (bool, error) {
return NewQuery(tx, this, adminId, userId).
return dbutils.NewQuery(tx, this, adminId, userId).
State(ACMETaskStateEnabled).
Pk(acmeTaskId).
Exist()
@@ -259,7 +262,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
var acmeTask *acme.Task = nil
if task.AuthType == acme.AuthTypeDNS {
// DNS服务商
dnsProvider, err := SharedDNSProviderDAO.FindEnabledDNSProvider(tx, int64(task.DnsProviderId))
dnsProvider, err := dns.SharedDNSProviderDAO.FindEnabledDNSProvider(tx, int64(task.DnsProviderId))
if err != nil {
errMsg = "查找DNS服务商账号信息时出错" + err.Error()
return
@@ -326,7 +329,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
// 保存证书
resultCertId = int64(task.CertId)
if resultCertId > 0 {
cert, err := SharedSSLCertDAO.FindEnabledSSLCert(tx, resultCertId)
cert, err := models.SharedSSLCertDAO.FindEnabledSSLCert(tx, resultCertId)
if err != nil {
errMsg = "证书生成成功,但查询已绑定的证书时出错:" + err.Error()
return
@@ -343,19 +346,19 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
return
}
err = SharedSSLCertDAO.UpdateCert(tx, resultCertId, cert.IsOn == 1, cert.Name, cert.Description, cert.ServerName, cert.IsCA == 1, certData, keyData, sslConfig.TimeBeginAt, sslConfig.TimeEndAt, sslConfig.DNSNames, sslConfig.CommonNames)
err = models.SharedSSLCertDAO.UpdateCert(tx, resultCertId, cert.IsOn == 1, cert.Name, cert.Description, cert.ServerName, cert.IsCA == 1, certData, keyData, sslConfig.TimeBeginAt, sslConfig.TimeEndAt, sslConfig.DNSNames, sslConfig.CommonNames)
if err != nil {
errMsg = "证书生成成功,但是修改数据库中的证书信息时出错:" + err.Error()
return
}
} else {
resultCertId, err = SharedSSLCertDAO.CreateCert(tx, int64(task.AdminId), int64(task.UserId), true, task.DnsDomain+"免费证书", "免费申请的证书", "", false, certData, keyData, sslConfig.TimeBeginAt, sslConfig.TimeEndAt, sslConfig.DNSNames, sslConfig.CommonNames)
resultCertId, err = models.SharedSSLCertDAO.CreateCert(tx, int64(task.AdminId), int64(task.UserId), true, task.DnsDomain+"免费证书", "免费申请的证书", "", false, certData, keyData, sslConfig.TimeBeginAt, sslConfig.TimeEndAt, sslConfig.DNSNames, sslConfig.CommonNames)
if err != nil {
errMsg = "证书生成成功,但是保存到数据库失败:" + err.Error()
return
}
err = SharedSSLCertDAO.UpdateCertACME(tx, resultCertId, int64(task.Id))
err = models.SharedSSLCertDAO.UpdateCertACME(tx, resultCertId, int64(task.Id))
if err != nil {
errMsg = "证书生成成功修改证书ACME信息时出错" + err.Error()
return

View File

@@ -1,4 +1,4 @@
package models
package acme
import (
_ "github.com/go-sql-driver/mysql"

View File

@@ -1,4 +1,4 @@
package models
package acme
import (
_ "github.com/go-sql-driver/mysql"

View File

@@ -1,4 +1,4 @@
package models
package acme
import (
_ "github.com/go-sql-driver/mysql"

View File

@@ -1,4 +1,4 @@
package models
package acme
// ACME任务运行日志
type ACMETaskLog struct {

View File

@@ -0,0 +1 @@
package acme

View File

@@ -1,4 +1,4 @@
package models
package acme
// ACME任务
type ACMETask struct {

View File

@@ -1,4 +1,4 @@
package models
package acme
import (
"encoding/json"

View File

@@ -1,4 +1,4 @@
package models
package acme
import (
"crypto/ecdsa"

View File

@@ -1,4 +1,4 @@
package models
package acme
import (
_ "github.com/go-sql-driver/mysql"

View File

@@ -1,4 +1,4 @@
package models
package acme
//
type ACMEUser struct {

View File

@@ -0,0 +1 @@
package acme

View File

@@ -1 +0,0 @@
package models

View File

@@ -0,0 +1,117 @@
package models
import (
"encoding/json"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
)
const (
ClientBrowserStateEnabled = 1 // 已启用
ClientBrowserStateDisabled = 0 // 已禁用
)
var clientBrowserNameAndIdCacheMap = map[string]int64{}
type ClientBrowserDAO dbs.DAO
func NewClientBrowserDAO() *ClientBrowserDAO {
return dbs.NewDAO(&ClientBrowserDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeClientBrowsers",
Model: new(ClientBrowser),
PkName: "id",
},
}).(*ClientBrowserDAO)
}
var SharedClientBrowserDAO *ClientBrowserDAO
func init() {
dbs.OnReady(func() {
SharedClientBrowserDAO = NewClientBrowserDAO()
})
}
// 启用条目
func (this *ClientBrowserDAO) EnableClientBrowser(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx).
Pk(id).
Set("state", ClientBrowserStateEnabled).
Update()
return err
}
// 禁用条目
func (this *ClientBrowserDAO) DisableClientBrowser(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx).
Pk(id).
Set("state", ClientBrowserStateDisabled).
Update()
return err
}
// 查找启用中的条目
func (this *ClientBrowserDAO) FindEnabledClientBrowser(tx *dbs.Tx, id int64) (*ClientBrowser, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", ClientBrowserStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*ClientBrowser), err
}
// 根据主键查找名称
func (this *ClientBrowserDAO) FindClientBrowserName(tx *dbs.Tx, id uint32) (string, error) {
return this.Query(tx).
Pk(id).
Result("name").
FindStringCol("")
}
// 根据浏览器名称查找浏览器ID
func (this *ClientBrowserDAO) FindBrowserIdWithNameCacheable(tx *dbs.Tx, browserName string) (int64, error) {
SharedCacheLocker.RLock()
browserId, ok := clientBrowserNameAndIdCacheMap[browserName]
if ok {
SharedCacheLocker.RUnlock()
return browserId, nil
}
SharedCacheLocker.RUnlock()
browserId, err := this.Query(tx).
Where("JSON_CONTAINS(codes, :browserName)").
Param("browserName", "\""+browserName+"\""). // 查询的需要是个JSON字符串所以这里加双引号
ResultPk().
FindInt64Col(0)
if err != nil {
return 0, err
}
if browserId > 0 {
// 只有找到的时候才放入缓存,以便于我们可以在不存在的时候创建一条新的记录
SharedCacheLocker.Lock()
clientBrowserNameAndIdCacheMap[browserName] = browserId
SharedCacheLocker.Unlock()
}
return browserId, nil
}
// 创建浏览器
func (this *ClientBrowserDAO) CreateBrowser(tx *dbs.Tx, browserName string) (int64, error) {
op := NewClientBrowserOperator()
op.Name = browserName
codes := []string{browserName}
codesJSON, err := json.Marshal(codes)
if err != nil {
return 0, err
}
op.Codes = codesJSON
op.State = ClientBrowserStateEnabled
return this.SaveInt64(tx, op)
}

View File

@@ -0,0 +1,20 @@
package models
// 终端浏览器信息
type ClientBrowser struct {
Id uint32 `field:"id"` // ID
Name string `field:"name"` // 浏览器名称
Codes string `field:"codes"` // 代号
State uint8 `field:"state"` // 状态
}
type ClientBrowserOperator struct {
Id interface{} // ID
Name interface{} // 浏览器名称
Codes interface{} // 代号
State interface{} // 状态
}
func NewClientBrowserOperator() *ClientBrowserOperator {
return &ClientBrowserOperator{}
}

View File

@@ -0,0 +1,119 @@
package models
import (
"encoding/json"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
)
const (
ClientSystemStateEnabled = 1 // 已启用
ClientSystemStateDisabled = 0 // 已禁用
)
var clientSystemNameAndIdCacheMap = map[string]int64{} // system name => id
type ClientSystemDAO dbs.DAO
func NewClientSystemDAO() *ClientSystemDAO {
return dbs.NewDAO(&ClientSystemDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeClientSystems",
Model: new(ClientSystem),
PkName: "id",
},
}).(*ClientSystemDAO)
}
var SharedClientSystemDAO *ClientSystemDAO
func init() {
dbs.OnReady(func() {
SharedClientSystemDAO = NewClientSystemDAO()
})
}
// 启用条目
func (this *ClientSystemDAO) EnableClientSystem(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx).
Pk(id).
Set("state", ClientSystemStateEnabled).
Update()
return err
}
// 禁用条目
func (this *ClientSystemDAO) DisableClientSystem(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx).
Pk(id).
Set("state", ClientSystemStateDisabled).
Update()
return err
}
// 查找启用中的条目
func (this *ClientSystemDAO) FindEnabledClientSystem(tx *dbs.Tx, id int64) (*ClientSystem, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", ClientSystemStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*ClientSystem), err
}
// 根据主键查找名称
func (this *ClientSystemDAO) FindClientSystemName(tx *dbs.Tx, id uint32) (string, error) {
return this.Query(tx).
Pk(id).
Result("name").
FindStringCol("")
}
// 根据操作系统名称查找系统ID
func (this *ClientSystemDAO) FindSystemIdWithNameCacheable(tx *dbs.Tx, systemName string) (int64, error) {
SharedCacheLocker.RLock()
systemId, ok := clientSystemNameAndIdCacheMap[systemName]
if ok {
SharedCacheLocker.RUnlock()
return systemId, nil
}
SharedCacheLocker.RUnlock()
systemId, err := this.Query(tx).
Where("JSON_CONTAINS(codes, :systemName)").
Param("systemName", "\""+systemName+"\""). // 查询的需要是个JSON字符串所以这里加双引号
ResultPk().
FindInt64Col(0)
if err != nil {
return 0, err
}
if systemId > 0 {
// 只有找到的时候才放入缓存,以便于我们可以在不存在的时候创建一条新的记录
SharedCacheLocker.Lock()
clientSystemNameAndIdCacheMap[systemName] = systemId
SharedCacheLocker.Unlock()
}
return systemId, nil
}
// 创建浏览器
func (this *ClientSystemDAO) CreateSystem(tx *dbs.Tx, systemName string) (int64, error) {
op := NewClientSystemOperator()
op.Name = systemName
codes := []string{systemName}
codesJSON, err := json.Marshal(codes)
if err != nil {
return 0, err
}
op.Codes = codesJSON
op.State = ClientSystemStateEnabled
return this.SaveInt64(tx, op)
}

View File

@@ -0,0 +1,20 @@
package models
// 终端操作系统信息
type ClientSystem struct {
Id uint32 `field:"id"` // ID
Name string `field:"name"` // 系统名称
Codes string `field:"codes"` // 代号
State uint8 `field:"state"` //
}
type ClientSystemOperator struct {
Id interface{} // ID
Name interface{} // 系统名称
Codes interface{} // 代号
State interface{} //
}
func NewClientSystemOperator() *ClientSystemOperator {
return &ClientSystemOperator{}
}

View File

@@ -1,4 +1,4 @@
package models
package dns
import (
"encoding/json"

View File

@@ -1,4 +1,4 @@
package models
package dns
import (
_ "github.com/go-sql-driver/mysql"

View File

@@ -1,4 +1,4 @@
package models
package dns
// 管理的域名
type DNSDomain struct {

View File

@@ -1,4 +1,4 @@
package models
package dns
import (
"encoding/json"

View File

@@ -1,6 +1,7 @@
package models
package dns
import (
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
@@ -107,14 +108,14 @@ func (this *DNSProviderDAO) UpdateDNSProvider(tx *dbs.Tx, dnsProviderId int64, n
// 计算服务商数量
func (this *DNSProviderDAO) CountAllEnabledDNSProviders(tx *dbs.Tx, adminId int64, userId int64) (int64, error) {
return NewQuery(tx, this, adminId, userId).
return dbutils.NewQuery(tx, this, adminId, userId).
State(DNSProviderStateEnabled).
Count()
}
// 列出单页服务商
func (this *DNSProviderDAO) ListEnabledDNSProviders(tx *dbs.Tx, adminId int64, userId int64, offset int64, size int64) (result []*DNSProvider, err error) {
_, err = NewQuery(tx, this, adminId, userId).
_, err = dbutils.NewQuery(tx, this, adminId, userId).
State(DNSProviderStateEnabled).
Offset(offset).
Limit(size).
@@ -126,7 +127,7 @@ func (this *DNSProviderDAO) ListEnabledDNSProviders(tx *dbs.Tx, adminId int64, u
// 列出所有服务商
func (this *DNSProviderDAO) FindAllEnabledDNSProviders(tx *dbs.Tx, adminId int64, userId int64) (result []*DNSProvider, err error) {
_, err = NewQuery(tx, this, adminId, userId).
_, err = dbutils.NewQuery(tx, this, adminId, userId).
State(DNSProviderStateEnabled).
DescPk().
Slice(&result).

View File

@@ -1,4 +1,4 @@
package models
package dns
import (
_ "github.com/go-sql-driver/mysql"

View File

@@ -1,4 +1,4 @@
package models
package dns
// DNS服务商
type DNSProvider struct {

View File

@@ -1,4 +1,4 @@
package models
package dns
import (
"encoding/json"

View File

@@ -3,6 +3,7 @@ package models
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
@@ -332,7 +333,7 @@ func (this *NodeClusterDAO) FindAllEnabledClustersWithGrantId(tx *dbs.Tx, grantI
func (this *NodeClusterDAO) CountAllEnabledClustersWithDNSProviderId(tx *dbs.Tx, dnsProviderId int64) (int64, error) {
return this.Query(tx).
State(NodeClusterStateEnabled).
Where("dnsDomainId IN (SELECT id FROM "+SharedDNSDomainDAO.Table+" WHERE state=1 AND providerId=:providerId)").
Where("dnsDomainId IN (SELECT id FROM "+dns.SharedDNSDomainDAO.Table+" WHERE state=1 AND providerId=:providerId)").
Param("providerId", dnsProviderId).
Count()
}
@@ -341,7 +342,7 @@ func (this *NodeClusterDAO) CountAllEnabledClustersWithDNSProviderId(tx *dbs.Tx,
func (this *NodeClusterDAO) FindAllEnabledClustersWithDNSProviderId(tx *dbs.Tx, dnsProviderId int64) (result []*NodeCluster, err error) {
_, err = this.Query(tx).
State(NodeClusterStateEnabled).
Where("dnsDomainId IN (SELECT id FROM "+SharedDNSDomainDAO.Table+" WHERE state=1 AND providerId=:providerId)").
Where("dnsDomainId IN (SELECT id FROM "+dns.SharedDNSDomainDAO.Table+" WHERE state=1 AND providerId=:providerId)").
Param("providerId", dnsProviderId).
Slice(&result).
DescPk().
@@ -462,7 +463,7 @@ func (this *NodeClusterDAO) CheckClusterDNS(tx *dbs.Tx, cluster *NodeCluster) (i
domainId := int64(cluster.DnsDomainId)
// 检查域名
domain, err := SharedDNSDomainDAO.FindEnabledDNSDomain(tx, domainId)
domain, err := dns.SharedDNSDomainDAO.FindEnabledDNSDomain(tx, domainId)
if err != nil {
return nil, err
}

View File

@@ -1 +0,0 @@
package models

View File

@@ -1 +0,0 @@
package models

View File

@@ -1,7 +1,8 @@
package models
package regions
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
@@ -15,6 +16,8 @@ const (
type RegionCityDAO dbs.DAO
var regionCityNameAndIdCacheMap = map[string]int64{} // city name @ province id => id
func NewRegionCityDAO() *RegionCityDAO {
return dbs.NewDAO(&RegionCityDAO{
DAOObject: dbs.DAOObject{
@@ -53,7 +56,7 @@ func (this *RegionCityDAO) DisableRegionCity(tx *dbs.Tx, id uint32) error {
}
// 查找启用中的条目
func (this *RegionCityDAO) FindEnabledRegionCity(tx *dbs.Tx, id uint32) (*RegionCity, error) {
func (this *RegionCityDAO) FindEnabledRegionCity(tx *dbs.Tx, id int64) (*RegionCity, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", RegionCityStateEnabled).
@@ -100,3 +103,31 @@ func (this *RegionCityDAO) CreateCity(tx *dbs.Tx, provinceId int64, name string,
}
return types.Int64(op.Id), nil
}
// 根据城市名查找城市ID
func (this *RegionCityDAO) FindCityIdWithNameCacheable(tx *dbs.Tx, provinceId int64, cityName string) (int64, error) {
key := cityName + "@" + numberutils.FormatInt64(provinceId)
SharedCacheLocker.RLock()
cityId, ok := regionCityNameAndIdCacheMap[key]
if ok {
SharedCacheLocker.RUnlock()
return cityId, nil
}
SharedCacheLocker.RUnlock()
cityId, err := this.Query(tx).
Attr("provinceId", provinceId).
Where("JSON_CONTAINS(codes, :cityName)").
Param("cityName", "\""+cityName+"\""). // 查询的需要是个JSON字符串所以这里加双引号
ResultPk().
FindInt64Col(0)
if err != nil {
return 0, err
}
SharedCacheLocker.Lock()
regionCityNameAndIdCacheMap[key] = cityId
SharedCacheLocker.Unlock()
return cityId, nil
}

View File

@@ -0,0 +1,22 @@
package regions
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/dbs"
"testing"
"time"
)
func TestRegionCityDAO_FindCityIdWithCityNameCacheable(t *testing.T) {
dbs.NotifyReady()
for i := 0; i < 5; i++ {
now := time.Now()
cityId, err := SharedRegionCityDAO.FindCityIdWithNameCacheable(nil, 1, "北京市")
if err != nil {
t.Fatal(err)
}
t.Log("cityId", cityId, time.Since(now).Seconds()*1000, "ms")
}
}

View File

@@ -1,4 +1,4 @@
package models
package regions
//
type RegionCity struct {

View File

@@ -0,0 +1 @@
package regions

View File

@@ -1,4 +1,4 @@
package models
package regions
import (
"encoding/json"
@@ -15,6 +15,8 @@ const (
RegionCountryStateDisabled = 0 // 已禁用
)
var regionCountryNameAndIdCacheMap = map[string]int64{} // country name => int
type RegionCountryDAO dbs.DAO
func NewRegionCountryDAO() *RegionCountryDAO {
@@ -83,8 +85,7 @@ func (this *RegionCountryDAO) FindCountryIdWithDataId(tx *dbs.Tx, dataId string)
}
// 根据国家名查找国家ID
// TODO 加入缓存
func (this *RegionCountryDAO) FindCountryIdWithCountryName(tx *dbs.Tx, countryName string) (int64, error) {
func (this *RegionCountryDAO) FindCountryIdWithName(tx *dbs.Tx, countryName string) (int64, error) {
return this.Query(tx).
Where("JSON_CONTAINS(codes, :countryName)").
Param("countryName", "\""+countryName+"\""). // 查询的需要是个JSON字符串所以这里加双引号
@@ -92,6 +93,28 @@ func (this *RegionCountryDAO) FindCountryIdWithCountryName(tx *dbs.Tx, countryNa
FindInt64Col(0)
}
// 根据国家名查找国家ID并可使用缓存
func (this *RegionCountryDAO) FindCountryIdWithNameCacheable(tx *dbs.Tx, countryName string) (int64, error) {
SharedCacheLocker.RLock()
provinceId, ok := regionCountryNameAndIdCacheMap[countryName]
if ok {
SharedCacheLocker.RUnlock()
return provinceId, nil
}
SharedCacheLocker.RUnlock()
countryId, err := this.FindCountryIdWithName(tx, countryName)
if err != nil {
return 0, err
}
SharedCacheLocker.Lock()
regionCountryNameAndIdCacheMap[countryName] = countryId
SharedCacheLocker.Unlock()
return countryId, nil
}
// 根据数据ID创建国家
func (this *RegionCountryDAO) CreateCountry(tx *dbs.Tx, name string, dataId string) (int64, error) {
op := NewRegionCountryOperator()

View File

@@ -0,0 +1,22 @@
package regions
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/dbs"
"testing"
"time"
)
func TestRegionCountryDAO_FindCountryIdWithCountryNameCacheable(t *testing.T) {
dbs.NotifyReady()
for i := 0; i < 5; i++ {
now := time.Now()
countryId, err := SharedRegionCountryDAO.FindCountryIdWithNameCacheable(nil, "中国")
if err != nil {
t.Fatal(err)
}
t.Log("countryId", countryId, time.Since(now).Seconds()*1000, "ms")
}
}

View File

@@ -1,4 +1,4 @@
package models
package regions
//
type RegionCountry struct {

View File

@@ -1,4 +1,4 @@
package models
package regions
import (
"encoding/json"

View File

@@ -1,6 +1,7 @@
package models
package regions
import (
"encoding/json"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
@@ -11,6 +12,8 @@ const (
RegionProviderStateDisabled = 0 // 已禁用
)
var regionProviderNameAndIdCacheMap = map[string]int64{} // provider name => id
type RegionProviderDAO dbs.DAO
func NewRegionProviderDAO() *RegionProviderDAO {
@@ -69,3 +72,42 @@ func (this *RegionProviderDAO) FindRegionProviderName(tx *dbs.Tx, id uint32) (st
Result("name").
FindStringCol("")
}
// 根据服务商名称查找服务商ID
func (this *RegionProviderDAO) FindProviderIdWithNameCacheable(tx *dbs.Tx, providerName string) (int64, error) {
SharedCacheLocker.RLock()
providerId, ok := regionProviderNameAndIdCacheMap[providerName]
if ok {
SharedCacheLocker.RUnlock()
return providerId, nil
}
SharedCacheLocker.RUnlock()
providerId, err := this.Query(tx).
Where("JSON_CONTAINS(codes, :providerName)").
Param("providerName", "\""+providerName+"\""). // 查询的需要是个JSON字符串所以这里加双引号
ResultPk().
FindInt64Col(0)
if err != nil {
return 0, err
}
SharedCacheLocker.Lock()
regionProviderNameAndIdCacheMap[providerName] = providerId
SharedCacheLocker.Unlock()
return providerId, nil
}
// 创建Provider
func (this *RegionProviderDAO) CreateProvider(tx *dbs.Tx, name string) (int64, error) {
op := NewRegionProviderOperator()
op.Name = name
codesJSON, err := json.Marshal([]string{name})
if err != nil {
return 0, err
}
op.Codes = codesJSON
return this.SaveInt64(tx, op)
}

View File

@@ -0,0 +1,33 @@
package regions
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/dbs"
"testing"
"time"
)
func TestRegionProviderDAO_FindProviderIdWithProviderNameCacheable(t *testing.T) {
dbs.NotifyReady()
for i := 0; i < 5; i++ {
now := time.Now()
providerId, err := SharedRegionProviderDAO.FindProviderIdWithNameCacheable(nil, "电信")
if err != nil {
t.Fatal(err)
}
t.Log("providerId", providerId, time.Since(now).Seconds()*1000, "ms")
}
t.Log("=====")
for i := 0; i < 5; i++ {
now := time.Now()
providerId, err := SharedRegionProviderDAO.FindProviderIdWithNameCacheable(nil, "胡乱填的")
if err != nil {
t.Fatal(err)
}
t.Log("providerId", providerId, time.Since(now).Seconds()*1000, "ms")
}
}

View File

@@ -1,4 +1,4 @@
package models
package regions
//
type RegionProvider struct {

View File

@@ -0,0 +1 @@
package regions

View File

@@ -1,7 +1,8 @@
package models
package regions
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
@@ -13,6 +14,8 @@ const (
RegionProvinceStateDisabled = 0 // 已禁用
)
var regionProvinceNameAndIdCacheMap = map[string]int64{} // province name @ country id => province id
type RegionProvinceDAO dbs.DAO
func NewRegionProvinceDAO() *RegionProvinceDAO {
@@ -81,15 +84,38 @@ func (this *RegionProvinceDAO) FindProvinceIdWithDataId(tx *dbs.Tx, dataId strin
}
// 根据省份名查找省份ID
// TODO 加入缓存
func (this *RegionProvinceDAO) FindProvinceIdWithProvinceName(tx *dbs.Tx, provinceName string) (int64, error) {
func (this *RegionProvinceDAO) FindProvinceIdWithName(tx *dbs.Tx, countryId int64, provinceName string) (int64, error) {
return this.Query(tx).
Attr("countryId", countryId).
Where("JSON_CONTAINS(codes, :provinceName)").
Param("provinceName", "\""+provinceName+"\""). // 查询的需要是个JSON字符串所以这里加双引号
ResultPk().
FindInt64Col(0)
}
// 根据省份名查找省份ID并可使用缓存
func (this *RegionProvinceDAO) FindProvinceIdWithNameCacheable(tx *dbs.Tx, countryId int64, provinceName string) (int64, error) {
var key = provinceName + "@" + numberutils.FormatInt64(countryId)
SharedCacheLocker.RLock()
provinceId, ok := regionProvinceNameAndIdCacheMap[key]
if ok {
SharedCacheLocker.RUnlock()
return provinceId, nil
}
SharedCacheLocker.RUnlock()
provinceId, err := this.FindProvinceIdWithName(tx, countryId, provinceName)
if err != nil {
return 0, err
}
SharedCacheLocker.Lock()
regionProvinceNameAndIdCacheMap[key] = provinceId
SharedCacheLocker.Unlock()
return provinceId, nil
}
// 创建省份
func (this *RegionProvinceDAO) CreateProvince(tx *dbs.Tx, countryId int64, name string, dataId string) (int64, error) {
op := NewRegionProvinceOperator()

View File

@@ -0,0 +1,31 @@
package regions
import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/dbs"
"testing"
"time"
)
func TestRegionProvinceDAO_FindProvinceIdWithProvinceName(t *testing.T) {
dbs.NotifyReady()
for i := 0; i < 5; i++ {
now := time.Now()
provinceId, err := SharedRegionProvinceDAO.FindProvinceIdWithNameCacheable(nil, 1, "安徽省")
if err != nil {
t.Fatal(err)
}
t.Log(provinceId, time.Since(now).Seconds()*1000, "ms")
}
t.Log("====")
for i := 0; i < 5; i++ {
now := time.Now()
provinceId, err := SharedRegionProvinceDAO.FindProvinceIdWithNameCacheable(nil, 2, "安徽省")
if err != nil {
t.Fatal(err)
}
t.Log(provinceId, time.Since(now).Seconds()*1000, "ms")
}
}

View File

@@ -1,4 +1,4 @@
package models
package regions
//
type RegionProvince struct {

View File

@@ -1,4 +1,4 @@
package models
package regions
import (
"encoding/json"

View File

@@ -0,0 +1,5 @@
package regions
import "sync"
var SharedCacheLocker = sync.RWMutex{}

View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
@@ -762,7 +763,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, serverId int64) (*serverc
return nil, err
}
if clusterDNS != nil && clusterDNS.DnsDomainId > 0 {
domain, err := SharedDNSDomainDAO.FindEnabledDNSDomain(tx, int64(clusterDNS.DnsDomainId))
domain, err := dns.SharedDNSDomainDAO.FindEnabledDNSDomain(tx, int64(clusterDNS.DnsDomainId))
if err != nil {
return nil, err
}

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

@@ -1,4 +1,4 @@
package models
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

@@ -1,4 +1,4 @@
package models
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

View File

@@ -0,0 +1,33 @@
package dbutils
import (
"github.com/iwind/TeaGo/dbs"
"sync"
)
var SharedCacheLocker = sync.RWMutex{}
// 处理JSON字节Slice
func JSONBytes(data []byte) []byte {
if len(data) == 0 {
return []byte("null")
}
return data
}
// 判断JSON是否不为空
func IsNotNull(data string) bool {
return len(data) > 0 && data != "null"
}
// 构造Query
func NewQuery(tx *dbs.Tx, dao dbs.DAOWrapper, adminId int64, userId int64) *dbs.Query {
query := dao.Object().Query(tx)
if adminId > 0 {
//query.Attr("adminId", adminId)
}
if userId > 0 {
query.Attr("userId", userId)
}
return query
}