|
| 1 | +import { debug } from "codebase-stats-collector/dist/console/console.js"; |
| 2 | +import { SummaryDashboard } from "codebase-stats-collector/dist/dashboard/summary-dashboard.js"; |
| 3 | +import { GitRepository } from "codebase-stats-collector/dist/git-reader/git-repository.js"; |
| 4 | +import { ExpandedCommit } from "codebase-stats-collector/dist/interfaces.js"; |
| 5 | +import { getNumberOfContributorsPerFile } from "codebase-stats-collector/dist/stats/number-of-contributors-per-file.js"; |
| 6 | +import { Readable } from "stream"; |
| 7 | + |
| 8 | +import { log } from "../console.js"; |
| 9 | + |
| 10 | +function setupProgressStream(summaryDashboard: SummaryDashboard): Readable { |
| 11 | + let commitsCounter = 0; |
| 12 | + |
| 13 | + const commitsStream = new Readable({ |
| 14 | + objectMode: true, |
| 15 | + read() { |
| 16 | + // do nothing. |
| 17 | + }, |
| 18 | + }); |
| 19 | + commitsStream.on("data", (commit: ExpandedCommit) => { |
| 20 | + debug("Commit", commit); |
| 21 | + |
| 22 | + commitsCounter += 1; |
| 23 | + summaryDashboard.setCurrentProgress(commitsCounter, commit); |
| 24 | + }); |
| 25 | + commitsStream.on("error", (err) => { |
| 26 | + debug("error reading commits", { err }); |
| 27 | + }); |
| 28 | + commitsStream.on("end", () => { |
| 29 | + debug("done reading commits", {}); |
| 30 | + }); |
| 31 | + commitsStream.on("close", () => { |
| 32 | + debug("stream closed", {}); |
| 33 | + }); |
| 34 | + return commitsStream; |
| 35 | +} |
| 36 | + |
| 37 | +export async function collectKnowledgeGaps( |
| 38 | + dir: string, |
| 39 | + ignoreFiles: string | null, |
| 40 | +): Promise<void> { |
| 41 | + // initialize repo |
| 42 | + const repo = new GitRepository(dir); |
| 43 | + |
| 44 | + // initialize summary dashboard: |
| 45 | + const summaryDashboard = new SummaryDashboard([]); |
| 46 | + summaryDashboard.startProgress(); |
| 47 | + |
| 48 | + const commitsStream = setupProgressStream(summaryDashboard); |
| 49 | + |
| 50 | + // number of total commits: |
| 51 | + const commits = await repo.getListOfCommits(); |
| 52 | + summaryDashboard.setCommits(commits); |
| 53 | + |
| 54 | + const commitsWithChangedFiles = await repo.getListOfCommitsWithChangedFiles({ |
| 55 | + stream: commitsStream, |
| 56 | + }); |
| 57 | + const contributorsPerFile = getNumberOfContributorsPerFile( |
| 58 | + commitsWithChangedFiles, |
| 59 | + ).filter((x) => x.isExistingFile); |
| 60 | + let knowledgeGaps = contributorsPerFile.sort((a, b) => { |
| 61 | + if (a.contributorsNames.length === b.contributorsNames.length) { |
| 62 | + return a.lastChange.getTime() - b.lastChange.getTime(); |
| 63 | + } |
| 64 | + return a.contributorsNames.length - b.contributorsNames.length; |
| 65 | + }); |
| 66 | + |
| 67 | + if (ignoreFiles) { |
| 68 | + knowledgeGaps = knowledgeGaps.filter((x) => !x.filePath.match(ignoreFiles)); |
| 69 | + } |
| 70 | + |
| 71 | + const topKnowledgeGaps = knowledgeGaps.slice(0, 50); |
| 72 | + |
| 73 | + log( |
| 74 | + "knowledge gaps (files with least number of contributors)", |
| 75 | + topKnowledgeGaps, |
| 76 | + ); |
| 77 | +} |
0 commit comments