-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
daccc0b
commit a407b30
Showing
5 changed files
with
208 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package loki | ||
|
||
import ( | ||
"github.com/netobserv/network-observability-console-plugin/pkg/model" | ||
pmodel "github.com/prometheus/common/model" | ||
) | ||
|
||
type MatrixMerger struct { | ||
index map[string]indexedSampleStream | ||
merged model.Matrix | ||
} | ||
|
||
func NewMatrixMerger() *MatrixMerger { | ||
return &MatrixMerger{ | ||
index: map[string]indexedSampleStream{}, | ||
merged: model.Matrix{}, | ||
} | ||
} | ||
|
||
type indexedSampleStream struct { | ||
sampleStream pmodel.SampleStream | ||
values map[string]interface{} | ||
index int | ||
} | ||
|
||
func (m *MatrixMerger) AddMatrix(from model.Matrix) model.Matrix { | ||
for _, sampleStream := range from { | ||
skey := sampleStream.Metric.String() | ||
idxSampleStream, sampleStreamExists := m.index[skey] | ||
if !sampleStreamExists { | ||
// SampleStream doesn't exist => create new index | ||
idxSampleStream = indexedSampleStream{ | ||
sampleStream: sampleStream, | ||
values: map[string]interface{}{}, | ||
index: len(m.index), | ||
} | ||
} | ||
// Merge content (values) | ||
for _, v := range sampleStream.Values { | ||
vkey := v.String() | ||
if _, valueExists := idxSampleStream.values[vkey]; !valueExists { | ||
// Add value to the existing sampleStream, and mark it as existing in idxSampleStream.values | ||
idxSampleStream.values[vkey] = nil | ||
if sampleStreamExists { | ||
idxSampleStream.sampleStream.Values = append(m.index[skey].sampleStream.Values, v) | ||
} | ||
} // Else: entry found => ignore duplicate | ||
} | ||
// Add or overwrite index | ||
m.index[skey] = idxSampleStream | ||
if !sampleStreamExists { | ||
// SampleStream doesn't exist => append it | ||
m.merged = append(m.merged, idxSampleStream.sampleStream) | ||
} else { | ||
m.merged[idxSampleStream.index] = idxSampleStream.sampleStream | ||
} | ||
} | ||
return m.merged | ||
} | ||
|
||
func (m *MatrixMerger) GetStreams() model.Matrix { | ||
return m.merged | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package loki | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/netobserv/network-observability-console-plugin/pkg/model" | ||
pmodel "github.com/prometheus/common/model" | ||
) | ||
|
||
func TestMatrixMerge(t *testing.T) { | ||
now := pmodel.Now() | ||
merger := NewMatrixMerger() | ||
baseline := pmodel.SampleStream{ | ||
Metric: pmodel.Metric{ | ||
"foo": "bar", | ||
}, | ||
Values: []pmodel.SamplePair{{ | ||
Timestamp: now, | ||
Value: pmodel.SampleValue(42), | ||
}}, | ||
} | ||
merger.AddMatrix(model.Matrix{baseline}) | ||
|
||
// Different metric, different value pair => no dedup | ||
merged := merger.AddMatrix(model.Matrix{{ | ||
Metric: pmodel.Metric{ | ||
"foo": "bar", | ||
"foo2": "bar2", | ||
}, | ||
Values: baseline.Values, | ||
}, { | ||
Metric: baseline.Metric, | ||
Values: []pmodel.SamplePair{{ | ||
Timestamp: now, | ||
Value: pmodel.SampleValue(12), | ||
}}, | ||
}}) | ||
assert.Len(t, merged, 2) | ||
assert.Len(t, merged[0].Values, 2) | ||
assert.Len(t, merged[1].Values, 1) | ||
|
||
// Same metrics in different order => no dedup | ||
merged = merger.AddMatrix(model.Matrix{{ | ||
Metric: pmodel.Metric{ | ||
"foo2": "bar2", | ||
"foo": "bar", | ||
}, | ||
Values: baseline.Values, | ||
}, { | ||
Metric: pmodel.Metric{ | ||
"foo2": "bar2", | ||
"foo": "bar", | ||
}, | ||
Values: baseline.Values, | ||
}, { | ||
Metric: pmodel.Metric{ | ||
"foo": "bar", | ||
"foo2": "bar2", | ||
}, | ||
Values: baseline.Values, | ||
}}) | ||
assert.Len(t, merged, 2) | ||
assert.Len(t, merged[0].Values, 2) | ||
assert.Len(t, merged[1].Values, 1) | ||
|
||
// Different timestamp => no dedup | ||
merged = merger.AddMatrix(model.Matrix{{ | ||
Metric: baseline.Metric, | ||
Values: []pmodel.SamplePair{{ | ||
Timestamp: now.Add(time.Hour), | ||
Value: pmodel.SampleValue(12), | ||
}}, | ||
}}) | ||
assert.Len(t, merged, 2) | ||
assert.Len(t, merged[0].Values, 3) | ||
assert.Len(t, merged[1].Values, 1) | ||
|
||
// some dedup | ||
merged = merger.AddMatrix(model.Matrix{{ | ||
// changed value => no dedup | ||
Metric: baseline.Metric, | ||
Values: []pmodel.SamplePair{{ | ||
Timestamp: now, | ||
Value: pmodel.SampleValue(8), | ||
}}, | ||
}, { | ||
// changed value => no dedup | ||
Metric: baseline.Metric, | ||
Values: []pmodel.SamplePair{{ | ||
Timestamp: now, | ||
Value: pmodel.SampleValue(0), | ||
}}, | ||
}, { | ||
// same as previously modified timestamp => will be added | ||
Metric: baseline.Metric, | ||
Values: []pmodel.SamplePair{{ | ||
Timestamp: now.Add(time.Hour), | ||
Value: pmodel.SampleValue(42), | ||
}}, | ||
}, | ||
// baseline itself => must be ignored | ||
baseline, | ||
}) | ||
|
||
// Different timestamp | ||
assert.Len(t, merged, 2) | ||
assert.Len(t, merged[0].Values, 6) | ||
assert.Len(t, merged[1].Values, 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters