Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/2789/delta mode with codecharta #3016

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions visualization/app/codeCharta/codeCharta.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { getCCFile } from "./util/fileHelper"
import { NameDataPair } from "./codeCharta.model"
import { onStoreChanged } from "./state/angular-redux/onStoreChanged/onStoreChanged"
import { referenceFileSelector } from "./state/selectors/referenceFile/referenceFile.selector"
import { comparisonFileSelector } from "./state/selectors/comparisonFile/comparisonFile.selector"

export class CodeChartaService {
static ROOT_NAME = "root"
static ROOT_PATH = `/${CodeChartaService.ROOT_NAME}`
static ROOT_PATH_COMPARISON = `/${CodeChartaService.ROOT_NAME}`
static readonly CC_FILE_EXTENSION = ".cc.json"
private fileStates: FileState[] = []
private recentFiles: string[] = []
Expand All @@ -20,6 +22,11 @@ export class CodeChartaService {
CodeChartaService.updateRootData(newReferenceFile.map.name)
}
})
unsubscribeComparisonFileSubscription = onStoreChanged(comparisonFileSelector, (_, newComparisonFile) => {
if (newComparisonFile) {
CodeChartaService.updateRootDataComparison(newComparisonFile.map.name)
}
})

static instance: CodeChartaService

Expand Down Expand Up @@ -130,4 +137,8 @@ export class CodeChartaService {
CodeChartaService.ROOT_NAME = rootName
CodeChartaService.ROOT_PATH = `/${CodeChartaService.ROOT_NAME}`
}

static updateRootDataComparison(rootName: string) {
CodeChartaService.ROOT_PATH_COMPARISON = `/${rootName}`
}
}
36 changes: 36 additions & 0 deletions visualization/app/codeCharta/model/files/files.helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {
getFileByFileName,
getVisibleFiles,
getVisibleFileStates,
haveEqualRootNames,
isDeltaState,
isPartialState
} from "./files.helper"
import { FileSelectionState, FileState } from "./files"
import { CCFile, NodeType } from "../../codeCharta.model"

describe("files", () => {
let files: FileState[]
Expand Down Expand Up @@ -188,4 +190,38 @@ describe("files", () => {
expect(fileStatesAvailable(files)).toBeTruthy()
})
})

describe("haveEqualRootNames", () => {
it("should return false if root names are different", () => {
const reference = { map: { name: "rootA", type: NodeType.FOLDER } } as CCFile
const comp = { map: { name: "rootB", type: NodeType.FOLDER } } as CCFile

expect(haveEqualRootNames(reference, comp)).toBeFalsy()
})

it("should return true if we compare root/app/index.ts and root/myApp.ts", () => {
const reference = {
map: {
name: "root",
type: NodeType.FOLDER,
children: [{ name: "myApp.ts", type: NodeType.FILE }]
}
} as CCFile
const comp = {
map: {
name: "root",
type: NodeType.FOLDER,
children: [
{
name: "app",
type: NodeType.FOLDER,
children: [{ name: "index.ts", type: NodeType.FILE }]
}
]
}
} as CCFile

expect(haveEqualRootNames(reference, comp)).toBeTruthy()
})
})
})
17 changes: 17 additions & 0 deletions visualization/app/codeCharta/model/files/files.helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CCFile } from "../../codeCharta.model"
import { FileSelectionState, FileState } from "./files"
import { mergeFolderChain } from "../../util/nodeDecorator"
import { CodeChartaService } from "../../codeCharta.service"

export function getCCFiles(fileStates: FileState[]) {
return fileStates.map(x => x.file)
Expand Down Expand Up @@ -37,3 +39,18 @@ export function isPartialState(fileStates: FileState[]) {
export function isEqual(file1: CCFile, file2: CCFile) {
return file1.fileMeta.fileChecksum === file2.fileMeta.fileChecksum
}

export function haveEqualRootNames(reference: CCFile, comparison: CCFile) {
const splitPathReference = reference.map.path.split("/")
const splitPathComparison = comparison.map.path.split("/")
if (splitPathComparison.length > splitPathReference.length) {
mergeFolderChain(reference.map)
CodeChartaService.updateRootData(reference.map.name)
}
if (splitPathComparison.length < splitPathReference.length) {
mergeFolderChain(comparison.map)
CodeChartaService.updateRootData(comparison.map.name)
}

return reference.map.name === comparison.map.name
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CodeMapNode, FileMeta } from "../../../codeCharta.model"
import { FileState } from "../../../model/files/files"
import { fileStatesAvailable, isPartialState, isDeltaState } from "../../../model/files/files.helper"
import { fileStatesAvailable, isPartialState, isDeltaState, haveEqualRootNames } from "../../../model/files/files.helper"
import { AggregationGenerator } from "../../../util/aggregationGenerator"
import { clone } from "../../../util/clone"
import { NodeDecorator } from "../../../util/nodeDecorator"
Expand Down Expand Up @@ -29,6 +29,7 @@ export const accumulatedDataSelector: (state: CcState) => AccumulatedData = crea
}

const data = getUndecoratedAccumulatedData(fileStates)

if (!data || !data.map) {
return accumulatedDataFallback
}
Expand All @@ -54,9 +55,11 @@ const getUndecoratedAccumulatedData = (fileStates: FileState[]) => {
}
if (isDeltaState(fileStates)) {
const [reference, comparison] = visibleFileStates
if (comparison && reference.file.map.name !== comparison.file.map.name) {

if (!comparison || !haveEqualRootNames(reference.file, comparison.file)) {
return AggregationGenerator.getAggregationFile(visibleFileStates.map(x => x.file))
}
//CodeChartaService.updateRootDataComparison(comparison.file.map.name)
return getDeltaFile(visibleFileStates)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { FileSelectionState, FileState } from "../../../model/files/files"
import { createSelector } from "../../angular-redux/createSelector"
import { filesSelector } from "../../store/files/files.selector"

type Selectable = Pick<FileState, "selectedAs">
type SelectableFile<File> = Selectable & { file: File }

export const _getComparisonFile = <File>(fileStates: SelectableFile<File>[]) =>
fileStates.find(file => file.selectedAs === FileSelectionState.Comparison)?.file

export const comparisonFileSelector = createSelector([filesSelector], _getComparisonFile)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { referenceFileSelector } from "../../../state/selectors/referenceFile/re
import { setDeltaComparison, setDeltaReference, switchReferenceAndComparison } from "../../../state/store/files/files.actions"
import { filesSelector } from "../../../state/store/files/files.selector"
import { pictogramBackgroundSelector } from "./pictogramBackground.selector"
import { comparisonFileSelector } from "../../../state/selectors/comparisonFile/comparisonFile.selector"

@Component({
selector: "cc-file-panel-delta-selector",
Expand All @@ -15,7 +16,7 @@ import { pictogramBackgroundSelector } from "./pictogramBackground.selector"
export class FilePanelDeltaSelectorComponent {
files$ = this.store.select(filesSelector)
referenceFile$ = this.store.select(referenceFileSelector)
comparisonFile$ = this.files$.pipe(map(files => files.find(file => file.selectedAs === FileSelectionState.Comparison)?.file))
comparisonFile$ = this.store.select(comparisonFileSelector)
possibleComparisonFiles$ = this.files$.pipe(map(files => files.filter(file => file.selectedAs !== FileSelectionState.Reference)))
pictogramBackground$ = this.store.select(pictogramBackgroundSelector)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,63 @@ Object {
}
`;

exports[`deltaGenerator getDeltaFile should result in expected deltaFiles when root is equal but Folders are merged (e.g. File1: 'root/' and File2: 'root/Parent Leaf') 1`] = `
Object {
"attributes": Object {},
"children": Array [
Object {
"attributes": Object {},
"children": Array [
Object {
"attributes": Object {
"functions": 10,
"mcc": 100,
"rloc": 1,
},
"deltas": Object {
"functions": 0,
"mcc": 99,
"rloc": -99,
},
"fileCount": Object {
"added": 0,
"changed": 1,
"removed": 0,
},
"isExcluded": false,
"isFlattened": false,
"name": "small leaf",
"path": "/root/Parent Leaf/small leaf",
"type": "File",
},
],
"deltas": Object {},
"fileCount": Object {
"added": 0,
"changed": 0,
"removed": 0,
},
"isExcluded": false,
"isFlattened": false,
"name": "root/Parent Leaf",
"path": "/root/Parent Leaf",
"type": "Folder",
},
],
"deltas": Object {},
"fileCount": Object {
"added": 0,
"changed": 0,
"removed": 0,
},
"isExcluded": false,
"isFlattened": false,
"name": "root",
"path": "/root",
"type": "Folder",
}
`;

exports[`deltaGenerator golden test 1`] = `
Object {
"attributes": Object {},
Expand Down
68 changes: 68 additions & 0 deletions visualization/app/codeCharta/util/deltaGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ describe("deltaGenerator", () => {
expect(result.map).toMatchSnapshot()
})

it("getDeltaFile should result in expected deltaFiles when root is equal but Folders are merged (e.g. File1: 'root/' and File2: 'root/Parent Leaf')", () => {
NodeDecorator.decorateMapWithPathAttribute(DELTA_MAP_MERGED_ROOT_NAME)
NodeDecorator.decorateMapWithPathAttribute(DELTA_MAP_SINGLE_ROOT_NAME)

const result = DeltaGenerator.getDeltaFile(DELTA_MAP_MERGED_ROOT_NAME, DELTA_MAP_SINGLE_ROOT_NAME)

expect(result.map).toMatchSnapshot()
})

it("should detect added and removed files and add result to delta attributes", () => {
// Here, "changed" means "added" or "removed"
const actualAmountOfChangedFiles: Pick<FileCount, "added" | "removed"> = { added: 0, removed: 0 }
Expand Down Expand Up @@ -350,3 +359,62 @@ export const TEST_DELTA_MAP_F: CCFile = {
},
settings: DEFAULT_CC_FILE_MOCK.settings
}

const DELTA_MAP_SINGLE_ROOT_NAME: CCFile = {
fileMeta: {
...DEFAULT_CC_FILE_MOCK.fileMeta,
fileName: "fileA",
fileChecksum: "md5-delta-fileA"
},
map: {
name: "root",
type: NodeType.FOLDER,
attributes: {},
isExcluded: false,
isFlattened: false,
children: [
{
name: "Parent Leaf",
type: NodeType.FOLDER,
attributes: {},
isExcluded: false,
isFlattened: false,
children: [
{
name: "small leaf",
type: NodeType.FILE,
attributes: { rloc: 1, functions: 10, mcc: 100 },
isExcluded: false,
isFlattened: false
}
]
}
]
},
settings: DEFAULT_CC_FILE_MOCK.settings
}

const DELTA_MAP_MERGED_ROOT_NAME: CCFile = {
fileMeta: {
...DEFAULT_CC_FILE_MOCK.fileMeta,
fileName: "fileB",
fileChecksum: "md5-delta-fileB"
},
map: {
name: "root/Parent Leaf",
type: NodeType.FOLDER,
attributes: {},
isExcluded: false,
isFlattened: false,
children: [
{
name: "small leaf",
type: NodeType.FILE,
attributes: { rloc: 100, functions: 10, mcc: 1 },
isExcluded: false,
isFlattened: false
}
]
},
settings: DEFAULT_CC_FILE_MOCK.settings
}
2 changes: 1 addition & 1 deletion visualization/app/codeCharta/util/nodeDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export const NodeDecorator = {
}
}

function mergeFolderChain(data: CodeMapNode) {
export function mergeFolderChain(data: CodeMapNode) {
// Nodes with only one child which also have children are merged into one node
// e.g. a /folder which includes anotherFolder that includes other files or folders
// will be merged to a node with path /folder/anotherFolder and children are set accordingly
Expand Down