Skip to content

Commit

Permalink
feat: values aggregates for events
Browse files Browse the repository at this point in the history
  • Loading branch information
BugGambit committed Jun 22, 2020
1 parent 2e6a4e2 commit d195799
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 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
27 changes: 23 additions & 4 deletions src/resources/events/eventsApi.ts
Expand Up @@ -4,15 +4,17 @@ import { CursorAndAsyncIterator } from '../../autoPagination';
import { BaseResourceAPI } from '../../resources/baseResourceApi';
import {
CogniteEvent,
EventAggregate,
EventAggregateQuery,
EventChange,
EventFilterRequest,
EventSearchRequest,
EventSort,
ExternalEvent,
IdEither,
IgnoreUnknownIds,
EventCountAggregate,
CountAggregateResponse,
UniqueValuesAggregateResponse,
EventUniqueValuesAggregate,
} from '../../types/types';

export class EventsAPI extends BaseResourceAPI<CogniteEvent> {
Expand Down Expand Up @@ -56,11 +58,28 @@ export class EventsAPI extends BaseResourceAPI<CogniteEvent> {
* ```
*/
public aggregate = (
query: EventAggregateQuery
): Promise<EventAggregate[]> => {
query: EventCountAggregate
): Promise<CountAggregateResponse[]> => {
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
34 changes: 27 additions & 7 deletions src/types/types.ts
Expand Up @@ -191,13 +191,22 @@ export interface Asset
parentExternalId?: CogniteExternalId;
}

export interface AggregateResponse {
export type AggregateResponse = CountAggregateResponse;

export interface CountAggregateResponse {
/**
* Size of the aggregation group
*/
count: number;
}

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

/**
* Response from asset aggregate endpoint
*/
Expand Down Expand Up @@ -585,8 +594,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 @@ -713,15 +722,26 @@ export type DeleteAssetMapping3D = AssetMapping3DBase;
export type EXECUTE = 'EXECUTE';

/**
* Query schema for asset aggregate endpoint
* Query schema for event aggregate endpoint
*/
export interface EventAggregateQuery {
export type EventAggregateQuery = EventCountAggregate;

export interface EventBaseAggregate {
/**
* Filter on events with strict matching.
*/
filter?: EventFilter;
}

export interface EventCountAggregate extends EventBaseAggregate {}

export interface EventUniqueValuesAggregate extends EventBaseAggregate {
/**
* 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 @@ -1148,8 +1168,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 d195799

Please sign in to comment.