Skip to content

Commit 840688d

Browse files
feat: strategy timings cache (#103)
## Description Saves strategy timestamps in cache to optimize reindex speed ## Note Since this involves messing around with the migration file , we'll have to be careful - Backup the branch on basiton server `git checkout -b bk-dev` - Run `db:cache:reset` - Switch to `dev` and pull in the latest changes - Run `db:cache:migrate` (this should create the new cache table for timings) We will likely need repeat the same for the next deployment (blue / green) cause the migration files have been edited ## Checklist before requesting a review - [ ] I have conducted a self-review of my code. - [x] I have conducted a QA. - [ ] If it is a core feature, I have included comprehensive tests. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Summary by CodeRabbit - **New Features** - Introduced a caching mechanism for strategy timings to boost data retrieval efficiency. - Added enhanced bootstrap commands for streamlined initialization and support for filtering event queries. - Implemented a new script for managing strategy timings retrieval and caching. - Added a new property for managing strategy timings in various handlers and processors. - Enhanced the `GetEventsFilters` type with an optional `eventName` property for improved query filtering. - Introduced a new schema for blockchain configurations in the bootstrap process. - Added a new command to the production operations guide for strategy timings initialization. - Added new types and properties related to strategy timings to improve type safety and functionality. - **Refactor** - Optimized logging within event processing to reduce verbosity and improve performance. - Updated database schema and migration routines to support the new caching functionality. - **Tests** - Expanded test suites to validate the caching integration and updated dependency management. - Added mocks for new caching functionality to enhance test coverage. - Updated test setups to include new dependencies related to strategy timings. - Corrected property naming inconsistencies in test cases. - **Chores** - Refined dependency configurations and scripts for improved system stability. <!-- end of auto-generated comment: release notes by coderabbit.ai --> https://www.loom.com/share/c22dfbde6567495bb66af0cf40df699d
2 parents 06294d9 + d12c6df commit 840688d

41 files changed

Lines changed: 630 additions & 58 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/production_operations.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ This guide is for production operations on the Gitcoin Data Layer.
8383

8484
2. `pnpm bootstrap:metadata`
8585
3. `pnpm bootstrap:pricing`
86+
4. `pnpm bootstrap:strategyTimings`
8687

8788
### Migrate processing tables: Run 2 times, first green, blue after.
8889

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,16 @@ cp scripts/hasura-config/.env.example scripts/hasura-config/.env
8585
pnpm db:cache:migrate
8686
```
8787

88-
7. Bootstrap DB with IPFS metadata and Pricing(optional):
88+
7. Bootstrap DB with IPFS metadata and Pricing and StrategyTimings(optional):
8989

9090
```
91+
# To run all of them
92+
pnpm bootstrap:all
93+
94+
# To run one specific one
9195
pnpm bootstrap:metadata
9296
pnpm bootstrap:pricing
97+
pnpm bootstrap:strategyTimings
9398
```
9499

95100
8. Apply migrations:

apps/processing/src/services/sharedDependencies.service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
KyselyRoundRepository,
2222
KyselyStrategyProcessingCheckpointRepository,
2323
KyselyStrategyRegistryRepository,
24+
KyselyStrategyTimingsCache,
2425
KyselyTransactionManager,
2526
} from "@grants-stack-indexer/repository";
2627
import {
@@ -136,6 +137,10 @@ export class SharedDependenciesService {
136137
kyselyDatabase,
137138
env.DATABASE_SCHEMA,
138139
);
140+
const strategyTimingsRepository = new KyselyStrategyTimingsCache(
141+
kyselyDatabase,
142+
env.DATABASE_SCHEMA,
143+
);
139144

140145
const strategyProcessingCheckpointRepository =
141146
new KyselyStrategyProcessingCheckpointRepository(kyselyDatabase, env.DATABASE_SCHEMA);
@@ -165,6 +170,7 @@ export class SharedDependenciesService {
165170
attestationRepository,
166171
transactionManager,
167172
legacyProjectRepository,
173+
strategyTimingsRepository,
168174
},
169175
registriesRepositories: {
170176
eventRegistryRepository,

apps/processing/test/unit/sharedDependencies.service.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ vi.mock("@grants-stack-indexer/repository", () => ({
5151
KyselyLegacyProjectRepository: vi.fn(),
5252
InMemoryPricingCache: vi.fn(),
5353
InMemoryMetadataCache: vi.fn(),
54+
KyselyStrategyTimingsCache: vi.fn(),
5455
}));
5556

5657
vi.mock("@grants-stack-indexer/pricing", () => ({

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
"type": "module",
99
"scripts": {
1010
"api:configure": "pnpm run --filter @grants-stack-indexer/hasura-config api:configure",
11+
"bootstrap:all": "pnpm run --filter @grants-stack-indexer/bootstrap bootstrap:all",
1112
"bootstrap:metadata": "pnpm run --filter @grants-stack-indexer/bootstrap bootstrap:metadata",
1213
"bootstrap:pricing": "pnpm run --filter @grants-stack-indexer/bootstrap bootstrap:pricing",
14+
"bootstrap:strategyTimings": "pnpm run --filter @grants-stack-indexer/bootstrap bootstrap:strategyTimings",
1315
"build": "turbo run build",
1416
"check-types": "turbo run check-types",
1517
"clean": "turbo run clean",

packages/data-flow/src/orchestrator.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,12 @@ export class Orchestrator {
196196
{ abortSignal: signal },
197197
);
198198
processedEvents++;
199-
this.logger.info(`Processed events: ${processedEvents}/${totalEvents}`, {
200-
className: Orchestrator.name,
201-
chainId: this.chainId,
202-
});
199+
if (processedEvents % 1000 === 0) {
200+
this.logger.info(`Processed events: ${processedEvents}/${totalEvents}`, {
201+
className: Orchestrator.name,
202+
chainId: this.chainId,
203+
});
204+
}
203205
} catch (error: unknown) {
204206
if (event) {
205207
await this.eventsRegistry.saveLastProcessedEvent(this.chainId, {

packages/data-flow/src/types/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import {
55
IApplicationPayoutRepository,
66
IApplicationRepository,
77
IAttestationRepository,
8+
ICache,
89
IDonationRepository,
910
ILegacyProjectRepository,
1011
IProjectRepository,
1112
IRoundRepository,
1213
ITransactionManager,
14+
StrategyTimings,
1315
} from "@grants-stack-indexer/repository";
14-
import { ICacheable } from "@grants-stack-indexer/shared";
16+
import { Address, ICacheable } from "@grants-stack-indexer/shared";
1517

1618
/**
1719
* The core dependencies for the data flow
@@ -34,4 +36,5 @@ export type CoreDependencies = Pick<
3436
attestationRepository: IAttestationRepository;
3537
transactionManager: ITransactionManager;
3638
legacyProjectRepository: ILegacyProjectRepository;
39+
strategyTimingsRepository: ICache<Address, StrategyTimings>;
3740
};

packages/data-flow/test/integration/helpers/dependencies.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const createMockRepositories = (): Pick<
2323
| "transactionManager"
2424
| "attestationRepository"
2525
| "legacyProjectRepository"
26+
| "strategyTimingsRepository"
2627
> => ({
2728
projectRepository: {
2829
getProjects: vi.fn(),
@@ -89,6 +90,10 @@ export const createMockRepositories = (): Pick<
8990
attestationRepository: {
9091
insertAttestation: vi.fn(),
9192
},
93+
strategyTimingsRepository: {
94+
get: vi.fn(),
95+
set: vi.fn(),
96+
},
9297
});
9398

9499
// Mock providers

packages/data-flow/test/unit/orchestrator.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
IApplicationPayoutRepository,
1111
IApplicationRepository,
1212
IAttestationRepository,
13+
ICache,
1314
IDonationRepository,
1415
IEventRegistryRepository,
1516
ILegacyProjectRepository,
@@ -18,6 +19,7 @@ import {
1819
ITransactionManager,
1920
RoundNotFound,
2021
RoundNotFoundForId,
22+
StrategyTimings,
2123
} from "@grants-stack-indexer/repository";
2224
import {
2325
AlloEvent,
@@ -123,6 +125,7 @@ describe("Orchestrator", { sequential: true }, () => {
123125
applicationPayoutRepository: {} as unknown as IApplicationPayoutRepository,
124126
attestationRepository: {} as unknown as IAttestationRepository,
125127
legacyProjectRepository: {} as unknown as ILegacyProjectRepository,
128+
strategyTimingsRepository: {} as unknown as ICache<Address, StrategyTimings>,
126129
pricingProvider: mockPricingProvider,
127130
metadataProvider: mockMetadataProvider,
128131
};

packages/data-flow/test/unit/retroactiveProcessor.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ import {
66
IApplicationPayoutRepository,
77
IApplicationRepository,
88
IAttestationRepository,
9+
ICache,
910
IDonationRepository,
1011
ILegacyProjectRepository,
1112
IProjectRepository,
1213
IRoundRepository,
1314
IStrategyProcessingCheckpointRepository,
1415
ITransactionManager,
1516
Strategy,
17+
StrategyTimings,
1618
} from "@grants-stack-indexer/repository";
1719
import {
20+
Address,
1821
ChainId,
1922
ContractToEventName,
2023
DeepPartial,
@@ -147,6 +150,7 @@ describe("RetroactiveProcessor", () => {
147150
attestationRepository: {} as IAttestationRepository,
148151
legacyProjectRepository: {} as ILegacyProjectRepository,
149152
transactionManager: {} as ITransactionManager,
153+
strategyTimingsRepository: {} as ICache<Address, StrategyTimings>,
150154
pricingProvider: {
151155
getTokenPrice: vi.fn(),
152156
getTokenPrices: vi.fn(),

0 commit comments

Comments
 (0)