Skip to content

Commit

Permalink
fix(dashboard): modeled datastreams are displayed in msw
Browse files Browse the repository at this point in the history
  • Loading branch information
ssjagad authored and diehbria committed Dec 18, 2023
1 parent 1618d50 commit a2833a1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ListAssetsResponse } from '@aws-sdk/client-iotsitewise';
import { type AssetSummary, type ListAssetsResponse } from '@aws-sdk/client-iotsitewise';
import { rest } from 'msw';

import { LIST_ASSETS_URL } from './constants';
Expand All @@ -7,11 +7,19 @@ import { summarizeAsset } from '../../resources/assets/summarizeAssetDescription

export function listAssetsHandler() {
return rest.get(LIST_ASSETS_URL, (_req, res, ctx) => {
const rootAssets = ASSET_HIERARCHY.getRootAssets();
const rootAssetSummaries = rootAssets.map(summarizeAsset);
const url = new URL(_req.url);
const assetModelId = url.searchParams.get('assetModelId');
let assetSummaries: AssetSummary[] = [];
if (assetModelId) {
const assets = ASSET_HIERARCHY.getAssetsByAssetModelId(assetModelId);
assetSummaries = assets.map(summarizeAsset);
} else {
const rootAssets = ASSET_HIERARCHY.getRootAssets();
assetSummaries = rootAssets.map(summarizeAsset);
}

const response: ListAssetsResponse = {
assetSummaries: rootAssetSummaries,
assetSummaries: assetSummaries,
nextToken: undefined,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,26 @@ export class AssetFactory {

public create({ assetName }: Pick<Asset, 'assetName'>): Asset {
const asset = {
...this.#createDefaults(),
...this.#createDefaults(assetName),
assetName,
};

return asset;
}

#createDefaults() {
#createDefaults(assetName: string | undefined) {
const assetId = uuid();
const assetArn = `arn:aws:iotsitewise:us-east-1:123456789012:asset/${assetId}`;
const assetModelId = this.#assetModel.assetModelId;
const assetProperties =
this.#assetModel.assetModelProperties?.map(({ id, name, dataType }) => ({
id,
name,
dataType,
name: name,
dataType: dataType,
path: [
{ id: assetId, name: assetName ?? '' },
{ id: id, name: name },
],
})) ?? [];
const assetHierarchies = this.#assetModel.assetModelHierarchies;
const assetCreationDate = new Date();
Expand Down
19 changes: 19 additions & 0 deletions packages/dashboard/src/msw/iot-sitewise/resources/assets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class AssetHierarchyClient {
return rootAssets;
}

public getAssetsByAssetModelId(assetModelId: string): Asset[] {
const assetList = this.#searchByAssetModelId(assetModelId, this.#assetHierarchy);
return assetList;
}

public findAssetById(assetId: string): Asset | undefined {
const node = this.#searchById(assetId, this.#assetHierarchy);
const asset = node?.asset;
Expand Down Expand Up @@ -69,6 +74,20 @@ class AssetHierarchyClient {
}
}

#searchByAssetModelId(assetModelId: string, assetHierarchy: AssetHierarchy, assetList?: Asset[]) {
const assetsMatchingModelIdList = assetList ? assetList : [];
for (const node of assetHierarchy) {
if (node.asset.assetModelId === assetModelId) {
assetsMatchingModelIdList.push(node.asset);
}

if (node.children) {
this.#searchByAssetModelId(assetModelId, node.children, assetsMatchingModelIdList);
}
}
return assetsMatchingModelIdList;
}

#searchParentById(
assetId: string,
assetHierarchy: AssetHierarchy,
Expand Down

0 comments on commit a2833a1

Please sign in to comment.