Skip to content

Commit b825bd0

Browse files
committed
introduce summary dashboard, show top 10 authors through the dashboard
1 parent a533c13 commit b825bd0

File tree

6 files changed

+77
-22
lines changed

6 files changed

+77
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { AggregateFileData } from "../stats/aggregate/aggregate.js";
2+
import { Contributor } from "../stats/list-of-contributors-per-file.js";
3+
4+
export class AggregateFileContributorsDashboard {
5+
constructor(private data: AggregateFileData<Contributor[]>) {}
6+
public displayDashboard(): string {
7+
const files = Object.keys(this.data);
8+
return `Number of files changed: ${files.length}`;
9+
}
10+
}

src/dashboard/summary-dashboard.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { log } from "../index.js";
2+
import { NumberOfCommitsByAuthor } from "../stats/number-of-commits-by-author.js";
3+
4+
function clearScreen() {
5+
process.stdout.write("\u001b[3J\u001b[2J\u001b[1J");
6+
// eslint-disable-next-line no-console
7+
console.clear();
8+
}
9+
10+
type TopAuthorRecord = {
11+
name: string;
12+
numberOfUpdates: number;
13+
};
14+
15+
export class SummaryDashboard {
16+
private topAuthorsAllTime: TopAuthorRecord[] = [];
17+
18+
public setNumberOfCommitsPerAuthor(data: NumberOfCommitsByAuthor) {
19+
const out: TopAuthorRecord[] = [];
20+
const authors = Object.keys(data);
21+
for (const author of authors) {
22+
out.push({ name: author, numberOfUpdates: data[author] });
23+
}
24+
25+
// Sort in descending order by numberOfUpdates
26+
out.sort((a, b) => b.numberOfUpdates - a.numberOfUpdates);
27+
28+
this.topAuthorsAllTime = out;
29+
this.render();
30+
}
31+
32+
private render() {
33+
clearScreen();
34+
log("Top 10 authors:", {});
35+
log("============", {});
36+
for (let i = 0; i < 10; i++) {
37+
log(this.topAuthorsAllTime[i].name, {
38+
numberOfUpdates: this.topAuthorsAllTime[i].numberOfUpdates,
39+
});
40+
}
41+
}
42+
}

src/git-reader/git-repository.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from "fs";
22
import git from "isomorphic-git";
33
import { Readable } from "stream";
44

5-
import { debug, log, time, timeLog } from "../index.js";
5+
import { log, time, timeLog } from "../index.js";
66
import { ChangedFile, Commit, ExpandedCommit } from "../interfaces.js";
77

88
interface GitReadOptions {
@@ -39,7 +39,7 @@ export class GitRepository {
3939
continue;
4040
}
4141

42-
debug("Getting files diff between two commits", {
42+
log("Getting files diff between two commits", {
4343
progress: `${i} of ${commits.length}`,
4444
commitDate: new Date(c.commit.author.timestamp * 1000).toISOString(),
4545
});

src/index.ts

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Readable } from "stream";
22

3+
import { AggregateFileContributorsDashboard } from "./dashboard/aggregate-file-contributors-dashboard.js";
4+
import { SummaryDashboard } from "./dashboard/summary-dashboard.js";
35
import { GitRepository } from "./git-reader/git-repository.js";
46
import { ExpandedCommit } from "./interfaces.js";
57
import { ListOfContributorsPerFileAggregate } from "./stats/aggregate/list-of-contributors-per-file-aggregate.js";
@@ -28,17 +30,6 @@ export function timeLog(timerName: string) {
2830
console.timeLog(timerName);
2931
}
3032
}
31-
function clearConsole() {
32-
process.stdout.write("\u001b[3J\u001b[2J\u001b[1J");
33-
// eslint-disable-next-line no-console
34-
console.clear();
35-
}
36-
37-
async function collectCommitsByAuthor(repo: GitRepository) {
38-
const commits = await repo.getListOfCommits();
39-
const commitsByAuthor = getNumberOfCommitsByAuthor(commits);
40-
log("commitsByAuthor", commitsByAuthor);
41-
}
4233

4334
async function collectHotFiles(commitsWithChangedFiles: ExpandedCommit[]) {
4435
const commitsPerFile = getNumberOfChangesPerFile(commitsWithChangedFiles);
@@ -97,28 +88,34 @@ async function main() {
9788
);
9889

9990
commitsStream.on("data", (commit) => {
100-
log("Commit", commit);
91+
debug("Commit", commit);
10192
intermediateAggregateMonthly.addCommit(commit);
10293
intermediateAggregateQuarterly.addCommit(commit);
10394

104-
clearConsole();
105-
10695
// log("monthly data: ", intermediateAggregateMonthly.getData());
107-
log("quarterly data: ", {
108-
data: JSON.stringify(intermediateAggregateQuarterly.displayReport()),
109-
});
96+
const quarterlyDashboard = new AggregateFileContributorsDashboard(
97+
intermediateAggregateQuarterly.getData()
98+
);
99+
log(quarterlyDashboard.displayDashboard(), {});
110100
});
111101
commitsStream.on("end", () => {
112102
log("done reading commits", {});
113103
});
114104

105+
// initialize dashboard
106+
const summaryDashboard = new SummaryDashboard();
107+
108+
// number of commits by author:
109+
const commits = await repo.getListOfCommits();
110+
const commitsByAuthor = getNumberOfCommitsByAuthor(commits);
111+
summaryDashboard.setNumberOfCommitsPerAuthor(commitsByAuthor);
112+
115113
const commitsWithChangedFiles = await repo.getListOfCommitsWithChangedFiles({
116114
stream: commitsStream,
117115
});
118116
log("Finished fetching a list of changed files", {
119117
numberOfFiles: commitsWithChangedFiles.length,
120118
});
121-
await collectCommitsByAuthor(repo);
122119
await collectHotFiles(commitsWithChangedFiles);
123120
await collectKnowledgeGaps(commitsWithChangedFiles);
124121
await collectDetailedContributorsPerFile(commitsWithChangedFiles);

src/stats/aggregate/aggregate.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ type AggregateKey = string;
66

77
type CommitToAggregateKey = (commit: Commit) => AggregateKey;
88

9+
export type AggregateFileData<T> = Record<FilePath, Record<AggregateKey, T>>;
10+
911
interface AggregateOptions {
1012
strategy: AggregateStrategy;
1113
}
@@ -69,7 +71,7 @@ export abstract class Aggregate<T> {
6971
);
7072
}
7173
}
72-
public getData(): Record<FilePath, Record<AggregateKey, T>> {
74+
public getData(): AggregateFileData<T> {
7375
return this.files;
7476
}
7577
public listFiles(): FilePath[] {

src/stats/number-of-commits-by-author.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Commit } from "../interfaces.js";
22

3-
export function getNumberOfCommitsByAuthor(commits: Commit[]) {
3+
export type NumberOfCommitsByAuthor = Record<string, number>;
4+
5+
export function getNumberOfCommitsByAuthor(
6+
commits: Commit[]
7+
): NumberOfCommitsByAuthor {
48
const authors: Record<string, number> = {};
59
commits.forEach((commit) => {
610
if (!authors[commit.commit.author.name]) {

0 commit comments

Comments
 (0)