Skip to content

Commit 66c4e50

Browse files
committed
feat: aggregate hotspots
1 parent bbdcfbf commit 66c4e50

2 files changed

Lines changed: 56 additions & 11 deletions

File tree

backend/src/index.ts

Lines changed: 18 additions & 2 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 { findHotspotFiles, HotspotCriteria } from "./services/hotspot";
15+
import { aggregateHotspots, findHotspotFiles, HotspotCriteria } from "./services/hotspot";
1616

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

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

96+
app.get("/api/hotspots/aggregated", async (req, res) => {
97+
const minScore = Number(req.query.minScore) || -1;
98+
const criteria = { minScore, module: '' };
99+
100+
try {
101+
const result = await aggregateHotspots(criteria, options);
102+
res.json(result);
103+
} catch (e) {
104+
console.log('error', e);
105+
res.status(500).json(e);
106+
}
107+
});
108+
96109
app.get("/api/hotspots", async (req, res) => {
97110
const minScore = Number(req.query.minScore) || -1;
111+
const module = req.query.module ? String(req.query.module) : '';
112+
113+
const criteria = { minScore, module };
98114

99115
try {
100-
const result = await findHotspotFiles({minScore: minScore}, options);
116+
const result = await findHotspotFiles(criteria, options);
101117
res.json(result);
102118
} catch (e) {
103119
console.log('error', e);

backend/src/services/hotspot.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Options } from "../options/options";
33
import { parseGitLog } from "../utils/git-parser";
44
import * as fs from 'fs';
55
import { calcComplexity } from "../utils/complexity";
6+
import { loadConfig } from "../infrastructure/config";
67

78
export 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+
3445
export 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

Comments
 (0)