-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: parse replay configuration from backend config (#3703)
- Loading branch information
Showing
5 changed files
with
244 additions
and
7 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,80 @@ | ||
package backendconfig | ||
|
||
import ( | ||
"github.com/samber/lo" | ||
) | ||
|
||
type EventReplayConfigs map[string]*EventReplayConfig | ||
|
||
// ApplyReplaySources reads the event replay configuration and adds replay sources to the config | ||
// A replay source is a copy of the original source with a different ID and source definition | ||
// This replay source contains as destinations replay destinations which are copies of the original destinations but with a different ID | ||
func (config *ConfigT) ApplyReplaySources() { | ||
if len(config.EventReplays) == 0 { | ||
return | ||
} | ||
originalSources := config.SourcesMap() | ||
originalDestinations := config.DestinationsMap() | ||
for _, replay := range config.EventReplays { | ||
sources := lo.OmitByValues(lo.MapValues(replay.Sources, func(value EventReplaySource, id string) *SourceT { | ||
s, ok := originalSources[value.OriginalSourceID] | ||
if !ok { | ||
return nil | ||
} | ||
newSource := *s | ||
newSource.ID = id | ||
newSource.OriginalID = s.ID | ||
newSource.WriteKey = id | ||
newSource.EventSchemasEnabled = false | ||
newSource.Config = lo.OmitByKeys(newSource.Config, []string{"eventUpload"}) // no event uploads for replay sources for now | ||
newSource.Destinations = nil // destinations are added later | ||
return &newSource | ||
}), []*SourceT{nil}) | ||
destinations := lo.OmitByValues(lo.MapValues(replay.Destinations, func(value EventReplayDestination, id string) *DestinationT { | ||
d, ok := originalDestinations[value.OriginalDestinationID] | ||
if !ok { | ||
return nil | ||
} | ||
newDestination := *d | ||
newDestination.ID = id | ||
return &newDestination | ||
}), []*DestinationT{nil}) | ||
|
||
// add destinations to sources | ||
for _, connection := range replay.Connections { | ||
source, ok := sources[connection.SourceID] | ||
if !ok { | ||
continue | ||
} | ||
destination, ok := destinations[connection.DestinationID] | ||
if !ok { | ||
continue | ||
} | ||
source.Destinations = append(source.Destinations, *destination) | ||
} | ||
|
||
// add replay sources to config, only the ones that have destinations | ||
config.Sources = append(config.Sources, lo.FilterMap(lo.Values(sources), func(source *SourceT, _ int) (SourceT, bool) { | ||
return *source, len(source.Destinations) > 0 | ||
})...) | ||
} | ||
} | ||
|
||
type EventReplayConfig struct { | ||
Sources map[string]EventReplaySource `json:"sources"` | ||
Destinations map[string]EventReplayDestination `json:"destinations"` | ||
Connections []EventReplayConnection `json:"connections"` | ||
} | ||
|
||
type EventReplaySource struct { | ||
OriginalSourceID string `json:"originalId"` | ||
} | ||
|
||
type EventReplayDestination struct { | ||
OriginalDestinationID string `json:"originalId"` | ||
} | ||
|
||
type EventReplayConnection struct { | ||
SourceID string `json:"sourceId"` | ||
DestinationID string `json:"destinationId"` | ||
} |
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,134 @@ | ||
package backendconfig | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestApplyReplayConfig(t *testing.T) { | ||
t.Run("Valid Replay Config", func(t *testing.T) { | ||
c := &ConfigT{ | ||
Sources: []SourceT{ | ||
{ | ||
ID: "s-1", | ||
Config: map[string]interface{}{"eventUpload": true}, | ||
SourceDefinition: SourceDefinitionT{ | ||
ID: "sd-1", | ||
Type: "type-1", | ||
Category: "category-1", | ||
}, | ||
Destinations: []DestinationT{ | ||
{ | ||
ID: "d-1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
EventReplays: map[string]EventReplayConfig{ | ||
"er-1": { | ||
Sources: map[string]EventReplaySource{ | ||
"er-s-1": { | ||
OriginalSourceID: "s-1", | ||
}, | ||
}, | ||
Destinations: map[string]EventReplayDestination{ | ||
"er-d-1": { | ||
OriginalDestinationID: "d-1", | ||
}, | ||
}, | ||
Connections: []EventReplayConnection{ | ||
{ | ||
SourceID: "er-s-1", | ||
DestinationID: "er-d-1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
c.ApplyReplaySources() | ||
|
||
require.Len(t, c.Sources, 2) | ||
require.Equal(t, "s-1", c.Sources[0].ID) | ||
require.Equal(t, "er-s-1", c.Sources[1].ID) | ||
require.Equal(t, "s-1", c.Sources[1].OriginalID) | ||
require.Equal(t, "er-s-1", c.Sources[1].WriteKey) | ||
require.Equal(t, map[string]interface{}{}, c.Sources[1].Config) | ||
require.Len(t, c.Sources[1].Destinations, 1) | ||
require.Equal(t, "er-d-1", c.Sources[1].Destinations[0].ID) | ||
}) | ||
|
||
t.Run("Invalid Replay Config", func(t *testing.T) { | ||
c := &ConfigT{ | ||
Sources: []SourceT{ | ||
{ | ||
ID: "s-1", | ||
Config: map[string]interface{}{"eventUpload": true}, | ||
SourceDefinition: SourceDefinitionT{ | ||
ID: "sd-1", | ||
Type: "type-1", | ||
Category: "category-1", | ||
}, | ||
Destinations: []DestinationT{ | ||
{ | ||
ID: "d-1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
EventReplays: map[string]EventReplayConfig{ | ||
"er-1": { | ||
Sources: map[string]EventReplaySource{ | ||
"er-s-1": { | ||
OriginalSourceID: "s-1", | ||
}, | ||
"er-s-2": { | ||
OriginalSourceID: "s-2", | ||
}, | ||
}, | ||
Destinations: map[string]EventReplayDestination{ | ||
"er-d-1": { | ||
OriginalDestinationID: "d-1", | ||
}, | ||
"er-d-2": { | ||
OriginalDestinationID: "d-2", | ||
}, | ||
}, | ||
Connections: []EventReplayConnection{ | ||
{ | ||
SourceID: "er-s-1", | ||
DestinationID: "er-d-1", | ||
}, | ||
{ | ||
SourceID: "er-s-1", | ||
DestinationID: "er-d-2", | ||
}, | ||
{ | ||
SourceID: "er-s-2", | ||
DestinationID: "er-d-1", | ||
}, | ||
{ | ||
SourceID: "er-s-2", | ||
DestinationID: "er-d-2", | ||
}, | ||
{ | ||
SourceID: "er-s-3", | ||
DestinationID: "er-d-3", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
c.ApplyReplaySources() | ||
|
||
require.Len(t, c.Sources, 2) | ||
require.Equal(t, "s-1", c.Sources[0].ID) | ||
require.Equal(t, "er-s-1", c.Sources[1].ID) | ||
require.Equal(t, "s-1", c.Sources[1].OriginalID) | ||
require.Equal(t, "er-s-1", c.Sources[1].WriteKey) | ||
require.Equal(t, map[string]interface{}{}, c.Sources[1].Config) | ||
require.Len(t, c.Sources[1].Destinations, 1) | ||
require.Equal(t, "er-d-1", c.Sources[1].Destinations[0].ID) | ||
}) | ||
} |
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