Skip to content

Commit

Permalink
Merge pull request #1241 from sultan-duisenbay/resultstore_build_over…
Browse files Browse the repository at this point in the history
…ride

Add build override feature
  • Loading branch information
google-oss-prow[bot] authored Jul 20, 2023
2 parents 23233ed + ef6eeb7 commit b9eca6e
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 8 deletions.
36 changes: 33 additions & 3 deletions pkg/updater/resultstore/resultstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func ResultStoreColumnReader(client *DownloadClient, reprocess time.Duration) up

for _, pr := range processedResults {
// TODO: Make group ID something other than start time.
inflatedCol := processGroup(pr)
inflatedCol := processGroup(tg, pr)
receivers <- *inflatedCol
}
return nil
Expand Down Expand Up @@ -184,6 +184,7 @@ func isolateActions(log logrus.FieldLogger, multiActionResults map[string]*multi
}
return singleActionResults
}

func timestampMilliseconds(t *timestamppb.Timestamp) float64 {
return float64(t.GetSeconds())*1000.0 + float64(t.GetNanos())/1000.0
}
Expand All @@ -205,19 +206,25 @@ var convertStatus = map[resultstorepb.Status]statuspb.TestStatus{
resultstorepb.Status_SKIPPED: statuspb.TestStatus_PASS_WITH_SKIPS,
}

func processGroup(result *processedResult) *updater.InflatedColumn {
// processGroup will convert grouped invocations into columns
func processGroup(tg *configpb.TestGroup, result *processedResult) *updater.InflatedColumn {
if result == nil || result.InvocationProto == nil {
return nil
}

started := result.InvocationProto.GetTiming().GetStartTime()
groupID := result.InvocationProto.GetId().GetInvocationId()
build := identifyBuild(tg, result)
// override build by invocation ID
if build == "" {
build = groupID
}
hint, err := started.AsTime().MarshalText()
if err != nil {
hint = []byte{}
}
col := &statepb.Column{
Build: groupID,
Build: build,
Name: groupID,
Started: timestampMilliseconds(started),
Hint: string(hint),
Expand Down Expand Up @@ -253,6 +260,29 @@ func processGroup(result *processedResult) *updater.InflatedColumn {
}
}

// identifyBuild applies build override configurations and assigns a build
// Returns an empty string if no configurations are present or no configs are correctly set.
// i.e. no key is found in properties.
func identifyBuild(tg *configpb.TestGroup, pr *processedResult) string {
switch {
case tg.GetBuildOverrideConfigurationValue() != "":
key := tg.GetBuildOverrideConfigurationValue()
for _, property := range pr.InvocationProto.GetProperties() {
if property.GetKey() == key {
return property.GetValue()
}
}
return ""
case tg.GetBuildOverrideStrftime() != "":
layout := updater.FormatStrftime(tg.BuildOverrideStrftime)
timing := pr.InvocationProto.GetTiming().GetStartTime()
startTime := time.Unix(timing.Seconds, int64(timing.Nanos)).UTC()
return startTime.Format(layout)
default:
return ""
}
}

func queryAfter(query string, when time.Time) string {
if query == "" {
return ""
Expand Down
93 changes: 92 additions & 1 deletion pkg/updater/resultstore/resultstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ func TestProcessGroup(t *testing.T) {
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := processGroup(tc.result)
got := processGroup(nil, tc.result)
if diff := cmp.Diff(tc.want, got, protocmp.Transform()); diff != "" {
t.Errorf("processGroup() differed (-want, +got): %s", diff)
}
Expand Down Expand Up @@ -1077,3 +1077,94 @@ func TestUpdateStop(t *testing.T) {
})
}
}

func TestIdentifyBuild(t *testing.T) {
cases := []struct {
name string
result *processedResult
tg *configpb.TestGroup
want string
}{
{
name: "no override configurations",
result: &processedResult{
InvocationProto: &resultstore.Invocation{
Name: invocationName("id-123"),
Id: &resultstore.Invocation_Id{
InvocationId: "id-123",
},
},
},
want: "",
},
{
name: "override by non-existent property key",
result: &processedResult{
InvocationProto: &resultstore.Invocation{
Name: invocationName("id-1234"),
Id: &resultstore.Invocation_Id{
InvocationId: "id-1234",
},
Properties: []*resultstore.Property{
{Key: "Luigi", Value: "Peaches"},
{Key: "Bowser", Value: "Pingui"},
},
},
},
tg: &configpb.TestGroup{
BuildOverrideConfigurationValue: "Mario",
},
want: "",
},
{
name: "override by existent property key",
result: &processedResult{
InvocationProto: &resultstore.Invocation{
Name: invocationName("id-1234"),
Id: &resultstore.Invocation_Id{
InvocationId: "id-1234",
},
Properties: []*resultstore.Property{
{Key: "Luigi", Value: "Peaches"},
{Key: "Bowser", Value: "Pingui"},
{Key: "Waluigi", Value: "Wapeaches"},
},
},
},
tg: &configpb.TestGroup{
BuildOverrideConfigurationValue: "Waluigi",
},
want: "Wapeaches",
},
{
name: "override by build time strf",
result: &processedResult{
InvocationProto: &resultstore.Invocation{
Name: invocationName("id-1234"),
Id: &resultstore.Invocation_Id{
InvocationId: "id-1234",
},
Timing: &resultstore.Timing{
StartTime: &timestamppb.Timestamp{
Seconds: 1689881216,
Nanos: 27847,
},
},
},
},
tg: &configpb.TestGroup{
BuildOverrideStrftime: "%Y-%m-%d-%H",
},
want: "2023-07-20-19",
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := identifyBuild(tc.tg, tc.result)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("queryAfter(...) differed (-want, +got): %s", diff)
}
})
}
}
6 changes: 3 additions & 3 deletions pkg/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,10 +870,10 @@ func deletedColumn(latestColumn InflatedColumn) []InflatedColumn {
}
}

// formatStrftime replaces python codes with what go expects.
// FormatStrftime replaces python codes with what go expects.
//
// aka %Y-%m-%d becomes 2006-01-02
func formatStrftime(in string) string {
func FormatStrftime(in string) string {
replacements := map[string]string{
"%p": "PM",
"%Y": "2006",
Expand All @@ -898,7 +898,7 @@ func overrideBuild(tg *configpb.TestGroup, cols []InflatedColumn) {
if fmt == "" {
return
}
fmt = formatStrftime(fmt)
fmt = FormatStrftime(fmt)
for _, col := range cols {
started := int64(col.Column.Started)
when := time.Unix(started/1000, (started%1000)*int64(time.Millisecond/time.Nanosecond))
Expand Down
2 changes: 1 addition & 1 deletion pkg/updater/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2149,7 +2149,7 @@ func TestFormatStrftime(t *testing.T) {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := formatStrftime(tc.name); got != tc.want {
if got := FormatStrftime(tc.name); got != tc.want {
t.Errorf("formatStrftime(%q) got %q want %q", tc.name, got, tc.want)
}
})
Expand Down

0 comments on commit b9eca6e

Please sign in to comment.