diff --git a/KNOWLEDGE_BASE.md b/KNOWLEDGE_BASE.md index 1073cf7f3..c86097248 100644 --- a/KNOWLEDGE_BASE.md +++ b/KNOWLEDGE_BASE.md @@ -531,7 +531,7 @@ Sources: [web app manifest](public/site.webmanifest), [service worker](public/sw ### Activity graphs and README embeds -Profiles display commit activity with current and longest streaks. The profile graph adapts to how long someone has been committing: the last 24 hours, 7 days, 30 days, or a year. Hovering a cell highlights that period's commits. Settings → Profile includes a live preview and copyable Markdown for embedding your /dev/null activity graph in a GitHub profile README. The graph is public at `/@{handle}/graph.svg`, uses your lane color, and supports `?theme=light`, `dark`, or `auto` (light by default). The SVG is cached for an hour, so a new commit may not appear in an external embed immediately. +Profiles display commit activity with current and longest streaks. The profile graph adapts to how long someone has been committing: the last 24 hours, 7 days, 30 days, 6 months, or a year. Hovering a cell highlights that period's commits. Settings → Profile includes a live preview and copyable Markdown for embedding your /dev/null activity graph in a GitHub profile README. The graph is public at `/@{handle}/graph.svg`, always shows the full year, uses your lane color, and supports `?theme=light`, `dark`, or `auto` (light by default). The SVG is cached for an hour, so a new commit may not appear in an external embed immediately. Sources: [graph embed settings](resources/js/components/activity-graph-embed.tsx), [graph controller](app/Http/Controllers/UserActivityGraphController.php), [graph route and cache](routes/web.php). diff --git a/app/Enums/ActivityScale.php b/app/Enums/ActivityScale.php index 0554828e1..5ea1ed373 100644 --- a/app/Enums/ActivityScale.php +++ b/app/Enums/ActivityScale.php @@ -12,14 +12,22 @@ enum ActivityScale: string case Day = 'day'; case Week = 'week'; case Month = 'month'; + case Half = 'half'; case Year = 'year'; /** - * The smallest scale whose window still contains everything since the given moment. + * The scales shorter than a year that a profile's activity can fit into, smallest first. */ - public static function since(CarbonInterface $since, CarbonImmutable $now): self + public const array FITTING = [self::Day, self::Week, self::Month, self::Half]; + + /** + * The smallest of the given scales whose window still contains everything since the given moment, or else the year. + * + * @param list $scales + */ + public static function since(CarbonInterface $since, CarbonImmutable $now, array $scales = self::FITTING): self { - foreach ([self::Day, self::Week, self::Month] as $scale) { + foreach ($scales as $scale) { if ($since->greaterThanOrEqualTo($scale->start($now))) { return $scale; } @@ -34,13 +42,14 @@ public function start(CarbonImmutable $now): CarbonImmutable self::Day => $now->startOfHour()->subHours(23), self::Week => $now->startOfDay()->subDays(6), self::Month => $now->startOfDay()->subDays(29), + self::Half => $now->subWeeks(25)->startOfWeek(CarbonInterface::SUNDAY), self::Year => $now->subWeeks(52)->startOfWeek(CarbonInterface::SUNDAY), }; } /** * The start of the window ending now, as the operations page has always counted: the last 24 hours, 7 days, or - * 30 days. The year keeps its whole weeks, since its grid needs them. + * 30 days. The week grids keep their whole weeks, since they need them. */ public function rollingStart(CarbonImmutable $now): CarbonImmutable { @@ -48,7 +57,7 @@ public function rollingStart(CarbonImmutable $now): CarbonImmutable self::Day => $now->subDay(), self::Week => $now->subWeek(), self::Month => $now->subDays(30), - self::Year => $this->start($now), + self::Half, self::Year => $this->start($now), }; } diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php index 1b1725481..0a054ff9b 100644 --- a/app/Http/Controllers/Api/UserController.php +++ b/app/Http/Controllers/Api/UserController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api; +use App\Enums\ActivityScale; use App\Http\Requests\ShowApiUserRequest; use App\Http\Resources\CommitResource; use App\Http\Resources\UserResource; @@ -39,7 +40,8 @@ public function show(ShowApiUserRequest $request, #[CurrentUser] ?User $currentU 'follows_you' => $user->isFollowing($currentUser), 'is_muting' => $currentUser->isMuting($user), 'pasteis_received' => $user->pasteis_received, - 'activity' => $activity->get($user, $currentUser), + // API clients don't know the half-year scale yet, so they keep getting the year for anyone past a month. + 'activity' => $activity->get($user, $currentUser, scales: [ActivityScale::Day, ActivityScale::Week, ActivityScale::Month]), 'streaks' => $userStreaks, 'achievements' => $achievements->get($user, $currentUser, $userStreaks), 'pinned_commit' => $pinnedCommit instanceof Commit ? CommitResource::make($pinnedCommit) : null, diff --git a/app/Http/Requests/ShowSiteActivityRequest.php b/app/Http/Requests/ShowSiteActivityRequest.php index 202649e22..29aeb381a 100644 --- a/app/Http/Requests/ShowSiteActivityRequest.php +++ b/app/Http/Requests/ShowSiteActivityRequest.php @@ -27,7 +27,7 @@ public function authorize(): Response public function rules(): array { return [ - 'scale' => ['nullable', Rule::enum(ActivityScale::class)], + 'scale' => ['nullable', Rule::enum(ActivityScale::class)->only([ActivityScale::Day, ActivityScale::Week, ActivityScale::Month, ActivityScale::Year])], 'metric' => ['nullable', Rule::enum(ActivityMetric::class)], ]; } diff --git a/app/Queries/UserActivityQuery.php b/app/Queries/UserActivityQuery.php index 6fffab00e..bedf43e39 100644 --- a/app/Queries/UserActivityQuery.php +++ b/app/Queries/UserActivityQuery.php @@ -16,15 +16,16 @@ public function __construct(private ActivityBucketsQuery $buckets) {} /** - * Without a scale, it is the smallest one that fits all of the visible activity. + * Without a scale, it is the smallest of the given ones that fits all of the visible activity, or else the year. * + * @param list $scales * @return array{scale: ActivityScale, buckets: list} */ - public function get(User $user, ?User $viewer, ?ActivityScale $scale = null): array + public function get(User $user, ?User $viewer, ?ActivityScale $scale = null, array $scales = ActivityScale::FITTING): array { $now = CarbonImmutable::now(); - $scale ??= $this->scale($user, $viewer, $now); + $scale ??= $this->scale($user, $viewer, $now, $scales); return [ 'scale' => $scale, @@ -32,13 +33,17 @@ public function get(User $user, ?User $viewer, ?ActivityScale $scale = null): ar ]; } - private function scale(User $user, ?User $viewer, CarbonImmutable $now): ActivityScale + /** + * @param list $scales + */ + private function scale(User $user, ?User $viewer, CarbonImmutable $now, array $scales): ActivityScale { $firstCommitAt = $user->commits()->visibleTo($viewer)->min('created_at'); return ActivityScale::since( is_string($firstCommitAt) ? CarbonImmutable::parse($firstCommitAt) : $user->created_at, $now, + $scales, ); } } diff --git a/resources/js/components/activity-graph.tsx b/resources/js/components/activity-graph.tsx index 937f6792b..3c599fadd 100644 --- a/resources/js/components/activity-graph.tsx +++ b/resources/js/components/activity-graph.tsx @@ -33,9 +33,17 @@ export const activityWindows: Record = { day: 'in the last 24 hours', week: 'in the last 7 days', month: 'in the last 30 days', + half: 'in the last 6 months', year: 'in the last year', }; +/** + * The scales drawn as a grid of weeks, one column per week, rather than a strip. + */ +export function isWeekGrid(scale: ActivityScale): boolean { + return scale === 'half' || scale === 'year'; +} + function levelColor(level: number): string { return level === 0 ? 'var(--muted)' @@ -85,7 +93,7 @@ export function activitySummary( ): string { const total = activityTotal(activity); - return total === 0 && activity.scale !== 'year' + return total === 0 && !isWeekGrid(activity.scale) ? `No ${noun[1]} yet` : `${count(total, noun)} ${activityWindows[activity.scale]}`; } @@ -178,7 +186,7 @@ export function ActivityGraph({ onPeriodHover?: (period: string | null) => void; }) { const total = activityTotal(activity); - const isEmpty = total === 0 && activity.scale !== 'year'; + const isEmpty = total === 0 && !isWeekGrid(activity.scale); return (
- {scale === 'year' ? ( - index % 7 === 0); const monthLabels = weekStarts.map((day, index) => { const changesMonth = @@ -404,7 +420,9 @@ function YearGrid({ .slice(1, 3) .some((week) => monthOf(week.date) !== monthOf(day.date)); - return changesMonth && !nextLabelIsClose + const isLastWeek = index === weekStarts.length - 1; + + return changesMonth && !nextLabelIsClose && !isLastWeek ? monthNames[monthOf(day.date)] : null; }); @@ -425,7 +443,17 @@ function YearGrid({
getContent(), flags: JSON_THROW_ON_ERROR)->bio_profiles)->toBeInstanceOf(stdClass::class); }); +it('keeps API clients on the year for a profile whose first visible commit is two months old', function (): void { + $this->travelTo(now()->setDate(2026, 10, 15)->setTime(12, 0)); + + $keyPair = apiKeyPairFor(User::factory()->create()); + $user = User::factory()->create(); + + Commit::factory()->for($user)->create(['created_at' => '2026-08-15 09:00:00']); + + signedApiRequest($keyPair, 'GET', route('api-user.show', $user)) + ->assertOk() + ->assertJsonPath('activity.scale', 'year') + ->assertJsonCount(369, 'activity.buckets') + ->assertJsonPath('activity.buckets.0.date', '2025-10-12'); +}); + it('counts the pending commits of a signed-in user on their own profile', function (): void { $user = User::factory()->create(); $keyPair = apiKeyPairFor($user); diff --git a/tests/Feature/Controllers/SiteActivityControllerTest.php b/tests/Feature/Controllers/SiteActivityControllerTest.php index 06d42c086..0269b234d 100644 --- a/tests/Feature/Controllers/SiteActivityControllerTest.php +++ b/tests/Feature/Controllers/SiteActivityControllerTest.php @@ -71,6 +71,7 @@ ->assertSessionHasErrors(); })->with([ 'unknown scale' => [['scale' => 'decade']], + 'half scale' => [['scale' => 'half']], 'unknown series' => [['metric' => 'stars']], ]); diff --git a/tests/Unit/ActivityCells.test.tsx b/tests/Unit/ActivityCells.test.tsx index de49592e6..d0bbfe1d3 100644 --- a/tests/Unit/ActivityCells.test.tsx +++ b/tests/Unit/ActivityCells.test.tsx @@ -90,3 +90,61 @@ test('labels the cells with the summary and paints them in the given color', () assert.match(html, /aria-label="3 replies in the last 7 days"/); assert.match(html, /--lane:var\(--muted-foreground\)/); }); + +test('summarizes the half-year window, counting zero instead of saying none yet', () => { + assert.equal( + activitySummary( + { scale: 'half', buckets: [{ date: '2026-10-15', count: 3 }] }, + ['commit', 'commits'], + ), + '3 commits in the last 6 months', + ); + assert.equal( + activitySummary({ scale: 'half', buckets: [] }, ['commit', 'commits']), + '0 commits in the last 6 months', + ); +}); + +test('draws half and year as week grids with one column per week', () => { + for (const [scale, weeks] of [ + ['half', 26], + ['year', 53], + ] as const) { + const buckets = Array.from({ length: weeks * 7 }, (_, day) => ({ + date: new Date(Date.UTC(2026, 0, 4 + day)) + .toISOString() + .slice(0, 10), + count: day % 3, + })); + const html = renderToString( + , + ); + + assert.match( + html, + new RegExp(`repeat\\(${weeks}, minmax\\(0, 1fr\\)\\)`), + ); + assert.match(html, /grid-rows-7/); + } +}); + +test('leaves the last week unlabelled when it starts a month, so the label cannot spill past the grid', () => { + const buckets = Array.from({ length: 26 * 7 }, (_, day) => ({ + date: new Date(Date.UTC(2026, 4, 10 + day)).toISOString().slice(0, 10), + count: 1, + })); + const html = renderToString( + , + ); + + assert.match(html, /whitespace-nowrap">OctNov ['2026-10-09 00:00:00', ActivityScale::Week], 'just before the last 7 days' => ['2026-10-08 23:59:59', ActivityScale::Month], 'start of the last 30 days' => ['2026-09-16 00:00:00', ActivityScale::Month], - 'just before the last 30 days' => ['2026-09-15 23:59:59', ActivityScale::Year], + 'just before the last 30 days' => ['2026-09-15 23:59:59', ActivityScale::Half], + 'start of the last 26 weeks' => ['2026-04-19 00:00:00', ActivityScale::Half], + 'just before the last 26 weeks' => ['2026-04-18 23:59:59', ActivityScale::Year], 'years ago' => ['2024-01-01 00:00:00', ActivityScale::Year], ]); @@ -41,6 +43,8 @@ 'just now' => ['2026-10-15 12:30:00', ActivityScale::Day], 'three days ago' => ['2026-10-12 09:00:00', ActivityScale::Week], 'two weeks ago' => ['2026-10-01 09:00:00', ActivityScale::Month], + 'two months ago' => ['2026-08-15 09:00:00', ActivityScale::Half], + 'four months ago' => ['2026-06-15 09:00:00', ActivityScale::Half], 'two years ago' => ['2024-10-15 09:00:00', ActivityScale::Year], ]); @@ -85,6 +89,31 @@ ]); }); +it('falls back to the year when the half year is not among the scales to choose from', function (): void { + $user = User::factory()->create(); + + Commit::factory()->for($user)->create(['created_at' => '2026-08-15 09:00:00']); + + expect(resolve(UserActivityQuery::class)->get($user, $user, scales: [ActivityScale::Day, ActivityScale::Week, ActivityScale::Month])) + ->scale->toBe(ActivityScale::Year) + ->buckets->toHaveCount(369); +}); + +it('counts the commits of each day of the last 26 weeks', function (): void { + $user = User::factory()->create(); + + Commit::factory()->for($user)->create(['created_at' => '2026-06-15 09:00:00']); + Commit::factory()->for($user)->count(2)->create(); + + $activity = resolve(UserActivityQuery::class)->get($user, $user); + + expect($activity['scale'])->toBe(ActivityScale::Half) + ->and($activity['buckets'])->toHaveCount(180) + ->and($activity['buckets'][0])->toBe(['date' => '2026-04-19', 'count' => 0]) + ->and($activity['buckets'][57])->toBe(['date' => '2026-06-15', 'count' => 1]) + ->and(end($activity['buckets']))->toBe(['date' => '2026-10-15', 'count' => 2]); +}); + it('counts the commits of each day of the last 30 days', function (): void { $user = User::factory()->create();