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(asset): resolve minion id #73

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ import {
CalibrationForecastResponse,
ColumnDescriptorType,
} from "./types";
import { SystemMetadata } from "datasources/system/types";

let datastore: AssetCalibrationDataSource, backendServer: MockProxy<BackendSrv>

beforeEach(() => {
[datastore, backendServer] = setupDataSource(AssetCalibrationDataSource);

backendServer.fetch
.calledWith(requestMatching({ url: '/nisysmgmt/v1/query-systems' }))
.mockReturnValue(createFetchResponse({ data: fakeSystems }));
});

const monthGroupCalibrationForecastResponseMock: CalibrationForecastResponse =
Expand Down Expand Up @@ -63,6 +68,18 @@ const locationGroupCalibrationForecastResponseMock: CalibrationForecastResponse
}
}

const minionIdLocationGroupCalibrationForecastResponseMock: CalibrationForecastResponse =
{
calibrationForecast: {
columns: [
{ name: "", values: [1], columnDescriptors: [{ value: "Minion1", type: ColumnDescriptorType.MinionId }] },
{ name: "", values: [2], columnDescriptors: [{ value: "Minion2", type: ColumnDescriptorType.MinionId }] },
{ name: "", values: [3], columnDescriptors: [{ value: "Minion3", type: ColumnDescriptorType.MinionId }] }
]
}
}


const modelGroupCalibrationForecastResponseMock: CalibrationForecastResponse =
{
calibrationForecast: {
Expand Down Expand Up @@ -143,6 +160,21 @@ const buildCalibrationForecastQuery = getQueryBuilder<AssetCalibrationQuery>()({
groupBy: []
});

const fakeSystems: SystemMetadata[] = [
{
id: 'Minion1',
alias: 'Minion1-alias',
state: 'CONNECTED',
workspace: '1',
},
{
id: 'Minion2',
alias: undefined,
state: 'DISCONNECTED',
workspace: '2',
},
];

describe('testDatasource', () => {
test('returns success', async () => {
backendServer.fetch
Expand Down Expand Up @@ -204,6 +236,16 @@ describe('queries', () => {
expect(result.data).toMatchSnapshot()
})

test('calibration forecast with minion ID location groupBy', async () => {
backendServer.fetch
.calledWith(requestMatching({ url: '/niapm/v1/assets/calibration-forecast' }))
.mockReturnValue(createFetchResponse(minionIdLocationGroupCalibrationForecastResponseMock as CalibrationForecastResponse))

const result = await datastore.query(buildCalibrationForecastQuery(locationBasedCalibrationForecastQueryMock))

expect(result.data).toMatchSnapshot()
})

test('calibration forecast with model groupBy', async () => {
backendServer.fetch
.calledWith(requestMatching({ url: '/niapm/v1/assets/calibration-forecast' }))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import {
AssetModel,
AssetsResponse,
CalibrationForecastResponse,
ColumnDescriptorType,
FieldDTOWithDescriptor,
} from './types';
import { SystemMetadata } from "../system/types";
import { defaultOrderBy, defaultProjection } from "../system/constants";
import TTLCache from '@isaacs/ttlcache';
import { metadataCacheTTL } from 'datasources/data-frame/constants';

export class AssetCalibrationDataSource extends DataSourceBase<AssetCalibrationQuery> {
constructor(
Expand All @@ -34,9 +37,13 @@ export class AssetCalibrationDataSource extends DataSourceBase<AssetCalibrationQ

baseUrl = this.instanceSettings.url + '/niapm/v1';

systemAliasCache: TTLCache<string, SystemMetadata> = new TTLCache<string, SystemMetadata>({ ttl: metadataCacheTTL });

async runQuery(query: AssetCalibrationQuery, options: DataQueryRequest): Promise<DataFrameDTO> {
await this.loadSystems();
return await this.processCalibrationForecastQuery(query as AssetCalibrationQuery, options);
}

async processCalibrationForecastQuery(query: AssetCalibrationQuery, options: DataQueryRequest) {
const result: DataFrameDTO = { refId: query.refId, fields: [] };
const from = options.range!.from.toISOString();
Expand Down Expand Up @@ -95,7 +102,14 @@ export class AssetCalibrationDataSource extends DataSourceBase<AssetCalibrationQ
}

createColumnNameFromDescriptor(field: FieldDTOWithDescriptor): string {
return field.columnDescriptors.map(descriptor => descriptor.value).join(' - ');
return field.columnDescriptors.map(descriptor => {
if (descriptor.type === ColumnDescriptorType.MinionId && this.systemAliasCache) {
const system = this.systemAliasCache.get(descriptor.value);

return system?.alias || descriptor.value
}
return descriptor.value
}).join(' - ');
}

formatDateForDay(date: string): string {
Expand Down Expand Up @@ -151,6 +165,15 @@ export class AssetCalibrationDataSource extends DataSourceBase<AssetCalibrationQ
}
}

private async loadSystems(): Promise<void> {
if (this.systemAliasCache.size > 0) {
return;
}

const systems = await this.querySystems('', ['id', 'alias','connected.data.state', 'workspace']);
systems.forEach(system => this.systemAliasCache.set(system.id, system));
}

async testDatasource(): Promise<TestDataSourceResponse> {
await this.get(this.baseUrl + '/assets?take=1');
return { status: 'success', message: 'Data source connected and authentication successful!' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,32 @@ exports[`queries calibration forecast with location groupBy 1`] = `
]
`;

exports[`queries calibration forecast with minion ID location groupBy 1`] = `
[
{
"fields": [
{
"name": "Group",
"values": [
"Minion1-alias",
"Minion2",
"Minion3",
],
},
{
"name": "Assets",
"values": [
1,
2,
3,
],
},
],
"refId": "A",
},
]
`;

exports[`queries calibration forecast with model and location groupBy 1`] = `
[
{
Expand Down