-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
227 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
import {MigrationInterface, QueryRunner} from "typeorm"; | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class AddEstimatedClusterMatching1728554628004 implements MigrationInterface { | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
export class AddEstimatedClusterMatching1728554628004 | ||
implements MigrationInterface | ||
{ | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
CREATE TABLE estimated_cluster_matching ( | ||
id SERIAL PRIMARY KEY, | ||
project_id INT NOT NULL, | ||
qf_round_id INT NOT NULL, | ||
matching DOUBLE PRECISION NOT NULL | ||
); | ||
`); | ||
// Create indexes on the new table | ||
await queryRunner.query(` | ||
|
||
// Create indexes on the new table | ||
await queryRunner.query(` | ||
CREATE INDEX estimated_cluster_matching_project_id_qfround_id | ||
ON estimated_cluster_matching (project_id, qf_round_id); | ||
`); | ||
await queryRunner.query(` | ||
|
||
await queryRunner.query(` | ||
CREATE INDEX estimated_cluster_matching_matching | ||
ON estimated_cluster_matching (matching); | ||
`); | ||
} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
// Revert changes if necessary by dropping the table and restoring the view | ||
await queryRunner.query(` | ||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
// Revert changes if necessary by dropping the table and restoring the view | ||
await queryRunner.query(` | ||
DROP TABLE IF EXISTS estimated_cluster_matching; | ||
`); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import axios from 'axios'; | ||
import { | ||
CocmAdapterInterface, | ||
EstimatedMatchingInput, | ||
ProjectsEstimatedMatchings, | ||
} from './cocmAdapterInterface'; | ||
import { logger } from '../../utils/logger'; | ||
import { i18n, translationErrorMessagesKeys } from '../../utils/errorMessages'; | ||
|
||
export class CocmAdapter implements CocmAdapterInterface { | ||
private ClusterMatchingURL; | ||
|
||
constructor() { | ||
this.ClusterMatchingURL = | ||
process.env.CLUSTER_MATCHING_API_URL || 'localhost'; | ||
} | ||
|
||
async fetchEstimatedClusterMatchings( | ||
matchingDataInput: EstimatedMatchingInput, | ||
): Promise<ProjectsEstimatedMatchings> { | ||
try { | ||
const result = await axios.post( | ||
this.ClusterMatchingURL, | ||
matchingDataInput, | ||
{ | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
); | ||
if (result?.data?.error !== null) { | ||
logger.error('clusterMatchingApi error', result.data.error); | ||
throw new Error( | ||
i18n.__(translationErrorMessagesKeys.CLUSTER_MATCHING_API_ERROR), | ||
); | ||
} | ||
return result.data; | ||
} catch (e) { | ||
logger.error('clusterMatchingApi error', e); | ||
throw new Error( | ||
i18n.__(translationErrorMessagesKeys.CLUSTER_MATCHING_API_ERROR), | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Example Data | ||
// { | ||
// "matching_data": [ | ||
// { | ||
// "matching_amount": 83.25, | ||
// "matching_percent": 50.0, | ||
// "project_name": "Test1", | ||
// "strategy": "COCM" | ||
// }, | ||
// { | ||
// "matching_amount": 83.25, | ||
// "matching_percent": 50.0, | ||
// "project_name": "Test3", | ||
// "strategy": "COCM" | ||
// } | ||
// ] | ||
// } | ||
|
||
export interface ProjectsEstimatedMatchings { | ||
matching_data: { | ||
matching_amount: number; | ||
matching_percent: number; | ||
project_name: string; | ||
strategy: string; | ||
}[]; | ||
} | ||
|
||
export interface EstimatedMatchingInput { | ||
votes_data: [ | ||
{ | ||
voter: string; | ||
payoutAddress: string; | ||
amountUSD: number; | ||
project_name: string; | ||
score: number; | ||
}, | ||
]; | ||
strategy: string; | ||
min_donation_threshold_amount: number; | ||
matching_cap_amount: number; | ||
matching_amount: number; | ||
passport_threshold: number; | ||
} | ||
|
||
export interface CocmAdapterInterface { | ||
fetchEstimatedClusterMatchings( | ||
matchingDataInput: EstimatedMatchingInput, | ||
): Promise<ProjectsEstimatedMatchings>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import axios from 'axios'; | ||
Check failure on line 1 in src/adapters/cocmAdapter/cocmMockAdapter.ts GitHub Actions / test
|
||
import { | ||
CocmAdapterInterface, | ||
ProjectsEstimatedMatchings, | ||
} from './cocmAdapterInterface'; | ||
import { i18n, translationErrorMessagesKeys } from '../../utils/errorMessages'; | ||
Check failure on line 6 in src/adapters/cocmAdapter/cocmMockAdapter.ts GitHub Actions / test
Check failure on line 6 in src/adapters/cocmAdapter/cocmMockAdapter.ts GitHub Actions / test
Check failure on line 6 in src/adapters/cocmAdapter/cocmMockAdapter.ts GitHub Actions / test
|
||
import { logger } from '../../utils/logger'; | ||
Check failure on line 7 in src/adapters/cocmAdapter/cocmMockAdapter.ts GitHub Actions / test
|
||
|
||
export class CocmMockAdapter implements CocmAdapterInterface { | ||
async fetchEstimatedClusterMatchings( | ||
_matchingDataInput, | ||
): Promise<ProjectsEstimatedMatchings> { | ||
return { | ||
matching_data: [ | ||
{ | ||
matching_amount: 83.25, | ||
matching_percent: 50.0, | ||
project_name: 'Test1', | ||
strategy: 'COCM', | ||
}, | ||
{ | ||
matching_amount: 83.25, | ||
matching_percent: 50.0, | ||
project_name: 'Test3', | ||
strategy: 'COCM', | ||
}, | ||
], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { schedule } from 'node-cron'; | ||
import { spawn, Worker, Thread } from 'threads'; | ||
import config from '../../config'; | ||
import { logger } from '../../utils/logger'; | ||
import { | ||
findActiveQfRound, | ||
findUsersWithoutMBDScoreInActiveAround, | ||
} from '../../repositories/qfRoundRepository'; | ||
import { findUserById } from '../../repositories/userRepository'; | ||
import { UserQfRoundModelScore } from '../../entities/userQfRoundModelScore'; | ||
|
||
const cronJobTime = | ||
(config.get('SYNC_ESTIMATED_CLUSTED_MATCHING_CRONJOB_EXPRESSION') as string) || | ||
'0 * * * * *'; | ||
|
||
export const runSyncEstimatedClusterMatchingCronjob = () => { | ||
logger.debug( | ||
'runSyncEstimatedClusterMatchingCronjob() has been called, cronJobTime', | ||
cronJobTime, | ||
); | ||
schedule(cronJobTime, async () => { | ||
await fetchAndUpdateClusterEstimatedMatching(); | ||
}); | ||
}; | ||
|
||
export const fetchAndUpdateClusterEstimatedMatching = async () => { | ||
const fetchWorker = await spawn( | ||
new Worker('../../workers/cocm/fetchEstimatedClusterMtchingWorker'), | ||
); | ||
|
||
const updateWorker = await spawn( | ||
new Worker('../../workers/cocm/updateProjectsEstimatedClusterMatchingWorker') | ||
); | ||
const activeQfRoundId = | ||
(await findActiveQfRound())?.id; | ||
if (!activeQfRoundId || activeQfRoundId === 0) return; | ||
|
||
for (const projectId of []) { | ||
try { | ||
|
||
// const userScore = await worker.syncUserScore({ | ||
// userWallet: user?.walletAddress, | ||
// }); | ||
} catch (e) { | ||
logger.info(`User with Id ${1} did not sync MBD score this batch`); | ||
} | ||
} | ||
await Thread.terminate(fetchWorker); | ||
await Thread.terminate(updateWorker); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// workers/auth.js | ||
import { expose } from 'threads/worker'; | ||
import { WorkerModule } from 'threads/dist/types/worker'; | ||
import { getClusterMatchingAdapter } from '../../adapters/adaptersFactory'; | ||
|
||
type FetchEstimatedClusterMatchingWorkerFunctions = 'fetchEstimatedClusterMatching'; | ||
|
||
export type FetchEstimatedClusterMatchingWorker = | ||
WorkerModule<FetchEstimatedClusterMatchingWorkerFunctions>; | ||
|
||
const worker: FetchEstimatedClusterMatchingWorker = { | ||
async fetchEstimatedClusterMatching(matchingDataInput: any) { | ||
return await getClusterMatchingAdapter().fetchEstimatedClusterMatchings(matchingDataInput); | ||
}, | ||
}; | ||
|
||
expose(worker); |
Empty file.