@@ -4,48 +4,86 @@ import { parseGitLog } from "../utils/git-parser";
44import * as fs from 'fs' ;
55import { calcComplexity } from "../utils/complexity" ;
66
7+ export type Hotspot = {
8+ commits : number ,
9+ changedLines : number ,
10+ complexity : number ,
11+ score : number ,
12+ } ;
13+
714// tuple to reduce payload size
8- export type Hotspot = [
15+ export type FlatHotspot = {
16+ fileName : string ,
917 commits : number ,
1018 changedLines : number ,
1119 complexity : number ,
12- ] ;
20+ score : number ,
21+ } ;
1322
1423export type HotspotResult = {
15- hotspots : Record < string , Hotspot > ;
24+ hotspots : FlatHotspot [ ] ;
1625} ;
1726
1827export type HotspotCriteria = {
19- minCommits : number ;
20- minChangedLines : number ;
21- minComplexity : number ;
28+ // minCommits: number;
29+ // minChangedLines: number;
30+ // minComplexity: number;
31+ minScore : number ,
2232} ;
2333
24- export async function findHotspots ( options : Options ) : Promise < HotspotResult > {
25- const result : HotspotResult = { hotspots : { } } ;
34+ export async function findHotspotFiles ( criteria : HotspotCriteria , options : Options ) : Promise < HotspotResult > {
35+ const hotspots : Record < string , Hotspot > = await _findHotspots ( options ) ;
36+
37+ const filtered : FlatHotspot [ ] = [ ] ;
38+ for ( const fileName of Object . keys ( hotspots ) ) {
39+ const hotspot = hotspots [ fileName ] ;
40+ hotspot . score = hotspot . complexity === - 1 ? - 1 : hotspot . complexity * hotspot . commits ;
41+ if ( hotspot . score >= criteria . minScore ) {
42+ filtered . push ( { fileName, ...hotspot } ) ;
43+ }
44+ }
45+
46+ return { hotspots : filtered } ;
47+ }
48+
49+ // export async function aggregateHotspots(criteria: HotspotCriteria, options: Options): Promise<HotspotResult> {
50+ // const hotspots: Record<string, Hotspot> = await _findHotspots(options);
51+
52+ // const config = loadConfig();
53+
54+ // return { hotspots: filtered };
55+ // }
56+
57+ async function _findHotspots ( options : Options ) {
58+ const hotspots : Record < string , Hotspot > = { } ;
2659
2760 await parseGitLog ( ( entry ) => {
2861 for ( const change of entry . body ) {
2962 let hotspot : Hotspot ;
30- if ( ! result . hotspots [ change . path ] ) {
63+ if ( ! hotspots [ change . path ] ) {
3164
3265 let cc = - 1 ;
3366 const filePath = path . join ( options . path , change . path ) ;
3467 if ( filePath . endsWith ( '.ts' ) && fs . existsSync ( filePath ) ) {
3568 cc = calcComplexity ( filePath ) ;
3669 }
3770
38- hotspot = [ 0 , 0 , cc ] ;
39- result . hotspots [ change . path ] = hotspot ;
71+ hotspot = {
72+ commits : 0 ,
73+ changedLines : 0 ,
74+ complexity : - 1 ,
75+ score : 0 ,
76+ } ;
77+
78+ hotspots [ change . path ] = hotspot ;
4079 } else {
41- hotspot = result . hotspots [ change . path ] ;
80+ hotspot = hotspots [ change . path ] ;
4281 }
4382
44- hotspot [ 0 ] ++ ;
45- hotspot [ 1 ] += change . linesAdded + change . linesRemoved ;
46-
83+ hotspot . commits ++ ;
84+ hotspot . changedLines += change . linesAdded + change . linesRemoved ;
4785 }
4886 } ) ;
49-
50- return result ;
87+ return hotspots ;
5188}
89+
0 commit comments