Skip to content

Commit

Permalink
Users/alkerezs/implement column descriptors (#70)
Browse files Browse the repository at this point in the history
Co-authored-by: semantic-release-bot <semantic-release-bot@martynus.net>
Co-authored-by: NI\akerezsi <alex.kerezsi@ni.com>
  • Loading branch information
3 people authored Sep 30, 2024
1 parent 2bf042c commit 3ee9cf2
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 64 deletions.
103 changes: 52 additions & 51 deletions src/datasources/asset-calibration/AssetCalibrationDataSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
AssetCalibrationQuery,
AssetCalibrationTimeBasedGroupByType,
CalibrationForecastResponse,
ColumnDescriptorType,
} from "./types";

let datastore: AssetCalibrationDataSource, backendServer: MockProxy<BackendSrv>
Expand All @@ -23,83 +24,83 @@ beforeEach(() => {

const monthGroupCalibrationForecastResponseMock: CalibrationForecastResponse =
{
calibrationForecast: {
columns: [
{ name: "Month", values: ["2022-01-01T00:00:00.0000000Z", "2022-02-01T00:00:00.0000000Z", "2022-03-01T00:00:00.0000000Z"] },
{ name: "Assets", values: [1, 0, 3] },
]
}
calibrationForecast: {
columns: [
{ name: "", values: ["2022-01-01T00:00:00.0000000Z", "2022-02-01T00:00:00.0000000Z", "2022-03-01T00:00:00.0000000Z"], columnDescriptors: [{ value: "Month", type: ColumnDescriptorType.Time }] },
{ name: "", values: [1, 0, 3], columnDescriptors: [{ value: "Assets", type: ColumnDescriptorType.Count }] }
]
}
}

const weekGroupCalibrationForecastResponseMock: CalibrationForecastResponse =
const dayGroupCalibrationForecastResponseMock: CalibrationForecastResponse =
{
calibrationForecast: {
columns: [
{ name: "Day", values: ["2022-01-01T00:00:00.0000000Z", "2022-01-02T00:00:00.0000000Z", "2022-01-03T00:00:00.0000000Z"] },
{ name: "Assets", values: [1, 2, 2] },
]
}
calibrationForecast: {
columns: [
{ name: "", values: ["2022-01-01T00:00:00.0000000Z", "2022-01-02T00:00:00.0000000Z", "2022-01-03T00:00:00.0000000Z"], columnDescriptors: [{ value: "Day", type: ColumnDescriptorType.Time }] },
{ name: "", values: [1, 2, 2], columnDescriptors: [{ value: "Assets", type: ColumnDescriptorType.Count }] }
]
}
}

const dayGroupCalibrationForecastResponseMock: CalibrationForecastResponse =
const weekGroupCalibrationForecastResponseMock: CalibrationForecastResponse =
{
calibrationForecast: {
columns: [
{ name: "Week", values: ["2022-01-03T00:00:00.0000000Z", "2022-01-10T00:00:00.0000000Z", "2022-01-17T00:00:00.0000000Z"] },
{ name: "Assets", values: [1, 2, 2] },
]
}
calibrationForecast: {
columns: [
{ name: "", values: ["2022-01-03T00:00:00.0000000Z", "2022-01-10T00:00:00.0000000Z", "2022-01-17T00:00:00.0000000Z"], columnDescriptors: [{ value: "Week", type: ColumnDescriptorType.Time }] },
{ name: "", values: [1, 2, 2], columnDescriptors: [{ value: "Assets", type: ColumnDescriptorType.Count }] }
]
}
}

const locationGroupCalibrationForecastResponseMock: CalibrationForecastResponse =
{
calibrationForecast: {
columns: [
{ name: "Location1", values: [1] },
{ name: "Location2", values: [2] },
{ name: "Location3", values: [3] },
]
}
calibrationForecast: {
columns: [
{ name: "", values: [1], columnDescriptors: [{ value: "Location1", type: ColumnDescriptorType.StringValue }] },
{ name: "", values: [2], columnDescriptors: [{ value: "Location2", type: ColumnDescriptorType.StringValue }] },
{ name: "", values: [3], columnDescriptors: [{ value: "Location3", type: ColumnDescriptorType.StringValue }] }
]
}
}

const modelGroupCalibrationForecastResponseMock: CalibrationForecastResponse =
{
calibrationForecast: {
columns: [
{ name: "Model1", values: [1] },
{ name: "Model2", values: [2] },
]
}
calibrationForecast: {
columns: [
{ name: "", values: [1], columnDescriptors: [{ value: "Model1", type: ColumnDescriptorType.StringValue }] },
{ name: "", values: [2], columnDescriptors: [{ value: "Model2", type: ColumnDescriptorType.StringValue }] }
]
}
}

const emptyGroupCalibrationForecastResponseMock: CalibrationForecastResponse =
{
calibrationForecast: {
columns: [
]
}
calibrationForecast: {
columns: [
]
}
}

const modelLocationGroupCalibrationForecastResponseMock: CalibrationForecastResponse =
{
calibrationForecast: {
columns: [
{ name: "Model1 - Localtion1", values: [1] },
{ name: "Model2 - Localtion1", values: [2] },
]
}
calibrationForecast: {
columns: [
{ name: "", values: [1], columnDescriptors: [{ value: "Model1", type: ColumnDescriptorType.StringValue }, { value: "Location1", type: ColumnDescriptorType.StringValue }] },
{ name: "", values: [2], columnDescriptors: [{ value: "Model2", type: ColumnDescriptorType.StringValue }, { value: "Location1", type: ColumnDescriptorType.StringValue }] }
]
}
}

const monthLocationGroupCalibrationForecastResponseMock: CalibrationForecastResponse =
{
calibrationForecast: {
columns: [
{ name: "Month", values: ["2022-01-01T00:00:00.0000000Z", "2022-02-01T00:00:00.0000000Z", "2022-03-01T00:00:00.0000000Z"] },
{ name: "Location1", values: [1, 2, 3] },
{ name: "Location2", values: [2, 4, 1] },
{ name: "Location3", values: [4, 3, 1] },
]
}
calibrationForecast: {
columns: [
{ name: "", values: ["2022-01-01T00:00:00.0000000Z", "2022-02-01T00:00:00.0000000Z", "2022-03-01T00:00:00.0000000Z"], columnDescriptors: [{ value: "Month", type: ColumnDescriptorType.Time }] },
{ name: "", values: [1, 2, 3], columnDescriptors: [{ value: "Location1", type: ColumnDescriptorType.StringValue }] },
{ name: "", values: [2, 4, 1], columnDescriptors: [{ value: "Location2", type: ColumnDescriptorType.StringValue }] },
{ name: "", values: [4, 3, 1], columnDescriptors: [{ value: "Location3", type: ColumnDescriptorType.StringValue }] }
]
}
}

const monthBasedCalibrationForecastQueryMock: AssetCalibrationQuery = {
Expand Down
12 changes: 10 additions & 2 deletions src/datasources/asset-calibration/AssetCalibrationDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
AssetModel,
AssetsResponse,
CalibrationForecastResponse,
FieldDTOWithDescriptor,
} from './types';
import { SystemMetadata } from "../system/types";
import { defaultOrderBy, defaultProjection } from "../system/constants";
Expand Down Expand Up @@ -61,6 +62,7 @@ export class AssetCalibrationDataSource extends DataSourceBase<AssetCalibrationQ

processResultsGroupedByTime(result: DataFrameDTO) {
result.fields.forEach(field => {
field.name = this.createColumnNameFromDescriptor(field as FieldDTOWithDescriptor);
switch (field.name) {
case AssetCalibrationForecastKey.Day:
field.values = field.values!.map(this.formatDateForDay)
Expand All @@ -83,13 +85,19 @@ export class AssetCalibrationDataSource extends DataSourceBase<AssetCalibrationQ
formattedFields.push({ name: "Assets", values: [] } as FieldDTO);

for (let columnIndex = 0; columnIndex < result.fields.length; columnIndex++) {
formattedFields[0].values!.push(result.fields[columnIndex].name)
formattedFields[1].values!.push(result.fields[columnIndex].values?.at(0))
const columnName = this.createColumnNameFromDescriptor(result.fields[columnIndex] as FieldDTOWithDescriptor);
const columnValue = result.fields[columnIndex].values?.at(0);
formattedFields[0].values!.push(columnName);
formattedFields[1].values!.push(columnValue);
}

result.fields = formattedFields;
}

createColumnNameFromDescriptor(field: FieldDTOWithDescriptor): string {
return field.columnDescriptors.map(descriptor => descriptor.value).join(' - ');
}

formatDateForDay(date: string): string {
return new Date(date).toISOString().split('T')[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@ exports[`queries asset calibration forecast with day groupBy 1`] = `
{
"fields": [
{
"name": "Week",
"columnDescriptors": [
{
"type": "Time",
"value": "Day",
},
],
"name": "Day",
"values": [
"2022-01-03 : 2022-01-09",
"2022-01-10 : 2022-01-16",
"2022-01-17 : 2022-01-23",
"2022-01-01",
"2022-01-02",
"2022-01-03",
],
},
{
"columnDescriptors": [
{
"type": "Count",
"value": "Assets",
},
],
"name": "Assets",
"values": [
1,
Expand All @@ -31,6 +43,12 @@ exports[`queries asset calibration forecast with month groupBy 1`] = `
{
"fields": [
{
"columnDescriptors": [
{
"type": "Time",
"value": "Month",
},
],
"name": "Month",
"values": [
"January 2022",
Expand All @@ -39,6 +57,12 @@ exports[`queries asset calibration forecast with month groupBy 1`] = `
],
},
{
"columnDescriptors": [
{
"type": "Count",
"value": "Assets",
},
],
"name": "Assets",
"values": [
1,
Expand All @@ -57,14 +81,26 @@ exports[`queries asset calibration forecast with week groupBy 1`] = `
{
"fields": [
{
"name": "Day",
"columnDescriptors": [
{
"type": "Time",
"value": "Week",
},
],
"name": "Week",
"values": [
"2022-01-01",
"2022-01-02",
"2022-01-03",
"2022-01-03 : 2022-01-09",
"2022-01-10 : 2022-01-16",
"2022-01-17 : 2022-01-23",
],
},
{
"columnDescriptors": [
{
"type": "Count",
"value": "Assets",
},
],
"name": "Assets",
"values": [
1,
Expand Down Expand Up @@ -111,8 +147,8 @@ exports[`queries calibration forecast with model and location groupBy 1`] = `
{
"name": "Group",
"values": [
"Model1 - Localtion1",
"Model2 - Localtion1",
"Model1 - Location1",
"Model2 - Location1",
],
},
{
Expand Down Expand Up @@ -157,6 +193,12 @@ exports[`queries calibration forecast with month and location groupBy 1`] = `
{
"fields": [
{
"columnDescriptors": [
{
"type": "Time",
"value": "Month",
},
],
"name": "Month",
"values": [
"January 2022",
Expand All @@ -165,6 +207,12 @@ exports[`queries calibration forecast with month and location groupBy 1`] = `
],
},
{
"columnDescriptors": [
{
"type": "StringValue",
"value": "Location1",
},
],
"name": "Location1",
"values": [
1,
Expand All @@ -173,6 +221,12 @@ exports[`queries calibration forecast with month and location groupBy 1`] = `
],
},
{
"columnDescriptors": [
{
"type": "StringValue",
"value": "Location2",
},
],
"name": "Location2",
"values": [
2,
Expand All @@ -181,6 +235,12 @@ exports[`queries calibration forecast with month and location groupBy 1`] = `
],
},
{
"columnDescriptors": [
{
"type": "StringValue",
"value": "Location3",
},
],
"name": "Location3",
"values": [
4,
Expand Down
18 changes: 17 additions & 1 deletion src/datasources/asset-calibration/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,21 @@ export enum AssetFilterProperties {
}

export interface CalibrationForecastModel {
columns: FieldDTO[],
columns: FieldDTOWithDescriptor[],
}

export interface FieldDTOWithDescriptor extends FieldDTO {
columnDescriptors: ColumnDescriptor[]
}

export interface ColumnDescriptor {
value: string
type: ColumnDescriptorType
}

export enum ColumnDescriptorType {
Time = "Time",
Count = "Count",
StringValue = "StringValue",
MinionId = "MinionId",
}

0 comments on commit 3ee9cf2

Please sign in to comment.