-
Notifications
You must be signed in to change notification settings - Fork 9
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
a4affba
commit bf7aab8
Showing
23 changed files
with
688 additions
and
103 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
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
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
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,62 @@ | ||
// PROPRIETARY AND CONFIDENTIAL | ||
// | ||
// Unauthorized copying of this file via any medium is strictly prohibited. | ||
// | ||
// Copyright (c) 2020-2021 Snowplow Analytics Ltd. All rights reserved. | ||
|
||
package models | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/snowplow-devops/stream-replicator/pkg/common" | ||
) | ||
|
||
// FilterResult contains the results from a target write operation | ||
type FilterResult struct { | ||
FilteredCount int64 | ||
|
||
// Filtered holds all the messages that were filtered out and acked without sending to the target | ||
Filtered []*Message | ||
|
||
// Delta between TimePulled and TimeOfAck tells us how well the | ||
// application is at processing filtered data internally | ||
MaxFilterLatency time.Duration | ||
MinFilterLatency time.Duration | ||
AvgFilterLatency time.Duration | ||
} | ||
|
||
// NewFilterResult uses the current time as the timeOfFilter and then calls NewFilterResultWithTime | ||
func NewFilterResult(filtered []*Message) *FilterResult { | ||
return NewFilterResultWithTime(filtered, time.Now().UTC()) | ||
} | ||
|
||
// NewFilterResultWithTime builds a result structure to return from a filtered message slice | ||
// attempt which contains the sfiltered message count as well as several | ||
// derived latency measures. | ||
func NewFilterResultWithTime(filtered []*Message, timeOfFilter time.Time) *FilterResult { | ||
r := FilterResult{ | ||
FilteredCount: int64(len(filtered)), | ||
} | ||
|
||
filteredLen := int64(len(filtered)) | ||
|
||
var sumFilterLatency time.Duration | ||
|
||
for _, msg := range filtered { | ||
filterLatency := timeOfFilter.Sub(msg.TimePulled) | ||
if r.MaxFilterLatency < filterLatency { | ||
r.MaxFilterLatency = filterLatency | ||
} | ||
if r.MinFilterLatency > filterLatency || r.MinFilterLatency == time.Duration(0) { | ||
r.MinFilterLatency = filterLatency | ||
} | ||
sumFilterLatency += filterLatency | ||
} | ||
|
||
if filteredLen > 0 { | ||
r.AvgFilterLatency = common.GetAverageFromDuration(sumFilterLatency, filteredLen) | ||
} | ||
|
||
return &r | ||
} |
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,72 @@ | ||
// PROPRIETARY AND CONFIDENTIAL | ||
// | ||
// Unauthorized copying of this file via any medium is strictly prohibited. | ||
// | ||
// Copyright (c) 2020-2021 Snowplow Analytics Ltd. All rights reserved. | ||
|
||
package models | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewFilterResult_EmptyWithoutTime(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
r := NewFilterResult(nil) | ||
assert.NotNil(r) | ||
|
||
assert.Equal(int64(0), r.FilteredCount) | ||
|
||
assert.Equal(time.Duration(0), r.MaxFilterLatency) | ||
assert.Equal(time.Duration(0), r.MinFilterLatency) | ||
assert.Equal(time.Duration(0), r.AvgFilterLatency) | ||
} | ||
|
||
func TestNewFilterResult_EmptyWithTime(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
r := NewFilterResultWithTime(nil, time.Now().UTC()) | ||
assert.NotNil(r) | ||
|
||
assert.Equal(int64(0), r.FilteredCount) | ||
|
||
assert.Equal(time.Duration(0), r.MaxFilterLatency) | ||
assert.Equal(time.Duration(0), r.MinFilterLatency) | ||
assert.Equal(time.Duration(0), r.AvgFilterLatency) | ||
} | ||
|
||
func TestNewFilterResult_WithMessages(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
timeNow := time.Now().UTC() | ||
|
||
filtered := []*Message{ | ||
{ | ||
Data: []byte("Baz"), | ||
PartitionKey: "partition1", | ||
TimeCreated: timeNow.Add(time.Duration(-50) * time.Minute), | ||
TimePulled: timeNow.Add(time.Duration(-4) * time.Minute), | ||
TimeTransformed: timeNow.Add(time.Duration(-2) * time.Minute), | ||
}, | ||
{ | ||
Data: []byte("Bar"), | ||
PartitionKey: "partition2", | ||
TimeCreated: timeNow.Add(time.Duration(-70) * time.Minute), | ||
TimePulled: timeNow.Add(time.Duration(-8) * time.Minute), | ||
TimeTransformed: timeNow.Add(time.Duration(-4) * time.Minute), | ||
}, | ||
} | ||
|
||
r := NewFilterResultWithTime(filtered, timeNow) | ||
assert.NotNil(r) | ||
|
||
assert.Equal(int64(2), r.FilteredCount) | ||
|
||
assert.Equal(time.Duration(8)*time.Minute, r.MaxFilterLatency) | ||
assert.Equal(time.Duration(4)*time.Minute, r.MinFilterLatency) | ||
assert.Equal(time.Duration(6)*time.Minute, r.AvgFilterLatency) | ||
} |
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
Oops, something went wrong.