From 6754b48e7d634720081d10cade6d38afef31e70c Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 24 Oct 2023 13:26:34 +0200 Subject: [PATCH] Create normal and day-equidistant commit date arrays --- scripts/graph/commits.ts | 43 +++++++++++++++++++++++++++++++++++++++- scripts/graph/util.ts | 2 ++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/scripts/graph/commits.ts b/scripts/graph/commits.ts index 0885af7..5285922 100644 --- a/scripts/graph/commits.ts +++ b/scripts/graph/commits.ts @@ -1,4 +1,5 @@ -import { CommitsResponse } from "./requests"; +import { CommitsResponse } from "./requests.js"; +import { SECONDS_PER_DAY } from "./util.js"; type Commit = { indexByHash: number; @@ -14,6 +15,8 @@ type Commit = { export class Commits { #graphId: number | null = null; #commitsByGraph: Commit[] = []; + #committerDatesNormal: Date[] = []; + #committerDatesDayEquidistant: Date[] = []; requiresUpdate(graphId: number): boolean { return this.#graphId === null || this.#graphId < graphId; @@ -33,8 +36,15 @@ export class Commits { commit.indexByGraph = idx; } + const committerDatesNormal = commits.map(c => c.committerDate); + const committerDatesDayEquidistant = this.#makeDayEquidistant(committerDatesNormal); + + // To prevent exceptions and other weirdness from messing up our state, + // we update everything in one go. this.#graphId = response.graphId; this.#commitsByGraph = commits; + this.#committerDatesNormal = this.#epochTimesToDates(committerDatesNormal); + this.#committerDatesDayEquidistant = this.#epochTimesToDates(committerDatesDayEquidistant); } #loadCommits(response: CommitsResponse): Commit[] { @@ -113,4 +123,35 @@ export class Commits { console.assert(sorted.length === commits.length); return sorted; } + + /** + * Assumes the times are sorted. + */ + #makeDayEquidistant(times: number[]): number[] { + const days: { day: number, amount: number; }[] = []; + for (const time of times) { + const day = time % SECONDS_PER_DAY; + const prev = days.at(-1); + if (prev === undefined || prev.day !== day) { + days.push({ day, amount: 1 }); + } else { + prev.amount++; + } + } + + const result: number[] = []; + for (const day of days) { + const secondsPerCommit = SECONDS_PER_DAY / day.amount; + for (let i = 0; i < day.amount; i++) { + const time = day.day * SECONDS_PER_DAY + secondsPerCommit * (i + 0.5); + result.push(time); + } + } + + return result; + } + + #epochTimesToDates(times: number[]): Date[] { + return times.map(t => new Date(1000 * t)); + } } diff --git a/scripts/graph/util.ts b/scripts/graph/util.ts index 4cd27ea..4cbb0d3 100644 --- a/scripts/graph/util.ts +++ b/scripts/graph/util.ts @@ -9,3 +9,5 @@ export function el(name: string, attributes: { [key: string]: string; }, ...chil element.append(...children); return element; } + +export const SECONDS_PER_DAY = 24 * 60 * 60;