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

asset metadata and mesuredat mapping correction #226

Merged
merged 4 commits into from
Sep 29, 2022
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
9 changes: 5 additions & 4 deletions features/AssetController.feature
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ Feature: DeviceManager asset controller
| size | 3 |
Then I should receive a "measures" array of objects matching:
# there is 6 measures with the 3 from fixtures
| _source.origin.assetId | _source.origin.id |
| "container-FRIDGE-linked" | "DummyMultiTemp-attached_ayse_linked_1" |
| "container-FRIDGE-linked" | "DummyMultiTemp-attached_ayse_linked_1" |
| "container-FRIDGE-linked" | "DummyMultiTemp-attached_ayse_linked_1" |
| _source.asset._id | _source.origin.id | _source.asset._source.model |
| "container-FRIDGE-linked" | "DummyMultiTemp-attached_ayse_linked_1" | "FRIDGE" |
| "container-FRIDGE-linked" | "DummyMultiTemp-attached_ayse_linked_1" | "FRIDGE" |
| "container-FRIDGE-linked" | "DummyMultiTemp-attached_ayse_linked_1" | "FRIDGE" |


Scenario: Register a measures in the asset, an other with different name and an older one and delete one
When I successfully execute the action "device-manager/asset":"pushMeasures" with args:
Expand Down
12 changes: 6 additions & 6 deletions features/DeviceController/LinkAsset.feature
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ Feature: LinkAsset
| payloads[0].lvlBattery | 1 |
And I refresh the collection "engine-ayse":"assets"
Then The document "engine-ayse":"assets":"container-FRIDGE-unlinked_1" content match:
| measures[0].type | "temperature" |
| measures[0].values.temperature | 1 |
| measures[0].deviceMeasureName | "innerTemp" |
| measures[0].assetMeasureName | "coreInnerTemp" |
| measures[0].origin.assetId | "container-FRIDGE-unlinked_1" |
| measures[0].origin.id | "DummyMultiTemp-attached_ayse_unlinked_1" |
| measures[0].type | "temperature" |
| measures[0].values.temperature | 1 |
| measures[0].deviceMeasureName | "innerTemp" |
| measures[0].assetMeasureName | "coreInnerTemp" |
| measures[0].asset._id | "container-FRIDGE-unlinked_1" |
| measures[0].origin.id | "DummyMultiTemp-attached_ayse_unlinked_1" |

Scenario: Link device to an asset and enriching the asset with before event
When I successfully execute the action "device-manager/device":"linkAsset" with args:
Expand Down
14 changes: 7 additions & 7 deletions features/PayloadController.feature
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Feature: Payloads Controller
| index | "engine-kuzzle" |
| collection | "measures" |
And I should receive a "hits" array of objects matching:
| _source.type | _source.origin.id | _source.origin.assetId | _source.origin.type |
| _source.type | _source.origin.id | _source.asset._id | _source.origin.type |
| "temperature" | "DummyMultiTemp-detached" | "type1-model1-reference1" | "device" |
| "battery" | "DummyMultiTemp-detached" | "type1-model1-reference1" | "device" |

Expand Down Expand Up @@ -186,13 +186,13 @@ Feature: Payloads Controller
| payloads[0].lvlBattery | 0.8 |
And I refresh the collection "engine-ayse":"measures"
Then When I successfully execute the action "document":"search" with args:
| index | "engine-ayse" |
| collection | "measures" |
| body | { query: { term:{"origin.assetId":"container-FRIDGE-unlinked_1"}}} |
| index | "engine-ayse" |
| collection | "measures" |
| body | { query: { term:{"asset._id":"container-FRIDGE-unlinked_1"}}} |
And I should receive a "hits" array of objects matching:
| _source.type | _source.origin.id |
| "temperature" | "DummyMultiTemp-enrich_me_master+container-FRIDGE-unlinked_1" |
| "battery" | "DummyMultiTemp-enrich_me_master+container-FRIDGE-unlinked_1" |
| _source.type | _source.origin.id | _source.asset._source.model |
| "temperature" | "DummyMultiTemp-enrich_me_master+container-FRIDGE-unlinked_1" | "FRIDGE" |
| "battery" | "DummyMultiTemp-enrich_me_master+container-FRIDGE-unlinked_1" | "FRIDGE" |
Then The document "device-manager":"devices":"DummyMultiTemp-enrich_me_master" content match:
| measures[0].origin.id | "DummyMultiTemp-enrich_me_master+container-FRIDGE-unlinked_1" |
| measures[1].origin.id | "DummyMultiTemp-enrich_me_master+container-FRIDGE-unlinked_1" |
Expand Down
20 changes: 10 additions & 10 deletions features/fixtures/application/testPipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ function checkEventWithDocument (app: Backend, event: string) {

export function registerTestPipes (app: Backend) {
app.pipe.register(
'device-manager:measures:process:before',
async ({ asset, device, measures }: { asset: BaseAsset, device: Device, measures: MeasureContent[] }) => {
if (device._id !== 'DummyMultiTemp-enrich_me_master') {
return { asset, device, measures };
}
'device-manager:measures:process:before',
async ({ asset, device, measures }: { asset: BaseAsset, device: Device, measures: MeasureContent[] }) => {
if (device._id !== 'DummyMultiTemp-enrich_me_master') {
return { asset, device, measures };
}

for (const measure of measures) {
measure.origin.id += `+${asset?._id}`;
}
for (const measure of measures) {
measure.origin.id += `+${asset?._id}`;
}

return { asset, device, measures };
});
return { asset, device, measures };
});

checkEventWithDocument(app, 'device-manager:device:provisioning:before');
checkEventWithDocument(app, 'device-manager:device:provisioning:after');
Expand Down
3 changes: 2 additions & 1 deletion lib/DeviceManagerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ export class DeviceManagerPlugin extends Plugin {
this,
this.batchController,
this.assetService,
this.measuresRegister);
this.measuresRegister,
this.assetCategoryService);
this.payloadService = new PayloadService(
this,
this.batchController,
Expand Down
34 changes: 29 additions & 5 deletions lib/core-classes/MeasureService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import {
} from '../types';
import { AssetService } from './AssetService';
import { MeasuresRegister } from './registers/MeasuresRegister';
import { AssetCategoryService } from './AssetCategoryService';

export class MeasureService {
private config: DeviceManagerConfiguration;
private context: PluginContext;
private batch: BatchController;
private assetService: AssetService;
private measuresRegister: MeasuresRegister;

private assetCategoryService: AssetCategoryService;
private get sdk () {
return this.context.accessors.sdk;
}
Expand All @@ -42,8 +43,10 @@ export class MeasureService {
plugin: DeviceManagerPlugin,
batchController: BatchController,
assetService: AssetService,
measuresRegister: MeasuresRegister
measuresRegister: MeasuresRegister,
assetCategoryService: AssetCategoryService
) {
this.assetCategoryService = assetCategoryService;
this.config = plugin.config as any;
this.context = plugin.context;

Expand Down Expand Up @@ -176,13 +179,30 @@ export class MeasureService {
}
}
}

public getAssetInfos (asset :BaseAsset) {
if (asset === null) {
return undefined;
}
return {
_id: asset._id,
_source: {
category: asset._source.category,
metadata: asset._source.metadata,
model: asset._source.model,
reference: asset._source.reference,
type: asset._source.type
}
};
}

private buildMeasures (
device: Device,
asset: BaseAsset,
measurements: Measurement[],
payloadUuids: string[],
): MeasureContent[] {

const measures: MeasureContent[] = [];

for (const measurement of measurements) {
Expand All @@ -193,16 +213,20 @@ export class MeasureService {

const deviceMeasureName = measurement.deviceMeasureName || measurement.type;


const assetMeasureName = asset === null
? undefined
: this.findAssetMeasureName(device, asset, deviceMeasureName);


const assetInfos = this.getAssetInfos(asset);

const measureContent: MeasureContent = {
asset: assetInfos,
assetMeasureName,
deviceMeasureName,
measuredAt: measurement.measuredAt,
origin: {
assetId: asset?._id,
deviceModel: device._source.model,
id: device._id,
payloadUuids,
Expand Down Expand Up @@ -327,13 +351,13 @@ export class MeasureService {
const measurement = jsonMeasurement as AssetMeasurement;

const assetMeasureName = measurement.assetMeasureName ?? measurement.type;

const assetInfos = this.getAssetInfos(asset);
validMeasures.push({
asset: assetInfos,
assetMeasureName,
deviceMeasureName: null,
measuredAt: measurement.measuredAt || Date.now(),
origin: {
assetId,
id: kuid,
type: 'user',
},
Expand Down
23 changes: 18 additions & 5 deletions lib/mappings/measuresMappings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable sort-keys */

import { metadataValue } from './metadataMappings';

export const measuresMappings = {
dynamic: 'strict',
properties: {
Expand All @@ -23,7 +25,7 @@ export const measuresMappings = {
/**
* Micro Timestamp of the measurement time.
*/
measuredAt: { type: 'double' },
measuredAt: { type: 'date' },

/**
* Name given by the decoder to the measure.
Expand All @@ -42,7 +44,20 @@ export const measuresMappings = {
dynamic: 'false',
properties: {}
},

asset: {
properties: {
_id: { type: 'keyword' },
_source: {
properties: {
category: { type: 'keyword' },
metadata: metadataValue,
model: { type: 'keyword' },
reference: { type: 'keyword' },
type: { type: 'keyword' },
}
}
}
},

/**
* Define the origin of the measure.
Expand Down Expand Up @@ -71,11 +86,9 @@ export const measuresMappings = {
*/
id: { type: 'keyword' },

// Asset linked to the device when the measure was made
assetId: { type: 'keyword' },

}
},

}
};

Expand Down
20 changes: 16 additions & 4 deletions lib/types/measures/MeasureContent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { JSONObject, KDocumentContent } from 'kuzzle';

import { MeasureUnit } from './MeasureDefinition';
import { FormattedMetadata } from '../AssetCategoryContent';

/**
* Represents a measurement sent with a payload.
Expand Down Expand Up @@ -56,6 +57,20 @@ export interface MeasureContent<TMeasurementValues extends JSONObject = JSONObje
*/
unit: MeasureUnit;

/**
* Asset linked to the device when the measure was made
*/
asset?: {
_id: string,
_source: {
category: string,
metadata: FormattedMetadata[],
model: string,
reference: string,
type: string
}
}

/**
* Define the origin of the measure.
*/
Expand All @@ -82,9 +97,6 @@ export interface MeasureContent<TMeasurementValues extends JSONObject = JSONObje
*/
id?: string;

/**
* Asset ID linked to the device when the measure was made
*/
assetId?: string;

}
}