Skip to content

Commit

Permalink
chore: dashboard documentation update
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Pauer committed Nov 27, 2024
1 parent 18385e6 commit cea67cc
Show file tree
Hide file tree
Showing 36 changed files with 1,820 additions and 162 deletions.
4 changes: 4 additions & 0 deletions apps/doc-site/msw/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { setupWorker } from 'msw/browser';
import { handlers } from './handlers';

export const worker = setupWorker(...handlers);
1 change: 1 addition & 0 deletions apps/doc-site/msw/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DEFAULT_REGION = 'us-east-1';
25 changes: 25 additions & 0 deletions apps/doc-site/msw/handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
batchGetAssetPropertyValueHandler,
batchGetAssetPropertyValueHistoryHandler,
batchGetAssetPropertyAggregatesHandler,
} from './iot-sitewise/handlers/batchGetAssetPropertyValue/batchGetAssetPropertyValueHandler';
import { describeAssetHandler } from './iot-sitewise/handlers/describeAsset/describeAssetHandler';
import { describeAssetModelHandler } from './iot-sitewise/handlers/describeAssetModel/describeAssetModelHandler';
import { listAssetsHandler } from './iot-sitewise/handlers/listAssets/listAssetsHandler';
import { listAssociatedAssetsHandler } from './iot-sitewise/handlers/listAssociatedAssets/listAssociatedAssetsHandler';
import { listAssetModelsHandler } from './iot-sitewise/handlers/listAssetModels/listAssetModels';
import { listAssetModelPropertiesHandler } from './iot-sitewise/handlers/listAssetModelProperties/listAssetModelProperties';
import { listAssetPropertiesHandler } from './iot-sitewise/handlers/listAssetProperties/listAssetProperties';

export const handlers = [
batchGetAssetPropertyAggregatesHandler(),
batchGetAssetPropertyValueHandler(),
batchGetAssetPropertyValueHistoryHandler(),
describeAssetHandler(),
describeAssetModelHandler(),
listAssetsHandler(),
listAssetModelsHandler(),
listAssociatedAssetsHandler(),
listAssetModelPropertiesHandler(),
listAssetPropertiesHandler(),
];
4 changes: 4 additions & 0 deletions apps/doc-site/msw/iot-sitewise/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { DEFAULT_REGION } from '../constants';

export const SITEWISE_CONTROL_PLANE_API_BASE_URL = `https://api.iotsitewise.${DEFAULT_REGION}.amazonaws.com`;
export const SITEWISE_DATA_PLANE_API_BASE_URL = `https://data.iotsitewise.${DEFAULT_REGION}.amazonaws.com`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
import {
type BatchGetAssetPropertyValueRequest,
type BatchGetAssetPropertyValueResponse,
type BatchGetAssetPropertyValueHistoryRequest,
type BatchGetAssetPropertyValueHistoryResponse,
type BatchGetAssetPropertyAggregatesRequest,
type BatchGetAssetPropertyAggregatesResponse,
type AssetPropertyValue,
type AggregatedValue,
type AggregateType,
type Quality,
} from '@aws-sdk/client-iotsitewise';
import { faker } from '@faker-js/faker';
import { delay, http, HttpResponse } from 'msw';
import {
BATCH_GET_ASSET_PROPERTY_AGGREGATES_URL,
BATCH_GET_ASSET_PROPERTY_VALUE_HISTORY_URL,
BATCH_GET_ASSET_PROPERTY_VALUE_URL,
} from './constants';

type PartialAggregatedValue = Partial<Omit<AggregatedValue, 'value'>>;

export class AggregatedValueFactory {
public create(
aggregateType: AggregateType,
partialAggregatedValue: PartialAggregatedValue = {}
): AggregatedValue {
const aggregateTypeMap = {
AVERAGE: 'average',
COUNT: 'count',
MAXIMUM: 'maximum',
MINIMUM: 'minimum',
STANDARD_DEVIATION: 'standardDeviation',
SUM: 'sum',
};

const aggregatedValue = {
...this.#createDefaults(),
...partialAggregatedValue,
value: {
[aggregateTypeMap[aggregateType]]: faker.number.float({
min: 0,
max: 100,
}),
},
};

return aggregatedValue;
}

#createDefaults() {
const quality = 'GOOD' as Quality;
const timestamp = Date.now() as unknown as AggregatedValue['timestamp'];
const defaults = { quality, timestamp };

return defaults;
}
}

type PartialAssetPropertyValue = Partial<Omit<AssetPropertyValue, 'value'>>;

export class AssetPropertyValueFactory {
public createIntegerAssetPropertyValue(
partialAssetPropertyValue: PartialAssetPropertyValue = {}
): AssetPropertyValue {
const assetPropertyValue = {
...this.#createDefaults(),
...partialAssetPropertyValue,
value: {
integerValue: faker.number.int({ min: 0, max: 100 }),
},
};

return assetPropertyValue;
}

public createDoubleAssetPropertyValue(
partialAssetPropertyValue: PartialAssetPropertyValue = {}
): AssetPropertyValue {
const assetPropertyValue = {
...this.#createDefaults(),
...partialAssetPropertyValue,
value: {
doubleValue: faker.number.float({ min: 0, max: 100, precision: 0.001 }),
},
};

return assetPropertyValue;
}

public createBooleanAssetPropertyValue(
partialAssetPropertyValue: PartialAssetPropertyValue = {}
): AssetPropertyValue {
const assetPropertyValue = {
...this.#createDefaults(),
...partialAssetPropertyValue,
value: {
booleanValue: faker.datatype.boolean(),
},
};

return assetPropertyValue;
}

public createStringAssetPropertyValue(
partialAssetPropertyValue: PartialAssetPropertyValue = {}
): AssetPropertyValue {
const assetPropertyValue = {
...this.#createDefaults(),
...partialAssetPropertyValue,
value: {
stringValue: faker.helpers.arrayElement(['ON', 'OFF']),
},
};

return assetPropertyValue;
}

#createDefaults() {
const quality = 'GOOD' as Quality;
const timestamp = { timeInSeconds: Math.floor(Date.now() / 1000) };
const defaults = { quality, timestamp };

return defaults;
}
}

export function batchGetAssetPropertyValueHandler() {
return http.post<
Record<string, string>,
BatchGetAssetPropertyValueRequest,
BatchGetAssetPropertyValueResponse,
string
>(BATCH_GET_ASSET_PROPERTY_VALUE_URL, async ({ request }) => {
const { entries = [] } = await request.json();

const factory = new AssetPropertyValueFactory();
const response = {
successEntries: entries.map(({ entryId }) => ({
entryId,
assetPropertyValue: factory.createDoubleAssetPropertyValue(),
})),
skippedEntries: [],
errorEntries: [],
nextToken: undefined,
} satisfies BatchGetAssetPropertyValueResponse;

await delay();
return HttpResponse.json(response, { status: 200 });
});
}

function parseTimeInSecondsDate(date: number | Date) {
// Parse date into Date object
if (typeof date === 'number') {
// date in type number are expressed in seconds
date = new Date(date * 1000);
}

return date;
}

export function batchGetAssetPropertyValueHistoryHandler() {
return http.post<
Record<string, string>,
BatchGetAssetPropertyValueHistoryRequest,
BatchGetAssetPropertyValueHistoryResponse,
string
>(BATCH_GET_ASSET_PROPERTY_VALUE_HISTORY_URL, async ({ request }) => {
const { entries = [], maxResults = 20_000 } = await request.json();
const maxResultsPerEntry = Math.floor(maxResults / entries.length);

const factory = new AssetPropertyValueFactory();
const response: BatchGetAssetPropertyValueHistoryResponse = {
successEntries: entries.map(
({ entryId, startDate = 0, endDate = Date.now() / 1000 }) => {
// Parse startDate into common Date object
startDate = parseTimeInSecondsDate(startDate);
// Parse endDate into common Date object
endDate = parseTimeInSecondsDate(endDate);

return {
entryId,
assetPropertyValueHistory: faker.date
.betweens({
from: startDate,
to: endDate,
count: maxResultsPerEntry,
})
.map((date) => {
const assetPropertyValue =
factory.createDoubleAssetPropertyValue({
timestamp: {
timeInSeconds: Math.floor(date.getTime() / 1000),
offsetInNanos: Math.floor(date.getTime() % 1000),
},
});

return assetPropertyValue;
}),
};
}
),
skippedEntries: [],
errorEntries: [],
};

await delay();
return HttpResponse.json(response, { status: 200 });
});
}

export function batchGetAssetPropertyAggregatesHandler() {
return http.post<
Record<string, string>,
BatchGetAssetPropertyAggregatesRequest,
BatchGetAssetPropertyAggregatesResponse,
string
>(BATCH_GET_ASSET_PROPERTY_AGGREGATES_URL, async ({ request }) => {
const { entries = [], maxResults = 4000 } = await request.json();
const maxResultsPerEntry = Math.floor(maxResults / entries.length);
const factory = new AggregatedValueFactory();

const response = {
successEntries: entries.map(
({
entryId,
startDate = 0,
endDate = Date.now(),
aggregateTypes = ['AVERAGE'],
}) => {
return {
entryId,
aggregatedValues: faker.date
.betweens({
from: startDate,
to: endDate,
count: maxResultsPerEntry,
})
.map((date) => {
const aggregatedValue = factory.create(
aggregateTypes[0] as AggregateType,
{
timestamp:
date.getTime() as unknown as AggregatedValue['timestamp'],
}
);

return aggregatedValue;
}),
};
}
),
skippedEntries: [],
errorEntries: [],
};

await delay();
return HttpResponse.json(response, { status: 200 });
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { SITEWISE_DATA_PLANE_API_BASE_URL } from '../../constants';

export const BATCH_GET_ASSET_PROPERTY_VALUE_URL = `${SITEWISE_DATA_PLANE_API_BASE_URL}/properties/batch/latest`;
export const BATCH_GET_ASSET_PROPERTY_VALUE_HISTORY_URL = `${SITEWISE_DATA_PLANE_API_BASE_URL}/properties/batch/history`;
export const BATCH_GET_ASSET_PROPERTY_AGGREGATES_URL = `${SITEWISE_DATA_PLANE_API_BASE_URL}/properties/batch/aggregates`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { SITEWISE_CONTROL_PLANE_API_BASE_URL } from '../../constants';

export const DESCRIBE_ASSET_URL = `${SITEWISE_CONTROL_PLANE_API_BASE_URL}/assets/:assetId`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { delay, http, HttpResponse } from 'msw';

import { DESCRIBE_ASSET_URL } from './constants';
import { ASSET_HIERARCHY } from '../../resources/assets';

export function describeAssetHandler() {
return http.get(DESCRIBE_ASSET_URL, async ({ params }) => {
const { assetId } = params as { assetId: string };

const asset = ASSET_HIERARCHY.findAssetById(assetId);

if (!asset) {
return HttpResponse.json(null, { status: 404 });
}

await delay();
return HttpResponse.json(asset, { status: 200 });
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { http, HttpResponse } from 'msw';
import { DESCRIBE_ASSET_URL } from './constants';

export function failingDescribeAssetHandler() {
return http.get(DESCRIBE_ASSET_URL, () => {
return HttpResponse.json(null, { status: 500 });
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { SITEWISE_CONTROL_PLANE_API_BASE_URL } from '../../constants';

export const DESCRIBE_ASSET_MODEL_URL = `${SITEWISE_CONTROL_PLANE_API_BASE_URL}/asset-models/:assetModelId`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { delay, http, HttpResponse } from 'msw';

import { DESCRIBE_ASSET_MODEL_URL } from './constants';
import { ASSET_MODELS } from '../../resources/assetModels';

export function describeAssetModelHandler() {
return http.get(DESCRIBE_ASSET_MODEL_URL, async ({ params }) => {
const { assetModelId } = params as { assetModelId: string };

const assetModel = ASSET_MODELS.findByAssetModelId(assetModelId);

if (!assetModel) {
return HttpResponse.json(null, { status: 404 });
}

await delay();
return HttpResponse.json(assetModel, { status: 200 });
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { SITEWISE_CONTROL_PLANE_API_BASE_URL } from '../../constants';

export const LIST_ASSET_MODEL_PROPERTIES_URL = `${SITEWISE_CONTROL_PLANE_API_BASE_URL}/asset-models/:assetModelId/properties`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { delay, http, HttpResponse } from 'msw';
import { LIST_ASSET_MODEL_PROPERTIES_URL } from './constants';
import { ASSET_MODELS } from '../../resources/assetModels';
import { type ListAssetModelPropertiesResponse } from '@aws-sdk/client-iotsitewise';

export function listAssetModelPropertiesHandler() {
return http.get(LIST_ASSET_MODEL_PROPERTIES_URL, async ({ params }) => {
const { assetModelId } = params as { assetModelId: string };

const assetModel = ASSET_MODELS.findByAssetModelId(assetModelId);

if (!assetModel) {
return HttpResponse.json(null, { status: 404 });
}

const response: ListAssetModelPropertiesResponse = {
assetModelPropertySummaries: assetModel.assetModelProperties,
};

await delay();
return HttpResponse.json(response, { status: 200 });
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { SITEWISE_CONTROL_PLANE_API_BASE_URL } from '../../constants';

export const LIST_ASSET_MODELS_URL = `${SITEWISE_CONTROL_PLANE_API_BASE_URL}/asset-models`;
Loading

0 comments on commit cea67cc

Please sign in to comment.