2021-09-06 08:12:48 +08:00
package models
import (
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
2021-09-08 19:34:31 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/reporterconfigs"
2021-09-06 08:12:48 +08:00
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
2021-09-12 20:21:42 +08:00
"github.com/iwind/TeaGo/types"
2021-09-06 08:12:48 +08:00
"time"
)
type ReportResultDAO dbs . DAO
func NewReportResultDAO ( ) * ReportResultDAO {
return dbs . NewDAO ( & ReportResultDAO {
DAOObject : dbs . DAOObject {
DB : Tea . Env ,
Table : "edgeReportResults" ,
Model : new ( ReportResult ) ,
PkName : "id" ,
} ,
} ) . ( * ReportResultDAO )
}
var SharedReportResultDAO * ReportResultDAO
func init ( ) {
dbs . OnReady ( func ( ) {
SharedReportResultDAO = NewReportResultDAO ( )
} )
}
// UpdateResult 创建结果
2021-09-08 19:34:31 +08:00
func ( this * ReportResultDAO ) UpdateResult ( tx * dbs . Tx , taskType string , targetId int64 , targetDesc string , reportNodeId int64 , level reporterconfigs . ReportLevel , isOk bool , costMs float64 , errString string ) error {
2021-09-06 08:12:48 +08:00
var countUp interface { } = 0
var countDown interface { } = 0
if isOk {
countUp = dbs . SQL ( "countUp+1" )
} else {
countDown = dbs . SQL ( "countDown+1" )
}
return this . Query ( tx ) .
InsertOrUpdateQuickly ( maps . Map {
"type" : taskType ,
"targetId" : targetId ,
"targetDesc" : targetDesc ,
"updatedAt" : time . Now ( ) . Unix ( ) ,
"reportNodeId" : reportNodeId ,
"isOk" : isOk ,
"costMs" : costMs ,
"error" : errString ,
"countUp" : countUp ,
"countDown" : countDown ,
2021-09-08 19:34:31 +08:00
"level" : level ,
2021-09-06 08:12:48 +08:00
} , maps . Map {
"targetDesc" : targetDesc ,
"updatedAt" : time . Now ( ) . Unix ( ) ,
"isOk" : isOk ,
"costMs" : costMs ,
"error" : errString ,
"countUp" : countUp ,
"countDown" : countDown ,
2021-09-08 19:34:31 +08:00
"level" : level ,
2021-09-06 08:12:48 +08:00
} )
}
// CountAllResults 计算结果数量
2021-09-08 19:34:31 +08:00
func ( this * ReportResultDAO ) CountAllResults ( tx * dbs . Tx , reportNodeId int64 , level reporterconfigs . ReportLevel , okState configutils . BoolState ) ( int64 , error ) {
2021-09-06 08:12:48 +08:00
var query = this . Query ( tx ) .
Attr ( "reportNodeId" , reportNodeId )
switch okState {
case configutils . BoolStateYes :
query . Attr ( "isOk" , 1 )
case configutils . BoolStateNo :
query . Attr ( "isOk" , 0 )
}
2021-09-08 19:34:31 +08:00
if len ( level ) > 0 {
query . Attr ( "level" , level )
}
2021-09-06 08:12:48 +08:00
return query .
2021-09-08 19:34:31 +08:00
Gt ( "updatedAt" , time . Now ( ) . Unix ( ) - 600 ) .
2021-09-06 08:12:48 +08:00
Count ( )
}
// ListResults 列出单页结果
2021-09-08 19:34:31 +08:00
func ( this * ReportResultDAO ) ListResults ( tx * dbs . Tx , reportNodeId int64 , okState configutils . BoolState , level reporterconfigs . ReportLevel , offset int64 , size int64 ) ( result [ ] * ReportResult , err error ) {
2021-09-06 08:12:48 +08:00
var query = this . Query ( tx ) .
Attr ( "reportNodeId" , reportNodeId )
switch okState {
case configutils . BoolStateYes :
query . Attr ( "isOk" , 1 )
case configutils . BoolStateNo :
query . Attr ( "isOk" , 0 )
}
2021-09-08 19:34:31 +08:00
if len ( level ) > 0 {
query . Attr ( "level" , level )
}
2021-09-06 08:12:48 +08:00
_ , err = query .
Attr ( "reportNodeId" , reportNodeId ) .
2021-09-08 19:34:31 +08:00
Gt ( "updatedAt" , time . Now ( ) . Unix ( ) - 600 ) .
2021-09-06 08:12:48 +08:00
Offset ( offset ) .
Limit ( size ) .
Desc ( "targetId" ) .
Slice ( & result ) .
FindAll ( )
return
}
2021-09-08 19:34:31 +08:00
// FindAvgCostMsWithTarget 获取某个对象的平均耗时
func ( this * ReportResultDAO ) FindAvgCostMsWithTarget ( tx * dbs . Tx , taskType reporterconfigs . TaskType , targetId int64 ) ( float64 , error ) {
return this . Query ( tx ) .
Attr ( "type" , taskType ) .
Attr ( "targetId" , targetId ) .
Where ( "reportNodeId IN (SELECT id FROM " + SharedReportNodeDAO . Table + " WHERE state=1 AND isOn=1)" ) .
Attr ( "isOk" , true ) .
Gt ( "updatedAt" , time . Now ( ) . Unix ( ) - 600 ) .
Avg ( "costMs" , 0 )
}
// FindAvgLevelWithTarget 获取某个对象的平均级别
func ( this * ReportResultDAO ) FindAvgLevelWithTarget ( tx * dbs . Tx , taskType reporterconfigs . TaskType , targetId int64 ) ( string , error ) {
ones , _ , err := this . Query ( tx ) .
Result ( "COUNT(*) AS c, level" ) .
Attr ( "type" , taskType ) .
Attr ( "targetId" , targetId ) .
Where ( "reportNodeId IN (SELECT id FROM " + SharedReportNodeDAO . Table + " WHERE state=1 AND isOn=1)" ) .
Gt ( "updatedAt" , time . Now ( ) . Unix ( ) - 600 ) .
Group ( "level" ) .
FindOnes ( )
if err != nil {
return "" , err
}
if len ( ones ) == 0 {
return reporterconfigs . ReportLevelNormal , nil
}
var total = 0
var levelMap = map [ string ] int { } // code => count
for _ , one := range ones {
var c = one . GetInt ( "c" )
total += c
levelMap [ one . GetString ( "level" ) ] = c
}
if total == 0 {
return reporterconfigs . ReportLevelNormal , nil
}
var half = total / 2
for _ , def := range reporterconfigs . FindAllReportLevels ( ) {
c , ok := levelMap [ def . Code ]
if ok {
half -= c
if half <= 0 {
return def . Code , nil
}
}
}
return "" , nil
}
// FindConnectivityWithTarget 获取某个对象的连通率
2021-09-12 20:21:42 +08:00
func ( this * ReportResultDAO ) FindConnectivityWithTarget ( tx * dbs . Tx , taskType reporterconfigs . TaskType , targetId int64 , groupId int64 ) ( float64 , error ) {
var query = this . Query ( tx ) .
2021-09-08 19:34:31 +08:00
Attr ( "type" , taskType ) .
2021-09-12 20:21:42 +08:00
Attr ( "targetId" , targetId )
if groupId > 0 {
query . Where ( "reportNodeId IN (SELECT id FROM " + SharedReportNodeDAO . Table + " WHERE state=1 AND isOn=1 AND JSON_CONTAINS(groupIds, :groupIdString))" ) .
Param ( "groupIdString" , types . String ( groupId ) )
} else {
query . Where ( "reportNodeId IN (SELECT id FROM " + SharedReportNodeDAO . Table + " WHERE state=1 AND isOn=1)" )
}
// 已汇报数据的数量
total , err := query .
2021-09-08 19:34:31 +08:00
Gt ( "updatedAt" , time . Now ( ) . Unix ( ) - 600 ) .
Count ( )
if err != nil {
return 0 , err
}
if total == 0 {
2021-09-12 20:21:42 +08:00
return 1 , nil
2021-09-08 19:34:31 +08:00
}
// 连通的数量
2021-09-12 20:21:42 +08:00
var connectedQuery = this . Query ( tx ) .
2021-09-08 19:34:31 +08:00
Attr ( "type" , taskType ) .
2021-09-12 20:21:42 +08:00
Attr ( "targetId" , targetId )
if groupId > 0 {
connectedQuery . Where ( "reportNodeId IN (SELECT id FROM " + SharedReportNodeDAO . Table + " WHERE state=1 AND isOn=1 AND JSON_CONTAINS(groupIds, :groupIdString))" ) .
Param ( "groupIdString" , types . String ( groupId ) )
} else {
connectedQuery . Where ( "reportNodeId IN (SELECT id FROM " + SharedReportNodeDAO . Table + " WHERE state=1 AND isOn=1)" )
}
countConnected , err := connectedQuery .
2021-09-08 19:34:31 +08:00
Attr ( "isOk" , true ) .
Gt ( "updatedAt" , time . Now ( ) . Unix ( ) - 600 ) .
Count ( )
if err != nil {
return 0 , err
}
return float64 ( countConnected ) / float64 ( total ) , nil
}