package statistics
import (
"context"
"time"
"ascend-common/common-utils/hwlog"
"clusterd/pkg/common/constant"
"clusterd/pkg/common/util"
"clusterd/pkg/domain/publicfault"
"clusterd/pkg/domain/statistics"
)
var StatisticFault FaultCache
func init() {
StatisticFault = FaultCache{
updateChan: make(chan struct{}, 1),
}
}
type FaultCache struct {
updateChan chan struct{}
}
func (fc *FaultCache) Notify() {
if len(fc.updateChan) == 0 {
fc.updateChan <- struct{}{}
}
}
func (fc *FaultCache) UpdateFault(ctx context.Context) {
for {
select {
case _, ok := <-ctx.Done():
if !ok {
hwlog.RunLog.Error("ctx stop channel is closed")
}
hwlog.RunLog.Info("receive ctx stop signal")
return
case _, ok := <-fc.updateChan:
if !ok {
hwlog.RunLog.Error("updateChan is closed")
return
}
fc.updateCacheToCM()
time.Sleep(1 * time.Second)
}
}
}
func (fc *FaultCache) updateCacheToCM() {
pubFaults, pubFaultsNum := publicfault.PubFaultCache.GetPubFaultsForCM()
faults := util.ObjToString(pubFaults)
faultNum := util.ObjToString(
constant.FaultNum{
PubFaultNum: pubFaultsNum,
})
if err := statistics.UpdateFaultToCM(faults, faultNum, pubFaultsNum > constant.MaxFaultNum); err != nil {
hwlog.RunLog.Errorf("update fault to cm failed, error: %v", err)
return
}
}
func (fc *FaultCache) LoadFaultData() {
if err := statistics.LoadFaultFromCM(); err != nil {
hwlog.RunLog.Errorf("load fault from cm failed, error: %v", err)
return
}
hwlog.RunLog.Info("load statistic fault from cm successfully")
}