Create SplitEntryMap type to simplify some definitions.

pull/11602/head
Andriy Dzikh 2021-06-09 15:21:43 -07:00
parent fb8e4d982b
commit e089973f65
2 changed files with 14 additions and 11 deletions

View File

@ -60,6 +60,9 @@ type TestEntry struct {
status string status string
} }
// A map with keys of (environment, test_name) to values of slcies of TestEntry.
type SplitEntryMap map[string]map[string][]TestEntry
// Reads CSV `file` and consumes each line to be a single TestEntry. // Reads CSV `file` and consumes each line to be a single TestEntry.
func ReadData(file io.Reader) []TestEntry { func ReadData(file io.Reader) []TestEntry {
testEntries := []TestEntry{} testEntries := []TestEntry{}
@ -110,8 +113,8 @@ func ReadData(file io.Reader) []TestEntry {
} }
// Splits `testEntries` up into maps indexed first by environment and then by test. // Splits `testEntries` up into maps indexed first by environment and then by test.
func SplitData(testEntries []TestEntry) map[string]map[string][]TestEntry { func SplitData(testEntries []TestEntry) SplitEntryMap {
splitEntries := make(map[string]map[string][]TestEntry) splitEntries := make(SplitEntryMap)
for _, entry := range testEntries { for _, entry := range testEntries {
appendEntry(splitEntries, entry.environment, entry.name, entry) appendEntry(splitEntries, entry.environment, entry.name, entry)
@ -121,7 +124,7 @@ func SplitData(testEntries []TestEntry) map[string]map[string][]TestEntry {
} }
// Appends `entry` to `splitEntries` at the `environment` and `test`. // Appends `entry` to `splitEntries` at the `environment` and `test`.
func appendEntry(splitEntries map[string]map[string][]TestEntry, environment, test string, entry TestEntry) { func appendEntry(splitEntries SplitEntryMap, environment, test string, entry TestEntry) {
// Lookup the environment. // Lookup the environment.
environmentSplit, ok := splitEntries[environment] environmentSplit, ok := splitEntries[environment]
if !ok { if !ok {
@ -141,8 +144,8 @@ func appendEntry(splitEntries map[string]map[string][]TestEntry, environment, te
} }
// Filters `splitEntries` to include only the most recent `date_range` dates. // Filters `splitEntries` to include only the most recent `date_range` dates.
func FilterRecentEntries(splitEntries map[string]map[string][]TestEntry, dateRange uint) map[string]map[string][]TestEntry { func FilterRecentEntries(splitEntries SplitEntryMap, dateRange uint) SplitEntryMap {
filteredEntries := make(map[string]map[string][]TestEntry) filteredEntries := make(SplitEntryMap)
for environment, environmentSplit := range splitEntries { for environment, environmentSplit := range splitEntries {
for test, testSplit := range environmentSplit { for test, testSplit := range environmentSplit {
@ -189,7 +192,7 @@ func FilterRecentEntries(splitEntries map[string]map[string][]TestEntry, dateRan
} }
// Computes the flake rates over each entry in `splitEntries`. // Computes the flake rates over each entry in `splitEntries`.
func ComputeFlakeRates(splitEntries map[string]map[string][]TestEntry) map[string]map[string]float32 { func ComputeFlakeRates(splitEntries SplitEntryMap) map[string]map[string]float32 {
flakeRates := make(map[string]map[string]float32) flakeRates := make(map[string]map[string]float32)
for environment, environmentSplit := range splitEntries { for environment, environmentSplit := range splitEntries {
for test, testSplit := range environmentSplit { for test, testSplit := range environmentSplit {

View File

@ -94,7 +94,7 @@ func TestReadData(t *testing.T) {
compareEntrySlices(t, actualData, expectedData, "") compareEntrySlices(t, actualData, expectedData, "")
} }
func compareSplitData(t *testing.T, actual, expected map[string]map[string][]TestEntry) { func compareSplitData(t *testing.T, actual, expected SplitEntryMap) {
for environment, actualTests := range actual { for environment, actualTests := range actual {
expectedTests, environmentOk := expected[environment] expectedTests, environmentOk := expected[environment]
if !environmentOk { if !environmentOk {
@ -159,7 +159,7 @@ func TestSplitData(t *testing.T) {
status: "Passed", status: "Passed",
} }
actual := SplitData([]TestEntry{entryE1T1_1, entryE1T1_2, entryE1T2, entryE2T1, entryE2T2}) actual := SplitData([]TestEntry{entryE1T1_1, entryE1T1_2, entryE1T2, entryE2T1, entryE2T2})
expected := map[string]map[string][]TestEntry{ expected := SplitEntryMap{
"env1": { "env1": {
"test1": {entryE1T1_1, entryE1T1_2}, "test1": {entryE1T1_1, entryE1T1_2},
"test2": {entryE1T2}, "test2": {entryE1T2},
@ -233,7 +233,7 @@ func TestFilterRecentEntries(t *testing.T) {
status: "Passed", status: "Passed",
} }
actualData := FilterRecentEntries(map[string]map[string][]TestEntry{ actualData := FilterRecentEntries(SplitEntryMap{
"env1": { "env1": {
"test1": { "test1": {
entryE1T1R1, entryE1T1R1,
@ -257,7 +257,7 @@ func TestFilterRecentEntries(t *testing.T) {
}, },
}, 2) }, 2)
expectedData := map[string]map[string][]TestEntry{ expectedData := SplitEntryMap{
"env1": { "env1": {
"test1": { "test1": {
entryE1T1R1, entryE1T1R1,
@ -281,7 +281,7 @@ func TestFilterRecentEntries(t *testing.T) {
} }
func TestComputeFlakeRates(t *testing.T) { func TestComputeFlakeRates(t *testing.T) {
actualData := ComputeFlakeRates(map[string]map[string][]TestEntry{ actualData := ComputeFlakeRates(SplitEntryMap{
"env1": { "env1": {
"test1": { "test1": {
{ {