This commit is contained in:
robin
2026-03-13 14:25:13 +08:00
parent a25a474d6a
commit afbaaa869c
95 changed files with 4591 additions and 2578 deletions

View File

@@ -0,0 +1,138 @@
package models
import (
"github.com/iwind/TeaGo/dbs"
)
type HTTPDNSAccessLogHourlyStat struct {
Hour string `field:"hour"`
CountRequests int64 `field:"countRequests"`
}
type HTTPDNSAccessLogDailyStat struct {
Day string `field:"day"`
CountRequests int64 `field:"countRequests"`
}
type HTTPDNSAccessLogTopAppStat struct {
AppId string `field:"appId"`
AppName string `field:"appName"`
CountRequests int64 `field:"countRequests"`
}
type HTTPDNSAccessLogTopDomainStat struct {
Domain string `field:"domain"`
CountRequests int64 `field:"countRequests"`
}
type HTTPDNSAccessLogTopNodeStat struct {
ClusterId uint32 `field:"clusterId"`
NodeId uint32 `field:"nodeId"`
CountRequests int64 `field:"countRequests"`
}
func (this *HTTPDNSAccessLogDAO) FindHourlyStats(tx *dbs.Tx, hourFrom string, hourTo string) (result []*HTTPDNSAccessLogHourlyStat, err error) {
rows, _, err := this.Query(tx).
Result("CONCAT(day, LPAD(HOUR(FROM_UNIXTIME(createdAt)),2,'0')) AS hour", "COUNT(*) AS countRequests").
Where("CONCAT(day, LPAD(HOUR(FROM_UNIXTIME(createdAt)),2,'0')) BETWEEN :hourFrom AND :hourTo").
Param("hourFrom", hourFrom).
Param("hourTo", hourTo).
Group("hour").
FindOnes()
if err != nil {
return nil, err
}
for _, row := range rows {
result = append(result, &HTTPDNSAccessLogHourlyStat{
Hour: row.GetString("hour"),
CountRequests: row.GetInt64("countRequests"),
})
}
return
}
func (this *HTTPDNSAccessLogDAO) FindDailyStats(tx *dbs.Tx, dayFrom string, dayTo string) (result []*HTTPDNSAccessLogDailyStat, err error) {
rows, _, err := this.Query(tx).
Result("day", "COUNT(*) AS countRequests").
Between("day", dayFrom, dayTo).
Group("day").
FindOnes()
if err != nil {
return nil, err
}
for _, row := range rows {
result = append(result, &HTTPDNSAccessLogDailyStat{
Day: row.GetString("day"),
CountRequests: row.GetInt64("countRequests"),
})
}
return
}
func (this *HTTPDNSAccessLogDAO) ListTopApps(tx *dbs.Tx, dayFrom string, dayTo string, limit int64) (result []*HTTPDNSAccessLogTopAppStat, err error) {
rows, _, err := this.Query(tx).
Result("appId", "MAX(appName) AS appName", "COUNT(*) AS countRequests").
Between("day", dayFrom, dayTo).
Group("appId").
Desc("countRequests").
Limit(limit).
FindOnes()
if err != nil {
return nil, err
}
for _, row := range rows {
result = append(result, &HTTPDNSAccessLogTopAppStat{
AppId: row.GetString("appId"),
AppName: row.GetString("appName"),
CountRequests: row.GetInt64("countRequests"),
})
}
return
}
func (this *HTTPDNSAccessLogDAO) ListTopDomains(tx *dbs.Tx, dayFrom string, dayTo string, limit int64) (result []*HTTPDNSAccessLogTopDomainStat, err error) {
rows, _, err := this.Query(tx).
Result("domain", "COUNT(*) AS countRequests").
Between("day", dayFrom, dayTo).
Where("domain != ''").
Group("domain").
Desc("countRequests").
Limit(limit).
FindOnes()
if err != nil {
return nil, err
}
for _, row := range rows {
result = append(result, &HTTPDNSAccessLogTopDomainStat{
Domain: row.GetString("domain"),
CountRequests: row.GetInt64("countRequests"),
})
}
return
}
func (this *HTTPDNSAccessLogDAO) ListTopNodes(tx *dbs.Tx, dayFrom string, dayTo string, limit int64) (result []*HTTPDNSAccessLogTopNodeStat, err error) {
rows, _, err := this.Query(tx).
Result("MIN(clusterId) AS clusterId", "nodeId", "COUNT(*) AS countRequests").
Between("day", dayFrom, dayTo).
Group("nodeId").
Desc("countRequests").
Limit(limit).
FindOnes()
if err != nil {
return nil, err
}
for _, row := range rows {
result = append(result, &HTTPDNSAccessLogTopNodeStat{
ClusterId: row.GetUint32("clusterId"),
NodeId: row.GetUint32("nodeId"),
CountRequests: row.GetInt64("countRequests"),
})
}
return
}

View File

@@ -113,3 +113,11 @@ func (this *HTTPDNSDomainDAO) ListEnabledDomainsWithAppId(tx *dbs.Tx, appDbId in
_, err = query.Slice(&result).FindAll()
return
}
func (this *HTTPDNSDomainDAO) CountEnabledDomains(tx *dbs.Tx, appDbId int64) (int64, error) {
query := this.Query(tx).State(HTTPDNSDomainStateEnabled)
if appDbId > 0 {
query = query.Attr("appId", appDbId)
}
return query.Count()
}

View File

@@ -221,6 +221,41 @@ func (this *NodeValueDAO) ListValuesForNSNodes(tx *dbs.Tx, item string, key stri
return
}
// ListValuesForHTTPDNSNodes 列出HTTPDNS节点相关的平均数据
func (this *NodeValueDAO) ListValuesForHTTPDNSNodes(tx *dbs.Tx, item string, key string, timeRange nodeconfigs.NodeValueRange) (result []*NodeValue, err error) {
query := this.Query(tx).
Attr("role", "httpdns").
Attr("item", item).
Result("AVG(JSON_EXTRACT(value, '$." + key + "')) AS value, MIN(createdAt) AS createdAt")
switch timeRange {
// TODO 支持更多的时间范围
case nodeconfigs.NodeValueRangeMinute:
fromMinute := timeutil.FormatTime("YmdHi", time.Now().Unix()-3600) // 一个小时之前的
query.Gte("minute", fromMinute)
query.Result("minute")
query.Group("minute")
default:
err = errors.New("invalid 'range' value: '" + timeRange + "'")
return
}
_, err = query.Slice(&result).
FindAll()
if err != nil {
return nil, err
}
for _, nodeValue := range result {
nodeValue.Value, _ = json.Marshal(maps.Map{
key: types.Float32(string(nodeValue.Value)),
})
}
return
}
// SumAllNodeValues 计算所有节点的某项参数值
func (this *NodeValueDAO) SumAllNodeValues(tx *dbs.Tx, role string, item nodeconfigs.NodeValueItem, param string, duration int32, durationUnit nodeconfigs.NodeValueDurationUnit) (total float64, avg float64, max float64, err error) {
if duration <= 0 {