Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shmsr committed Sep 22, 2023
1 parent 0724a42 commit 4eefdd9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 125 deletions.
17 changes: 10 additions & 7 deletions x-pack/metricbeat/module/azure/app_insights/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ func groupMetricsByTime(metrics []MetricValue) map[metricTimeKey][]MetricValue {

for _, metric := range metrics {
// The start and end times are truncated to the nearest second.
// This is done to ensure that metrics that fall within the same second are grouped together, even if their actual times are slightly different.
// This is done to ensure that metrics that fall within the same
// second are grouped together, even if their actual time are
// slightly different.
timeKey := newMetricTimeKey(
metric.Start.Time.Truncate(time.Second),
metric.End.Time.Truncate(time.Second),
Expand All @@ -182,10 +184,13 @@ func groupMetricsByTime(metrics []MetricValue) map[metricTimeKey][]MetricValue {

// groupMetricsByDimension groups the given metrics by their dimension keys.
func groupMetricsByDimension(metrics []MetricValue) map[string][]MetricValue {
keys := make(map[string][]MetricValue)
var firstStart, firstEnd *date.Time
var (
keys = make(map[string][]MetricValue)
firstStart, firstEnd *date.Time
helper func(metrics []MetricValue)
)

var helper func(metrics []MetricValue)
// Review comment: Can you add some more comments to this helper func?
helper = func(metrics []MetricValue) {
for _, metric := range metrics {
dimensionKey := getSortedKeys(metric.SegmentName)
Expand Down Expand Up @@ -222,9 +227,7 @@ func groupMetricsByDimension(metrics []MetricValue) map[string][]MetricValue {
}
} else if dimensionKey != "" {
m := metric
m.Start = firstStart
m.End = firstEnd

m.Start, m.End = firstStart, firstEnd
keys[dimensionKey] = append(keys[dimensionKey], m)
}
}
Expand Down
181 changes: 63 additions & 118 deletions x-pack/metricbeat/module/azure/app_insights/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,99 +17,62 @@ import (
"github.com/elastic/elastic-agent-libs/mapstr"
)

func newMetricsTest(timestamp1, timestamp2, timestamp3 *date.Time) []MetricValue {
return []MetricValue{
func newMetricsTest(ts ...*date.Time) []MetricValue {
type values struct {
SegmentName map[string]string
Value map[string]interface{}
T *date.Time
}

const numOfMetricValue = 3

if numOfMetricValue != len(ts) {
panic("number of arguments to newMetricsTest is not correct")
}

vals := [numOfMetricValue]values{
{
SegmentName: map[string]string{},
Value: map[string]interface{}{},
Segments: []MetricValue{
{
SegmentName: map[string]string{},
Value: map[string]interface{}{},
Segments: []MetricValue{
{
SegmentName: map[string]string{
"request_url_host": "",
},
Value: map[string]interface{}{
"users_count.unique": 44,
},
Segments: nil,
Interval: "",
Start: nil,
End: nil,
},
},
Interval: "",
Start: nil,
End: nil,
},
},
Interval: "P5M",
Start: timestamp1,
End: timestamp1,
SegmentName: map[string]string{"request_url_host": ""},
Value: map[string]interface{}{"users_count.unique": 44},
T: ts[0],
},
{
SegmentName: map[string]string{},
Value: map[string]interface{}{},
Segments: []MetricValue{
{
SegmentName: map[string]string{},
Value: map[string]interface{}{},
Segments: []MetricValue{
{
SegmentName: map[string]string{
"request_url_host": "",
},
Value: map[string]interface{}{
"sessions_count.unique": 44,
},
Segments: nil,
Interval: "",
Start: nil,
End: nil,
},
},
Interval: "",
Start: nil,
End: nil,
},
},
Interval: "P5M",
Start: timestamp2,
End: timestamp2,
SegmentName: map[string]string{"request_url_host": ""},
Value: map[string]interface{}{"sessions_count.unique": 44},
T: ts[1],
},
{
SegmentName: map[string]string{},
Value: map[string]interface{}{},
Segments: []MetricValue{
{
SegmentName: map[string]string{},
Value: map[string]interface{}{},
Segments: []MetricValue{
{
SegmentName: map[string]string{
"request_url_host": "localhost",
},
Value: map[string]interface{}{
"sessions_count.unique": 44,
SegmentName: map[string]string{"request_url_host": "localhost"},
Value: map[string]interface{}{"sessions_count.unique": 44},
T: ts[2],
},
}

mv := make([]MetricValue, 0, 3)
for i := range vals {
mv = append(mv,
MetricValue{
SegmentName: map[string]string{},
Value: map[string]interface{}{},
Segments: []MetricValue{
{
SegmentName: map[string]string{},
Value: map[string]interface{}{},
Segments: []MetricValue{
{
SegmentName: vals[i].SegmentName,
Value: vals[i].Value,
},
Segments: nil,
Interval: "",
Start: nil,
End: nil,
},
},
Interval: "",
Start: nil,
End: nil,
},
Interval: "P5M",
Start: vals[i].T, End: vals[i].T,
},
Interval: "P5M",
Start: timestamp3,
End: timestamp3,
},
)
}

return mv
}

func TestGroupMetrics(t *testing.T) {
Expand All @@ -128,10 +91,8 @@ func TestGroupMetrics(t *testing.T) {
Value: map[string]interface{}{
"users_count.unique": 44,
},
Segments: nil,
Interval: "",
Start: timestamp1,
End: timestamp1,
Start: timestamp1,
End: timestamp1,
},
{
SegmentName: map[string]string{
Expand All @@ -140,10 +101,8 @@ func TestGroupMetrics(t *testing.T) {
Value: map[string]interface{}{
"sessions_count.unique": 44,
},
Segments: nil,
Interval: "",
Start: timestamp2,
End: timestamp2,
Start: timestamp2,
End: timestamp2,
},
}

Expand All @@ -155,10 +114,8 @@ func TestGroupMetrics(t *testing.T) {
Value: map[string]interface{}{
"sessions_count.unique": 44,
},
Segments: nil,
Interval: "",
Start: timestamp3,
End: timestamp3,
Start: timestamp3,
End: timestamp3,
},
}

Expand Down Expand Up @@ -205,10 +162,8 @@ func TestGroupMetrics(t *testing.T) {
Value: map[string]interface{}{
"users_count.unique": 44,
},
Segments: nil,
Interval: "",
Start: timestamp1,
End: timestamp1,
Start: timestamp1,
End: timestamp1,
},
{
SegmentName: map[string]string{
Expand All @@ -217,10 +172,8 @@ func TestGroupMetrics(t *testing.T) {
Value: map[string]interface{}{
"sessions_count.unique": 44,
},
Segments: nil,
Interval: "",
Start: timestamp2,
End: timestamp2,
Start: timestamp2,
End: timestamp2,
},
}

Expand All @@ -232,10 +185,8 @@ func TestGroupMetrics(t *testing.T) {
Value: map[string]interface{}{
"sessions_count.unique": 44,
},
Segments: nil,
Interval: "",
Start: timestamp3,
End: timestamp3,
Start: timestamp3,
End: timestamp3,
},
}

Expand All @@ -247,10 +198,8 @@ func TestGroupMetrics(t *testing.T) {
Value: map[string]interface{}{
"users_count.unique": 44,
},
Segments: nil,
Interval: "",
Start: timestamp1,
End: timestamp1,
Start: timestamp1,
End: timestamp1,
},
}

Expand All @@ -262,10 +211,8 @@ func TestGroupMetrics(t *testing.T) {
Value: map[string]interface{}{
"sessions_count.unique": 44,
},
Segments: nil,
Interval: "",
Start: timestamp2,
End: timestamp2,
Start: timestamp2,
End: timestamp2,
},
}

Expand All @@ -277,10 +224,8 @@ func TestGroupMetrics(t *testing.T) {
Value: map[string]interface{}{
"sessions_count.unique": 44,
},
Segments: nil,
Interval: "",
Start: timestamp3,
End: timestamp3,
Start: timestamp3,
End: timestamp3,
},
}

Expand Down

0 comments on commit 4eefdd9

Please sign in to comment.