2019-05-19 06:06:06 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-12-29 09:42:54 +01:00
|
|
|
"encoding/json"
|
2020-01-02 16:53:54 +01:00
|
|
|
"log"
|
2019-12-29 12:49:20 +01:00
|
|
|
"regexp"
|
2019-05-19 06:06:06 +02:00
|
|
|
)
|
|
|
|
|
2019-12-31 01:32:06 +01:00
|
|
|
func resetMsgParsingRules() error {
|
2019-12-29 14:00:54 +01:00
|
|
|
var rules, rules2 []MessageParsingRule
|
2020-01-02 16:17:22 +01:00
|
|
|
var count int64
|
2019-12-29 12:45:26 +01:00
|
|
|
|
|
|
|
b, err := Asset("data/msg_rules.json")
|
2019-12-31 01:32:06 +01:00
|
|
|
logOnError(err, "resetMsgParsingRules : load data/msg_rules.json")
|
2019-12-29 12:45:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(b, &rules)
|
|
|
|
|
2019-12-29 14:01:27 +01:00
|
|
|
for _, r := range rules {
|
2019-12-29 14:00:54 +01:00
|
|
|
r2 := MessageParsingRule{
|
|
|
|
Priority: r.Priority,
|
|
|
|
Description: r.Description,
|
|
|
|
Rule: r.Rule,
|
2019-12-29 14:01:09 +01:00
|
|
|
MsgType: r.MsgType,
|
2019-12-29 14:00:54 +01:00
|
|
|
}
|
2019-12-29 13:51:08 +01:00
|
|
|
|
2019-12-29 14:00:54 +01:00
|
|
|
r2.MsgTypeID64, err = codeObjSubTypeId(r2.MsgType)
|
2019-12-31 01:32:06 +01:00
|
|
|
logOnError(err, "resetMsgParsingRules : codeObjSubTypeId("+r2.MsgType+")")
|
2019-12-29 12:45:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 14:00:54 +01:00
|
|
|
r2.re, err = regexp.Compile(r2.Rule)
|
2019-12-31 01:32:06 +01:00
|
|
|
logOnError(err, "resetMsgParsingRules : Compile("+r.Rule+")")
|
2019-12-29 12:45:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-29 14:02:20 +01:00
|
|
|
|
2019-12-29 14:00:54 +01:00
|
|
|
rules2 = append(rules2, r2)
|
2019-12-29 12:45:26 +01:00
|
|
|
}
|
|
|
|
|
2019-12-29 12:45:49 +01:00
|
|
|
_, err = db.Exec(`TRUNCATE TABLE msg_rules;`)
|
2019-12-31 01:32:06 +01:00
|
|
|
logOnError(err, "resetMsgParsingRules : truncate table msg_rules")
|
2019-12-26 09:16:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:45:26 +01:00
|
|
|
stmt, err := db.Prepare(`INSERT INTO msg_rules (prio, msg_type_id, descn, rule)
|
2019-12-29 13:40:16 +01:00
|
|
|
VALUES (?, ?, ?, ?);`)
|
2019-12-31 01:32:06 +01:00
|
|
|
logOnError(err, "resetMsgParsingRules : prepare statement")
|
2019-12-29 08:50:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-29 12:45:26 +01:00
|
|
|
defer stmt.Close()
|
2020-01-02 16:17:22 +01:00
|
|
|
count = 0
|
2019-12-29 14:00:54 +01:00
|
|
|
for _, r2 := range rules2 {
|
2019-12-29 13:54:41 +01:00
|
|
|
|
2019-12-29 14:00:54 +01:00
|
|
|
_, err = stmt.Exec(r2.Priority, r2.MsgTypeID64, r2.Description, r2.Rule)
|
2019-12-31 01:32:06 +01:00
|
|
|
logOnError(err, "resetMsgParsingRules : insert statement "+r2.Description)
|
2019-12-29 12:45:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-02 16:17:22 +01:00
|
|
|
count += 1
|
2019-12-29 09:40:52 +01:00
|
|
|
|
2019-12-29 12:45:26 +01:00
|
|
|
}
|
2019-12-29 08:50:56 +01:00
|
|
|
|
2020-01-02 16:53:54 +01:00
|
|
|
log.Printf("%d rules loaded.\n", count)
|
2020-01-02 16:17:22 +01:00
|
|
|
|
2019-12-26 09:16:19 +01:00
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|