More complete reponse for flush

This commit is contained in:
ppom 2023-10-04 12:00:00 +02:00
parent 32f6aca1b5
commit 3767fc6cf8
4 changed files with 61 additions and 58 deletions

View File

@ -25,8 +25,6 @@ type Request struct {
type Response struct { type Response struct {
Err error Err error
ClientStatus ClientStatus ClientStatus ClientStatus
FlushedMatches map[string]map[string]int
FlushedActions map[string]map[string]map[string]int
} }
func SendAndRetrieve(data Request) Response { func SendAndRetrieve(data Request) Response {
@ -50,20 +48,23 @@ func SendAndRetrieve(data Request) Response {
} }
type PatternStatus struct { type PatternStatus struct {
Matches int `yaml:"matches_since_last_trigger"` Matches int `yaml:"matches"`
Actions map[string][]string `yaml:"pending_actions"` Actions map[string][]string `yaml:"actions"`
} }
type MapPatternStatus map[string]*PatternStatus type MapPatternStatus map[string]*PatternStatus
type MapPatternStatusFlush MapPatternStatus
type ClientStatus map[string]map[string]MapPatternStatus type ClientStatus map[string]map[string]MapPatternStatus
type ClientStatusFlush ClientStatus
// This block is made to hide pending_actions when empty // This block is made to hide pending_actions when empty
// and matches_since_last_trigger when zero // and matches_since_last_trigger when zero
type FullPatternStatus PatternStatus type FullPatternStatus PatternStatus
type MatchesStatus struct { type MatchesStatus struct {
Matches int `yaml:"matches_since_last_trigger"` Matches int `yaml:"matches"`
} }
type ActionsStatus struct { type ActionsStatus struct {
Actions map[string][]string `yaml:"pending_actions"` Actions map[string][]string `yaml:"actions"`
} }
func (mps MapPatternStatus) MarshalYAML() (interface{}, error) { func (mps MapPatternStatus) MarshalYAML() (interface{}, error) {
@ -84,6 +85,35 @@ func (mps MapPatternStatus) MarshalYAML() (interface{}, error) {
return ret, nil return ret, nil
} }
func (mps MapPatternStatusFlush) MarshalYAML() (interface{}, error) {
var ret interface{}
for _, v := range mps {
if v.Matches == 0 {
if len(v.Actions) != 0 {
ret = ActionsStatus{v.Actions}
}
} else {
if len(v.Actions) != 0 {
ret = v
} else {
ret = MatchesStatus{v.Matches}
}
}
}
return ret, nil
}
func (csf ClientStatusFlush) MarshalYAML() (interface{}, error) {
ret := make(map[string]map[string]MapPatternStatusFlush)
for k, v := range csf {
ret[k] = make(map[string]MapPatternStatusFlush)
for kk, vv := range v {
ret[k][kk] = MapPatternStatusFlush(vv)
}
}
return ret, nil
}
// end block // end block
func usage(err string) { func usage(err string) {
@ -112,10 +142,7 @@ func ClientFlush(pattern, streamfilter string) {
log.Fatalln("Received error from daemon:", response.Err) log.Fatalln("Received error from daemon:", response.Err)
os.Exit(1) os.Exit(1)
} }
text, err := yaml.Marshal(struct { text, err := yaml.Marshal(ClientStatusFlush(response.ClientStatus))
Matches map[string]map[string]int
Actions map[string]map[string]map[string]int
}{response.FlushedMatches, response.FlushedActions})
if err != nil { if err != nil {
log.Fatalln("Failed to convert daemon binary response to text format:", err) log.Fatalln("Failed to convert daemon binary response to text format:", err)
} }

View File

@ -127,14 +127,14 @@ func ActionsManager() {
actionsLock.Unlock() actionsLock.Unlock()
action.exec(pattern) action.exec(pattern)
case fo := <-flushToActionsC: case fo := <-flushToActionsC:
ret := make(map[*Action]int) ret := make(ActionsMap)
actionsLock.Lock() actionsLock.Lock()
for pa := range actions { for pa := range actions {
if pa.p == fo.p { if pa.p == fo.p {
for range actions[pa] { for range actions[pa] {
pa.a.exec(pa.p) pa.a.exec(pa.p)
} }
ret[pa.a] = len(actions[pa]) ret[pa] = actions[pa]
delete(actions, pa) delete(actions, pa)
} }
} }
@ -188,12 +188,12 @@ func MatchesManager() {
} }
func matchesManagerHandleFlush(fo FlushMatchOrder) { func matchesManagerHandleFlush(fo FlushMatchOrder) {
ret := make(map[*Filter]int) ret := make(MatchesMap)
matchesLock.Lock() matchesLock.Lock()
for pf := range matches { for pf := range matches {
if fo.p == pf.p { if fo.p == pf.p {
if fo.ret != nil { if fo.ret != nil {
ret[pf.f] = len(matches[pf]) ret[pf] = matches[pf]
} }
delete(matches, pf) delete(matches, pf)
} }

View File

@ -6,15 +6,16 @@ import (
"net" "net"
"os" "os"
"path" "path"
"sync"
"time" "time"
) )
func genClientStatus() ClientStatus { func genClientStatus(local_actions ActionsMap, local_matches MatchesMap, local_actionsLock, local_matchesLock *sync.Mutex) ClientStatus {
cs := make(ClientStatus) cs := make(ClientStatus)
matchesLock.Lock() local_matchesLock.Lock()
// Painful data manipulation // Painful data manipulation
for pf, times := range matches { for pf, times := range local_matches {
pattern, filter := pf.p, pf.f pattern, filter := pf.p, pf.f
if cs[filter.stream.name] == nil { if cs[filter.stream.name] == nil {
cs[filter.stream.name] = make(map[string]MapPatternStatus) cs[filter.stream.name] = make(map[string]MapPatternStatus)
@ -25,11 +26,11 @@ func genClientStatus() ClientStatus {
cs[filter.stream.name][filter.name][pattern] = &PatternStatus{len(times), nil} cs[filter.stream.name][filter.name][pattern] = &PatternStatus{len(times), nil}
} }
matchesLock.Unlock() local_matchesLock.Unlock()
actionsLock.Lock() local_actionsLock.Lock()
// Painful data manipulation // Painful data manipulation
for pa := range actions { for pa, times := range local_actions {
pattern, action := pa.p, pa.a pattern, action := pa.p, pa.a
if cs[action.filter.stream.name] == nil { if cs[action.filter.stream.name] == nil {
cs[action.filter.stream.name] = make(map[string]MapPatternStatus) cs[action.filter.stream.name] = make(map[string]MapPatternStatus)
@ -44,39 +45,14 @@ func genClientStatus() ClientStatus {
if ps.Actions == nil { if ps.Actions == nil {
ps.Actions = make(map[string][]string) ps.Actions = make(map[string][]string)
} }
for then := range actions[pa] { for then := range times {
ps.Actions[action.name] = append(ps.Actions[action.name], then.Format(time.DateTime)) ps.Actions[action.name] = append(ps.Actions[action.name], then.Format(time.DateTime))
} }
} }
actionsLock.Unlock() local_actionsLock.Unlock()
return cs return cs
} }
func genFlushedMatches(og map[*Filter]int) map[string]map[string]int {
ret := make(map[string]map[string]int)
for filter, nb := range og {
if ret[filter.stream.name] == nil {
ret[filter.stream.name] = make(map[string]int)
}
ret[filter.stream.name][filter.name] = nb
}
return ret
}
func genFlushedActions(og map[*Action]int) map[string]map[string]map[string]int {
ret := make(map[string]map[string]map[string]int)
for action, nb := range og {
if ret[action.filter.stream.name] == nil {
ret[action.filter.stream.name] = make(map[string]map[string]int)
}
if ret[action.filter.stream.name][action.filter.name] == nil {
ret[action.filter.stream.name][action.filter.name] = make(map[string]int)
}
ret[action.filter.stream.name][action.filter.name][action.name] = nb
}
return ret
}
func createOpenSocket() net.Listener { func createOpenSocket() net.Listener {
err := os.MkdirAll(path.Dir(*SocketPath), 0755) err := os.MkdirAll(path.Dir(*SocketPath), 0755)
if err != nil { if err != nil {
@ -120,17 +96,17 @@ func SocketManager(streams map[string]*Stream) {
switch request.Request { switch request.Request {
case Show: case Show:
response.ClientStatus = genClientStatus() response.ClientStatus = genClientStatus(actions, matches, &actionsLock, &matchesLock)
case Flush: case Flush:
le := LogEntry{time.Now(), request.Pattern, "", "", false} le := LogEntry{time.Now(), request.Pattern, "", "", false}
matches := FlushMatchOrder{request.Pattern, make(chan map[*Filter]int)} matchesC := FlushMatchOrder{request.Pattern, make(chan MatchesMap)}
actions := FlushActionOrder{request.Pattern, make(chan map[*Action]int)} actionsC := FlushActionOrder{request.Pattern, make(chan ActionsMap)}
flushToMatchesC <- matches flushToMatchesC <- matchesC
flushToActionsC <- actions flushToActionsC <- actionsC
flushToDatabaseC <- le flushToDatabaseC <- le
response.FlushedMatches = genFlushedMatches(<-matches.ret) var lock sync.Mutex
response.FlushedActions = genFlushedActions(<-actions.ret) response.ClientStatus = genClientStatus(<-actionsC.ret, <-matchesC.ret, &lock, &lock)
default: default:
log.Println("ERROR Invalid Message from cli: unrecognised Request type") log.Println("ERROR Invalid Message from cli: unrecognised Request type")
return return

View File

@ -102,9 +102,9 @@ type PAT struct {
type FlushMatchOrder struct { type FlushMatchOrder struct {
p string p string
ret chan map[*Filter]int ret chan MatchesMap
} }
type FlushActionOrder struct { type FlushActionOrder struct {
p string p string
ret chan map[*Action]int ret chan ActionsMap
} }