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(deviceManagerEngine): free devices after engine deletion #382

Open
wants to merge 13 commits into
base: 2-dev
Choose a base branch
from
Open
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
17 changes: 2 additions & 15 deletions lib/modules/asset/AssetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,26 +144,13 @@ export class AssetService extends DigitalTwinService {
request: KuzzleRequest,
): Promise<KDocument<AssetContent>> {
const asset = await this.get(engineId, assetId, request);
const unknownMetadata = {};

for (const key in metadata) {
if (key in asset._source.metadata) {
asset._source.metadata[key] = metadata[key];
} else {
unknownMetadata[key] = metadata[key];
}
}
// ? If metadata key is unknown on the asset we check that it exists in the assetModel mappings
if (Object.keys(unknownMetadata).length > 0) {
const assetModel = await ask<AskModelAssetGet>(
"ask:device-manager:model:asset:get",
{ engineGroup: engineId.split("-")[1], model: asset._source.model },
);
for (const key in unknownMetadata) {
if (key in assetModel.asset.metadataMappings) {
asset._source.metadata[key] = unknownMetadata[key];
}
}
}

const updatedPayload = await this.app.trigger<EventAssetUpdateBefore>(
"device-manager:asset:update:before",
{ asset, metadata },
Expand Down
39 changes: 37 additions & 2 deletions lib/modules/plugin/DeviceManagerEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,14 @@ export class DeviceManagerEngine extends AbstractEngine<DeviceManagerPlugin> {
InternalCollection.DEVICES,
InternalCollection.MEASURES,
];

await Promise.all(
collections.map(async (collection) => {
if (await this.sdk.collection.exists(index, collection)) {
await this.sdk.collection.delete(index, collection);
}
}),
);

await this.detachDevicesFromAdminIndex(index);
return {
collections,
};
Expand Down Expand Up @@ -654,4 +653,40 @@ export class DeviceManagerEngine extends AbstractEngine<DeviceManagerPlugin> {

return result.hits.map((elt) => elt._source);
}

/**
* Detach all devices of an index in the admin index
*
* @param engineId The target engine Id
* @returns {any}
*/
private async detachDevicesFromAdminIndex(engineId: string) {
Copy link
Member

Choose a reason for hiding this comment

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

I feel like this method should be named detachDevicesFromTenantIndex

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think tenantIndex could lead to confusion as this functions is related only to the 'platform' index which is referred as adminIndex in KDM.

const devices = [];

let result = await this.sdk.document.search(
this.adminIndex,
"devices",
{
_source: false,
query: { bool: { must: { term: { engineId } } } },
},
{
scroll: "2s",
size: 100,
},
);
while (result !== null) {
devices.push(...result.hits);
result = await result.next();
}
if (devices.length > 0) {
void this.sdk.document.mUpdate(
this.adminIndex,
"devices",
devices.map((device) => {
return { _id: device._id, body: { assetId: null, engineId: null } };
}),
);
}
}
}
69 changes: 69 additions & 0 deletions tests/scenario/modules/engine/engine-deletion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { beforeAllCreateEngines } from "../../../hooks/engines";
import { beforeEachLoadFixtures } from "../../../hooks/fixtures";

import { useSdk } from "../../../helpers";

jest.setTimeout(10000);

describe("Engine deletion", () => {
const sdk = useSdk();

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

beforeEach(async () => {
await beforeAllCreateEngines(sdk);
await beforeEachLoadFixtures(sdk);
});

afterAll(async () => {
sdk.disconnect();
});
const adminIndex = "device-manager";
const engineId = "engine-ayse";

it("Deletes the engine from admin index", async () => {
const engine = await sdk.document.get(
adminIndex,
"config",
`engine-device-manager--${engineId}`,
);

expect(engine).toBeTruthy();

await sdk.query({
controller: "device-manager/engine",
action: "delete",
index: engineId,
});

const promise = sdk.document.get(
adminIndex,
"config",
`engine-device-manager--${engineId}`,
);

await expect(promise).rejects.toThrow();
});
it("Detach devices from engine in the admin index on engine deletion", async () => {
const devices = await sdk.document.search(adminIndex, "devices", {
_source: false,
query: { bool: { must: { term: { engineId } } } },
});
expect(devices.total).toBeGreaterThan(0);

await sdk.query({
controller: "device-manager/engine",
action: "delete",
index: engineId,
});

const result = await sdk.document.search(adminIndex, "devices", {
_source: false,
query: { bool: { must: { term: { engineId } } } },
});
expect(result.total).toBe(0);
});
});