Skip to content

Commit

Permalink
Census3 getStrategySize function changed to getStrategyEstimation
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvelmer committed Dec 13, 2023
1 parent fd3a086 commit 20c865e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 27 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [0.7.0] - 2023-12-13

### Changed

- Changed sha256 library from `@ethersproject/sha2` to `js-sha256` for web workers.
- Using API endpoint for estimating blocks from dates when creating an election.
- [**BREAKING**] Census3 `getStrategySize` function changed to `getStrategyEstimation` giving estimated time and size for the given strategy.

## [0.6.1] - 2023-11-29

Expand Down Expand Up @@ -389,6 +390,7 @@ which extend from the abstract `Election` class.

- First unstable version of the SDK for testing purposes

[0.7.0]: https://github.com/vocdoni/vocdoni-sdk/releases/tag/v0.7.0
[0.6.1]: https://github.com/vocdoni/vocdoni-sdk/releases/tag/v0.6.1
[0.6.0]: https://github.com/vocdoni/vocdoni-sdk/releases/tag/v0.6.0
[0.5.3]: https://github.com/vocdoni/vocdoni-sdk/releases/tag/v0.5.3
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@vocdoni/sdk",
"author": "Vocdoni",
"version": "0.6.1",
"version": "0.7.0",
"description": "⚒️An SDK for building applications on top of Vocdoni API",
"repository": "https://github.com/vocdoni/vocdoni-sdk.git",
"license": "AGPL-3.0-or-later",
Expand Down
41 changes: 26 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}',
SIZE = '/strategies/{id}/size',
SIZE_QUEUE = '/strategies/{id}/size/queue/{queueId}',
ESTIMATION = '/strategies/{id}/estimation',
ESTIMATION_QUEUE = '/strategies/{id}/estimation/queue/{queueId}',
VALIDATE_PREDICATE = '/strategies/predicate/validate',
OPERATORS = '/strategies/predicate/operators',
}
Expand Down Expand Up @@ -83,7 +83,7 @@ export type Census3StrategyToken = {

export type Census3CreateStrategyToken = Omit<Census3StrategyToken, 'chainAddress'>;

export interface ICensus3StrategySizeQueueResponse {
export interface ICensus3StrategyEstimationQueueResponse {
/**
* If the queue has been done
*/
Expand All @@ -105,9 +105,19 @@ export interface ICensus3StrategySizeQueueResponse {
};

/**
* The size of the strategy
* The estimation of the strategy
*/
size: number;
estimation: {
/**
* The estimation of the size
*/
size: number;

/**
* The estimation of the time to create the census
*/
timeToCreateCensus: number;
};
}

export interface ICensus3StrategyImportQueueResponse {
Expand Down Expand Up @@ -280,35 +290,36 @@ export abstract class Census3StrategyAPI extends Census3API {
}

/**
* Returns the size of the strategy
* Returns the estimation of size and time (in milliseconds) to create the census generated for the provided strategy
*
* @param {string} url API endpoint URL
* @param {number} id The identifier of the strategy
* @returns {Promise<ICensus3QueueResponse>} The queue identifier
*/
public static size(url: string, id: number): Promise<ICensus3QueueResponse> {
public static estimation(url: string, id: number): Promise<ICensus3QueueResponse> {
return axios
.get<ICensus3QueueResponse>(url + Census3StrategyAPIMethods.SIZE.replace('{id}', String(id)))
.get<ICensus3QueueResponse>(url + Census3StrategyAPIMethods.ESTIMATION.replace('{id}', String(id)))
.then((response) => response.data)
.catch(this.isApiError);
}

/**
* Returns the information of the strategy size queue
* Returns the information of the strategy estimation queue
*
* @param {string} url API endpoint URL
* @param {number} strategyId The identifier of the strategy
* @param {string} queueId The identifier of the strategy size queue
* @returns {Promise<ICensus3StrategySizeQueueResponse>}
* @param {string} queueId The identifier of the strategy estimation queue
* @returns {Promise<ICensus3StrategyEstimationQueueResponse>}
*/
public static sizeQueue(
public static estimationQueue(
url: string,
strategyId: number,
queueId: string
): Promise<ICensus3StrategySizeQueueResponse> {
): Promise<ICensus3StrategyEstimationQueueResponse> {
return axios
.get<ICensus3StrategySizeQueueResponse>(
url + Census3StrategyAPIMethods.SIZE_QUEUE.replace('{id}', String(strategyId)).replace('{queueId}', queueId)
.get<ICensus3StrategyEstimationQueueResponse>(
url +
Census3StrategyAPIMethods.ESTIMATION_QUEUE.replace('{id}', String(strategyId)).replace('{queueId}', queueId)
)
.then((response) => response.data)
.catch(this.isApiError);
Expand Down
18 changes: 11 additions & 7 deletions src/census3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,34 +181,38 @@ export class VocdoniCensus3Client {
}

/**
* Returns the size of the strategy based on the id
* Returns the estimation of size and time (in milliseconds) to create the census generated for the provided strategy
*
* @param {number} id The id of the strategy
* @returns {Promise<Strategy>} The strategy size
* @returns {Promise<Strategy>} The strategy estimation
*/
getStrategySize(id: number): Promise<number> {
getStrategyEstimation(id: number): Promise<{ size: number; timeToCreateCensus: number }> {
invariant(id || id >= 0, 'No strategy id');
const waitForQueue = (queueId: string, wait?: number, attempts?: number): Promise<number> => {
const waitForQueue = (
queueId: string,
wait?: number,
attempts?: number
): Promise<{ size: number; timeToCreateCensus: number }> => {
const waitTime = wait ?? this.queueWait?.retryTime;
const attemptsNum = attempts ?? this.queueWait?.attempts;
invariant(waitTime, 'No queue wait time set');
invariant(attemptsNum >= 0, 'No queue attempts set');

return attemptsNum === 0
? Promise.reject('Time out waiting for queue with id: ' + queueId)
: Census3StrategyAPI.sizeQueue(this.url, id, queueId).then((queue) => {
: Census3StrategyAPI.estimationQueue(this.url, id, queueId).then((queue) => {
switch (true) {
case queue.done && queue.error?.code?.toString().length > 0:
return Promise.reject(new Error('Could not create the census'));
case queue.done:
return Promise.resolve(queue.size);
return Promise.resolve(queue.estimation);
default:
return delay(waitTime).then(() => waitForQueue(queueId, waitTime, attemptsNum - 1));
}
});
};

return Census3StrategyAPI.size(this.url, id)
return Census3StrategyAPI.estimation(this.url, id)
.then((queueResponse) => queueResponse.queueID)
.then((queueId) => waitForQueue(queueId));
}
Expand Down
8 changes: 5 additions & 3 deletions test/census3/integration/strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ describe('Census3 strategies integration tests', () => {
});
}
}, 5000);
it('should return the given strategy size', async () => {
it('should return the given strategy estimation', async () => {
const client = new VocdoniCensus3Client({ env: EnvOptions.DEV });
const strategies = await client.getStrategies();
if (strategies.length > 0) {
const size = await client.getStrategySize(strategies[0].ID);
expect(typeof size).toBe('number');
const estimation = await client.getStrategyEstimation(strategies[0].ID);
expect(typeof estimation).toBe('object');
expect(typeof estimation.size).toBe('number');
expect(typeof estimation.timeToCreateCensus).toBe('number');
}
}, 25000);
it('should create a new strategy', async () => {
Expand Down

0 comments on commit 20c865e

Please sign in to comment.