Skip to content

Commit

Permalink
feat: add dataset.statistics (#621)
Browse files Browse the repository at this point in the history
Part of apify/apify-core#18807 -
Implementation of new API endpoint `v2/datasets/{datasetId}/statistics`
  • Loading branch information
MFori authored Jan 15, 2025
1 parent 6327f3b commit 6aeb2b7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/resource_clients/dataset.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import ow from 'ow';

import { ApifyApiError } from '../apify_api_error';
import { ApiClientSubResourceOptions } from '../base/api_client';
import { ResourceClient } from '../base/resource_client';
import { ApifyResponse } from '../http_client';
import { cast, PaginatedList } from '../utils';
import { ApifyRequestConfig, ApifyResponse } from '../http_client';
import { cast, catchNotFoundOrThrow, PaginatedList, pluckData } from '../utils';

export class DatasetClient<
Data extends Record<string | number, any> = Record<string | number, unknown>,
Expand Down Expand Up @@ -130,6 +131,24 @@ export class DatasetClient<
});
}

/**
* https://docs.apify.com/api/v2#tag/DatasetsStatistics/operation/dataset_statistics_get
*/
async getStatistics(): Promise<DatasetStatistics | undefined> {
const requestOpts: ApifyRequestConfig = {
url: this._url('statistics'),
method: 'GET',
params: this._params(),
};
try {
const response = await this.httpClient.call(requestOpts);
return cast(pluckData(response.data));
} catch (err) {
catchNotFoundOrThrow(err as ApifyApiError);
}
return undefined;
}

private _createPaginationList(response: ApifyResponse, userProvidedDesc: boolean): PaginatedList<Data> {
return {
items: response.data,
Expand Down Expand Up @@ -210,3 +229,14 @@ export interface DatasetClientDownloadItemsOptions extends DatasetClientListItem
xmlRoot?: string;
xmlRow?: string;
}

export interface DatasetStatistics {
fieldStatistics: Record<string, FieldStatistics>;
}

export interface FieldStatistics {
min?: number;
max?: number;
nullCount?: number;
emptyCount?: number;
}
12 changes: 12 additions & 0 deletions test/datasets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,5 +309,17 @@ describe('Dataset methods', () => {
expect(res).toBeUndefined();
validateRequest({}, { datasetId }, data, expectedHeaders);
});

test('statistics() works', async () => {
const datasetId = 'some-id';

const res = await client.dataset(datasetId).getStatistics();
expect(res.id).toEqual('get-statistics');
validateRequest({}, { datasetId });

const browserRes = await page.evaluate((id) => client.dataset(id).getStatistics(), datasetId);
expect(browserRes).toEqual(res);
validateRequest({}, { datasetId });
});
});
});
1 change: 1 addition & 0 deletions test/mock_server/routes/datasets.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const ROUTES = [
{ id: 'update-dataset', method: 'PUT', path: '/:datasetId' },
{ id: 'list-items', method: 'GET', path: '/:datasetId/items', type: 'responseJsonMock' },
{ id: 'push-items', method: 'POST', path: '/:datasetId/items' },
{ id: 'get-statistics', method: 'GET', path: '/:datasetId/statistics' },
];

addRoutes(datasets, ROUTES);
Expand Down

0 comments on commit 6aeb2b7

Please sign in to comment.