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

styleSettings API introduction #38

Merged
merged 1 commit into from
Jan 24, 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
22 changes: 20 additions & 2 deletions packages/components/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* It contains typing information for all components that exist in this project.
*/
import { HTMLStencilElement, JSXBase } from "@stencil/core/internal";
import { AnyDataStreamQuery, AssetSummaryQuery, AssetTreeSubscription, DataModule, SiteWiseAssetTreeQuery, TimeSeriesDataRequest, TimeSeriesDataRequestSettings } from "@iot-app-kit/core";
import { DataStream, MinimalViewPortConfig } from "@synchro-charts/core";
import { AnyDataStreamQuery, AssetSummaryQuery, AssetTreeSubscription, DataModule, DataStream, SiteWiseAssetTreeQuery, StyleSettingsMap, TimeSeriesDataRequest, TimeSeriesDataRequestSettings } from "@iot-app-kit/core";
import { MinimalViewPortConfig } from "@synchro-charts/core";
import { ColumnDefinition, FilterTexts, ResourceExplorerQuery, SitewiseAssetResource } from "./components/iot-resource-explorer/types";
import { TableProps } from "@awsui/components-react/table";
import { EmptyStateProps, ITreeNode, UseTreeCollection } from "@iot-app-kit/related-table";
Expand All @@ -24,6 +24,7 @@ export namespace Components {
"isEditing": boolean | undefined;
"queries": AnyDataStreamQuery[];
"settings": TimeSeriesDataRequestSettings | undefined;
"styleSettings": StyleSettingsMap | undefined;
"viewport": MinimalViewPortConfig;
"widgetId": string;
}
Expand All @@ -32,12 +33,14 @@ export namespace Components {
"queries": AnyDataStreamQuery[];
"renderFunc": ({ dataStreams }: { dataStreams: DataStream[] }) => unknown;
"request": TimeSeriesDataRequest;
"styleSettings": StyleSettingsMap | undefined;
}
interface IotKpi {
"appKit": DataModule;
"isEditing": boolean | undefined;
"queries": AnyDataStreamQuery[];
"settings": TimeSeriesDataRequestSettings | undefined;
"styleSettings": StyleSettingsMap | undefined;
"viewport": MinimalViewPortConfig;
"widgetId": string;
}
Expand All @@ -46,6 +49,8 @@ export namespace Components {
"isEditing": boolean | undefined;
"queries": AnyDataStreamQuery[];
"settings": TimeSeriesDataRequestSettings | undefined;
"styleSettings": StyleSettingsMap | undefined;
"styles": StyleSettingsMap | undefined;
"viewport": MinimalViewPortConfig;
"widgetId": string;
}
Expand All @@ -70,6 +75,7 @@ export namespace Components {
"isEditing": boolean | undefined;
"queries": AnyDataStreamQuery[];
"settings": TimeSeriesDataRequestSettings | undefined;
"styleSettings": StyleSettingsMap | undefined;
"viewport": MinimalViewPortConfig;
"widgetId": string;
}
Expand All @@ -78,6 +84,7 @@ export namespace Components {
"isEditing": boolean | undefined;
"queries": AnyDataStreamQuery[];
"settings": TimeSeriesDataRequestSettings | undefined;
"styleSettings": StyleSettingsMap | undefined;
"viewport": MinimalViewPortConfig;
"widgetId": string;
}
Expand All @@ -86,13 +93,15 @@ export namespace Components {
"isEditing": boolean | undefined;
"queries": AnyDataStreamQuery[];
"settings": TimeSeriesDataRequestSettings | undefined;
"styleSettings": StyleSettingsMap | undefined;
"viewport": MinimalViewPortConfig;
"widgetId": string;
}
interface IotTable {
"appKit": DataModule;
"queries": AnyDataStreamQuery[];
"settings": TimeSeriesDataRequestSettings | undefined;
"styleSettings": StyleSettingsMap | undefined;
"viewport": MinimalViewPortConfig;
"widgetId": string;
}
Expand Down Expand Up @@ -270,6 +279,7 @@ declare namespace LocalJSX {
"isEditing"?: boolean | undefined;
"queries"?: AnyDataStreamQuery[];
"settings"?: TimeSeriesDataRequestSettings | undefined;
"styleSettings"?: StyleSettingsMap | undefined;
"viewport"?: MinimalViewPortConfig;
"widgetId"?: string;
}
Expand All @@ -278,12 +288,14 @@ declare namespace LocalJSX {
"queries"?: AnyDataStreamQuery[];
"renderFunc"?: ({ dataStreams }: { dataStreams: DataStream[] }) => unknown;
"request"?: TimeSeriesDataRequest;
"styleSettings"?: StyleSettingsMap | undefined;
}
interface IotKpi {
"appKit"?: DataModule;
"isEditing"?: boolean | undefined;
"queries"?: AnyDataStreamQuery[];
"settings"?: TimeSeriesDataRequestSettings | undefined;
"styleSettings"?: StyleSettingsMap | undefined;
"viewport"?: MinimalViewPortConfig;
"widgetId"?: string;
}
Expand All @@ -292,6 +304,8 @@ declare namespace LocalJSX {
"isEditing"?: boolean | undefined;
"queries"?: AnyDataStreamQuery[];
"settings"?: TimeSeriesDataRequestSettings | undefined;
"styleSettings"?: StyleSettingsMap | undefined;
"styles"?: StyleSettingsMap | undefined;
"viewport"?: MinimalViewPortConfig;
"widgetId"?: string;
}
Expand All @@ -316,6 +330,7 @@ declare namespace LocalJSX {
"isEditing"?: boolean | undefined;
"queries"?: AnyDataStreamQuery[];
"settings"?: TimeSeriesDataRequestSettings | undefined;
"styleSettings"?: StyleSettingsMap | undefined;
"viewport"?: MinimalViewPortConfig;
"widgetId"?: string;
}
Expand All @@ -324,6 +339,7 @@ declare namespace LocalJSX {
"isEditing"?: boolean | undefined;
"queries"?: AnyDataStreamQuery[];
"settings"?: TimeSeriesDataRequestSettings | undefined;
"styleSettings"?: StyleSettingsMap | undefined;
"viewport"?: MinimalViewPortConfig;
"widgetId"?: string;
}
Expand All @@ -332,13 +348,15 @@ declare namespace LocalJSX {
"isEditing"?: boolean | undefined;
"queries"?: AnyDataStreamQuery[];
"settings"?: TimeSeriesDataRequestSettings | undefined;
"styleSettings"?: StyleSettingsMap | undefined;
"viewport"?: MinimalViewPortConfig;
"widgetId"?: string;
}
interface IotTable {
"appKit"?: DataModule;
"queries"?: AnyDataStreamQuery[];
"settings"?: TimeSeriesDataRequestSettings | undefined;
"styleSettings"?: StyleSettingsMap | undefined;
"viewport"?: MinimalViewPortConfig;
"widgetId"?: string;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Component, Prop, h } from '@stencil/core';
import { MinimalViewPortConfig } from '@synchro-charts/core';
import { AnyDataStreamQuery, DataModule, TimeSeriesDataRequestSettings } from '@iot-app-kit/core';
import { StyleSettingsMap } from '../types';
import { AnyDataStreamQuery, DataModule, TimeSeriesDataRequestSettings, StyleSettingsMap } from '@iot-app-kit/core';

const DEFAULT_VIEWPORT = { duration: 10 * 1000 * 60 };

Expand All @@ -22,7 +21,7 @@ export class IotBarChart {

@Prop() settings: TimeSeriesDataRequestSettings | undefined;

@Prop() styles: StyleSettingsMap | undefined;
@Prop() styleSettings: StyleSettingsMap | undefined;

getSettings(): TimeSeriesDataRequestSettings {
return {
Expand All @@ -37,6 +36,7 @@ export class IotBarChart {
<iot-connector
appKit={this.appKit}
queries={this.queries}
styleSettings={this.styleSettings}
request={{
settings,
viewport: this.viewport,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { bindStylesToDataStreams } from './bindStylesToDataStreams';
import { DATA_STREAM, DATA_STREAM_2 } from '../../testing/mockWidgetProperties';

it('returns empty array when provided no data streams', () => {
expect(
bindStylesToDataStreams({
dataStreams: [],
styleSettings: { someStyle: { color: 'red' } },
})
).toEqual([]);
});

it('returns data streams when no matching styles', () => {
expect(
bindStylesToDataStreams({
dataStreams: [DATA_STREAM],
styleSettings: { someStyle: { color: 'red' } },
})
).toEqual([DATA_STREAM]);
});

it('associates styles to corresponding data stream', () => {
expect(
bindStylesToDataStreams({
dataStreams: [{ ...DATA_STREAM, refId: 'someStyle' }],
styleSettings: { someStyle: { color: 'red' } },
})
).toEqual([{ ...DATA_STREAM, refId: 'someStyle', color: 'red' }]);
});

it('associates styles to corresponding data stream for multiple data streams', () => {
expect(
bindStylesToDataStreams({
dataStreams: [
{ ...DATA_STREAM, refId: 'someStyle' },
{ ...DATA_STREAM_2, refId: 'someStyle2' },
],
styleSettings: { someStyle: { color: 'red' }, someStyle2: { color: 'blue' } },
})
).toEqual([
{ ...DATA_STREAM, refId: 'someStyle', color: 'red' },
{ ...DATA_STREAM_2, refId: 'someStyle2', color: 'blue' },
]);
});

it('returns data stream when no matching refId', () => {
expect(
bindStylesToDataStreams({
dataStreams: [{ ...DATA_STREAM, refId: 'someStyle100' }],
styleSettings: { someStyle: { color: 'red' } },
})
).toEqual([{ ...DATA_STREAM, refId: 'someStyle100' }]);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { StyleSettingsMap, DataStream } from '@iot-app-kit/core';

// If the data stream has a reference id with associated styles, append those styles to the data stream.
export const bindStylesToDataStreams = ({
dataStreams,
styleSettings,
}: {
dataStreams: DataStream[];
styleSettings?: StyleSettingsMap;
}): DataStream[] =>
dataStreams.map((dataStream) =>
styleSettings == null || dataStream.refId == null
? dataStream
: {
...dataStream,
...styleSettings[dataStream.refId],
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,36 @@ it('updates with new queries', async () => {
],
});
});

it('binds styles to data streams', async () => {
const renderFunc = jest.fn();
const { assetId, propertyId } = toSiteWiseAssetProperty(DATA_STREAM.id);
const REF_ID = 'some-ref-id';

await connectorSpecPage({
renderFunc,
styleSettings: {
[REF_ID]: {
color: 'red',
name: 'my-name',
},
},
queries: [
{
source: 'test-mock',
assets: [{ assetId, properties: [{ propertyId, refId: REF_ID }] }],
} as SiteWiseDataStreamQuery,
],
});

expect(renderFunc).lastCalledWith({
dataStreams: [
expect.objectContaining({
id: DATA_STREAM.id,
refId: REF_ID,
color: 'red',
name: 'my-name',
}),
],
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Component, Listen, Prop, State, Watch } from '@stencil/core';
import { DataStream } from '@synchro-charts/core';
import isEqual from 'lodash.isequal';
import {
AnyDataStreamQuery,
SubscriptionUpdate,
subscribeToDataStreams,
DataModule,
DataStream,
TimeSeriesDataRequest,
StyleSettingsMap,
} from '@iot-app-kit/core';
import { bindStylesToDataStreams } from './bindStylesToDataStreams';

@Component({
tag: 'iot-connector',
Expand All @@ -22,6 +24,8 @@ export class IotConnector {

@Prop() renderFunc: ({ dataStreams }: { dataStreams: DataStream[] }) => unknown;

@Prop() styleSettings: StyleSettingsMap | undefined;

@State() dataStreams: DataStream[] = [];

private update: (subscriptionUpdate: SubscriptionUpdate<AnyDataStreamQuery>) => void;
Expand All @@ -37,7 +41,7 @@ export class IotConnector {
request: this.request,
},
(dataStreams: DataStream[]) => {
this.dataStreams = dataStreams;
this.dataStreams = bindStylesToDataStreams({ dataStreams, styleSettings: this.styleSettings });
Copy link
Contributor

Choose a reason for hiding this comment

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

(random thoughts) It initially felt strange to me to bind style to dataStreams, since styles are metadata and not streamed. I see that it follows the synchro-chart API though, and these are essentially DataStreamStyles. Would we ever want to swap out synchro-charts as a viz library? If not then this approach seems fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

how we bind styleSettings will be component specific, right now iot-connector is a bit specific to Synchro Charts, but we can refactor it as necessary as the need arises as iot-connector is purely an internal helper and not intended to be used as part of the offerings of the library

}
);

Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/components/iot-kpi/iot-kpi.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Component, Prop, h } from '@stencil/core';
import { MinimalViewPortConfig } from '@synchro-charts/core';
import { AnyDataStreamQuery, DataModule, TimeSeriesDataRequestSettings } from '@iot-app-kit/core';
import { StyleSettingsMap } from '../types';
import { AnyDataStreamQuery, DataModule, TimeSeriesDataRequestSettings, StyleSettingsMap } from '@iot-app-kit/core';

const DEFAULT_VIEWPORT = { duration: 10 * 1000 };

Expand All @@ -22,7 +21,7 @@ export class IotKpi {

@Prop() settings: TimeSeriesDataRequestSettings | undefined;

@Prop() styles: StyleSettingsMap | undefined;
@Prop() styleSettings: StyleSettingsMap | undefined;

getSettings(): TimeSeriesDataRequestSettings {
return {
Expand All @@ -37,6 +36,7 @@ export class IotKpi {
<iot-connector
appKit={this.appKit}
queries={this.queries}
styleSettings={this.styleSettings}
request={{
settings,
viewport: this.viewport,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Component, Prop, h } from '@stencil/core';
import { MinimalViewPortConfig } from '@synchro-charts/core';
import { AnyDataStreamQuery, DataModule, TimeSeriesDataRequestSettings } from '@iot-app-kit/core';
import { StyleSettingsMap } from '../types';
import { AnyDataStreamQuery, DataModule, TimeSeriesDataRequestSettings, StyleSettingsMap } from '@iot-app-kit/core';

const DEFAULT_VIEWPORT = { duration: 10 * 1000 * 60 };

Expand All @@ -24,6 +23,8 @@ export class IotLineChart {

@Prop() styles: StyleSettingsMap | undefined;

@Prop() styleSettings: StyleSettingsMap | undefined;

getSettings(): TimeSeriesDataRequestSettings {
return {
...this.settings,
Expand All @@ -39,6 +40,7 @@ export class IotLineChart {
<iot-connector
appKit={this.appKit}
queries={this.queries}
styleSettings={this.styleSettings}
request={{
settings,
viewport: this.viewport,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Component, Prop, h } from '@stencil/core';
import { MinimalViewPortConfig } from '@synchro-charts/core';
import { AnyDataStreamQuery, DataModule, TimeSeriesDataRequestSettings } from '@iot-app-kit/core';
import { StyleSettingsMap } from '../types';
import { AnyDataStreamQuery, DataModule, TimeSeriesDataRequestSettings, StyleSettingsMap } from '@iot-app-kit/core';

const DEFAULT_VIEWPORT = { duration: 10 * 1000 * 60 };

Expand All @@ -22,7 +21,7 @@ export class IotScatterChart {

@Prop() settings: TimeSeriesDataRequestSettings | undefined;

@Prop() styles: StyleSettingsMap | undefined;
@Prop() styleSettings: StyleSettingsMap | undefined;

getSettings(): TimeSeriesDataRequestSettings {
return {
Expand All @@ -37,6 +36,7 @@ export class IotScatterChart {
<iot-connector
appKit={this.appKit}
queries={this.queries}
styleSettings={this.styleSettings}
request={{
settings,
viewport: this.viewport,
Expand Down
Loading