Skip to content

Commit

Permalink
feat(statistic): Add new clones statistic
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Kucherenko committed Aug 16, 2018
1 parent 41ffaf4 commit 5c4761d
Show file tree
Hide file tree
Showing 21 changed files with 362 additions and 209 deletions.
62 changes: 27 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@
"cli-table2": "^0.2.0",
"colors": "^1.3.1",
"commander": "^2.17.0",
"dirty": "^1.1.0",
"fs-extra": "^7.0.0",
"glob": "^7.1.2",
"ora": "^3.0.0",
"prismjs": "^1.15.0"
},
"devDependencies": {
Expand All @@ -64,7 +62,6 @@
"@types/commander": "^2.12.2",
"@types/fs-extra": "^5.0.4",
"@types/glob": "^5.0.35",
"@types/ora": "^1.3.4",
"@types/prismjs": "^1.9.0",
"@types/sinon": "^5.0.1",
"ava": "^1.0.0-beta.5.1",
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const options: IOptions = prepareOptions(cli);
const cpd: JSCPD = new JSCPD({
...options,
storeOptions: {
"*": {type: 'memory'}
"*": {type: 'files'}
}
});

Expand Down
80 changes: 80 additions & 0 deletions src/clone/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {IStore} from "../interfaces/store/store.interface";
import {md5} from "../utils";
import {CLONE_EVENT, Events} from "../events";
import {IClone} from "../interfaces/clone.interface";
import {StoresManager} from "../stores/stores-manager";
import {CLONES_DB, getHashDbName, SOURCES_DB} from "../stores/models";
import {IMapFrame} from "../interfaces/map-frame.interface";
import {ISource} from "../interfaces/source.interface";

export function addClone(clone: IClone) {
const clonesStore: IStore<IClone> = StoresManager.getStore(CLONES_DB);
const cloneId: string = md5(JSON.stringify(clone));
clonesStore.set(cloneId, clone);
addCloneToSource(cloneId, [clone.duplicationA.sourceId, clone.duplicationB.sourceId]);
Events.emit(CLONE_EVENT, {...clone, is_new: true});
}

function addCloneToSource(cloneId: string, sourcesIds: string[]) {
const sourcesStore: IStore<ISource> = StoresManager.getStore(SOURCES_DB);
sourcesIds.map( sid => {
const source: ISource = sourcesStore.get(sid);
if (source && source.clones) {
if (!source.clones.includes(cloneId)) {
source.clones = source.clones.concat(cloneId);
sourcesStore.set(sid, source);
}
}
});
}

export function createClone(
startMap: IMapFrame,
endMap: IMapFrame,
format: string
): IClone {
const hashesStore: IStore<IMapFrame> = StoresManager.getStore(getHashDbName(format));

const sourceStart: IMapFrame = hashesStore.get(startMap.id);
const sourceEnd: IMapFrame = hashesStore.get(endMap.id);

const fragment: string = getFragment(
startMap.start.sourceId,
startMap.start.range[0],
endMap.end.range[1]
);

const clone: IClone = {
format,
fragment,
duplicationA: {
sourceId: startMap.start.sourceId,
start: startMap.start,
end: endMap.end
},
duplicationB: {
sourceId: sourceStart.start.sourceId,
start: sourceStart.start,
end: sourceEnd.end
}
};
return clone;
}

export function getFragment(
sourceId: string,
start: number,
end: number
): string {
const sourcesStore: IStore<ISource> = StoresManager.getStore(SOURCES_DB);
return sourcesStore.get(sourceId).source.substring(start, end);
}


export function isCloneLinesBiggerLimit(clone: IClone, minLines: number): boolean {
return (
clone.duplicationA.end.loc.end.line -
clone.duplicationA.start.loc.end.line >=
minLines
);
}

0 comments on commit 5c4761d

Please sign in to comment.