Implements activity endpoint for project graph

Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
Sebastian Martinez
2022-08-24 12:07:02 +02:00
committed by Alexis Sellier
parent 7481808014
commit 3a5527208b
4 changed files with 24 additions and 26 deletions
+2 -2
View File
@@ -1,9 +1,9 @@
<script lang="ts">
import { onMount } from "svelte";
import type { CommitGroup } from "./commit";
import type { WeeklyActivity } from "./commit";
export let strokeWidth: number;
export let points: CommitGroup[];
export let points: WeeklyActivity[];
export let viewBoxWidth: number;
export let viewBoxHeight: number;
+3 -11
View File
@@ -13,18 +13,10 @@
export let anchor: proj.Anchor | null = null;
export let compact = false;
const getTimestampOneYearAgo = () => {
const now = new Date();
const oneYearAgo = new Date(now.getFullYear() - 1, now.getMonth(), now.getDate());
return Math.floor(oneYearAgo.getTime() / 1000).toString();
};
const loadCommits = async () => {
const commits = await Project.getCommits(project.urn, seed.api, {
parent: project.head ?? null,
since: getTimestampOneYearAgo(),
});
return groupCommitsByWeek(commits.headers);
const commits = await Project.getActivity(project.urn, seed.api);
return groupCommitsByWeek(commits.activity);
};
</script>
+12 -13
View File
@@ -60,6 +60,13 @@ export interface CommitGroup {
week: number;
}
export interface WeeklyActivity {
date: string;
time: number;
commits: number[];
week: number;
}
export interface DiffStats {
additions: number;
deletions: number;
@@ -137,30 +144,22 @@ export async function fetchCommits(project: Project, parentCommit: string): Prom
return groupCommitHistory(commitsQuery);
}
export function groupCommitsByWeek(commits: CommitMetadata[]): CommitGroup[] {
const groupedCommits: CommitGroup[] = [];
export function groupCommitsByWeek(commits: number[]): WeeklyActivity[] {
const groupedCommits: WeeklyActivity[] = [];
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;
});
commits = commits.sort((a, b) => a > b ? -1 : a < b ? 1 : 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);
let weekAccumulator = Math.floor(getDaysPassed(new Date(commits[0] * 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 time = commit * 1000;
const date = new Date(time);
const isNewWeek =
!groupedCommits.length ||
+7
View File
@@ -349,6 +349,13 @@ export class Project implements ProjectInfo {
return new Request(`projects/${urn}/commits`, host).get(params);
}
static async getActivity(
urn: string,
host: Host,
): Promise<{ activity: number[] }> {
return new Request(`projects/${urn}/activity`, host).get();
}
async getCommit(commit: string): Promise<Commit> {
return new Request(`projects/${this.urn}/commits/${commit}`, this.seed.api).get();
}