@@ -3,6 +3,7 @@ import { Options } from "../options/options";
33import { parseGitLog } from "../utils/git-parser" ;
44import * as fs from 'fs' ;
55import { calcComplexity } from "../utils/complexity" ;
6+ import { loadConfig } from "../infrastructure/config" ;
67
78export type Hotspot = {
89 commits : number ,
@@ -28,11 +29,21 @@ export type HotspotCriteria = {
2829 // minCommits: number;
2930 // minChangedLines: number;
3031 // minComplexity: number;
32+ module : string ,
3133 minScore : number ,
3234} ;
3335
36+ export type AggregatedHotspot = {
37+ module : string ;
38+ count : number ;
39+ }
40+
41+ export type AggregatedHotspotsResult = {
42+ aggregated : AggregatedHotspot [ ] ;
43+ }
44+
3445export async function findHotspotFiles ( criteria : HotspotCriteria , options : Options ) : Promise < HotspotResult > {
35- const hotspots : Record < string , Hotspot > = await _findHotspots ( options ) ;
46+ const hotspots : Record < string , Hotspot > = await _findHotspots ( criteria , options ) ;
3647
3748 const filtered : FlatHotspot [ ] = [ ] ;
3849 for ( const fileName of Object . keys ( hotspots ) ) {
@@ -43,26 +54,44 @@ export async function findHotspotFiles(criteria: HotspotCriteria, options: Optio
4354 }
4455 }
4556
57+ filtered . sort ( ( a , b ) => a . score - b . score ) ;
58+
4659 return { hotspots : filtered } ;
4760}
4861
49- // export async function aggregateHotspots(criteria: HotspotCriteria, options: Options): Promise<HotspotResult> {
50- // const hotspots: Record<string, Hotspot> = await _findHotspots(options);
62+ export async function aggregateHotspots ( criteria : HotspotCriteria , options : Options ) : Promise < AggregatedHotspotsResult > {
63+ const hotspots = ( await findHotspotFiles ( criteria , options ) ) . hotspots ;
64+ const config = loadConfig ( options ) ;
5165
52- // const config = loadConfig();
66+ const result : AggregatedHotspot [ ] = [ ] ;
67+ for ( const module of config . scopes ) {
68+ let count = 0 ;
69+ for ( const hotspot of hotspots ) {
70+ if ( hotspot . fileName . startsWith ( module ) && hotspot . score >= criteria . minScore ) {
71+ count ++ ;
72+ }
73+ }
74+ result . push ( { module, count } ) ;
75+ }
5376
54- // return { hotspots: filtered };
55- // }
77+ result . sort ( ( a , b ) => a . count - b . count ) ;
78+ return { aggregated : result } ;
79+ }
5680
57- async function _findHotspots ( options : Options ) {
81+ async function _findHotspots ( criteria : HotspotCriteria , options : Options ) {
5882 const hotspots : Record < string , Hotspot > = { } ;
5983
6084 await parseGitLog ( ( entry ) => {
6185 for ( const change of entry . body ) {
6286 let hotspot : Hotspot ;
87+
88+ if ( ! change . path . startsWith ( criteria . module ) ) {
89+ continue ;
90+ }
91+
6392 if ( ! hotspots [ change . path ] ) {
6493
65- let cc = - 1 ;
94+ let cc = 1 ;
6695 const filePath = path . join ( options . path , change . path ) ;
6796 if ( filePath . endsWith ( '.ts' ) && fs . existsSync ( filePath ) ) {
6897 cc = calcComplexity ( filePath ) ;
@@ -71,7 +100,7 @@ async function _findHotspots(options: Options) {
71100 hotspot = {
72101 commits : 0 ,
73102 changedLines : 0 ,
74- complexity : - 1 ,
103+ complexity : cc ,
75104 score : 0 ,
76105 } ;
77106
0 commit comments