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: support asset selection by external id #390

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
42 changes: 36 additions & 6 deletions src/components/query/PropertyQueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { QueryOptions } from './QueryOptions';

type Props = SitewiseQueryEditorProps<SitewiseQuery | AssetPropertyAggregatesQuery | ListAssociatedAssetsQuery>;

const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89abAB][0-9a-f]{3}-[0-9a-f]{12}$/i;

const resolutions: Array<SelectableValue<SiteWiseResolution>> = [
{
value: SiteWiseResolution.Auto,
Expand All @@ -40,6 +42,7 @@ const resolutions: Array<SelectableValue<SiteWiseResolution>> = [
];

interface State {
assetId?: string;
asset?: AssetInfo;
property?: AssetPropertyInfo;
assets: Array<SelectableValue<string>>;
Expand All @@ -52,24 +55,30 @@ const ALL_HIERARCHIES = '*';

export class PropertyQueryEditor extends PureComponent<Props, State> {
state: State = {
assetId: this.props.query.assetIds && this.props.query.assetIds[0],
assets: [],
assetProperties: [],
loading: true,
openModal: false,
};

async updateInfo() {
const { query, datasource } = this.props;
const { onChange, query, datasource } = this.props;
const update: State = {
loading: false,
} as State;

const cache = datasource.getCache(query.region);
if (query?.assetIds?.length) {
try {
update.asset = await cache.getAssetInfo(query.assetIds![0]);
update.asset = await cache.getAssetInfo(query.assetIds[0]);
const ps = await cache.listAssetProperties(query.assetIds[0]);
update.assetProperties = ps?.map(({ id, name }) => ({ id, name })) || [];
// Update external ids to asset ids in the query
if (update.asset?.id && query.assetIds[0].startsWith('externalId')) {
query.assetIds[0] = update.asset.id;
onChange(query);
}
} catch (err) {
console.warn('error reading asset info', err);
update.property = undefined;
Expand Down Expand Up @@ -98,7 +107,7 @@ export class PropertyQueryEditor extends PureComponent<Props, State> {

if (assetChanged || propChanged || regionChanged) {
if (!query.assetIds?.length && !regionChanged) {
this.setState({ asset: undefined, property: undefined, loading: false });
this.setState({ assetId: undefined, asset: undefined, property: undefined, loading: false });
} else {
this.setState({ loading: true });
this.updateInfo();
Expand Down Expand Up @@ -168,7 +177,15 @@ export class PropertyQueryEditor extends PureComponent<Props, State> {

onSetAssetId = (assetId?: string) => {
const { onChange, query } = this.props;
onChange({ ...query, assetIds: assetId ? [assetId] : undefined });
if (!assetId) {
this.setState({ assetId: undefined });
onChange({ ...query, assetIds: undefined });
} else {
const assetIds =
uuidRegex.test(assetId) || assetId.startsWith('externalId:') ? [assetId] : [`externalId:${assetId}`];
Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't tested this, but I assume that if the user is using a template variable, i.e $myAssetId, then this will append the externalId to it, which we don't want. The easiest solution to this would probably be to add a check for startsWith($)

Copy link
Collaborator

Choose a reason for hiding this comment

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

this.setState({ assetId: assetIds[0] });
onChange({ ...query, assetIds });
}
};

onSetPropertyId = (propertyId?: string) => {
Expand Down Expand Up @@ -300,7 +317,7 @@ export class PropertyQueryEditor extends PureComponent<Props, State> {
if (loading) {
current = query.assetIds.map((assetId) => ({ label: 'loading...', value: assetId }));
} else {
current = query.assetIds.map((assetId) => ({ label: `ID: ${assetId}`, value: assetId }));
current = query.assetIds.map((assetId) => ({ label: `ID: ${this.state.assetId}`, value: assetId }));
}
}

Expand Down Expand Up @@ -341,6 +358,19 @@ export class PropertyQueryEditor extends PureComponent<Props, State> {
</div>
);

const assetTooltip = (
<div>
Set the asset ID. It can be either the actual ID in UUID format, or else "externalId:" followed by the external
ID, if it has one.
<LinkButton
href="https://docs.aws.amazon.com/iot-sitewise/latest/userguide/object-ids.html#external-ids"
target="_blank"
>
API Docs <Icon name="external-link-alt" />
</LinkButton>
</div>
);

return (
<>
<EditorRow>
Expand All @@ -359,7 +389,7 @@ export class PropertyQueryEditor extends PureComponent<Props, State> {
<>
<EditorRow>
<EditorFieldGroup>
<EditorField label="Asset" htmlFor="asset" width={30}>
<EditorField label="Asset" tooltip={assetTooltip} tooltipInteractive htmlFor="asset" width={30}>
<Select
id="asset"
inputId="asset"
Expand Down
Loading