krz/hutch-stats

Server-side utility for calculating contributions for sourcehut users. contributions server sourcehut stats

Commit 90e61ed3e8

90e61ed3e8e765ff5557bc59a6a7b141124dba01

parent: 01c1b50541

Verified · cmc

cmc <hello@cleberg.net> · 2026-04-11 23:54 UTC

refactor: retain and backfill only the last year of activity

Layout: unified · split

API.md +13 −21
@@ -140,9 +140,6 @@ Response `200 OK`:
140140 "is_recent_window_backfilled": false,
141141 "recent_backfill_state": "in_progress",
142142 "recent_backfill_completed_at": null,
143 "is_backfilled": false,
144 "backfill_state": "in_progress",
145 "backfill_completed_at": null,
146143 "days": [
147144 { "date": "2026-03-01", "count": 0, "score": 0.0 },
148145 { "date": "2026-03-02", "count": 3, "score": 2.5 }
@@ -158,12 +155,9 @@ Response fields:
158155- `is_indexed` boolean: whether the service has already completed at least one successful recent/incremental poll for this actor
159156- `last_polled_at` string or `null`: most recent successful poll time, if any
160157- `indexing_state` string: one of `pending`, `indexed`, or `error`
161- `is_recent_window_backfilled` boolean: whether the prioritized recent history window has completed backfill
158- `is_recent_window_backfilled` boolean: whether the service has finished filling the retained one-year history window
162159- `recent_backfill_state` string: one of `pending`, `in_progress`, `completed`, or `error`
163- `recent_backfill_completed_at` string or `null`: when recent-window backfill completed, if it has
164- `is_backfilled` boolean: whether historical backfill has completed for this actor
165- `backfill_state` string: one of `pending`, `in_progress`, `completed`, or `error`
166- `backfill_completed_at` string or `null`: when full historical backfill completed, if it has
160- `recent_backfill_completed_at` string or `null`: when one-year backfill completed, if it has
167161- `days` array:
168162 - `date` string `YYYY-MM-DD`
169163 - `count` integer contribution count for the day
@@ -175,15 +169,19 @@ Indexing state semantics:
175169- `indexed`: at least one successful poll has completed for the actor
176170- `error`: the most recent poll attempt for the actor failed
177171
178Backfill state semantics:
172Recent backfill semantics:
179173
180- recent-window fields:
181 - represent the prioritized visible-history window for client UX
182- `pending`: the actor has not started historical backfill yet
183- `in_progress`: historical backfill is actively progressing in bounded background batches
184- `completed`: historical backfill has completed for all supported services
174- the service only retains and backfills the most recent 365 days of activity
175- `pending`: the actor has not started one-year backfill yet
176- `in_progress`: one-year backfill is actively progressing in bounded background batches
177- `completed`: the retained one-year window is fully backfilled
185178- `error`: the most recent backfill attempt failed
186179
180Retention notes:
181
182- activity older than 365 days is not retained
183- scheduled polling periodically prunes contribution rows older than the retained window
184
187185Possible errors:
188186
189187- `400 Bad Request` for invalid or conflicting date input
@@ -218,7 +216,7 @@ Behavior notes:
218216
219217- This endpoint has the same actor-registration and alias-resolution behavior as the calendar endpoint.
220218- This endpoint returns immediately and does not block on SourceHut polling.
221- This endpoint also reflects historical backfill state so clients can distinguish recent indexing from complete history.
219- This endpoint also reflects whether the retained one-year history window has been fully backfilled yet.
222220
223221Example:
224222
@@ -239,9 +237,6 @@ Response `200 OK`:
239237 "is_recent_window_backfilled": true,
240238 "recent_backfill_state": "completed",
241239 "recent_backfill_completed_at": "2026-04-11T18:02:00Z",
242 "is_backfilled": false,
243 "backfill_state": "in_progress",
244 "backfill_completed_at": null,
245240 "total_events": 126,
246241 "total_score": 116.75,
247242 "active_days": 14,
@@ -261,9 +256,6 @@ Response fields:
261256- `is_recent_window_backfilled` boolean
262257- `recent_backfill_state` string
263258- `recent_backfill_completed_at` string or `null`
264- `is_backfilled` boolean
265- `backfill_state` string
266- `backfill_completed_at` string or `null`
267259- `total_events` integer
268260- `total_score` float
269261- `active_days` integer
README.md +3 −8
@@ -211,9 +211,6 @@ Example response:
211211 "is_recent_window_backfilled": true,
212212 "recent_backfill_state": "completed",
213213 "recent_backfill_completed_at": "2026-04-11T18:02:00Z",
214 "is_backfilled": false,
215 "backfill_state": "in_progress",
216 "backfill_completed_at": null,
217214 "days": [
218215 {"date": "2026-03-28", "count": 3, "score": 3.5},
219216 {"date": "2026-03-29", "count": 0, "score": 0.0},
@@ -241,9 +238,6 @@ Example response:
241238 "is_recent_window_backfilled": true,
242239 "recent_backfill_state": "completed",
243240 "recent_backfill_completed_at": "2026-04-11T18:02:00Z",
244 "is_backfilled": false,
245 "backfill_state": "in_progress",
246 "backfill_completed_at": null,
247241 "total_events": 42,
248242 "total_score": 37.5,
249243 "active_days": 18,
@@ -326,14 +320,15 @@ The SourceHut-specific assumptions are isolated to the service modules:
326320
327321- `src/srht_contrib/services/todo.py` uses the authenticated `events(cursor)` feed first, then falls back to tracker/ticket event traversal for reliable contribution discovery.
328322- `src/srht_contrib/services/git.py` discovers owned repositories for an actor, polls each repository `log(cursor)`, and attributes commits through the configured alias map.
323- the service only retains the most recent 365 days of contribution history and periodically prunes older rows
329324
330325## Known Limitations
331326
332327- `git.sr.ht` polling assumes the actor's repositories are discoverable through the SourceHut GraphQL API
333328- scheduled polling runs in-process, so it is not a distributed scheduler
334329- newly requested actors are indexed asynchronously, so the first public read may be empty until a scheduler or manual poll runs
335- the recent visible-history window is prioritized first, but deep-history backfill can still take many scheduler passes for active users
336- full historical backfill can take many scheduler passes for active users because it runs in bounded batches
330- the service is intentionally limited to a rolling one-year history window; older activity is not retained
331- one-year backfill still runs in bounded batches, so a newly requested actor may take multiple scheduler passes before their visible graph is fully filled in
337332- alias management is config-driven; there is no alias CRUD API yet
338333- current deployment model is trusted-operator V1, not a public multi-tenant service
339334
src/srht_contrib/jobs/poller.py +13 −39
@@ -13,14 +13,13 @@ from srht_contrib.schemas import NormalizedEvent
1313from srht_contrib.services.git import GitIngestionService
1414from srht_contrib.services.srht_client import SourceHutClientError
1515from srht_contrib.services.todo import TodoIngestionService
16from srht_contrib.utils.retention import RETENTION_DAYS, prune_contribution_events
1617from srht_contrib.utils.repositories import canonicalize_repository_name
1718
1819
1920logger = logging.getLogger(__name__)
2021SYNC_OVERLAP = timedelta(hours=24)
21RECENT_BACKFILL_DAYS = 365
2222RECENT_BACKFILL_BATCHES_PER_SERVICE = 5
23FULL_BACKFILL_BATCHES_PER_SERVICE = 1
2423
2524
2625class PollerService:
@@ -61,6 +60,9 @@ class PollerService:
6160 logger.exception("Scheduled poll failed for actor=%s", actor)
6261 except Exception:
6362 logger.exception("Unexpected scheduled poll failure for actor=%s", actor)
63 deleted = self.prune_old_events(db)
64 if deleted:
65 logger.info("Pruned %s contribution events older than %s days", deleted, RETENTION_DAYS)
6466 return results
6567
6668 def track_actor_request(self, db: Session, actor: str, *, update_last_requested: bool = True) -> TrackedActor:
@@ -70,7 +72,6 @@ class PollerService:
7072 actor=actor,
7173 is_active=True,
7274 recent_backfill_status="pending",
73 backfill_status="pending",
7475 )
7576 db.add(tracked_actor)
7677
@@ -185,11 +186,11 @@ class PollerService:
185186
186187 def _run_backfill_batches(self, db: Session, actor: str) -> int:
187188 tracked_actor = self.track_actor_request(db, actor, update_last_requested=False)
188 if tracked_actor.recent_backfill_status == "completed" and tracked_actor.backfill_status == "completed":
189 if tracked_actor.recent_backfill_status == "completed":
189190 return 0
190191
191192 now = datetime.now(tz=UTC)
192 recent_since = now - timedelta(days=RECENT_BACKFILL_DAYS)
193 recent_since = now - timedelta(days=RETENTION_DAYS)
193194 db.add(tracked_actor)
194195 db.flush()
195196 total_inserted = 0
@@ -223,34 +224,6 @@ class PollerService:
223224 db.add(tracked_actor)
224225 db.flush()
225226
226 if tracked_actor.recent_backfill_status == "completed" and tracked_actor.backfill_status != "completed":
227 if tracked_actor.backfill_started_at is None:
228 tracked_actor.backfill_started_at = now
229 tracked_actor.backfill_status = "in_progress"
230 tracked_actor.last_backfill_error = None
231 total_inserted += self._run_backfill_scope(
232 db,
233 actor=actor,
234 scope="full",
235 services=[
236 (self.todo_service.service_name, self.todo_service.fetch_backfill_batch),
237 (self.git_service.service_name, self.git_service.fetch_backfill_batch),
238 ],
239 batches_per_service=FULL_BACKFILL_BATCHES_PER_SERVICE,
240 since=None,
241 )
242 full_statuses = db.scalars(
243 select(ServiceBackfillState.status)
244 .where(ServiceBackfillState.actor == actor)
245 .where(ServiceBackfillState.scope == "full")
246 ).all()
247 if full_statuses and all(status == "completed" for status in full_statuses):
248 tracked_actor.backfill_status = "completed"
249 tracked_actor.backfill_completed_at = datetime.now(tz=UTC)
250 tracked_actor.last_backfill_error = None
251 db.add(tracked_actor)
252 db.flush()
253
254227 return total_inserted
255228
256229 def _run_backfill_scope(
@@ -328,12 +301,8 @@ class PollerService:
328301 state.status = "error"
329302 state.last_error = str(exc)
330303 state.updated_at = datetime.now(tz=UTC)
331 if scope == "recent":
332 tracked_actor.recent_backfill_status = "error"
333 tracked_actor.last_recent_backfill_error = str(exc)
334 else:
335 tracked_actor.backfill_status = "error"
336 tracked_actor.last_backfill_error = str(exc)
304 tracked_actor.recent_backfill_status = "error"
305 tracked_actor.last_recent_backfill_error = str(exc)
337306 db.add(state)
338307 db.add(tracked_actor)
339308 db.flush()
@@ -343,3 +312,8 @@ class PollerService:
343312 db.flush()
344313
345314 return total_inserted
315
316 def prune_old_events(self, db: Session) -> int:
317 deleted = prune_contribution_events(db)
318 db.commit()
319 return deleted
src/srht_contrib/schemas.py −3
@@ -31,9 +31,6 @@ class ContributionIndexMetadata(BaseModel):
3131 is_recent_window_backfilled: bool = False
3232 recent_backfill_state: Literal["pending", "in_progress", "completed", "error"] = "pending"
3333 recent_backfill_completed_at: datetime | None = None
34 is_backfilled: bool = False
35 backfill_state: Literal["pending", "in_progress", "completed", "error"] = "pending"
36 backfill_completed_at: datetime | None = None
3734
3835
3936class ContributionCalendarResponse(ContributionIndexMetadata):
src/srht_contrib/services/aggregator.py −6
@@ -65,9 +65,6 @@ class ContributionAggregator:
6565 is_recent_window_backfilled=calendar.is_recent_window_backfilled,
6666 recent_backfill_state=calendar.recent_backfill_state,
6767 recent_backfill_completed_at=calendar.recent_backfill_completed_at,
68 is_backfilled=calendar.is_backfilled,
69 backfill_state=calendar.backfill_state,
70 backfill_completed_at=calendar.backfill_completed_at,
7168 )
7269
7370 def _index_metadata(self, db: Session, actor: str) -> ContributionIndexMetadata:
@@ -90,9 +87,6 @@ class ContributionAggregator:
9087 is_recent_window_backfilled=(tracked_actor.recent_backfill_status == "completed") if tracked_actor is not None else False,
9188 recent_backfill_state=(tracked_actor.recent_backfill_status if tracked_actor is not None else "pending"),
9289 recent_backfill_completed_at=tracked_actor.recent_backfill_completed_at if tracked_actor is not None else None,
93 is_backfilled=(tracked_actor.backfill_status == "completed") if tracked_actor is not None else False,
94 backfill_state=(tracked_actor.backfill_status if tracked_actor is not None else "pending"),
95 backfill_completed_at=tracked_actor.backfill_completed_at if tracked_actor is not None else None,
9690 )
9791
9892 def _query_daily_aggregates(self, db: Session, actor: str, start: date, end: date) -> list[DailyAggregate]:
src/srht_contrib/utils/retention.py added +17
@@ -0,0 +1,17 @@
1from __future__ import annotations
2
3from datetime import UTC, datetime, timedelta
4
5from sqlalchemy import delete
6from sqlalchemy.orm import Session
7
8from srht_contrib.models import ContributionEvent
9
10
11RETENTION_DAYS = 365
12
13
14def prune_contribution_events(db: Session, *, now: datetime | None = None) -> int:
15 cutoff = (now or datetime.now(tz=UTC)) - timedelta(days=RETENTION_DAYS)
16 result = db.execute(delete(ContributionEvent).where(ContributionEvent.occurred_at < cutoff))
17 return int(result.rowcount or 0)
tests/test_contributions_api.py −4
@@ -68,9 +68,6 @@ def test_public_read_registers_actor_for_lazy_indexing(client: TestClient, db_se
6868 assert response.json()["is_recent_window_backfilled"] is False
6969 assert response.json()["recent_backfill_state"] == "pending"
7070 assert response.json()["recent_backfill_completed_at"] is None
71 assert response.json()["is_backfilled"] is False
72 assert response.json()["backfill_state"] == "pending"
73 assert response.json()["backfill_completed_at"] is None
7471 assert response.json()["last_polled_at"] is None
7572 assert tracked_actor is not None
7673 assert tracked_actor.is_active is True
@@ -117,7 +114,6 @@ def test_contribution_stats_api(client: TestClient, db_session) -> None:
117114 assert response.json()["indexing_state"] == "indexed"
118115 assert response.json()["is_recent_window_backfilled"] is False
119116 assert response.json()["recent_backfill_state"] == "pending"
120 assert response.json()["is_backfilled"] is False
121117
122118
123119def test_invalid_date_input_returns_400(client: TestClient) -> None:
tests/test_ingestion.py +61 −13
@@ -4,7 +4,7 @@ from sqlalchemy import select
44
55from srht_contrib.config import Settings
66from srht_contrib.jobs.poller import PollerService
7from srht_contrib.models import ServiceBackfillState, SyncState, TrackedActor, TrackedRepository
7from srht_contrib.models import ContributionEvent, ServiceBackfillState, SyncState, TrackedActor, TrackedRepository
88from srht_contrib.schemas import NormalizedEvent
99from srht_contrib.services.git import GitIngestionService, GitPollResult
1010from srht_contrib.services.todo import TodoIngestionService, TodoPollResult
@@ -94,7 +94,7 @@ class BackfillingTodoService:
9494 repo_name="todo",
9595 resource_id="backfill-ticket",
9696 external_uid=f"todo:backfill:{actor}",
97 occurred_at=datetime(2024, 1, 1, 12, 0, tzinfo=UTC),
97 occurred_at=datetime(2026, 1, 1, 12, 0, tzinfo=UTC),
9898 weight=1.0,
9999 raw_payload_json=None,
100100 )
@@ -119,7 +119,13 @@ class QueueShrinkingTodoService:
119119 def fetch_backfill_batch(self, actor: str, cursor_state: dict | None = None) -> BackfillBatchResult:
120120 import copy
121121
122 state = {"tracker_queue": ["t1", "t2"], "current_tracker": None, "current_ticket": None, "trackers_loaded": True, "trackers_cursor": None}
122 state = {
123 "tracker_queue": ["t1", "t2", "t3", "t4", "t5", "t6"],
124 "current_tracker": None,
125 "current_ticket": None,
126 "trackers_loaded": True,
127 "trackers_cursor": None,
128 }
123129 if cursor_state:
124130 state.update(copy.deepcopy(cursor_state))
125131 if not state["tracker_queue"]:
@@ -156,7 +162,12 @@ class QueueShrinkingGitService:
156162 def fetch_backfill_batch(self, actor: str, cursor_state: dict | None = None) -> BackfillBatchResult:
157163 import copy
158164
159 state = {"repository_queue": ["r1", "r2"], "current_repository": None, "discovery_complete": True, "discovery_cursor": None}
165 state = {
166 "repository_queue": ["r1", "r2", "r3", "r4", "r5", "r6"],
167 "current_repository": None,
168 "discovery_complete": True,
169 "discovery_cursor": None,
170 }
160171 if cursor_state:
161172 state.update(copy.deepcopy(cursor_state))
162173 if not state["repository_queue"]:
@@ -561,9 +572,7 @@ def test_poll_marks_backfill_complete_and_persists_service_state(db_session) ->
561572 assert tracked_actor is not None
562573 assert tracked_actor.recent_backfill_status == "completed"
563574 assert tracked_actor.recent_backfill_completed_at is not None
564 assert tracked_actor.backfill_status == "completed"
565 assert tracked_actor.backfill_completed_at is not None
566 assert [f"{state.scope}:{state.service}" for state in service_states] == ["full:git", "full:todo", "recent:git", "recent:todo"]
575 assert [f"{state.scope}:{state.service}" for state in service_states] == ["recent:git", "recent:todo"]
567576 assert all(state.status == "completed" for state in service_states)
568577
569578
@@ -576,7 +585,7 @@ def test_backfill_cursor_state_shrinks_across_repeated_polls(db_session) -> None
576585 for state in db_session.scalars(
577586 select(ServiceBackfillState)
578587 .where(ServiceBackfillState.actor == "~ccleberg")
579 .where(ServiceBackfillState.scope == "full")
588 .where(ServiceBackfillState.scope == "recent")
580589 ).all()
581590 }
582591
@@ -586,11 +595,50 @@ def test_backfill_cursor_state_shrinks_across_repeated_polls(db_session) -> None
586595 for state in db_session.scalars(
587596 select(ServiceBackfillState)
588597 .where(ServiceBackfillState.actor == "~ccleberg")
589 .where(ServiceBackfillState.scope == "full")
598 .where(ServiceBackfillState.scope == "recent")
590599 ).all()
591600 }
592601
593 assert first_states["git"]["repository_queue"] == ["r2"]
594 assert first_states["todo"]["tracker_queue"] == ["t2"]
595 assert second_states["git"]["repository_queue"] == []
596 assert second_states["todo"]["tracker_queue"] == []
602 assert first_states["git"]["repository_queue"] == ["r6"]
603 assert first_states["todo"]["tracker_queue"] == ["t6"]
604 assert second_states["git"] is None
605 assert second_states["todo"] is None
606
607
608def test_prune_old_events_removes_data_older_than_one_year(db_session) -> None:
609 poller = PollerService(todo_service=BackfillingTodoService(), git_service=EmptyGitService())
610 db_session.add_all(
611 [
612 ContributionEvent(
613 service="todo",
614 event_type="ticket_created",
615 actor="~ccleberg",
616 repo_name="todo",
617 resource_id="old",
618 external_uid="todo:old",
619 occurred_at=datetime(2025, 1, 1, 12, 0, tzinfo=UTC),
620 weight=1.0,
621 raw_payload_json=None,
622 ),
623 ContributionEvent(
624 service="todo",
625 event_type="ticket_created",
626 actor="~ccleberg",
627 repo_name="todo",
628 resource_id="recent",
629 external_uid="todo:recent",
630 occurred_at=datetime(2026, 4, 1, 12, 0, tzinfo=UTC),
631 weight=1.0,
632 raw_payload_json=None,
633 ),
634 ]
635 )
636 db_session.commit()
637
638 deleted = poller.prune_old_events(db_session)
639 remaining = db_session.scalars(
640 select(ContributionEvent.external_uid).order_by(ContributionEvent.external_uid)
641 ).all()
642
643 assert deleted == 1
644 assert remaining == ["todo:recent"]