Skip to content

Commit

Permalink
feat: script should support more than 1 local git repository gf-589 (#…
Browse files Browse the repository at this point in the history
…589)

- moved helpers into src/libs/helpers
- changed functions to arrow functions
  • Loading branch information
Artem Zahorodniuk authored and Artem Zahorodniuk committed Oct 2, 2024
1 parent aff30fd commit f73b4ed
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 46 deletions.
47 changes: 1 addition & 46 deletions scripts/analytics/src/modules/analytics/analytics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
EMPTY_LENGTH,
FIRST_ARRAY_INDEX,
} from "./libs/constants/constants.js";
import { mergeStats } from "./libs/helpers/helpers.js";
import {
type ActivityLogCreateItemRequestDto,
type CommitStatistics,
Expand Down Expand Up @@ -122,50 +123,4 @@ class AnalyticsService {
}
}

function mergeStats(
statsAll: ActivityLogCreateItemRequestDto[],
): ActivityLogCreateItemRequestDto[] {
return mergeByCriteria(
statsAll,
(item1, item2) => item1.date === item2.date,
(mergedItem, item) =>
(mergedItem.items = mergeStatsItems([
...mergedItem.items,
...item.items,
])),
);
}

function mergeStatsItems(items: CommitStatistics[]): CommitStatistics[] {
return mergeByCriteria(
items,
(item1, item2) =>
item1.authorEmail === item2.authorEmail &&
item1.authorName === item2.authorName,
(mergedItem, item) => (mergedItem.commitsNumber += item.commitsNumber),
);
}

function mergeByCriteria<T>(
items: T[],
compareFunction: (item1: T, item2: T) => boolean,
mergeFunction: (mergedItem: T, item: T) => void,
): T[] {
const mergedItems: T[] = [];

for (const item of items) {
const mergedItem = mergedItems.find((mergedItem) =>
compareFunction(mergedItem, item),
);

if (mergedItem) {
mergeFunction(mergedItem, item);
} else {
mergedItems.push(item);
}
}

return mergedItems;
}

export { AnalyticsService };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { mergeStats } from "./merge-stats/merge-stats.helper.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const mergeArrayItems = <T>(
items: T[],
compareFunction: (item1: T, item2: T) => boolean,
mergeFunction: (mergedItem: T, item: T) => void,
) => {
const mergedItems: T[] = [];

for (const item of items) {
const mergedItem = mergedItems.find((mergedItem) =>
compareFunction(mergedItem, item),
);

if (mergedItem) {
mergeFunction(mergedItem, item);
} else {
mergedItems.push(item);
}
}

return mergedItems;
};

export { mergeArrayItems };
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { ActivityLogCreateItemRequestDto } from "../../types/types.js";
import { mergeArrayItems } from "../merge-array-items/merge-array-items.helper.js";

const mergeStatsItems = (items: ActivityLogCreateItemRequestDto["items"]) =>
mergeArrayItems(
items,
(item1, item2) =>
item1.authorEmail === item2.authorEmail &&
item1.authorName === item2.authorName,
(mergedItem, item) => (mergedItem.commitsNumber += item.commitsNumber),
);

export { mergeStatsItems };
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { ActivityLogCreateItemRequestDto } from "../../types/types.js";
import { mergeArrayItems } from "../merge-array-items/merge-array-items.helper.js";
import { mergeStatsItems } from "../merge-stats-items/merge-stats-items.helper.js";

const mergeStats = (statsAll: ActivityLogCreateItemRequestDto[]) =>
mergeArrayItems(
statsAll,
(item1, item2) => item1.date === item2.date,
(mergedItem, item) =>
(mergedItem.items = mergeStatsItems([
...mergedItem.items,
...item.items,
])),
);

export { mergeStats };

0 comments on commit f73b4ed

Please sign in to comment.