73 lines
1.5 KiB
Go
73 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"regexp"
|
|
)
|
|
|
|
func resetMsgParsingRules() error {
|
|
var rules, rules2 []MessageParsingRule
|
|
var count int64
|
|
|
|
b, err := Asset("data/msg_rules.json")
|
|
logOnError(err, "resetMsgParsingRules : load data/msg_rules.json")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = json.Unmarshal(b, &rules)
|
|
|
|
for _, r := range rules {
|
|
r2 := MessageParsingRule{
|
|
Priority: r.Priority,
|
|
Description: r.Description,
|
|
Rule: r.Rule,
|
|
MsgType: r.MsgType,
|
|
}
|
|
|
|
r2.MsgTypeID64, err = codeObjSubTypeId(r2.MsgType)
|
|
logOnError(err, "resetMsgParsingRules : codeObjSubTypeId("+r2.MsgType+")")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
r2.re, err = regexp.Compile(r2.Rule)
|
|
logOnError(err, "resetMsgParsingRules : Compile("+r.Rule+")")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
rules2 = append(rules2, r2)
|
|
}
|
|
|
|
_, err = db.Exec(`TRUNCATE TABLE msg_rules;`)
|
|
logOnError(err, "resetMsgParsingRules : truncate table msg_rules")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
stmt, err := db.Prepare(`INSERT INTO msg_rules (prio, msg_type_id, descn, rule)
|
|
VALUES (?, ?, ?, ?);`)
|
|
logOnError(err, "resetMsgParsingRules : prepare statement")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer stmt.Close()
|
|
count = 0
|
|
for _, r2 := range rules2 {
|
|
|
|
_, err = stmt.Exec(r2.Priority, r2.MsgTypeID64, r2.Description, r2.Rule)
|
|
logOnError(err, "resetMsgParsingRules : insert statement "+r2.Description)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
count += 1
|
|
|
|
}
|
|
|
|
fmt.Printf("resetMsgParsingRules : %d rules loaded.\n", count)
|
|
|
|
return nil
|
|
|
|
}
|