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(digitalTwin): allow to modify mapping from application #322

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion lib/modules/device/collections/deviceMappings.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { CollectionMappings } from "kuzzle";

/**
* Base mappings for the "devices" collection.
*
* Those mappings does not contains the `measures` and `metadata` mappings.
*/
export const devicesMappings = {
export const devicesMappings: CollectionMappings = {
dynamic: "strict",
properties: {
model: {
Expand Down
20 changes: 8 additions & 12 deletions lib/modules/plugin/DeviceManagerEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,20 @@ import { JSONObject } from "kuzzle-sdk";
import { AbstractEngine, ConfigManager } from "kuzzle-plugin-commons";
import { EngineContent } from "kuzzle-plugin-commons";

import {
assetsMappings,
assetsHistoryMappings,
assetGroupsMappings,
} from "../asset";
import { assetsHistoryMappings, assetGroupsMappings } from "../asset";
import {
AssetModelContent,
DeviceModelContent,
MeasureModelContent,
} from "../model";
import { getEmbeddedMeasureMappings, measuresMappings } from "../measure";
import { devicesMappings } from "../device";
import { onAsk } from "../shared";
import { NamedMeasures } from "../decoder";

import { DeviceManagerConfiguration } from "./types/DeviceManagerConfiguration";
import { DeviceManagerPlugin } from "./DeviceManagerPlugin";
import { InternalCollection } from "./types/InternalCollection";

const digitalTwinMappings = {
asset: assetsMappings,
device: devicesMappings,
} as const;

export type AskEngineList = {
name: "ask:device-manager:engine:list";

Expand Down Expand Up @@ -211,6 +201,12 @@ export class DeviceManagerEngine extends AbstractEngine<DeviceManagerPlugin> {
private async getDigitalTwinMappings<
TDigitalTwinModelContent extends AssetModelContent | DeviceModelContent
>(digitalTwinType: "asset" | "device", engineGroup?: string) {
if (
this.config.engineCollections[digitalTwinType] === undefined ||
this.config.engineCollections[digitalTwinType].mappings === undefined
) {
throw new InternalError(`Cannot find mapping for "${digitalTwinType}"`);
}
const models = await this.getModels<TDigitalTwinModelContent>(
this.config.adminIndex,
digitalTwinType,
Expand All @@ -223,7 +219,7 @@ export class DeviceManagerEngine extends AbstractEngine<DeviceManagerPlugin> {
);

const mappings = JSON.parse(
JSON.stringify(digitalTwinMappings[digitalTwinType])
JSON.stringify(this.config.engineCollections[digitalTwinType].mappings)
);

for (const model of models) {
Expand Down
10 changes: 9 additions & 1 deletion lib/modules/plugin/DeviceManagerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {

import { DeviceModule, devicesMappings } from "../device";
import { MeasureModule } from "../measure";
import { AssetModule } from "../asset";
import { AssetModule, assetsMappings } from "../asset";
import {
DecoderModule,
NamedMeasures,
Expand Down Expand Up @@ -217,6 +217,14 @@ export class DeviceManagerPlugin extends Plugin {
},
settings: {},
},
asset: {
name: InternalCollection.ASSETS,
mappings: assetsMappings,
},
device: {
name: InternalCollection.DEVICES,
mappings: devicesMappings,
},
},
};
/* eslint-enable sort-keys */
Expand Down
10 changes: 9 additions & 1 deletion lib/modules/plugin/types/DeviceManagerConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JSONObject } from "kuzzle-sdk";
import { CollectionMappings, JSONObject } from "kuzzle-sdk";

export type DeviceManagerConfiguration = {
/**
Expand Down Expand Up @@ -52,5 +52,13 @@ export type DeviceManagerConfiguration = {
mappings: JSONObject;
settings: JSONObject;
};
asset: {
name: string;
mappings: CollectionMappings;
};
device: {
name: string;
mappings: CollectionMappings;
};
};
};
11 changes: 11 additions & 0 deletions tests/application/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ const app = new Backend("kuzzle");

const deviceManager = new DeviceManagerPlugin();

//? Add custom mapping properties
deviceManager.config.engineCollections.asset.mappings.properties["softTenant"] = {
type: "keyword",
fields: { text: { type: "text" } },
sebtiz13 marked this conversation as resolved.
Show resolved Hide resolved
};
deviceManager.config.engineCollections.device.mappings.properties["softTenant"] = {
type: "keyword",
fields: { text: { type: "text" } },
sebtiz13 marked this conversation as resolved.
Show resolved Hide resolved
};


deviceManager.models.registerDevice("DummyTempPosition", {
decoder: new DummyTempPositionDecoder(),
metadataMappings: {
Expand Down
43 changes: 43 additions & 0 deletions tests/scenario/custom-mapping.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { JSONObject } from "kuzzle-sdk";
import { useSdk } from "../helpers";
import { beforeAllCreateEngines } from "../hooks/engines";

describe("Assets mapping", () => {
const sdk = useSdk();

beforeAll(async () => {
await sdk.connect();

await beforeAllCreateEngines(sdk);
});

afterAll(async () => {
sdk.disconnect();
});

it("asset mapping can be customize by application", async () => {
const mapping: JSONObject = await sdk.collection.getMapping(
"engine-ayse",
"assets"
);

expect(mapping.properties?.softTenant).toBeDefined();
expect(mapping.properties.softTenant).toMatchObject({
type: "keyword",
fields: { text: { type: "text" } },
});
});

it("device mapping can be customize by application", async () => {
const mapping: JSONObject = await sdk.collection.getMapping(
"engine-ayse",
"devices"
);

expect(mapping.properties?.softTenant).toBeDefined();
expect(mapping.properties.softTenant).toMatchObject({
type: "keyword",
fields: { text: { type: "text" } },
});
});
});
Loading