87 lines
2.3 KiB
Go
87 lines
2.3 KiB
Go
package comtrade
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"wave_record/config"
|
|
"wave_record/constant"
|
|
"wave_record/util"
|
|
|
|
"github.com/yonwoo9/go-comtrade"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
var ParseFunc = func(parseConfig interface{}) {
|
|
logger := zap.L()
|
|
comtradeConfig, ok := parseConfig.(config.ComtradeFileConfig)
|
|
if !ok {
|
|
// TODO 增加日志报错中输出文件名
|
|
logger.Error("conversion parsing comtrade parameter type failed")
|
|
return
|
|
}
|
|
|
|
parseComtradeData(comtradeConfig.ConfigFilePath, *&comtradeConfig.DatafilePath)
|
|
}
|
|
|
|
func ParseComtradeFile(monitorDir string, addChan chan string, comtradeMap *sync.Map) {
|
|
logger := zap.L()
|
|
|
|
for {
|
|
addFilePath, ok := <-addChan
|
|
if !ok {
|
|
logger.Error("monitor add channel closed", zap.Bool("channel_status", ok))
|
|
return
|
|
}
|
|
|
|
lastElement := filepath.Base(addFilePath)
|
|
fileName := strings.Split(lastElement, ".")[0]
|
|
fileExtension := filepath.Ext(lastElement)
|
|
logger.Info("add comtrade file info", zap.String("file_path", addFilePath),
|
|
zap.String("file_name", fileName), zap.String("file_extension", fileExtension))
|
|
|
|
switch fileExtension {
|
|
case constant.ConfigFileSuffix:
|
|
configFilePath := addFilePath
|
|
|
|
dataFilePath, exist := comtradeMap.Load(addFilePath)
|
|
if exist {
|
|
if dataFilePath == "" {
|
|
continue
|
|
}
|
|
|
|
go parseComtradeData(configFilePath, dataFilePath.(string))
|
|
} else {
|
|
comtradeMap.Store(configFilePath, "")
|
|
}
|
|
case constant.DataFileSuffix:
|
|
configFileName := strings.Join([]string{fileName, constant.ConfigFileSuffix}, "")
|
|
configFilePath := filepath.Join(monitorDir, configFileName)
|
|
logger.Info("config path of comtrade file", zap.String("file_path", configFilePath))
|
|
exist := util.FileExists(configFilePath)
|
|
if exist {
|
|
comtradeMap.Store(configFilePath, addFilePath)
|
|
addChan <- configFilePath
|
|
continue
|
|
}
|
|
addChan <- addFilePath
|
|
default:
|
|
logger.Warn("no support file style", zap.String("file_style", fileExtension))
|
|
time.Sleep(5 * time.Second)
|
|
}
|
|
}
|
|
}
|
|
|
|
func parseComtradeData(configFilePath, dataFilePath string) (*comtrade.Comtrade, error) {
|
|
logger := zap.L()
|
|
|
|
comtrade, err := comtrade.ParseComtrade(configFilePath, dataFilePath)
|
|
if err != nil {
|
|
logger.Error("parse comtrade file failed", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
return comtrade, nil
|
|
}
|