Skip to content

Commit

Permalink
feat(events): unique values aggregate (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
BugGambit committed Jun 23, 2020
1 parent 7337e0e commit 2a08f44
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/__tests__/resources/events.integration.spec.ts
Expand Up @@ -95,6 +95,18 @@ describe('Events integration test', () => {
expect(aggregates[0].count).toBeDefined();
});

test('values aggregate', async () => {
const aggregates = await client.events.uniqueValuesAggregate({
filter: {
source: 'WORKMATE',
},
fields: ['type'],
});
expect(aggregates.length).toBe(1);
expect(aggregates[0].count).toBeDefined();
expect(aggregates[0].value).toBeDefined();
});

test('delete', async () => {
await client.events.delete(createdEvents.map(event => ({ id: event.id })));
});
Expand Down
23 changes: 21 additions & 2 deletions src/resources/events/eventsApi.ts
Expand Up @@ -3,16 +3,18 @@
import { CursorAndAsyncIterator } from '../../autoPagination';
import { BaseResourceAPI } from '../../resources/baseResourceApi';
import {
AggregateResponse,
CogniteEvent,
EventAggregate,
EventAggregateQuery,
EventChange,
EventFilterRequest,
EventSearchRequest,
EventSort,
EventUniqueValuesAggregate,
ExternalEvent,
IdEither,
IgnoreUnknownIds,
UniqueValuesAggregateResponse,
} from '../../types/types';

export class EventsAPI extends BaseResourceAPI<CogniteEvent> {
Expand Down Expand Up @@ -57,10 +59,27 @@ export class EventsAPI extends BaseResourceAPI<CogniteEvent> {
*/
public aggregate = (
query: EventAggregateQuery
): Promise<EventAggregate[]> => {
): Promise<AggregateResponse[]> => {
return super.aggregateEndpoint(query);
};

/**
* [Aggregate events](https://docs.cognite.com/api/v1/#operation/aggregateEvents)
*
* ```js
* const aggregates = await client.events.uniqueValuesAggregate({ filter: { assetIds: [1, 2, 3] }, fields: ['subtype'] });
* console.log('Unique values: ', aggregates)
* ```
*/
public uniqueValuesAggregate = (
query: EventUniqueValuesAggregate
): Promise<UniqueValuesAggregateResponse[]> => {
return super.aggregateEndpoint({
...query,
aggregate: 'uniqueValues',
});
};

/**
* [Retrieve events](https://doc.cognitedata.com/api/v1/#operation/byIdsEvents)
* <!-- or [similar](https://doc.cognitedata.com/api/v1/#operation/getEventByInternalId) -->
Expand Down
24 changes: 19 additions & 5 deletions src/types/types.ts
Expand Up @@ -198,6 +198,13 @@ export interface AggregateResponse {
count: number;
}

export interface UniqueValuesAggregateResponse extends AggregateResponse {
/**
* A unique value from the requested field
*/
value: string | number;
}

/**
* Response from asset aggregate endpoint
*/
Expand Down Expand Up @@ -645,8 +652,8 @@ export interface DatapointsDeleteRange {
}

export type DatapointsDeleteRequest =
| InternalId & DatapointsDeleteRange
| ExternalId & DatapointsDeleteRange;
| (InternalId & DatapointsDeleteRange)
| (ExternalId & DatapointsDeleteRange);

export interface DatapointsGetAggregateDatapoint extends DatapointsMetadata {
datapoints: GetAggregateDatapoint[];
Expand Down Expand Up @@ -773,7 +780,7 @@ export type DeleteAssetMapping3D = AssetMapping3DBase;
export type EXECUTE = 'EXECUTE';

/**
* Query schema for asset aggregate endpoint
* Query schema for event aggregate endpoint
*/
export interface EventAggregateQuery {
/**
Expand All @@ -782,6 +789,13 @@ export interface EventAggregateQuery {
filter?: EventFilter;
}

export interface EventUniqueValuesAggregate extends EventAggregateQuery {
/**
* The field name(s) to apply the aggregation on. Currently limited to one field.
*/
fields: ('type' | 'subtype' | 'dataSetId')[];
}

export type EventChange = EventChangeById | EventChangeByExternalId;

export interface EventChangeByExternalId extends EventPatch, ExternalId {}
Expand Down Expand Up @@ -1209,8 +1223,8 @@ export interface ItemsWrapper<T> {
export type LIST = 'LIST';

export type LatestDataBeforeRequest =
| InternalId & LatestDataPropertyFilter
| ExternalId & LatestDataPropertyFilter;
| (InternalId & LatestDataPropertyFilter)
| (ExternalId & LatestDataPropertyFilter);

export interface LatestDataPropertyFilter {
/**
Expand Down

0 comments on commit 2a08f44

Please sign in to comment.