Skip to content

Commit edc5acd

Browse files
committed
Sources
- New Hours Of Service Violation - New Safety Event Actions - Find User Details
1 parent 00eb589 commit edc5acd

File tree

8 files changed

+235
-192
lines changed

8 files changed

+235
-192
lines changed

components/motive/motive.app.mjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,31 @@ export default {
9494
...opts,
9595
});
9696
},
97+
async *paginate({
98+
fn, params = {}, fieldName, maxResults = null, ...opts
99+
}) {
100+
let hasMore = false;
101+
let count = 0;
102+
let page = 0;
103+
104+
do {
105+
params.page = ++page;
106+
const data = await fn({
107+
params,
108+
...opts,
109+
});
110+
const items = data[fieldName];
111+
for (const d of items) {
112+
yield d;
113+
114+
if (maxResults && ++count === maxResults) {
115+
return count;
116+
}
117+
}
118+
119+
hasMore = items.length;
120+
121+
} while (hasMore);
122+
},
97123
},
98124
};
Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,63 @@
1-
import { v4 as uuid } from "uuid";
1+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
22
import motive from "../../motive.app.mjs";
33

44
export default {
55
props: {
66
motive,
7-
http: "$.interface.http",
87
db: "$.service.db",
8+
timer: {
9+
type: "$.interface.timer",
10+
default: {
11+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
12+
},
13+
},
914
},
1015
methods: {
11-
_getHookId() {
12-
return this.db.get("hookId");
16+
_getLastId() {
17+
return this.db.get("lastId") || 0;
1318
},
14-
_setHookId(hookId) {
15-
this.db.set("hookId", hookId);
19+
_setLastId(lastId) {
20+
this.db.set("lastId", lastId);
1621
},
17-
filterEvent() {
18-
return true;
22+
async emitEvent(maxResults = false) {
23+
const lastId = this._getLastId();
24+
const fieldName = this.getFieldName();
25+
26+
const response = this.motive.paginate({
27+
fn: this.getFunction(),
28+
fieldName: `${fieldName}s`,
29+
});
30+
31+
let responseArray = [];
32+
for await (const item of response) {
33+
const data = item[fieldName];
34+
if (data.id <= lastId) break;
35+
responseArray.push(data);
36+
}
37+
38+
if (responseArray.length) {
39+
if (maxResults && (responseArray.length > maxResults)) {
40+
responseArray.length = maxResults;
41+
}
42+
this._setLastId(responseArray[0].id);
43+
}
44+
45+
for (const item of responseArray.reverse()) {
46+
const data = item[fieldName];
47+
this.$emit(data, {
48+
id: data.id,
49+
summary: this.getSummary(data),
50+
ts: Date.parse(data.start_time || new Date()),
51+
});
52+
}
1953
},
2054
},
2155
hooks: {
2256
async deploy() {
23-
const response = await this.motive.createWebhook({
24-
data: {
25-
url: this.http.endpoint,
26-
secret: uuid(),
27-
actions: this.getActions(),
28-
format: "json",
29-
enabled: true,
30-
webhook_type: "POST",
31-
},
32-
});
33-
this._setHookId(response.company_webhook.id);
34-
},
35-
async activate() {
36-
await this.motive.updateWebhook({
37-
webhookId: this._getHookId(),
38-
data: {
39-
enabled: true,
40-
},
41-
});
42-
},
43-
async deactivate() {
44-
await this.motive.updateWebhook({
45-
webhookId: this._getHookId(),
46-
data: {
47-
enabled: false,
48-
},
49-
});
57+
await this.emitEvent(25);
5058
},
5159
},
52-
async run({ body }) {
53-
if (this.filterEvent(body)) {
54-
this.$emit(body, {
55-
id: body.id,
56-
summary: this.getSummary(body),
57-
ts: Date.parse(body.end_time || new Date()),
58-
});
59-
}
60+
async run() {
61+
await this.emitEvent();
6062
},
6163
};

components/motive/sources/new-hours-of-service-violation-instant/new-hours-of-service-violation-instant.mjs

Lines changed: 0 additions & 27 deletions
This file was deleted.

components/motive/sources/new-hours-of-service-violation-instant/test-event.mjs

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import common from "../common/base.mjs";
2+
import sampleEmit from "./test-event.mjs";
3+
4+
export default {
5+
...common,
6+
key: "motive-new-hours-of-service-violation",
7+
name: "New Hours of Service Violation",
8+
description: "Emit new event when a hos is emited.",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
methods: {
13+
...common.methods,
14+
getFunction() {
15+
return this.motive.listHosViolations;
16+
},
17+
getFieldName() {
18+
return "hos_violation";
19+
},
20+
getSummary(item) {
21+
return `New HOS Violation: ${item.id}`;
22+
},
23+
},
24+
sampleEmit,
25+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default {
2+
"id": 97,
3+
"type": "ca_duty_15",
4+
"start_time": "2016-03-12T19:00:00Z",
5+
"end_time": null,
6+
"name": "15 Hour On Duty Limit",
7+
"user": {
8+
"id": 156,
9+
"first_name": "Harold",
10+
"last_name": "Hoeger",
11+
"username": "hobart",
12+
"email": null,
13+
"driver_company_id": null,
14+
"status": "deactivated",
15+
"role": "driver"
16+
}
17+
}

components/motive/sources/new-safety-event-instant/new-safety-event-instant.mjs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ import sampleEmit from "./test-event.mjs";
33

44
export default {
55
...common,
6-
key: "motive-new-safety-event-instant",
7-
name: "New Safety Event (Instant)",
6+
key: "motive-new-safety-event",
7+
name: "New Safety Event",
88
description: "Emit new safety-related events like harsh braking or acceleration.",
99
version: "0.0.1",
1010
type: "source",
1111
dedupe: "unique",
1212
methods: {
1313
...common.methods,
14-
getActions() {
15-
return [
16-
"driver_performance_event_created",
17-
];
14+
getFunction() {
15+
return this.motive.listDriverPerformanceEvents;
16+
},
17+
getFieldName() {
18+
return "driver_performance_event";
1819
},
1920
getSummary(event) {
2021
return `New Safety Event: ${event.type}`;

0 commit comments

Comments
 (0)