github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/robots/coverage/diff/diff.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package diff calculates the difference of two coverage lists and produces a collection of
    18  // individual coverage difference. The result is formatted optionally to a table in coverage bot post
    19  package diff
    20  
    21  import (
    22  	"k8s.io/test-infra/gopherage/pkg/cov/junit/calculation"
    23  )
    24  
    25  // deltaSensitivity is checked against to tell whether the coverage delta is worth reporting. Coverage delta is displayed as a percentage with one decimal place. Any difference smaller than this value will not appear different.
    26  const deltaSensitivity = 0.001
    27  
    28  type coverageChange struct {
    29  	name      string
    30  	baseRatio float32
    31  	newRatio  float32
    32  }
    33  
    34  func isChangeSignificant(baseRatio, newRatio float32) bool {
    35  	diff := newRatio - baseRatio
    36  	if diff < 0 {
    37  		diff = -diff
    38  	}
    39  	return diff > deltaSensitivity
    40  }
    41  
    42  // toMap returns maps the file name to its coverage for faster retrieval
    43  // & membership check
    44  func toMap(g *calculation.CoverageList) map[string]calculation.Coverage {
    45  	m := make(map[string]calculation.Coverage)
    46  	for _, cov := range g.Group {
    47  		m[cov.Name] = cov
    48  	}
    49  	return m
    50  }
    51  
    52  // findChanges compares the newList of coverage against the base list and returns the result
    53  func findChanges(baseList *calculation.CoverageList, newList *calculation.CoverageList) []*coverageChange {
    54  	var changes []*coverageChange
    55  	baseFilesMap := toMap(baseList)
    56  	for _, newCov := range newList.Group {
    57  		baseCov, ok := baseFilesMap[newCov.Name]
    58  		var baseRatio float32
    59  		if !ok {
    60  			baseRatio = -1
    61  		} else {
    62  			baseRatio = baseCov.Ratio()
    63  		}
    64  		newRatio := newCov.Ratio()
    65  		if isChangeSignificant(baseRatio, newRatio) {
    66  			changes = append(changes, &coverageChange{
    67  				name:      newCov.Name,
    68  				baseRatio: baseRatio,
    69  				newRatio:  newRatio,
    70  			})
    71  		}
    72  	}
    73  	return changes
    74  }