Skip to content

Commit

Permalink
add logs manipulation and parsing features
Browse files Browse the repository at this point in the history
  • Loading branch information
y0sher committed Dec 13, 2023
1 parent 40b39ac commit 8bd43f0
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions e2e/logs_catcher/logs/logs.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
package logs

import "strings"
import (
"fmt"
"strings"
)

type RAW []string

type Parsed []map[string]any
type ParsedLine map[string]any

type Parsed []ParsedLine

type Parser func(log string) (map[string]any, error)

func GrepLine(line string, matches []string) bool {
func (pl ParsedLine) Get(key string) (any, error) {
if f, ok := pl[key]; ok {
return f, nil
}
return nil, fmt.Errorf("field doesn't exist %v", key)
}

func (p Parsed) Fields(key string, failOnError bool) (RAW, error) {
var res RAW
for _, v := range p {
got, err := v.Get(key)
if err == nil {
res = append(res, fmt.Sprint(got))
}

if failOnError {
return nil, err
}

}
return res, nil
}

func GrepLine(line string, matches RAW) bool {
matched := true
for _, m := range matches {
if !strings.Contains(line, m) {
Expand All @@ -18,7 +46,7 @@ func GrepLine(line string, matches []string) bool {
return matched
}

func (r RAW) Grep(matches []string) []string {
func (r RAW) Grep(matches []string) RAW {
raw := make([]string, 0)
for _, log := range r {
if GrepLine(log, matches) {
Expand All @@ -30,12 +58,16 @@ func (r RAW) Grep(matches []string) []string {
}

func (r RAW) ParseAll(p Parser) Parsed {
parsed := make([]map[string]any, 0)
parsed := Parsed{}

for _, log := range r {
singleparsed, err := p(log)
removeall := strings.Split(log, "{")
removeall2 := strings.Split(removeall[1], "}")
comb := strings.Join([]string{"{", removeall2[0], "}"}, "")
singleparsed, err := p(comb)
if err != nil {
// todo: log parse err
fmt.Println("Error parsing log ", err, log)
continue
}
parsed = append(parsed, singleparsed)
Expand All @@ -45,7 +77,7 @@ func (r RAW) ParseAll(p Parser) Parsed {
}

func (p Parsed) GrepCondition(find map[string]func(any) bool) Parsed {
parsed := make([]map[string]any, 0)
parsed := Parsed{} // make([]map[string]any, 0)
LogLoop:
for _, log := range p {
checkedGood := true
Expand Down

0 comments on commit 8bd43f0

Please sign in to comment.