Skip to content

Commit

Permalink
Census3 getStrategyEstimation accepts anonymous flag and returns …
Browse files Browse the repository at this point in the history
…`accuracy` for anonymous censuses.
  • Loading branch information
marcvelmer committed Jan 10, 2024
1 parent bd70e7b commit 7b41362
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/api/census3/census.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions src/api/census3/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}

Expand Down Expand Up @@ -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<ICensus3QueueResponse>} The queue identifier
*/
public static estimation(url: string, id: number): Promise<ICensus3QueueResponse> {
public static estimation(url: string, id: number, anonymous: boolean = false): Promise<ICensus3QueueResponse> {
return axios
.get<ICensus3QueueResponse>(url + Census3StrategyAPIMethods.ESTIMATION.replace('{id}', String(id)))
.get<ICensus3QueueResponse>(
url + Census3StrategyAPIMethods.ESTIMATION.replace('{id}', String(id)) + '?anonymous=' + String(anonymous)
)
.then((response) => response.data)
.catch(this.isApiError);
}
Expand Down
10 changes: 7 additions & 3 deletions src/census3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Strategy>} 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');
Expand All @@ -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));
}
Expand Down
1 change: 1 addition & 0 deletions test/census3/integration/strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 7b41362

Please sign in to comment.