feat(store): fetch contributions incrementally and persist history in SQLite
Bound each provider's startFetch()/resolveFetch() to an optional since/until window, wire a SQLite-backed ContributionStore into ContributionAggregator (fetch only the trailing window past the last stored date, merge into the store, prune by CONTRIBUTIONS_RETENTION_DAYS), and add a graph:contributions:refetch command to force a full or ranged re-fetch in <=365-day chunks. This is the root fix for the full 365-day-refetch timeout that used to hit on every cache miss.
This commit is contained in:
@@ -50,16 +50,18 @@ final class GitLabProvider implements ProviderInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{baseUrl: string, userId: int, after: string, page: int, response: ResponseInterface}
|
||||
* Resolves the username to a numeric user ID, then fires the first page of the events request.
|
||||
*
|
||||
* @return array{baseUrl: string, userId: int, after: string, before: ?string, page: int, response: ResponseInterface}
|
||||
*
|
||||
* ponytail: the user-id lookup and event pagination stay sequential within
|
||||
* this one provider (each page depends on the previous). Parallelism here
|
||||
* only spans across providers; revisit only if GitLab pagination itself
|
||||
* becomes the bottleneck.
|
||||
*/
|
||||
public function startFetch(): array
|
||||
public function startFetch(?\DateTimeImmutable $since = null, ?\DateTimeImmutable $until = null): array
|
||||
{
|
||||
|
||||
|
||||
$this->logger->debug('GitLabProvider: fetching contributions', ['user' => $this->username, 'url' => $this->baseUrl]);
|
||||
|
||||
$userResponse = $this->client->request('GET', "$this->baseUrl/api/v4/users", [
|
||||
@@ -72,26 +74,31 @@ final class GitLabProvider implements ProviderInterface
|
||||
throw new NotFoundHttpException("GitLab: user '{$this->username}' not found on $this->baseUrl");
|
||||
}
|
||||
$userId = $users[0]['id'];
|
||||
$after = (new \DateTimeImmutable('-365 days'))->format('Y-m-d');
|
||||
$after = ($since ?? new \DateTimeImmutable('-365 days'))->format('Y-m-d');
|
||||
$before = $until?->format('Y-m-d');
|
||||
|
||||
$query = ['after' => $after, 'per_page' => 100, 'page' => 1];
|
||||
if ($before !== null) {
|
||||
$query['before'] = $before;
|
||||
}
|
||||
|
||||
$response = $this->client->request('GET', "$this->baseUrl/api/v4/users/$userId/events", [
|
||||
'headers' => ['PRIVATE-TOKEN' => $this->token],
|
||||
'query' => [
|
||||
'after' => $after,
|
||||
'per_page' => 100,
|
||||
'page' => 1,
|
||||
],
|
||||
'query' => $query,
|
||||
]);
|
||||
|
||||
return ['baseUrl' => $this->baseUrl, 'userId' => $userId, 'after' => $after, 'page' => 1, 'response' => $response];
|
||||
return ['baseUrl' => $this->baseUrl, 'userId' => $userId, 'after' => $after, 'before' => $before, 'page' => 1, 'response' => $response];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, int> date (Y-m-d) => event count
|
||||
* Follows pagination (100 events/page) until a short page is returned, counting events per day.
|
||||
*
|
||||
* @param array{baseUrl: string, userId: int, after: string, before: ?string, page: int, response: ResponseInterface} $handle
|
||||
* @return array<string, int> date (Y-m-d) => event count
|
||||
*/
|
||||
public function resolveFetch(mixed $handle): array
|
||||
{
|
||||
['baseUrl' => $baseUrl, 'userId' => $userId, 'after' => $after, 'page' => $page, 'response' => $response] = $handle;
|
||||
['baseUrl' => $baseUrl, 'userId' => $userId, 'after' => $after, 'before' => $before, 'page' => $page, 'response' => $response] = $handle;
|
||||
|
||||
$result = [];
|
||||
|
||||
@@ -106,13 +113,14 @@ final class GitLabProvider implements ProviderInterface
|
||||
$page++;
|
||||
|
||||
if (count($events) === 100) {
|
||||
$query = ['after' => $after, 'per_page' => 100, 'page' => $page];
|
||||
if ($before !== null) {
|
||||
$query['before'] = $before;
|
||||
}
|
||||
|
||||
$response = $this->client->request('GET', "$baseUrl/api/v4/users/$userId/events", [
|
||||
'headers' => ['PRIVATE-TOKEN' => $this->token],
|
||||
'query' => [
|
||||
'after' => $after,
|
||||
'per_page' => 100,
|
||||
'page' => $page,
|
||||
],
|
||||
'query' => $query,
|
||||
]);
|
||||
}
|
||||
} while (count($events) === 100);
|
||||
|
||||
Reference in New Issue
Block a user