2020-05-21 06:30:41 +00:00
|
|
|
/*
|
2020-05-25 16:19:20 +00:00
|
|
|
Copyright 2020 Qiniu Cloud (qiniu.com)
|
2020-05-21 06:30:41 +00:00
|
|
|
|
|
|
|
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-05-21 06:30:41 +00:00
|
|
|
|
2020-05-22 02:33:03 +00:00
|
|
|
type DeltaCov struct {
|
|
|
|
FileName string
|
|
|
|
BasePer string
|
|
|
|
NewPer string
|
|
|
|
DeltaPer string
|
|
|
|
LineCovLink string
|
2020-05-21 06:30:41 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 02:33:03 +00:00
|
|
|
type DeltaCovList []DeltaCov
|
|
|
|
|
|
|
|
// get full delta coverage between new and base profile
|
2020-05-29 02:58:24 +00:00
|
|
|
func GetFullDeltaCov(newList CoverageList, baseList CoverageList) (delta DeltaCovList) {
|
2020-05-22 02:33:03 +00:00
|
|
|
newMap := newList.Map()
|
|
|
|
baseMap := baseList.Map()
|
2020-05-21 06:30:41 +00:00
|
|
|
|
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
|
2020-05-21 06:30:41 +00:00
|
|
|
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))})
|
2020-05-21 06:30:41 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
//get two profile diff cov
|
2020-05-29 02:58:24 +00:00
|
|
|
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%" {
|
2020-05-21 06:30:41 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-05-22 02:33:03 +00:00
|
|
|
delta = append(delta, v)
|
2020-05-21 06:30:41 +00:00
|
|
|
}
|
2020-05-22 02:33:03 +00:00
|
|
|
return
|
|
|
|
}
|
2020-05-21 06:30:41 +00:00
|
|
|
|
2020-05-22 02:33:03 +00:00
|
|
|
//get two profile diff cov of changed files
|
2020-05-29 02:58:24 +00:00
|
|
|
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-05-21 06:30:41 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 02:33:03 +00:00
|
|
|
//calculate two coverage delta
|
2020-05-21 06:30:41 +00:00
|
|
|
func Delta(new Coverage, base Coverage) float32 {
|
|
|
|
baseRatio, _ := base.Ratio()
|
|
|
|
newRatio, _ := new.Ratio()
|
|
|
|
return newRatio - baseRatio
|
|
|
|
}
|
2020-05-22 02:33:03 +00:00
|
|
|
|
|
|
|
//calculate two coverage delta
|
|
|
|
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
|
2020-05-29 02:58:24 +00:00
|
|
|
func (d DeltaCovList) Map() map[string]DeltaCov {
|
2020-05-22 02:33:03 +00:00
|
|
|
m := make(map[string]DeltaCov)
|
2020-05-29 02:58:24 +00:00
|
|
|
for _, c := range d {
|
2020-05-22 02:33:03 +00:00
|
|
|
m[c.FileName] = c
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort DeltaCovList with filenames
|
2020-05-29 02:58:24 +00:00
|
|
|
func (d DeltaCovList) Sort() {
|
2020-05-22 02:33:03 +00:00
|
|
|
sort.SliceStable(d, func(i, j int) bool {
|
2020-05-29 02:58:24 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DeltaCov) GetLineCovLink() string {
|
|
|
|
return c.LineCovLink
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DeltaCov) SetLineCovLink(link string) {
|
|
|
|
c.LineCovLink = link
|
|
|
|
}
|