diff --git a/CHANGELOG.md b/CHANGELOG.md index d775cb47..85f43bc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Supported tokens from Census3 using `getSupportedTokens()` returns now a token summary. +- Census3 `getStrategyEstimation` accepts `anonymous` flag and returns `accuracy` for anonymous censuses. ### Fixed diff --git a/README.md b/README.md index 0e79b5ec..65153d59 100644 --- a/README.md +++ b/README.md @@ -810,9 +810,13 @@ const strategy = await client.getStrategy(1); ~~~ ~~~ts -// Get strategy size -const size = await client.getStrategySize(1); -// 12455 +// Get strategy estimation information for creating census +const size = await client.getStrategyEstimation(1); +// { +// "size": 5516, +// "timeToCreateCensus": 3296, +// "accuracy": 100 +// } ~~~ ~~~ts diff --git a/src/api/census3/census.ts b/src/api/census3/census.ts index bdc21f81..f26ccd95 100644 --- a/src/api/census3/census.ts +++ b/src/api/census3/census.ts @@ -50,6 +50,11 @@ export interface ICensus3CensusResponse { * If the census is anonymous or not */ anonymous: boolean; + + /** + * The accuracy for an anonymous census + */ + accuracy: number; } export interface ICensus3CensusQueueResponse { diff --git a/src/api/census3/strategy.ts b/src/api/census3/strategy.ts index d0a65a55..abb1d33b 100644 --- a/src/api/census3/strategy.ts +++ b/src/api/census3/strategy.ts @@ -117,6 +117,11 @@ export interface ICensus3StrategyEstimationQueueResponse { * The estimation of the time to create the census */ timeToCreateCensus: number; + + /** + * The accuracy for an anonymous census + */ + accuracy: number; }; } @@ -294,11 +299,14 @@ export abstract class Census3StrategyAPI extends Census3API { * * @param {string} url API endpoint URL * @param {number} id The identifier of the strategy + * @param {boolean} anonymous If the estimation should be done for anonymous census * @returns {Promise} The queue identifier */ - public static estimation(url: string, id: number): Promise { + public static estimation(url: string, id: number, anonymous: boolean = false): Promise { return axios - .get(url + Census3StrategyAPIMethods.ESTIMATION.replace('{id}', String(id))) + .get( + url + Census3StrategyAPIMethods.ESTIMATION.replace('{id}', String(id)) + '?anonymous=' + String(anonymous) + ) .then((response) => response.data) .catch(this.isApiError); } diff --git a/src/census3.ts b/src/census3.ts index 56cd8a68..3e80d77d 100644 --- a/src/census3.ts +++ b/src/census3.ts @@ -186,15 +186,19 @@ export class VocdoniCensus3Client { * 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 + * @param {boolean} anonymous If the estimation should be done for anonymous census * @returns {Promise} The strategy estimation */ - getStrategyEstimation(id: number): Promise<{ size: number; timeToCreateCensus: number }> { + getStrategyEstimation( + id: number, + anonymous: boolean = false + ): Promise<{ size: number; timeToCreateCensus: number; accuracy: number }> { invariant(id || id >= 0, 'No strategy id'); const waitForQueue = ( queueId: string, wait?: number, attempts?: number - ): Promise<{ size: number; timeToCreateCensus: number }> => { + ): Promise<{ size: number; timeToCreateCensus: number; accuracy: number }> => { const waitTime = wait ?? this.queueWait?.retryTime; const attemptsNum = attempts ?? this.queueWait?.attempts; invariant(waitTime, 'No queue wait time set'); @@ -214,7 +218,7 @@ export class VocdoniCensus3Client { }); }; - return Census3StrategyAPI.estimation(this.url, id) + return Census3StrategyAPI.estimation(this.url, id, anonymous) .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 102fad0d..8607ce75 100644 --- a/test/census3/integration/strategy.test.ts +++ b/test/census3/integration/strategy.test.ts @@ -53,6 +53,7 @@ describe('Census3 strategies integration tests', () => { expect(typeof estimation).toBe('object'); expect(typeof estimation.size).toBe('number'); expect(typeof estimation.timeToCreateCensus).toBe('number'); + expect(typeof estimation.accuracy).toBe('number'); } }, 25000); it('should create a new strategy', async () => {