-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add logs for debugging * Update main.go * Update main.go * Update main.go * Print missing DownloadTestResults input * Add dimension to the step name * Update state duration calculation * Test printStepsStatesToStartTime * Improve state duration calculation * Code cleanup * Update step_states.go
- Loading branch information
Showing
3 changed files
with
188 additions
and
14 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,100 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"sort" | ||
"time" | ||
|
||
toolresults "google.golang.org/api/toolresults/v1beta3" | ||
) | ||
|
||
type stepStates struct { | ||
name string | ||
stateToStartTime map[string]time.Time | ||
} | ||
|
||
func newStepStates(step toolresults.Step) stepStates { | ||
return stepStates{ | ||
name: createStepNameWithDimensions(step), | ||
stateToStartTime: map[string]time.Time{}, | ||
} | ||
} | ||
|
||
func (s *stepStates) saveState(state string, startTime time.Time) { | ||
if _, ok := s.stateToStartTime[state]; ok { | ||
return | ||
} | ||
|
||
// Haven't seen this state yet -> set state start time | ||
s.stateToStartTime[state] = startTime | ||
} | ||
|
||
func (s *stepStates) print(currentTime time.Time, w io.Writer) { | ||
if _, err := fmt.Fprintln(w, s.name); err != nil { | ||
fmt.Printf("Failed to print step status durations: %s", err) | ||
return | ||
} | ||
|
||
var states []string | ||
for state := range s.stateToStartTime { | ||
states = append(states, state) | ||
} | ||
|
||
sort.Slice(states, func(i, j int) bool { | ||
stateI, stateJ := states[i], states[j] | ||
startTimeI, startTimeJ := s.stateToStartTime[stateI], s.stateToStartTime[stateJ] | ||
|
||
return startTimeI.Before(startTimeJ) | ||
}) | ||
|
||
for i, state := range states { | ||
startTime := s.stateToStartTime[state] | ||
|
||
var endTime time.Time | ||
if i == len(states)-1 { | ||
endTime = currentTime | ||
} else { | ||
nextState := states[i+1] | ||
endTime = s.stateToStartTime[nextState] | ||
} | ||
|
||
duration := endTime.Sub(startTime) | ||
|
||
if _, err := fmt.Fprintf(w, "- time spent in %s state: ~%s\n", state, duration.Round(time.Second).String()); err != nil { | ||
fmt.Printf("Failed to print step status durations: %s", err) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func createStepNameWithDimensions(step toolresults.Step) string { | ||
dimensions := createDimensions(step) | ||
return fmt.Sprintf("%s (%s %s %s %s)", step.Name, dimensions["Model"], dimensions["Version"], dimensions["Orientation"], dimensions["Locale"]) | ||
} | ||
|
||
func updateStepsStates(stepIDtoStates map[string]stepStates, response toolresults.ListStepsResponse) { | ||
for _, step := range response.Steps { | ||
stepStates, ok := stepIDtoStates[step.StepId] | ||
if !ok { | ||
stepStates = newStepStates(*step) | ||
stepIDtoStates[step.StepId] = stepStates | ||
} | ||
|
||
stepStates.saveState(step.State, time.Now()) | ||
} | ||
} | ||
|
||
func printStepsStates(stepIDtoStates map[string]stepStates, currentTime time.Time, w io.Writer) { | ||
var stepIDs []string | ||
for stepID := range stepIDtoStates { | ||
stepIDs = append(stepIDs, stepID) | ||
} | ||
|
||
sort.Strings(stepIDs) | ||
|
||
for _, stepID := range stepIDs { | ||
stepState := stepIDtoStates[stepID] | ||
stepState.print(currentTime, w) | ||
} | ||
} |
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,61 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func testRefTime() time.Time { | ||
return time.Date(2022, 1, 1, 12, 30, 0, 0, time.UTC) | ||
} | ||
|
||
func Test_printStepsStates(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
stepIDToStepStates map[string]stepStates | ||
currentTime time.Time | ||
}{ | ||
{ | ||
name: "", | ||
stepIDToStepStates: map[string]stepStates{ | ||
"ID_1": { | ||
name: "iOS Tests", | ||
stateToStartTime: map[string]time.Time{ | ||
"pending": testRefTime(), | ||
"inProgress": testRefTime().Add(60 * time.Second), | ||
"complete": testRefTime().Add(90 * time.Second), | ||
}, | ||
}, | ||
"ID_2": { | ||
name: "iOS Unit Tests", | ||
stateToStartTime: map[string]time.Time{ | ||
"pending": testRefTime(), | ||
"inProgress": testRefTime().Add(40 * time.Second), | ||
"complete": testRefTime().Add(90 * time.Second), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var b bytes.Buffer | ||
printStepsStates(tt.stepIDToStepStates, testRefTime().Add(90*time.Second), &b) | ||
|
||
actual := b.String() | ||
expected := `iOS Tests | ||
- time spent in pending state: ~1m0s | ||
- time spent in inProgress state: ~30s | ||
- time spent in complete state: ~0s | ||
iOS Unit Tests | ||
- time spent in pending state: ~40s | ||
- time spent in inProgress state: ~50s | ||
- time spent in complete state: ~0s | ||
` | ||
if actual != expected { | ||
t.Fatalf("%s != %s", actual, expected) | ||
} | ||
}) | ||
} | ||
} |