Skip to content

Commit

Permalink
fix: only list asset models for the requested engine group (#367)
Browse files Browse the repository at this point in the history
* fix: only list asset models for the requested engine group

* test: ensure asset model definition parity with the IoT Platform
  • Loading branch information
Kuruyia authored Aug 30, 2024
1 parent 5310ad1 commit 8a14207
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 62 deletions.
15 changes: 11 additions & 4 deletions lib/modules/model/ModelService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,17 @@ export class ModelService extends BaseService {
): Promise<SearchResult<KHit<AssetModelContent>>> {
const query = {
bool: {
must: [searchParams.searchBody.query, { match: { type: "asset" } }],
should: [
{ match: { engineGroup } },
{ match: { engineGroup: "commons" } },
must: [
searchParams.searchBody.query,
{ match: { type: "asset" } },
{
bool: {
should: [
{ match: { engineGroup } },
{ match: { engineGroup: "commons" } },
],
},
},
],
},
};
Expand Down
32 changes: 27 additions & 5 deletions tests/application/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import { Backend, KuzzleRequest } from "kuzzle";
import { DeviceManagerPlugin } from "../../index";

import { containerAssetDefinition } from "./assets/Container";
import { roomAssetDefinition } from "./assets/Room";
import { streetLampAssetDefinition } from "./assets/StreetLamp";
import { warehouseAssetDefinition } from "./assets/Warehouse";
import { DummyTempDecoder, DummyTempPositionDecoder } from "./decoders";
import { TestsController } from "./tests/controller";
import { registerTestPipes } from "./tests/pipes";
import { accelerationMeasureDefinition } from "./measures/AccelerationMeasure";
import { brightnessMeasureDefinition } from "./measures/BrightnessMeasure";
import { co2MeasureDefinition } from "./measures/CO2Measure";
import { illuminanceMeasureDefinition } from "./measures/IlluminanceMeasure";
import { powerConsumptionMeasureDefinition } from "./measures/PowerConsumptionMeasure";

const app = new Backend("kuzzle");

Expand All @@ -26,7 +32,7 @@ deviceManager.config.engineCollections.device.mappings.properties["softTenant"]
};


deviceManager.models.registerDevice("DummyTempPosition",
deviceManager.models.registerDevice("DummyTempPosition",
{
decoder: new DummyTempPositionDecoder(),
metadataMappings: {
Expand All @@ -35,17 +41,16 @@ deviceManager.models.registerDevice("DummyTempPosition",
}
);

deviceManager.models.registerDevice("DummyTemp",
{
deviceManager.models.registerDevice("DummyTemp",
{
decoder: new DummyTempDecoder(),
metadataMappings: {
color: { type: "keyword" },
},
}
);

// Register an asset for the "commons" group

// Register assets for the "commons" group
deviceManager.models.registerAsset(
"commons",
"Container",
Expand All @@ -58,7 +63,24 @@ deviceManager.models.registerAsset(
warehouseAssetDefinition
);

// Register assets for specialized groups
deviceManager.models.registerAsset(
"air_quality",
"Room",
roomAssetDefinition
);

deviceManager.models.registerAsset(
"public_lighting",
"StreetLamp",
streetLampAssetDefinition
);

deviceManager.models.registerMeasure("acceleration", accelerationMeasureDefinition);
deviceManager.models.registerMeasure("brightness", brightnessMeasureDefinition);
deviceManager.models.registerMeasure("co2", co2MeasureDefinition);
deviceManager.models.registerMeasure("illuminance", illuminanceMeasureDefinition);
deviceManager.models.registerMeasure("powerConsumption", powerConsumptionMeasureDefinition);

registerTestPipes(app);

Expand Down
26 changes: 26 additions & 0 deletions tests/application/assets/Room.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { AssetModelDefinition } from "../../../index";

export const roomAssetDefinition: AssetModelDefinition = {
measures: [
{
name: "temperature",
type: "temperature",
},
{
name: "humidity",
type: "humidity",
},
{
name: "co2",
type: "co2",
},
{
name: "illuminance",
type: "illuminance",
},
],
metadataMappings: {
floor: { type: "integer" },
width: { type: "integer" },
},
};
18 changes: 18 additions & 0 deletions tests/application/assets/StreetLamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { AssetModelDefinition } from "../../../index";

export const streetLampAssetDefinition: AssetModelDefinition = {
measures: [
{
name: "brightness",
type: "brightness",
},
{
name: "powerConsumption",
type: "powerConsumption",
},
],
metadataMappings: {
position: { type: "geo_point" },
street: { type: "keyword" },
},
};
5 changes: 5 additions & 0 deletions tests/application/measures/BrightnessMeasure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { MeasureDefinition } from "lib/modules/measure";

export const brightnessMeasureDefinition: MeasureDefinition = {
valuesMappings: { lumens: { type: "float" } },
};
5 changes: 5 additions & 0 deletions tests/application/measures/CO2Measure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { MeasureDefinition } from "lib/modules/measure";

export const co2MeasureDefinition: MeasureDefinition = {
valuesMappings: { co2: { type: "float" } },
};
5 changes: 5 additions & 0 deletions tests/application/measures/IlluminanceMeasure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { MeasureDefinition } from "lib/modules/measure";

export const illuminanceMeasureDefinition: MeasureDefinition = {
valuesMappings: { illuminance: { type: "float" } },
};
5 changes: 5 additions & 0 deletions tests/application/measures/PowerConsumptionMeasure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { MeasureDefinition } from "lib/modules/measure";

export const powerConsumptionMeasureDefinition: MeasureDefinition = {
valuesMappings: { watt: { type: "float" } },
};
35 changes: 21 additions & 14 deletions tests/hooks/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,29 @@ export async function truncateCollection(

async function deleteModels(sdk: Kuzzle) {
await sdk.collection.refresh("device-manager", "models");
await sdk.document.deleteByQuery("device-manager", "models", {
query: {
ids: {
values: [
"model-measure-presence",
"model-asset-plane",
"model-asset-AdvancedPlane",
"model-device-Zigbee",
"model-device-Bluetooth",
"model-device-Enginko",
"model-asset-TestHouse",
"model-asset-AdvancedWarehouse",
],
await sdk.document.deleteByQuery(
"device-manager",
"models",
{
query: {
ids: {
values: [
"model-measure-presence",
"model-asset-Plane",
"model-asset-AdvancedPlane",
"model-device-Zigbee",
"model-device-Bluetooth",
"model-device-Enginko",
"model-asset-TestHouse",
"model-asset-AdvancedWarehouse",
],
},
},
},
});
{
refresh: "wait_for",
},
);
}

export async function beforeEachTruncateCollections(sdk: Kuzzle) {
Expand Down
36 changes: 27 additions & 9 deletions tests/scenario/modules/assets/action-export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ function getExportedColums(row) {
return {
model: parsedRow[0],
reference: parsedRow[1],
position: parsedRow[2],
positionAccuracy: parsedRow[3],
positionAltitude: parsedRow[4],
temperatureExt: parsedRow[5],
temperatureInt: parsedRow[6],
temperatureWeather: parsedRow[7],
lastMeasuredAt: parsedRow[8],
lastMeasuredAtISO: parsedRow[9],
brightnessLumens: parsedRow[2],
co2: parsedRow[3],
humidity: parsedRow[4],
illuminance: parsedRow[5],
position: parsedRow[6],
positionAccuracy: parsedRow[7],
positionAltitude: parsedRow[8],
powerConsumptionWatt: parsedRow[9],
temperature: parsedRow[10],
temperatureExt: parsedRow[11],
temperatureInt: parsedRow[12],
temperatureWeather: parsedRow[13],
lastMeasuredAt: parsedRow[14],
lastMeasuredAtISO: parsedRow[15],
};
}

Expand Down Expand Up @@ -79,7 +85,7 @@ describe("AssetsController:exportMeasures", () => {
writeFileSync("./assets.csv", csv.join(""));

expect(csv[0]).toBe(
"Model,Reference,position,position.accuracy,position.altitude,temperatureExt,temperatureInt,temperatureWeather,lastMeasuredAt,lastMeasuredAtISO\n",
"Model,Reference,brightness.lumens,co2,humidity,illuminance,position,position.accuracy,position.altitude,powerConsumption.watt,temperature,temperatureExt,temperatureInt,temperatureWeather,lastMeasuredAt,lastMeasuredAtISO\n",
);

expect(csv).toHaveLength(assetCount + 1);
Expand All @@ -88,9 +94,15 @@ describe("AssetsController:exportMeasures", () => {

expect(row1.model).toBe("Container");
expect(typeof row1.reference).toBe("string");
expect(typeof parseFloat(row1.brightnessLumens)).toBe("number");
expect(typeof parseFloat(row1.co2)).toBe("number");
expect(typeof parseFloat(row1.humidity)).toBe("number");
expect(typeof parseFloat(row1.illuminance)).toBe("number");
expect(typeof row1.position).toBe("string");
expect(typeof parseFloat(row1.positionAccuracy)).toBe("number");
expect(typeof parseFloat(row1.positionAltitude)).toBe("number");
expect(typeof parseFloat(row1.powerConsumptionWatt)).toBe("number");
expect(typeof parseFloat(row1.temperature)).toBe("number");
expect(typeof parseFloat(row1.temperatureExt)).toBe("number");
expect(typeof parseFloat(row1.temperatureInt)).toBe("number");
expect(typeof parseFloat(row1.temperatureWeather)).toBe("number");
Expand All @@ -101,9 +113,15 @@ describe("AssetsController:exportMeasures", () => {

expect(row2.model).toBe("Warehouse");
expect(typeof row2.reference).toBe("string");
expect(typeof parseFloat(row2.brightnessLumens)).toBe("number");
expect(typeof parseFloat(row2.co2)).toBe("number");
expect(typeof parseFloat(row2.humidity)).toBe("number");
expect(typeof parseFloat(row2.illuminance)).toBe("number");
expect(typeof row2.position).toBe("string");
expect(typeof parseFloat(row2.positionAccuracy)).toBe("number");
expect(typeof parseFloat(row2.positionAltitude)).toBe("number");
expect(typeof parseFloat(row2.powerConsumptionWatt)).toBe("number");
expect(typeof parseFloat(row2.temperature)).toBe("number");
expect(typeof parseFloat(row2.temperatureExt)).toBe("number");
expect(typeof parseFloat(row2.temperatureInt)).toBe("number");
expect(typeof parseFloat(row2.temperatureWeather)).toBe("number");
Expand Down
Loading

0 comments on commit 8a14207

Please sign in to comment.