Skip to content

Commit

Permalink
feat: add support for null / nan
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbuss committed Jan 11, 2025
1 parent 9ec7b4b commit bf99838
Show file tree
Hide file tree
Showing 51 changed files with 157 additions and 112 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"devDependencies": {
"@aws-sdk/types": "^3.696.0",
"@iot-app-kit/eslint-config": "*",
"@iot-app-kit/helpers": "*",
"@iot-app-kit/ts-config": "*",
"@iot-app-kit/vite-config": "*",
"@types/lodash-es": "^4.17.12",
Expand Down
2 changes: 1 addition & 1 deletion packages/core-util/src/sdks/number.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Primitive } from '@iot-app-kit/core';
import type { Primitive } from '@iot-app-kit/helpers';

/**
* Rounds a number to a given precision
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/common/dataFilters.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { bisector } from 'd3-array';

import type { Primitive } from '@iot-app-kit/helpers';

import { isHistoricalViewport } from './predicates';
import { parseDuration } from './time';
import type { Viewport } from '../data-module/data-cache/requestTypes';
import type { DataPoint, Primitive } from '../data-module/types';
import type { DataPoint } from '../data-module/types';

// By doing the mapping to a date within the bisector
// we eliminate the need to iterate over the entire data.
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/common/number.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Primitive } from '../data-module/types';
import type { Primitive } from '@iot-app-kit/helpers';

const MAX_PRECISION = 4;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Primitive } from '@iot-app-kit/helpers';
import {
MINUTE_IN_MS,
SECOND_IN_MS,
Expand All @@ -14,7 +15,6 @@ import { getExpiredCacheIntervals } from './expiredCacheIntervals';
import { pointBisector } from '../../../common/dataFilters';
import type {
DataPoint,
Primitive,
RequestInformation,
RequestInformationAndRange,
} from '../../types';
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/data-module/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import type { Primitive } from '@iot-app-kit/helpers';

export type { Primitive } from '@iot-app-kit/helpers';

import { type AggregateType, type Quality } from '@aws-sdk/client-iotsitewise';
import type {
TimeSeriesDataRequest,
Expand Down Expand Up @@ -27,8 +31,6 @@ export interface DataPoint<T extends Primitive = Primitive>

export type Resolution = number;

export type Primitive = string | number | boolean;

export type DataStreamId = string;

export type DataBase = {
Expand Down
1 change: 1 addition & 0 deletions packages/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"@iot-app-kit/atoms": "*",
"@iot-app-kit/core": "*",
"@iot-app-kit/core-util": "*",
"@iot-app-kit/helpers": "*",
"@iot-app-kit/react-components": "*",
"@iot-app-kit/source-iotsitewise": "*",
"@popperjs/core": "^2.11.8",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Primitive } from '@iot-app-kit/core';
import type { Primitive } from '@iot-app-kit/helpers';
import { type Quality } from '@aws-sdk/client-iotsitewise';

export type CSVDownloadObject = {
Expand Down
8 changes: 3 additions & 5 deletions packages/dashboard/src/components/dashboard/queryContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { useRefreshRate } from '../../customization/hooks/useRefreshRate';
import { alarmModelQueryToSiteWiseAssetQuery } from '../../customization/widgets/utils/alarmModelQueryToAlarmQuery';
import { assetModelQueryToSiteWiseAssetQuery } from '../../customization/widgets/utils/assetModelQueryToAssetQuery';

import type {
DataStream,
Primitive,
TimeSeriesDataQuery,
} from '@iot-app-kit/core';
import type { Primitive } from '@iot-app-kit/helpers';

import type { DataStream, TimeSeriesDataQuery } from '@iot-app-kit/core';
import type {
AlarmDataQuery,
SiteWiseAlarmDataStreamQuery,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ export class AssetPropertyValueFactory {
...this.#createDefaults(),
...partialAssetPropertyValue,
value: {
// nullValue: { valueType: 'I' },
integerValue: faker.number.int({ min: 0, max: 100 }),
},
} as any,
};

return assetPropertyValue;
Expand All @@ -81,8 +82,9 @@ export class AssetPropertyValueFactory {
...this.#createDefaults(),
...partialAssetPropertyValue,
value: {
// nullValue: { valueType: 'D' },
doubleValue: faker.number.float({ min: 0, max: 100, precision: 0.001 }),
},
} as any,
};

return assetPropertyValue;
Expand All @@ -95,8 +97,9 @@ export class AssetPropertyValueFactory {
...this.#createDefaults(),
...partialAssetPropertyValue,
value: {
// nullValue: { valueType: 'B' },
booleanValue: faker.datatype.boolean(),
},
} as any,
};

return assetPropertyValue;
Expand All @@ -109,8 +112,9 @@ export class AssetPropertyValueFactory {
...this.#createDefaults(),
...partialAssetPropertyValue,
value: {
// nullValue: { valueType: 'S' },
stringValue: faker.helpers.arrayElement(['ON', 'OFF']),
},
} as any,
};

return assetPropertyValue;
Expand Down Expand Up @@ -206,6 +210,7 @@ export function batchGetAssetPropertyValueHistoryHandler() {
};

await delay();

return HttpResponse.json(response, { status: 200 });
});
}
Expand Down
3 changes: 3 additions & 0 deletions packages/helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
"typescript": "^5.5.4",
"vite": "^5.4.11"
},
"peerDependencies": {
"@aws-sdk/client-iotsitewise": "^3.696.0"
},
"iotAppKit": {
"scope": "protected"
}
Expand Down
4 changes: 4 additions & 0 deletions packages/helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ export {
NANO_SECOND_IN_MS,
SECOND_IN_MS,
} from './constants/time';

export { toValue } from './utils/toValue';

export type { Primitive } from './types';
1 change: 1 addition & 0 deletions packages/helpers/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Primitive = string | number | boolean | null;
52 changes: 52 additions & 0 deletions packages/helpers/src/utils/toValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { Primitive } from '../types';
import type { Variant } from '@aws-sdk/client-iotsitewise';

/**
* Extracts the value out of a SiteWise Model Variant
*
* SiteWise Model values can either be a string, number or boolean.
*
* NOTE: Currently we treat booleans as strings.
*/
export const toValue = (variant: Variant | undefined): Primitive => {
if (variant == null) {
throw new Error('variant is undefined');
}

const { doubleValue, integerValue, stringValue, booleanValue } = variant;

if (doubleValue != null) {
return doubleValue;
}

if (integerValue != null) {
return integerValue;
}

if (stringValue != null) {
return stringValue;
}

if (booleanValue != null) {
return booleanValue.toString();
}

if ('nullValue' in variant && variant.nullValue != null) {
/**
* nullValue is not a nullish value
* it's an object with the string type of
* what the value should be.
*/
return null;
}

/**
* A variant with no properties is treated
* as null data so that datastreams do not
* break when the sdk updates
*/
return null;
// throw new Error(
// 'Expected value to have at least one property value, but instead it has none!'
// );
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Primitive } from '@iot-app-kit/helpers';
import {
type DataStream,
type Primitive,
type ResolutionConfig,
type Threshold,
type ThresholdValue,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type Primitive } from '@iot-app-kit/core';
import type { Primitive } from '@iot-app-kit/helpers';
import { isNumeric, round } from '@iot-app-kit/core-util';

export const formatValue =
(significantDigits = 4) =>
(value: Primitive) =>
isNumeric(value) ? `${round(value, significantDigits)}` : value.toString();
isNumeric(value) ? `${round(value, significantDigits)}` : value?.toString();

export type XYPlotTooltipValueOptions = {
value?: Primitive;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Primitive } from '@iot-app-kit/helpers';
import {
type DataStream,
type Primitive,
type Threshold,
type ThresholdValue,
} from '@iot-app-kit/core';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type DataStream, type Primitive } from '@iot-app-kit/core';
import type { Primitive } from '@iot-app-kit/helpers';
import { type DataStream } from '@iot-app-kit/core';
import { type ChartLegend, type ChartOptions } from '../../types';
import { ChartLegendTable } from './table';
import { type DataStreamInformation, type TrendCursor } from './types';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type DataStream, type Primitive } from '@iot-app-kit/core';
import type { Primitive } from '@iot-app-kit/helpers';
import { type DataStream } from '@iot-app-kit/core';
import type { AlarmAssistantContext } from '../../../../common/assistantProps';

export type TrendCursorValues = { [id in string]?: number };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from 'react';
import { useMeasure } from 'react-use';
import { isNumeric, round } from '@iot-app-kit/core-util';
import { type Primitive } from '@iot-app-kit/core';
import type { Primitive } from '@iot-app-kit/helpers';

import Icon from '@cloudscape-design/components/icon';
import {
Expand All @@ -24,7 +24,7 @@ import { useHighlightedDataStreams } from '../hooks/useHighlightedDataStreams';
import './yAxisMenu.css';

const getValue = (value: Primitive, significantDigits = 4) =>
isNumeric(value) ? `${round(value, significantDigits)}` : value.toString();
isNumeric(value) ? `${round(value, significantDigits)}` : value?.toString();

const MENU_OFFSET = 5;
const MENU_FONT_SIZE = 14;
Expand Down
2 changes: 1 addition & 1 deletion packages/react-components/src/components/chart/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Primitive } from '@iot-app-kit/helpers';
import {
type DataStream,
type Primitive,
type StyledThreshold,
type ThresholdSettings,
type Viewport,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Primitive } from '@iot-app-kit/core';
import type { Primitive } from '@iot-app-kit/helpers';

export const convertDataset = (gaugeValue?: Primitive) => {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { AlarmStateTextWithAssistant } from '../alarm-components/alarm-state/alarmStateTextWithAssistant';
import { AlarmStateText } from '../alarm-components/alarm-state/alarmStateText';
import { getPreciseValue } from '../../utils/getPreciseValue';
import { type Primitive } from '@iot-app-kit/core';
import type { Primitive } from '@iot-app-kit/helpers';
import { GaugeErrorText } from './gaugeErrorText';
import { type AlarmContent } from '../alarm-components/alarm-content/types';
import { type AssistantProperty } from '../../common/assistantProps';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Primitive } from '@iot-app-kit/core';
import type { Primitive } from '@iot-app-kit/helpers';
import isEqual from 'lodash-es/isEqual';
import merge from 'lodash-es/merge';
import { useEffect, useReducer } from 'react';
Expand Down
2 changes: 1 addition & 1 deletion packages/react-components/src/components/gauge/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Primitive } from '@iot-app-kit/helpers';
import {
type StyleSettingsMap,
type Threshold,
type Viewport,
type Primitive,
} from '@iot-app-kit/core';
import type { WidgetSettings } from '../../common/dataTypes';
import { type AssistantProperty } from '../../common/assistantProps';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type Primitive, type Threshold } from '@iot-app-kit/core';
import type { Primitive } from '@iot-app-kit/helpers';
import { type Threshold } from '@iot-app-kit/core';

export const thresholdsToColor = ({
gaugeValue,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { BatchGetAssetPropertyValueSuccessEntry } from '@aws-sdk/client-iotsitewise';
import type { DataStreamRequestEntry, DataStreamResource } from './types';
import type { DataStreamResourceWithLatestValue } from '../../types/resources';
import { toValue } from '@iot-app-kit/helpers';

type SuccessEntry = BatchGetAssetPropertyValueSuccessEntry;

Expand Down Expand Up @@ -41,11 +42,10 @@ function createDataStreamWithLatestValue<DataStream extends DataStreamResource>(
requestEntry: DataStreamRequestEntry<DataStream>,
successEntry?: SuccessEntry
): DataStreamResourceWithLatestValue<DataStream> {
const variant = successEntry?.assetPropertyValue?.value;
const dataStreamWithLatestValue = {
...requestEntry.dataStream,
latestValue: Object.values(
successEntry?.assetPropertyValue?.value ?? {}
).at(0),
latestValue: variant && toValue(variant),
latestValueTimestamp:
successEntry?.assetPropertyValue?.timestamp?.timeInSeconds,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { AssetSummary } from '@aws-sdk/client-iotsitewise';
import { type Primitive } from '@iot-app-kit/helpers';

export interface AssetModelResource {
assetModelId: string;
Expand Down Expand Up @@ -59,6 +60,6 @@ export type TimeSeriesResourceWithLatestValue =

export type DataStreamResourceWithLatestValue<DataStreamResource> =
DataStreamResource & {
latestValue?: number | string | boolean;
latestValue?: Primitive;
latestValueTimestamp?: number;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Primitive } from '@iot-app-kit/core';
import type { Primitive } from '@iot-app-kit/helpers';
import { round } from '@iot-app-kit/core-util';

export const Value: React.FC<{
Expand Down
Loading

0 comments on commit bf99838

Please sign in to comment.