Skip to content

Commit

Permalink
New strategy estimation endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvelmer committed Apr 10, 2024
1 parent 118f64f commit 0a4f670
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
31 changes: 16 additions & 15 deletions src/api/census3/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ enum Census3StrategyAPIMethods {
IMPORT = '/strategies/import/{cid}',
IMPORT_QUEUE = '/strategies/import/queue/{queueId}',
STRATEGY = '/strategies/{id}',
ESTIMATION = '/strategies/{id}/estimation',
ESTIMATION_QUEUE = '/strategies/{id}/estimation/queue/{queueId}',
ESTIMATION = '/strategies/estimation',
ESTIMATION_QUEUE = '/strategies/estimation/queue/{queueId}',
VALIDATE_PREDICATE = '/strategies/predicate/validate',
OPERATORS = '/strategies/predicate/operators',
HOLDERS = '/strategies/{id}/holders',
Expand Down Expand Up @@ -346,15 +346,22 @@ export abstract class Census3StrategyAPI extends Census3API {
* Returns the estimation of size and time (in milliseconds) to create the census generated for the provided strategy
*
* @param url - API endpoint URL
* @param id - The identifier of the strategy
* @param predicate - The predicate of the strategy
* @param tokens - The token list for the strategy
* @param anonymous - If the estimation should be done for anonymous census
* @returns The queue identifier
*/
public static estimation(url: string, id: number, anonymous: boolean = false): Promise<ICensus3QueueResponse> {
public static estimation(
url: string,
predicate: string,
tokens: { [key: string]: Census3CreateStrategyToken },
anonymous: boolean = false
): Promise<ICensus3QueueResponse> {
return axios
.get<ICensus3QueueResponse>(
url + Census3StrategyAPIMethods.ESTIMATION.replace('{id}', String(id)) + '?anonymous=' + String(anonymous)
)
.post<ICensus3QueueResponse>(url + Census3StrategyAPIMethods.ESTIMATION + '?anonymous=' + String(anonymous), {
predicate,
tokens,
})
.then((response) => response.data)
.catch(this.isApiError);
}
Expand All @@ -363,18 +370,12 @@ export abstract class Census3StrategyAPI extends Census3API {
* Returns the information of the strategy estimation queue
*
* @param url - API endpoint URL
* @param strategyId - The identifier of the strategy
* @param queueId - The identifier of the strategy estimation queue
*/
public static estimationQueue(
url: string,
strategyId: number,
queueId: string
): Promise<ICensus3StrategyEstimationQueueResponse> {
public static estimationQueue(url: string, queueId: string): Promise<ICensus3StrategyEstimationQueueResponse> {
return axios
.get<ICensus3StrategyEstimationQueueResponse>(
url +
Census3StrategyAPIMethods.ESTIMATION_QUEUE.replace('{id}', String(strategyId)).replace('{queueId}', queueId)
url + Census3StrategyAPIMethods.ESTIMATION_QUEUE.replace('{queueId}', queueId)
)
.then((response) => response.data)
.catch(this.isApiError);
Expand Down
24 changes: 22 additions & 2 deletions src/census3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,26 @@ export class VocdoniCensus3Client {
anonymous: boolean = false
): Promise<{ size: number; timeToCreateCensus: number; accuracy: number }> {
invariant(id || id >= 0, 'No strategy id');
return this.getStrategy(id).then((strategy) =>
this.getPredicateEstimation(strategy.predicate, strategy.tokens, anonymous)
);
}

/**
* Returns the estimation of size and time (in milliseconds) to create the census generated for the provided predicate and tokens
*
* @param predicate - The predicate of the strategy
* @param tokens - The token list for the strategy
* @param anonymous - If the estimation should be done for anonymous census
* @returns The predicate estimation
*/
getPredicateEstimation(
predicate: string,
tokens: { [key: string]: StrategyToken },
anonymous: boolean = false
): Promise<{ size: number; timeToCreateCensus: number; accuracy: number }> {
invariant(predicate, 'No predicate set');
invariant(tokens, 'No tokens set');
const waitForQueue = (
queueId: string,
wait?: number,
Expand All @@ -248,7 +268,7 @@ export class VocdoniCensus3Client {

return attemptsNum === 0
? Promise.reject('Time out waiting for queue with id: ' + queueId)
: Census3StrategyAPI.estimationQueue(this.url, id, queueId).then((queue) => {
: Census3StrategyAPI.estimationQueue(this.url, queueId).then((queue) => {
switch (true) {
case queue.done && queue.error?.code?.toString().length > 0:
return Promise.reject(new Error('Could not create the census'));
Expand All @@ -260,7 +280,7 @@ export class VocdoniCensus3Client {
});
};

return Census3StrategyAPI.estimation(this.url, id, anonymous)
return Census3StrategyAPI.estimation(this.url, predicate, tokens, anonymous)
.then((queueResponse) => queueResponse.queueID)
.then((queueId) => waitForQueue(queueId));
}
Expand Down
6 changes: 5 additions & 1 deletion test/census3/integration/strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ describe('Census3 strategies integration tests', () => {
expect(typeof estimation.size).toBe('number');
expect(typeof estimation.timeToCreateCensus).toBe('number');
expect(typeof estimation.accuracy).toBe('number');

const strategy = await client.getStrategy(strategies[0].ID);
const predicateEstimation = await client.getPredicateEstimation(strategy.predicate, strategy.tokens);
expect(predicateEstimation).toStrictEqual(estimation);
}
}, 25000);
}, 65000);
it('should create a new strategy', async () => {
const client = new VocdoniCensus3Client({ env: EnvOptions.DEV });
const supportedTokens = await client.getSupportedTokens();
Expand Down

0 comments on commit 0a4f670

Please sign in to comment.