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 API actions to prune payloads collection #49

Merged
merged 10 commits into from
Apr 2, 2021
55 changes: 55 additions & 0 deletions doc/1/controllers/device/clean/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
code: true
type: page
title: clean
description: Cleans the payload collection
---

# clean

Cleans payloads according to their age and/or validity and/or to which device model they are affiliated.

---

## Query Syntax

### HTTP

```http
URL: http://kuzzle:7512/_/device-manager/devices/_clean
Method: DELETE
```

### Other protocols

```js
{
"controller": "device-manager/device",
"action": "clean",
"body": {
//
}
}
```

### Kourou

```bash
kourou device-manager/device:clean '{
days: <days>,
valid: true|false,
deviceModel: "<deviceModel>"
}'
```

---

## Arguments

- `days`: Specify on which period of time you want keep payloads (`default: 7`).
Yoann-Abbes marked this conversation as resolved.
Show resolved Hide resolved
- `valid`: Specify if valid, invalid or both payload should be deleted (`default: true`).

Choose a reason for hiding this comment

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

How can we keep both valid and invalid payloads with a boolean option? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm it's a mistake, we can't

- `deviceModel`: deviceModel name.

## Response

Returns the number of deleted payloads.
36 changes: 36 additions & 0 deletions features/DeviceController.feature
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,39 @@ Feature: Device Manager device controller
| _id | "DummyTemp_attached-ayse-unlinked" |
Then I should receive an error matching:
| message | "Device \"DummyTemp_attached-ayse-unlinked\" is not linked to an asset" |


Yoann-Abbes marked this conversation as resolved.
Show resolved Hide resolved
Scenario: Clean payloads collection
Given I successfully execute the action "collection":"truncate" with args:
| index | "device-manager" |
| collection | "payloads" |
Then I successfully receive a "dummy-temp" payload with:
| deviceEUI | "12345" |
| register55 | 23.3 |
| batteryLevel | 0.8 |
And I successfully receive a "dummy-temp-position" payload with:
| deviceEUI | "12345" |
| register55 | 23.3 |
| location.lat | 42.2 |
| location.lon | 2.42 |
| location.accu | 2100 |
And I successfully execute the action "collection":"refresh" with args:
| index | "device-manager" |
| collection | "payloads" |
Then I successfully execute the action "document":"search" with args:
| index | "device-manager" |
| collection | "payloads" |
Then I should receive a result matching:
| total | 2 |
And I successfully execute the action "device-manager/device":"clean" with args:
| body.days | 0 |
| body.valid | true |
| body.deviceModel | "DummyTemp" |
And I successfully execute the action "collection":"refresh" with args:
| index | "device-manager" |
| collection | "payloads" |
Then I successfully execute the action "document":"search" with args:
| index | "device-manager" |
| collection | "payloads" |
Then I should receive a result matching:
| total | 1 |
2 changes: 1 addition & 1 deletion lib/controllers/CRUDController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class CRUDController extends NativeController {
*
* @param request
*/
async delete (request: KuzzleRequest) {
delete (request: KuzzleRequest) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should stay async since handler function should always return a promise.

Here if anything goes wrong the method will throw an exception and not return a rejected promise

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why only delete ? The others are not async

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm they all should be async then

const index = this.getIndex(request);
const id = this.getId(request);

Expand Down
30 changes: 30 additions & 0 deletions lib/controllers/DeviceController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export class DeviceController extends CRUDController {
handler: this.unlink.bind(this),
http: [{ verb: 'delete', path: 'device-manager/:index/devices/:_id/_unlink' }]
},
clean: {
handler: this.clean.bind(this),
http: [{ verb: 'delete', path: 'device-manager/devices/_clean' }]
},
}
};
}
Expand Down Expand Up @@ -122,6 +126,32 @@ export class DeviceController extends CRUDController {
await this.deviceService.unlink(device);
}

/**
* Clean payload collection for a time period
*/
async clean (request: KuzzleRequest) {
const body = this.getBody(request);

const date = new Date().setDate(new Date().getDate() - body.days || 7);
const filter = []
filter.push({
range: {
'_kuzzle_info.createdAt': {
lt: date
}
}
}, { term: { valid: body.valid || true } });

if (body.deviceModel) {
filter.push({ term: { deviceModel: body.deviceModel } });
}

return await this.as(request.context.user).bulk.deleteByQuery(
this.config.adminIndex,
'payloads',
{ query: { bool: { filter } } });
}

private async getDevice (deviceId: string): Promise<Device> {
const document: any = await this.sdk.document.get(
this.config.adminIndex,
Expand Down