Skip to content

Commit

Permalink
feat: upload statistics for the last week on each run gf-600
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemZag committed Oct 8, 2024
1 parent 0421bb8 commit f3db701
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'";
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type GITService = {
getFetchCommand: () => string;
getShortLogCommand: (since: string) => string;
getShortLogCommand: () => string;
};

export { type GITService };
36 changes: 20 additions & 16 deletions scripts/analytics/src/modules/analytics/analytics.service.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
Expand All @@ -37,22 +34,34 @@ class AnalyticsService {
private async collectStatsByRepository(
repoPath: string,
): Promise<ActivityLogCreateItemRequestDto[]> {
const stats: ActivityLogCreateItemRequestDto[] = [];
const statsByDate = new Map<string, ActivityLogCreateItemRequestDto>();
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),
Expand All @@ -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<void> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 };

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { type CommitStatistics } from "./commit-statistics.type.js";
export {
type ActivityLogCreateItemRequestDto,
type ActivityLogCreateRequestDto,
Expand Down

0 comments on commit f3db701

Please sign in to comment.