-
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(statistic): Add new clones statistic
- Loading branch information
Andrii Kucherenko
committed
Aug 16, 2018
1 parent
41ffaf4
commit 5c4761d
Showing
21 changed files
with
362 additions
and
209 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} |
Oops, something went wrong.