Skip to content

Commit bbdcfbf

Browse files
committed
feat: filter hotspots by score
1 parent 653797f commit bbdcfbf

2 files changed

Lines changed: 58 additions & 20 deletions

File tree

backend/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { calcModuleInfo } from "./services/module-info";
1212
import { calcTeamAlignment } from "./services/team-alignment";
1313
import { openSync } from './utils/open';
1414
import { ensureConfig } from "./infrastructure/config";
15-
import { findHotspots, HotspotCriteria } from "./services/hotspot";
15+
import { findHotspotFiles, HotspotCriteria } from "./services/hotspot";
1616

1717
const options = parseOptions(process.argv.slice(2));
1818

@@ -94,10 +94,10 @@ app.get("/api/team-alignment", async (req, res) => {
9494
});
9595

9696
app.get("/api/hotspots", async (req, res) => {
97-
// const byUser = Boolean(req.query.byUser);
97+
const minScore = Number(req.query.minScore) || -1;
9898

9999
try {
100-
const result = await findHotspots(options);
100+
const result = await findHotspotFiles({minScore: minScore}, options);
101101
res.json(result);
102102
} catch (e) {
103103
console.log('error', e);

backend/src/services/hotspot.ts

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,86 @@ import { parseGitLog } from "../utils/git-parser";
44
import * as fs from 'fs';
55
import { 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

1423
export type HotspotResult = {
15-
hotspots: Record<string, Hotspot>;
24+
hotspots: FlatHotspot[];
1625
};
1726

1827
export 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

Comments
 (0)