go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/testfailuredetection/rank.go (about) 1 // Copyright 2023 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package testfailuredetection 16 17 import ( 18 "context" 19 "fmt" 20 "sort" 21 "strings" 22 "time" 23 24 "go.chromium.org/luci/bisection/model" 25 "go.chromium.org/luci/common/logging" 26 ) 27 28 // First sorts the input slice and returns the first element in the sorted slice. 29 func First(ctx context.Context, bundles []*model.TestFailureBundle) *model.TestFailureBundle { 30 Sort(bundles) 31 // Log the sorted bundles for debugging purpose. 32 lines := []string{} 33 for i, b := range bundles { 34 tf := b.Primary() 35 line := fmt.Sprintf("%d. primary test %s(%s), redundancy score %f, regression end position %d", i, tf.TestID, tf.VariantHash, tf.RedundancyScore, tf.RegressionEndPosition) 36 lines = append(lines, line) 37 } 38 logging.Infof(ctx, fmt.Sprintf("sorted bundles\n%s", strings.Join(lines, "\n"))) 39 return bundles[0] 40 } 41 42 // Sort sorts slice of testFailureBundles by their 43 // redundancy score and regression end position. 44 func Sort(bundles []*model.TestFailureBundle) { 45 sort.Sort(sortableTestFailureBundles(bundles)) 46 } 47 48 type sortableTestFailureBundles []*model.TestFailureBundle 49 50 // Len is the number of elements in the collection. 51 func (s sortableTestFailureBundles) Len() int { 52 return len(s) 53 } 54 55 // Less reports whether the element with 56 // index i should sort before the element with index j. 57 func (s sortableTestFailureBundles) Less(i int, j int) bool { 58 // We sort test bundle by recency descendingly first (truncated to day), and by redundancy score ascendingly. 59 di := s[i].Primary().StartHour.Truncate(time.Hour * 24) 60 dj := s[j].Primary().StartHour.Truncate(time.Hour * 24) 61 return di.After(dj) || (di == dj && s[i].Primary().RedundancyScore < s[j].Primary().RedundancyScore) 62 } 63 64 // Swap swaps the elements with indexes i and j. 65 func (s sortableTestFailureBundles) Swap(i int, j int) { 66 s[i], s[j] = s[j], s[i] 67 }