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(softTenants): add softTenant ids to assets measures documents #383

Merged
merged 6 commits into from
Dec 5, 2024
Merged
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
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"javascript.preferences.quoteStyle": "double",
"exportall.config.quote": "\"",
"exportall.config.relExclusion": [
"/lib/modules/shared/exports.ts",
"/lib/modules/shared/types/exports.ts"
]
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it normal for this file to be there?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, It's useful to share basic VSCode settings.
Especially on certain repositories like this one which still have a specific configuration like double quote style instead of simple quote.

Maybe we should rationalize these types of configurations between project but that need a huge PR to reformat all code, to accepted style. So for the moment IMHO, it's the better solution to share these settings for contributors

4 changes: 2 additions & 2 deletions doc/2/controllers/assets/upsert/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ The Upsert operation allows you to create a new asset or update an existing one
### HTTP

```http
URL: http://kuzzle:7512/_/device-manager/:engineId/assets/:_id
Method: POST
URL: http://kuzzle:7512/_/device-manager/:engineId/assets
Method: PUT
```

## Other protocols
Expand Down
4 changes: 2 additions & 2 deletions doc/2/controllers/devices/upsert/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ The Upsert operation allows you to create a new device or update an existing one
### HTTP

```http
URL: http://kuzzle:7512/_/device-manager/:engineId/devices/:_id
Method: POST
URL: http://kuzzle:7512/_/device-manager/:engineId/devices
Method: PUT
```

## Other protocols
Expand Down
1 change: 1 addition & 0 deletions lib/modules/asset/AssetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ export class AssetService extends DigitalTwinService {
metadata: { ...assetMetadata, ...metadata },
model,
reference,
softTenant: [],
},
},
{
Expand Down
4 changes: 1 addition & 3 deletions lib/modules/asset/AssetsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ export class AssetsController {
},
upsert: {
handler: this.upsert.bind(this),
http: [
{ path: "device-manager/:engineId/assets/:_id", verb: "post" },
],
http: [{ path: "device-manager/:engineId/assets", verb: "put" }],
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a breaking API change, this should be marked as such in the commits and lead to a semver-major release.

},
delete: {
handler: this.delete.bind(this),
Expand Down
5 changes: 4 additions & 1 deletion lib/modules/asset/collections/assetsMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export const assetsMappings: CollectionMappings = {
date: { type: "date" },
},
},

softTenant: {
Copy link
Contributor

Choose a reason for hiding this comment

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

So there could be only 1 softTenant?

Copy link
Member

Choose a reason for hiding this comment

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

No, but it's currently implemented like that and change this need a migration of database so it's breaking if we change to pluralize now

Copy link
Contributor

Choose a reason for hiding this comment

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

Elasticsearch doesn't have array types in mappings, an object and an array of objects will have the exact same mapping definition.

type: "keyword",
fields: { text: { type: "text" } },
},
metadata: {
properties: {
// populated with asset models
Expand Down
1 change: 1 addition & 0 deletions lib/modules/asset/model/AssetSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class AssetSerializer {
metadata: asset._source.metadata,
model: asset._source.model,
reference: asset._source.reference,
softTenant: asset._source.softTenant,
};
}
}
8 changes: 6 additions & 2 deletions lib/modules/asset/types/AssetApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ interface AssetsControllerRequest {
controller: AssetsControllerName;

engineId: string;

/**
* ? Request parameter used by SoftTenant module
* ! Not used directly in this plugin
*/
softTenantId?: string | string[] | null;
}

export interface ApiAssetGetRequest extends AssetsControllerRequest {
Expand Down Expand Up @@ -61,8 +67,6 @@ export type ApiAssetMetadataReplaceResult = KDocument<AssetContent>;
export interface ApiAssetUpsertRequest extends AssetsControllerRequest {
action: "upsert";

_id: string;

refresh?: string;

body: {
Expand Down
7 changes: 6 additions & 1 deletion lib/modules/asset/types/AssetContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export interface AssetContent<
id: string;
date: number;
}>;
/**
* Id's of soft tenants
* ! Not used directly in this plugin
*/
softTenant?: Array<string>;
}

/**
Expand All @@ -55,5 +60,5 @@ export type AssetMeasureContext<TMetadata extends Metadata = Metadata> = {
measureName: string;
} & Pick<
AssetContent<JSONObject, TMetadata>,
"model" | "reference" | "metadata" | "groups"
"model" | "reference" | "metadata" | "groups" | "softTenant"
>;
2 changes: 0 additions & 2 deletions lib/modules/decoder/types/DecoderApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ interface DecodersControllerRequest {

export interface ApiDecoderListRequest extends DecodersControllerRequest {
action: "list";

_id: string;
}
export type ApiDecoderListResult = {
decoders: DecoderContent[];
Expand Down
4 changes: 1 addition & 3 deletions lib/modules/device/DevicesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ export class DevicesController {
},
upsert: {
handler: this.upsert.bind(this),
http: [
{ path: "device-manager/:engineId/devices/:_id", verb: "post" },
],
http: [{ path: "device-manager/:engineId/devices", verb: "put" }],
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a breaking API change, this should be marked as such in the commits and lead to a semver-major release.

},
replaceMetadata: {
handler: this.replaceMetadata.bind(this),
Expand Down
2 changes: 0 additions & 2 deletions lib/modules/device/types/DeviceApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ export type ApiDeviceMetadataReplaceResult = KDocument<DeviceContent>;
export interface ApiDeviceUpsertRequest extends DevicesControllerRequest {
action: "upsert";

_id: string;

refresh?: string;

body: {
Expand Down
4 changes: 4 additions & 0 deletions lib/modules/measure/collections/measuresMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export const measuresMappings = {
date: { type: "date" },
},
},
softTenant: {
type: "keyword",
fields: { text: { type: "text" } },
},
},
},

Expand Down
1 change: 1 addition & 0 deletions lib/modules/shared/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./SchemaValidationError";
6 changes: 1 addition & 5 deletions lib/modules/shared/exports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
export * from "./types/DigitalTwinContent";
export * from "./types/DigitalTwinEvents";
export * from "./types/DigitalTwinMeasures";
export * from "./types/Metadata";
export * from "./types/EmbeddedMeasure";
export * from "./types/exports";
14 changes: 3 additions & 11 deletions lib/modules/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
export * from "./Module";
export * from "./errors";
export * from "./services";
export * from "./types/DigitalTwinApi";
export * from "./types/DigitalTwinContent";
export * from "./types/DigitalTwinEvents";
export * from "./types/DigitalTwinMeasures";
export * from "./types/EmbeddedMeasure";
export * from "./types/KuzzleRole";
export * from "./types/Metadata";
export * from "./utils/flattenObject";
export * from "./utils/keepStack";
export * from "./utils/lock";
export * from "./utils/objectDiff";
export * from "./types";
export * from "./utils";
17 changes: 17 additions & 0 deletions lib/modules/shared/types/Models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { MeasureDefinition } from "../../measure";
import { AssetModelDefinition, DeviceModelDefinition } from "../../model";

interface ModelDefinition<Definition> {
/**
* Name of model
*/
modelName: string;
/**
* Model definition used by the plugin to create associated ressources
* and updates mappings.
*/
definition: Definition;
}
export type AssetModel = ModelDefinition<AssetModelDefinition>;
export type DeviceModel = ModelDefinition<DeviceModelDefinition>;
export type MeasureModel = ModelDefinition<MeasureDefinition>;
6 changes: 6 additions & 0 deletions lib/modules/shared/types/exports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from "./DigitalTwinContent";
export * from "./DigitalTwinEvents";
export * from "./DigitalTwinMeasures";
export * from "./EmbeddedMeasure";
export * from "./Metadata";
export * from "./Models";
9 changes: 9 additions & 0 deletions lib/modules/shared/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export * from "./DigitalTwinApi";
export * from "./DigitalTwinContent";
export * from "./DigitalTwinEvents";
export * from "./DigitalTwinMeasures";
export * from "./EmbeddedMeasure";
export * from "./KuzzleRole";
export * from "./Metadata";
export * from "./Models";
export * from "./exports";
5 changes: 5 additions & 0 deletions lib/modules/shared/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./AJValidator";
export * from "./flattenObject";
export * from "./keepStack";
export * from "./lock";
export * from "./objectDiff";
77 changes: 4 additions & 73 deletions tests/application/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,94 +3,25 @@ import util from "node:util";
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";
import { magicHouseAssetDefinition } from "./assets/MagicHouse";
import { magiculeMeasureDefinition } from "./measures/Magicule";
import { registerModels } from "./models";

const app = new Backend("kuzzle");

const deviceManager = new DeviceManagerPlugin();

//? Add custom mapping properties
deviceManager.config.engineCollections.asset.mappings.properties["softTenant"] = {
deviceManager.config.engineCollections.asset.mappings.properties["custom"] = {
type: "keyword",
fields: { text: { type: "text" } },
};
deviceManager.config.engineCollections.device.mappings.properties["softTenant"] = {
deviceManager.config.engineCollections.device.mappings.properties["custom"] = {
type: "keyword",
fields: { text: { type: "text" } },
};


deviceManager.models.registerDevice("DummyTempPosition",
{
decoder: new DummyTempPositionDecoder(),
metadataMappings: {
serial: { type: "keyword" },
},
}
);

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

// Register assets for the "commons" group
deviceManager.models.registerAsset(
"commons",
"Container",
containerAssetDefinition
);

deviceManager.models.registerAsset(
"commons",
"Warehouse",
warehouseAssetDefinition
);

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

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

deviceManager.models.registerAsset(
"commons",
"MagicHouse",
magicHouseAssetDefinition
)

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

registerModels(deviceManager);
registerTestPipes(app);

app.plugin.use(deviceManager);
Expand Down
Loading