goc/pkg/cover/delta.go

137 lines
3.4 KiB
Go
Raw Normal View History

/*
2020-05-25 16:19:20 +00:00
Copyright 2020 Qiniu Cloud (qiniu.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cover
2020-05-22 02:33:03 +00:00
import "sort"
2020-07-31 07:16:55 +00:00
// DeltaCov contains the info of a delta coverage
2020-05-22 02:33:03 +00:00
type DeltaCov struct {
FileName string
BasePer string
NewPer string
DeltaPer string
LineCovLink string
}
2020-07-31 07:16:55 +00:00
// DeltaCovList is the list of DeltaCov
2020-05-22 02:33:03 +00:00
type DeltaCovList []DeltaCov
2020-07-31 07:16:55 +00:00
// GetFullDeltaCov get full delta coverage between new and base profile
func GetFullDeltaCov(newList CoverageList, baseList CoverageList) (delta DeltaCovList) {
2020-05-22 02:33:03 +00:00
newMap := newList.Map()
baseMap := baseList.Map()
2020-05-22 02:33:03 +00:00
for file, n := range newMap {
b, ok := baseMap[file]
//if the file not in base profile, set None
if !ok {
2020-05-22 02:33:03 +00:00
delta = append(delta, DeltaCov{
FileName: file,
BasePer: "None",
NewPer: n.Percentage(),
DeltaPer: PercentStr(Delta(n, b))})
continue
}
2020-05-22 02:33:03 +00:00
delta = append(delta, DeltaCov{
FileName: file,
BasePer: b.Percentage(),
NewPer: n.Percentage(),
DeltaPer: PercentStr(Delta(n, b))})
}
for file, b := range baseMap {
//if the file not in new profile, set None
if n, ok := newMap[file]; !ok {
delta = append(delta, DeltaCov{
FileName: file,
BasePer: b.Percentage(),
NewPer: "None",
DeltaPer: PercentStr(Delta(n, b))})
}
}
return
}
2020-07-31 07:16:55 +00:00
// GetDeltaCov get two profile diff cov
func GetDeltaCov(newList CoverageList, baseList CoverageList) (delta DeltaCovList) {
2020-05-22 02:33:03 +00:00
d := GetFullDeltaCov(newList, baseList)
for _, v := range d {
if v.DeltaPer == "0.0%" {
continue
}
2020-05-22 02:33:03 +00:00
delta = append(delta, v)
}
2020-05-22 02:33:03 +00:00
return
}
2020-07-31 07:16:55 +00:00
// GetChFileDeltaCov get two profile diff cov of changed files
func GetChFileDeltaCov(newList CoverageList, baseList CoverageList, changedFiles []string) (list DeltaCovList) {
2020-05-22 02:33:03 +00:00
d := GetFullDeltaCov(newList, baseList)
dMap := d.Map()
for _, file := range changedFiles {
if _, ok := dMap[file]; ok {
list = append(list, dMap[file])
}
}
return
}
2020-07-31 07:16:55 +00:00
// Delta calculate two coverage delta
func Delta(new Coverage, base Coverage) float32 {
baseRatio, _ := base.Ratio()
newRatio, _ := new.Ratio()
return newRatio - baseRatio
}
2020-05-22 02:33:03 +00:00
2020-07-31 07:16:55 +00:00
// TotalDelta calculate two coverage delta
2020-05-22 02:33:03 +00:00
func TotalDelta(new CoverageList, base CoverageList) float32 {
baseRatio, _ := base.TotalRatio()
newRatio, _ := new.TotalRatio()
return newRatio - baseRatio
}
// Map returns maps the file name to its DeltaCov for faster retrieval & membership check
func (d DeltaCovList) Map() map[string]DeltaCov {
2020-05-22 02:33:03 +00:00
m := make(map[string]DeltaCov)
for _, c := range d {
2020-05-22 02:33:03 +00:00
m[c.FileName] = c
}
return m
}
2020-07-31 07:16:55 +00:00
// Sort sort DeltaCovList with filenames
func (d DeltaCovList) Sort() {
2020-05-22 02:33:03 +00:00
sort.SliceStable(d, func(i, j int) bool {
return d[i].Name() < d[j].Name()
2020-05-22 02:33:03 +00:00
})
}
// Name returns the file name
func (c *DeltaCov) Name() string {
return c.FileName
}
2020-07-31 07:16:55 +00:00
// GetLineCovLink get the LineCovLink of the DeltaCov
2020-05-22 02:33:03 +00:00
func (c *DeltaCov) GetLineCovLink() string {
return c.LineCovLink
}
2020-07-31 07:16:55 +00:00
// SetLineCovLink set LineCovLink of the DeltaCov
2020-05-22 02:33:03 +00:00
func (c *DeltaCov) SetLineCovLink(link string) {
c.LineCovLink = link
}