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

Add device:mUnlink API action #60

Merged
merged 5 commits into from
Apr 7, 2021
Merged

Add device:mUnlink API action #60

merged 5 commits into from
Apr 7, 2021

Conversation

rolljee
Copy link
Contributor

@rolljee rolljee commented Apr 1, 2021

No description provided.

@rolljee rolljee self-assigned this Apr 1, 2021
@rolljee rolljee linked an issue Apr 1, 2021 that may be closed by this pull request

---

### Optional:

* `refresh`: if set to `wait_for`, Kuzzle will not respond until the documents are indexed
* `strict`: (boolean) if set, makes the process fail preemptively if at least one link cannot be applied (e.g. devices that aren't attached to a tenant, or because of non-existing assets)
Copy link
Contributor

@Yoann-Abbes Yoann-Abbes Apr 1, 2021

Choose a reason for hiding this comment

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

Should not be true by default?

Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO it's better not because some of the device can still be attached and you can correct the errors and then reimport the same document

@@ -18,7 +18,7 @@ The device document will be duplicated inside the tenant "devices" collection.
### HTTP

``` http
URL: http://kuzzle:7512/_/device-manager/devices/_mAttach
URL: http://kuzzle:7512/_/device-manager/devices/_mAttach[?refresh=wait_for][&strict=true|false]
Copy link
Contributor

Choose a reason for hiding this comment

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

Boolean flag are true if set and false if not set, they can't have explicit value


---

### Optional:

* `refresh`: if set to `wait_for`, Kuzzle will not respond until the documents are indexed
* `strict`: (boolean) if set, makes the process fail preemptively if at least one link cannot be applied (e.g. devices that aren't attached to a tenant, or because of non-existing assets)
Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO it's better not because some of the device can still be attached and you can correct the errors and then reimport the same document

for (const device of devicesContent) {
// Build Kuzzle mQueries
deviceDocuments.push({ _id: device._id, body: { assetId: null } });
assetDocuments.push({ _id: device._source.assetId, body: { measures: null } });
Copy link
Contributor

Choose a reason for hiding this comment

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

An asset can have multiple linked device and thus measure from those devices. Here you are reseting every measures.

private async eraseAssetMeasure (tenantId: string, device: Device) {
const asset = await this.sdk.document.get(tenantId, 'assets', device._source.assetId);

const measure = {};
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpicking/naming: this object represents one or more measures


const measure = {};

for (const [key, value] of Object.entries(device._source.measures)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpicking/naming: key and value represents the measureName and the measure itself

@@ -190,7 +190,7 @@ Feature: Device Manager device controller
Then The document "tenant-ayse":"devices":"DummyTemp_attached-ayse-unlinked" content match:
| assetId | null |
And The document "tenant-ayse":"assets":"PERFO-unlinked" content match:
| measures | null |
| measures | {} |
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we have a test with an asset who have the measures of two different devices to ensure the feature is working as expected ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes ofc

@@ -236,6 +236,7 @@ export class DeviceService {
}

async mUnlink (devices: Device[], { strict }) {
console.log('devices', devices);
Copy link
Contributor

Choose a reason for hiding this comment

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

Debug leftover

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, i was somehow sure i deleted them ... 👎

@@ -294,12 +295,16 @@ export class DeviceService {
const updatedAssets = await this.writeToDatabase(
assetDocuments,
async (assetDocuments: DeviceMRequestContent[]): Promise<JSONObject> => {

console.log(JSON.stringify(assetDocuments));
Copy link
Contributor

Choose a reason for hiding this comment

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

DItto


const updated = await this.sdk.document.mUpdate(
document.tenantId,
'assets',
assetDocuments);

console.log(updated);
Copy link
Contributor

Choose a reason for hiding this comment

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

DItto

measure[key] = value;
for (const [measureName] of Object.entries(device._source.measures)) {
if (measures[measureName]) {
delete measures[measureName];
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid the usage of delete who always trigger deoptimization from turbofan because the object internal shape is changing.
You can set the value to undefined, it will be ignored by the JSON parsers

Copy link
Contributor

@Aschen Aschen left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@Aschen Aschen merged commit 6ae0fab into 1-dev Apr 7, 2021
@Aschen Aschen deleted the feat/add-m-unlink branch April 7, 2021 11:55
@Aschen Aschen changed the title add mUnlink feature tests and documentation Add device:mUnlink API action Apr 7, 2021
@Yoann-Abbes Yoann-Abbes mentioned this pull request May 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add bulk API actions
3 participants