Skip to content

Commit

Permalink
improve model1.Fields readability
Browse files Browse the repository at this point in the history
  • Loading branch information
crossRT committed Nov 29, 2024
1 parent 15a715e commit 4220f9d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 28 deletions.
47 changes: 19 additions & 28 deletions internal/model1/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
package model1

import (
"fmt"
"reflect"
"regexp"
"strings"
)

// Fields represents a collection of row fields.
Expand All @@ -16,34 +13,12 @@ type Fields []string
// Customize returns a subset of fields.
func (f Fields) Customize(cols []int, out Fields, extractionInfoBag ExtractionInfoBag) {
for i, c := range cols {
if c < 0 {

// If current index can retrieve an extractionInfo from extractionInfoBag,
// meaning this column has to retrieve the actual value from other field.
// For example: `LABELS[kubernetes.io/hostname]` needs to extract the value from column `LABELS`
if extractionInfo, ok := extractionInfoBag[i]; ok {
idxInFields := extractionInfo.IdxInFields
key := extractionInfo.Key

// Escape dots from the key
// For example: `kubernetes.io/hostname` needs to be escaped to `kubernetes\.io/hostname`
escapedKey := strings.ReplaceAll(key, ".", "\\.")

// Extract the value by using regex
pattern := fmt.Sprintf(`%s=([^ ]+)`, escapedKey)
regex := regexp.MustCompile(pattern)

// Find the value in the field that store original values
matches := regex.FindStringSubmatch(f[idxInFields])
if len(matches) > 1 {
out[i] = matches[1]
continue
}
}

out[i] = NAValue
if c < 0 {
out[i] = getValueOfInvalidColumn(f, i, extractionInfoBag)
continue
}

if c < len(f) {
out[i] = f[c]
}
Expand All @@ -68,3 +43,19 @@ func (f Fields) Clone() Fields {

return cp
}

func getValueOfInvalidColumn(f Fields, i int, extractionInfoBag ExtractionInfoBag) string {

extractionInfo, ok := extractionInfoBag[i]

// If the extractionInfo is existed in extractionInfoBag,
// meaning this column has to retrieve the actual value from other field.
// For example: `LABELS[kubernetes.io/hostname]` needs to extract the value from column `LABELS`
if ok {
idxInFields := extractionInfo.IdxInFields
escapedKey := escapeDots(extractionInfo.Key)
return extractValueFromField(escapedKey, f[idxInFields])
}

return NAValue
}
21 changes: 21 additions & 0 deletions internal/model1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package model1
import (
"fmt"
"math"
"regexp"
"sort"
"strings"

Expand Down Expand Up @@ -164,3 +165,23 @@ func lessNumber(s1, s2 string) bool {

return sortorder.NaturalLess(v1, v2)
}

// Escape dots from the string
// For example: `kubernetes.io/hostname` needs to be escaped to `kubernetes\.io/hostname`
func escapeDots(s string) string {
return strings.ReplaceAll(s, ".", "\\.")
}

func extractValueFromField(key string, field string) string {
// Extract the value by using regex
pattern := fmt.Sprintf(`%s=([^ ]+)`, key)
regex := regexp.MustCompile(pattern)

// Find the value in the field that store original values
matches := regex.FindStringSubmatch(field)
if len(matches) > 1 {
return matches[1]
}

return ""
}
26 changes: 26 additions & 0 deletions internal/model1/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,29 @@ func BenchmarkDurationToSecond(b *testing.B) {
durationToSeconds(t)
}
}

func TestEscapeDots(t *testing.T) {
var s string

s = escapeDots("kubernetes.io/hostname")
assert.Equal(t, "kubernetes\\.io/hostname", s)

s = escapeDots("kubernetes-io/hostname")
assert.Equal(t, "kubernetes-io/hostname", s)
}

func TestExtractValueFromFields(t *testing.T) {
k := escapeDots("kubernetes.io/hostname")
f := "kubernetes.io/arch=amd64 kubernetes.io/hostname=a-b-c-d kubernetes.io/os=linux"

var s string

s = extractValueFromField(k, f)
assert.Equal(t, "a-b-c-d", s)

s = extractValueFromField(k, "kubernetes.io/hostname=e-f-g-h "+f)
assert.Equal(t, "e-f-g-h", s)

s = extractValueFromField("random-key", f)
assert.Equal(t, "", s)
}

0 comments on commit 4220f9d

Please sign in to comment.