diff --git a/scripts/analytics/src/libs/modules/git-service/base-git-service.module.ts b/scripts/analytics/src/libs/modules/git-service/base-git-service.module.ts index 365922396..539f399f4 100644 --- a/scripts/analytics/src/libs/modules/git-service/base-git-service.module.ts +++ b/scripts/analytics/src/libs/modules/git-service/base-git-service.module.ts @@ -5,8 +5,8 @@ class BaseGITService implements GITService { return "git fetch"; }; - public getShortLogCommand = (since: string): string => { - return `git shortlog -sne --all --no-merges --since="${since}"`; + public getShortLogCommand = (): string => { + return "git shortlog -sne --all --no-merges --group='%cs %cn <%ce>' --since='1 week ago'"; }; } diff --git a/scripts/analytics/src/libs/modules/git-service/libs/types/git-service.type.ts b/scripts/analytics/src/libs/modules/git-service/libs/types/git-service.type.ts index 9cfa209ab..e0515fef5 100644 --- a/scripts/analytics/src/libs/modules/git-service/libs/types/git-service.type.ts +++ b/scripts/analytics/src/libs/modules/git-service/libs/types/git-service.type.ts @@ -1,6 +1,6 @@ type GITService = { getFetchCommand: () => string; - getShortLogCommand: (since: string) => string; + getShortLogCommand: () => string; }; export { type GITService }; diff --git a/scripts/analytics/src/modules/analytics/analytics.service.ts b/scripts/analytics/src/modules/analytics/analytics.service.ts index f55135fc0..f7ca07626 100644 --- a/scripts/analytics/src/modules/analytics/analytics.service.ts +++ b/scripts/analytics/src/modules/analytics/analytics.service.ts @@ -1,6 +1,6 @@ import fs from "node:fs/promises"; -import { executeCommand, formatDate } from "~/libs/helpers/helpers.js"; +import { executeCommand } from "~/libs/helpers/helpers.js"; import { type GITService } from "~/libs/modules/git-service/git-service.js"; import { logger } from "~/libs/modules/logger/logger.js"; import { type AnalyticsScriptConfig } from "~/libs/types/types.js"; @@ -12,10 +12,7 @@ import { FIRST_ARRAY_INDEX, } from "./libs/constants/constants.js"; import { mergeStats } from "./libs/helpers/helpers.js"; -import { - type ActivityLogCreateItemRequestDto, - type CommitStatistics, -} from "./libs/types/types.js"; +import { type ActivityLogCreateItemRequestDto } from "./libs/types/types.js"; type Constructor = { analyticsApi: typeof analyticsApi; @@ -37,22 +34,34 @@ class AnalyticsService { private async collectStatsByRepository( repoPath: string, ): Promise { - const stats: ActivityLogCreateItemRequestDto[] = []; + const statsByDate = new Map(); const shortLogResult = await executeCommand( - this.gitService.getShortLogCommand("midnight"), + this.gitService.getShortLogCommand(), repoPath, ); - const commitItems: CommitStatistics[] = []; let match; while ( (match = COMMIT_REGEX.exec(shortLogResult.stdout.toString())) !== null ) { - const [, commitsNumber, authorName, authorEmail] = match; + const [, commitsNumber, commitDate, authorName, authorEmail] = match; + + if (!commitDate) { + logger.error("Invalid data from git log."); + + continue; + } + + let statsForCommitDate = statsByDate.get(commitDate); + + if (!statsForCommitDate) { + statsForCommitDate = { date: commitDate, items: [] }; + statsByDate.set(commitDate, statsForCommitDate); + } if (commitsNumber && authorName && authorEmail) { - commitItems.push({ + statsForCommitDate.items.push({ authorEmail, authorName, commitsNumber: Number.parseInt(commitsNumber, 10), @@ -62,12 +71,7 @@ class AnalyticsService { } } - stats.push({ - date: formatDate(new Date(), "yyyy-MM-dd"), - items: commitItems, - }); - - return stats; + return [...statsByDate.values()]; } private async fetchRepository(repoPath: string): Promise { diff --git a/scripts/analytics/src/modules/analytics/libs/constants/commit-regex.constant.ts b/scripts/analytics/src/modules/analytics/libs/constants/commit-regex.constant.ts index 08dbfb0fb..ac80e51a9 100644 --- a/scripts/analytics/src/modules/analytics/libs/constants/commit-regex.constant.ts +++ b/scripts/analytics/src/modules/analytics/libs/constants/commit-regex.constant.ts @@ -1,3 +1,3 @@ -const COMMIT_REGEX = /^\s*(\d+)\s+(.+?)\s+<(.+?)>/gm; +const COMMIT_REGEX = /^\s*(\d+)\s+(\d{4}-\d{2}-\d{2})\s+(.+?)\s+<(.+?)>/gm; export { COMMIT_REGEX }; diff --git a/scripts/analytics/src/modules/analytics/libs/types/commit-statistics.type.ts b/scripts/analytics/src/modules/analytics/libs/types/commit-statistics.type.ts deleted file mode 100644 index d917dfec6..000000000 --- a/scripts/analytics/src/modules/analytics/libs/types/commit-statistics.type.ts +++ /dev/null @@ -1,7 +0,0 @@ -type CommitStatistics = { - authorEmail: string; - authorName: string; - commitsNumber: number; -}; - -export { type CommitStatistics }; diff --git a/scripts/analytics/src/modules/analytics/libs/types/types.ts b/scripts/analytics/src/modules/analytics/libs/types/types.ts index 2e0620038..247c75933 100644 --- a/scripts/analytics/src/modules/analytics/libs/types/types.ts +++ b/scripts/analytics/src/modules/analytics/libs/types/types.ts @@ -1,4 +1,3 @@ -export { type CommitStatistics } from "./commit-statistics.type.js"; export { type ActivityLogCreateItemRequestDto, type ActivityLogCreateRequestDto,