Skip to content

Commit

Permalink
feat(#43): line level impact
Browse files Browse the repository at this point in the history
  • Loading branch information
williamfzc committed Jul 13, 2023
1 parent 1b3104d commit 7c85e39
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cmd/srctx/diff/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func MainDiff(opts *Options) error {
return err
}
case nodeLevelFile:
err = fileLevelMain(opts, lineMap)
err = fileLevelMain(opts, lineMap, totalLineCountMap)
if err != nil {
return err
}
Expand Down
17 changes: 12 additions & 5 deletions cmd/srctx/diff/core_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import (
"os"

"github.com/gocarina/gocsv"
"github.com/williamfzc/srctx/object"

"github.com/opensibyl/sibyl2/pkg/core"
log "github.com/sirupsen/logrus"
"github.com/williamfzc/srctx/diff"
"github.com/williamfzc/srctx/graph/file"
)

func fileLevelMain(opts *Options, lineMap diff.AffectedLineMap) error {
func fileLevelMain(opts *Options, lineMap diff.AffectedLineMap, totalLineCountMap map[string]int) error {
log.Infof("file level main entry")
fileGraph, err := createFileGraph(opts)
if err != nil {
Expand All @@ -28,10 +26,19 @@ func fileLevelMain(opts *Options, lineMap diff.AffectedLineMap) error {
startPoints = append(startPoints, pv)
}
// start scan
stats := make([]*object.ImpactUnit, 0)
stats := make([]*ImpactUnitWithFile, 0)
for _, eachPtr := range startPoints {
eachStat := fileGraph.Stat(eachPtr)
stats = append(stats, eachStat)
wrappedStat := WrapImpactUnitWithFile(eachStat)

// fill with file info
if totalLineCount, ok := totalLineCountMap[eachStat.FileName]; ok {
wrappedStat.TotalLineCount = totalLineCount
}
if affectedLineCount, ok := lineMap[eachStat.FileName]; ok {
wrappedStat.AffectedLineCount = len(affectedLineCount)
}
stats = append(stats, wrappedStat)
log.Infof("start point: %v, refed: %d, ref: %d", eachPtr.Id(), len(eachStat.ReferencedIds), len(eachStat.ReferenceIds))
}
log.Infof("diff finished.")
Expand Down
21 changes: 17 additions & 4 deletions cmd/srctx/diff/core_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"errors"
"os"

"github.com/williamfzc/srctx/object"

"github.com/gocarina/gocsv"
"github.com/opensibyl/sibyl2/pkg/core"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -35,10 +33,25 @@ func funcLevelMain(opts *Options, lineMap diff.AffectedLineMap, totalLineCountMa
}

// start scan
stats := make([]*object.ImpactUnit, 0)
stats := make([]*ImpactUnitWithFile, 0)
for _, eachPtr := range startPoints {
eachStat := funcGraph.Stat(eachPtr)
stats = append(stats, eachStat)
wrappedStat := WrapImpactUnitWithFile(eachStat)

totalLineCount := len(eachPtr.GetSpan().Lines())
affectedLineCount := 0

if lines, ok := lineMap[eachPtr.Path]; ok {
for _, eachLine := range lines {
if eachPtr.GetSpan().ContainLine(eachLine) {
affectedLineCount++
}
}
}
wrappedStat.TotalLineCount = totalLineCount
wrappedStat.AffectedLineCount = affectedLineCount

stats = append(stats, wrappedStat)
log.Infof("start point: %v, refed: %d, ref: %d", eachPtr.Id(), len(eachStat.ReferencedIds), len(eachStat.ReferenceIds))
}
log.Infof("diff finished.")
Expand Down
27 changes: 14 additions & 13 deletions cmd/srctx/diff/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"

"github.com/williamfzc/srctx/object"

"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -101,19 +103,18 @@ func NewOptionsFromJSONFile(fp string) (*Options, error) {
return &opts, nil
}

type FileVertex struct {
FileName string `csv:"fileName" json:"fileName"`
AffectedLinePercent float32 `csv:"affectedLinePercent" json:"affectedLinePercent"`
AffectedFunctionPercent float32 `csv:"affectedFunctionPercent" json:"affectedFunctionPercent"`
AffectedReferencePercent float32 `csv:"affectedReferencePercent" json:"affectedReferencePercent"`

AffectedLines int `csv:"affectedLines" json:"affectedLines"`
TotalLines int `csv:"totalLines" json:"totalLines"`
type ImpactUnitWithFile struct {
*object.ImpactUnit

AffectedFunctions int `csv:"affectedFunctions" json:"affectedFunctions"`
TotalFunctions int `csv:"totalFunctions" json:"totalFunctions"`
// line level impact
AffectedLineCount int `csv:"affectedLineCount" json:"affectedLineCount"`
TotalLineCount int `csv:"totalLineCount" json:"totalLineCount"`
}

AffectedReferences int `csv:"affectedReferences" json:"affectedReferences"`
AffectedReferenceIds []string `csv:"-" json:"-"`
TotalReferences int `csv:"totalReferences" json:"totalReferences"`
func WrapImpactUnitWithFile(impactUnit *object.ImpactUnit) *ImpactUnitWithFile {
return &ImpactUnitWithFile{
ImpactUnit: impactUnit,
AffectedLineCount: 0,
TotalLineCount: 0,
}
}
17 changes: 7 additions & 10 deletions object/stat.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package object

type FileImpactPart struct {
// file level
type FileInfoPart struct {
FileName string `csv:"fileName" json:"fileName"`

// line level impact
AffectedLineCount int `csv:"affectedLineCount" json:"affectedLineCount"`
TotalLineCount int `csv:"totalLineCount" json:"totalLineCount"`
// actually graph will not access the real file system
// so of course it knows nothing about the real files
// all the data we can access is from the indexing file
}

type UnitImpactPart struct {
Expand Down Expand Up @@ -35,17 +34,15 @@ type ImpactDetails struct {
}

type ImpactUnit struct {
*FileImpactPart
*FileInfoPart
*UnitImpactPart
*ImpactDetails `json:"-" csv:"-"`
}

func NewImpactUnit() *ImpactUnit {
return &ImpactUnit{
FileImpactPart: &FileImpactPart{
FileName: "",
AffectedLineCount: 0,
TotalLineCount: 0,
FileInfoPart: &FileInfoPart{
FileName: "",
},
UnitImpactPart: &UnitImpactPart{
UnitName: "",
Expand Down

0 comments on commit 7c85e39

Please sign in to comment.