Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(alpha): monitoring tasks api #921

Merged
merged 6 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ The CogniteClientAlpha can be initialized/configured in the same manner as the o

## Documentation

- contact engineering the team
[cognitedata.github.io/cognite-sdk-js/alpha](https://cognitedata.github.io/cognite-sdk-js/alpha/classes/_alpha_src_cogniteclient_.cogniteclientalpha.html)
9 changes: 9 additions & 0 deletions packages/alpha/src/__tests__/api/alertsApi.int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,13 @@ describe('alerts api', () => {
]);
expect(response).toEqual({});
});

itif(client)('delete channel', async () => {
const response = await client!.alerts.deleteChannels([
{
externalId: channelExternalId,
},
]);
expect(response).toEqual({});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for status 200?

Copy link
Collaborator Author

@polomani polomani Nov 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for status 200?

we technically don't have access to the status at this level anymore (at least the easy way), if 400+ status happens it will throw an error at earlier stages

});
});
114 changes: 114 additions & 0 deletions packages/alpha/src/__tests__/api/monitoringTasksApi.int.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright 2022 Cognite AS

import { Channel } from 'alpha/src/types';
import CogniteClientAlpha from '../../cogniteClient';
import {
CLIENT_ID,
CLIENT_SECRET,
setupLoggedInClient,
TEST_PROJECT,
} from '../testUtils';

type SessionsResponse = {
items: [{ nonce: string; status: string }];
};

const itif = (condition: any) => (condition ? it : it.skip);

describe('monitoring tasks api', () => {
const client: CogniteClientAlpha | null = setupLoggedInClient();
const ts = Date.now();
const monitoringTaskExternalId = `test_mt_${ts}`;
const channelExternalId = `test_channel_mt_${ts}`;
const sessionsApi = `/api/v1/projects/${TEST_PROJECT}/sessions`;
const testMtParams = {
threshold: 50.1,
timeseriesExternalId: 'test_functions',
granularity: '1m',
};
const testMtOverlap = 1000 * 60;
const testMtInterval = 5 * 60 * 1000;
const testMtModelExternalId = 'air-upper-threshold';
let channel: Channel;

itif(client)('create monitoring task', async () => {
const sessionsRes = await client!.post<SessionsResponse>(sessionsApi, {
data: {
items: [
{
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
},
],
},
});
const res = await client!.alerts.createChannels([
{
externalId: channelExternalId,
name: channelExternalId,
description: 'test',
},
]);
channel = res[0];
expect(channel).toBeTruthy();
const response = await client!.monitoringTasks.create([
{
externalId: monitoringTaskExternalId,
channelId: channel.id,
interval: testMtInterval,
nonce: sessionsRes?.data?.items[0]?.nonce,
modelExternalId: testMtModelExternalId,
overlap: testMtOverlap,
parameters: testMtParams,
},
]);
expect(response.length).toBe(1);
expect(response[0].externalId).toBe(monitoringTaskExternalId);
});

itif(client)('list all monitoring tasks', async () => {
const response = await client!.monitoringTasks.list({
filter: {},
});
expect(response.items.length).toBeGreaterThan(0);
});

itif(client)('list created monitoring task', async () => {
const response = await client!.monitoringTasks.list({
filter: { externalIds: [monitoringTaskExternalId] },
});
expect(response.items.length).toBe(1);
expect(response.items[0].externalId).toEqual(monitoringTaskExternalId);
expect(response.items[0].parameters).toEqual(testMtParams);
expect(response.items[0].interval).toEqual(testMtInterval);
expect(response.items[0].overlap).toEqual(testMtOverlap);
expect(response.items[0].modelExternalId).toEqual(testMtModelExternalId);
});

itif(client)('list created monitoring task by channel', async () => {
const response = await client!.monitoringTasks.list({
filter: { channelIds: [channel.id] },
});
expect(response.items.length).toBe(1);
expect(response.items[0].externalId).toEqual(monitoringTaskExternalId);
expect(response.items[0].channelId).toEqual(channel.id);
});

itif(client)('delete monitoring task', async () => {
const response = await client!.monitoringTasks.delete([
{
externalId: monitoringTaskExternalId,
},
]);
expect(response).toEqual({});
});

itif(client)('delete channel', async () => {
const response = await client!.alerts.deleteChannels([
{
externalId: channelExternalId,
},
]);
expect(response).toEqual({});
});
});
14 changes: 7 additions & 7 deletions packages/alpha/src/__tests__/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ import CogniteClient from '../cogniteClient';
import { name } from '../../package.json';
import { ConfidentialClientApplication } from '@azure/msal-node';

const project = process.env.COGNITE_PROJECT_ALERTS_API!;
const clientId = process.env.COGNITE_CLIENT_ID_ALERTS_API!;
const clientSecret = process.env.COGNITE_CLIENT_SECRET_ALERTS_API!;
export const TEST_PROJECT = process.env.COGNITE_PROJECT_ALERTS_API!;
export const CLIENT_ID = process.env.COGNITE_CLIENT_ID_ALERTS_API!;
export const CLIENT_SECRET = process.env.COGNITE_CLIENT_SECRET_ALERTS_API!;
const azureTenant = process.env.COGNITE_TENANT_ID_ALERTS_API!;

export function setupLoggedInClient() {
if (project && clientId && clientSecret && azureTenant) {
if (TEST_PROJECT && CLIENT_ID && CLIENT_SECRET && azureTenant) {
const pca = new ConfidentialClientApplication({
auth: {
clientId,
clientSecret,
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
authority: `https://login.microsoftonline.com/${azureTenant}`,
},
});

const client = new CogniteClient({
project,
project: TEST_PROJECT,
baseUrl: 'https://azure-dev.cognitedata.com',
appId: `JS SDK integration tests (${name})`,
getToken: () =>
Expand Down
2 changes: 2 additions & 0 deletions packages/alpha/src/api/alerts/alertsApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2022 Cognite AS

import { BaseResourceAPI, CDFHttpClient, MetadataMap } from '@cognite/sdk-core';
import { IdEither } from '@cognite/sdk';
import {
Expand Down
2 changes: 2 additions & 0 deletions packages/alpha/src/api/alerts/channelsApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2022 Cognite AS

import { BaseResourceAPI, CDFHttpClient, MetadataMap } from '@cognite/sdk-core';
import { IdEither } from '@cognite/sdk';
import {
Expand Down
2 changes: 2 additions & 0 deletions packages/alpha/src/api/alerts/subscribersApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2022 Cognite AS

import { BaseResourceAPI, CDFHttpClient, MetadataMap } from '@cognite/sdk-core';
import {
Subscriber,
Expand Down
2 changes: 2 additions & 0 deletions packages/alpha/src/api/alerts/subscriptionsApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2022 Cognite AS

import { BaseResourceAPI, CDFHttpClient, MetadataMap } from '@cognite/sdk-core';
import {
Subscription,
Expand Down
25 changes: 25 additions & 0 deletions packages/alpha/src/api/monitoringTasks/monitoringTasksApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2022 Cognite AS

import { BaseResourceAPI, IdEither } from '@cognite/sdk-core';
import {
MonitoringTask,
MonitoringTaskCreate,
MonitoringTaskFilterQuery,
} from '../../types';

export class MonitoringTasksAPI extends BaseResourceAPI<MonitoringTask> {
public create = async (items: MonitoringTaskCreate[]) => {
return this.createEndpoint(items);
};

public list = async (filter?: MonitoringTaskFilterQuery) => {
return this.listEndpoint<MonitoringTaskFilterQuery>(
this.callListEndpointWithPost,
filter
);
};

public delete = async (ids: IdEither[]) => {
return this.deleteEndpoint(ids);
};
}
10 changes: 10 additions & 0 deletions packages/alpha/src/cogniteClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import {
import { accessApi } from '@cognite/sdk-core';
import { version } from '../package.json';
import { AlertsAPI } from './api/alerts/alertsApi';
import { MonitoringTasksAPI } from './api/monitoringTasks/monitoringTasksApi';

export default class CogniteClientAlpha extends CogniteClientStable {
private alertsApi?: AlertsAPI;
private monitoringTasksApi?: MonitoringTasksAPI;

/**
* Create a new SDK client (alpha)
Expand All @@ -22,12 +24,20 @@ export default class CogniteClientAlpha extends CogniteClientStable {
return accessApi(this.alertsApi);
}

public get monitoringTasks() {
return accessApi(this.monitoringTasksApi);
}

protected initAPIs() {
super.initAPIs();

this.httpClient.setDefaultHeader('cdf-version', 'alpha');

this.alertsApi = this.apiFactory(AlertsAPI, 'alerts');
this.monitoringTasksApi = this.apiFactory(
MonitoringTasksAPI,
'monitoringtask'
);
}

protected get version() {
Expand Down
47 changes: 47 additions & 0 deletions packages/alpha/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,53 @@ import {

export * from '@cognite/sdk';

export interface MonitoringTaskParametersCreate {
timeseriesExternalId: CogniteExternalId;
granularity?: string;
threshold: number;
}

export interface MonitoringTaskParameters {
timeseriesExternalId: CogniteInternalId;
granularity?: string;
threshold: number;
}

export interface MonitoringTaskCreate {
externalId: CogniteExternalId;
modelExternalId: CogniteExternalId;
channelId: CogniteInternalId;
interval: number;
overlap: number;
parameters: MonitoringTaskParametersCreate;
nonce: string;
}

export interface MonitoringTask {
id: CogniteInternalId;
externalId?: CogniteExternalId;
modelExternalId: CogniteExternalId;
channelId: CogniteInternalId;
interval: number;
overlap: number;
parameters: MonitoringTaskParameters;
}

export interface MonitoringTaskParametersFilter {
timeseriesExternalId: CogniteInternalId;
}

export interface MonitoringTaskFilter {
externalIds?: CogniteExternalId[];
ids?: CogniteInternalId[];
channelIds?: CogniteInternalId[];
parameters?: MonitoringTaskParametersFilter;
}

export interface MonitoringTaskFilterQuery extends FilterQuery {
filter?: MonitoringTaskFilter;
}

export interface AlertFilter {
channelIds?: CogniteInternalId[];
channelExternalIds?: CogniteExternalId[];
Expand Down
3 changes: 2 additions & 1 deletion packages/stable/src/__tests__/api/geospatial.int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ const DUMMY_TEST_FEATURE: GeospatialFeature[] = [
},
];

describe('Geospatial integration test', () => {
// re-enable this when geospacial is back to life
describe.skip('Geospatial integration test', () => {
let client: CogniteClient;
let featureType: FeatureType;

Expand Down