Add code activity graph to project widget

Signed-off-by: Sebastian Martinez <sebastinez@me.com>
This commit is contained in:
Sebastian Martinez
2022-04-04 21:32:25 +02:00
parent 2bed0e4fd0
commit 5384556f14
8 changed files with 258 additions and 38 deletions
+64 -3
View File
@@ -17,7 +17,6 @@ export interface GroupedCommitsHistory {
}
export interface Author {
avatar: string;
email: string;
name: string;
}
@@ -54,8 +53,10 @@ export interface CommitHeader {
// A set of commits grouped by time.
export interface CommitGroup {
time: string;
date: string;
time: number;
commits: CommitMetadata[];
week: number;
}
export interface CommitStats {
@@ -112,8 +113,10 @@ export function groupCommits(commits: { header: CommitHeader; context: CommitCon
if (isNewDay) {
groupedCommits.push({
time: formatGroupTime(time),
date: formatGroupTime(time),
time,
commits: [],
week: 0
});
groupDate = date;
}
@@ -128,3 +131,61 @@ export function groupCommits(commits: { header: CommitHeader; context: CommitCon
export const formatCommitTime = (t: number): string => {
return new Date(t * 1000).toUTCString();
};
export function groupCommitsByWeek(commits: CommitMetadata[]): CommitGroup[] {
const groupedCommits: CommitGroup[] = [];
let groupDate: Date | undefined = undefined;
if (commits.length === 0) {
return [];
}
commits = commits.sort((a, b) => {
if (a.header.committerTime > b.header.committerTime) {
return -1;
} else if (a.header.committerTime < b.header.committerTime) {
return 1;
}
return 0;
});
// A accumulator that increments by the amount of weeks between weekly commit groups
let weekAccumulator = Math.floor(getDaysPassed(new Date(commits[0].header.committerTime * 1000), new Date()) / 7);
// Loops over all commits and stores them by week with some additional metadata in groupedCommits.
for (const commit of commits) {
const time = commit.header.committerTime * 1000;
const date = new Date(time);
const isNewWeek =
!groupedCommits.length ||
!groupDate ||
getDaysPassed(date, groupDate) > 7 ||
date.getFullYear() < groupDate.getFullYear();
if (isNewWeek) {
let daysPassed = 0;
if (groupDate) {
daysPassed = getDaysPassed(date, groupDate);
}
groupedCommits.push({
date: formatGroupTime(time),
time,
commits: [],
week: Math.floor(daysPassed / 7) + weekAccumulator
});
groupDate = date;
weekAccumulator += Math.floor(daysPassed / 7);
}
groupedCommits[groupedCommits.length - 1].commits.push(commit);
}
return groupedCommits;
}
// Get amount of days passed between two dates
function getDaysPassed(from: Date, to: Date): number {
return Math.floor(
(to.getTime() - from.getTime()) / (24 * 60 * 60 * 1000)
);
}