diff --git a/CHANGELOG.md b/CHANGELOG.md index e8bebf1c..150d1790 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/package.json b/package.json index e9b5054a..53c2d694 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/api/census3/strategy.ts b/src/api/census3/strategy.ts index 11e261bf..d0a65a55 100644 --- a/src/api/census3/strategy.ts +++ b/src/api/census3/strategy.ts @@ -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', } @@ -83,7 +83,7 @@ export type Census3StrategyToken = { export type Census3CreateStrategyToken = Omit; -export interface ICensus3StrategySizeQueueResponse { +export interface ICensus3StrategyEstimationQueueResponse { /** * If the queue has been done */ @@ -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 { @@ -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} The queue identifier */ - public static size(url: string, id: number): Promise { + public static estimation(url: string, id: number): Promise { return axios - .get(url + Census3StrategyAPIMethods.SIZE.replace('{id}', String(id))) + .get(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} + * @param {string} queueId The identifier of the strategy estimation queue + * @returns {Promise} */ - public static sizeQueue( + public static estimationQueue( url: string, strategyId: number, queueId: string - ): Promise { + ): Promise { return axios - .get( - url + Census3StrategyAPIMethods.SIZE_QUEUE.replace('{id}', String(strategyId)).replace('{queueId}', queueId) + .get( + url + + Census3StrategyAPIMethods.ESTIMATION_QUEUE.replace('{id}', String(strategyId)).replace('{queueId}', queueId) ) .then((response) => response.data) .catch(this.isApiError); diff --git a/src/census3.ts b/src/census3.ts index b9abdf06..c410cc54 100644 --- a/src/census3.ts +++ b/src/census3.ts @@ -181,14 +181,18 @@ 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} The strategy size + * @returns {Promise} The strategy estimation */ - getStrategySize(id: number): Promise { + getStrategyEstimation(id: number): Promise<{ size: number; timeToCreateCensus: number }> { invariant(id || id >= 0, 'No strategy id'); - const waitForQueue = (queueId: string, wait?: number, attempts?: number): Promise => { + 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'); @@ -196,19 +200,19 @@ export class VocdoniCensus3Client { 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)); } diff --git a/test/census3/integration/strategy.test.ts b/test/census3/integration/strategy.test.ts index 7ddb8765..102fad0d 100644 --- a/test/census3/integration/strategy.test.ts +++ b/test/census3/integration/strategy.test.ts @@ -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 () => {