Skip to content

Commit b7824ee

Browse files
committed
drimify init
1 parent 97e66e8 commit b7824ee

File tree

2 files changed

+152
-2
lines changed

2 files changed

+152
-2
lines changed

components/drimify/drimify.app.mjs

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,71 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "drimify",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
applicationId: {
8+
type: "string",
9+
label: "Application ID",
10+
description: "The ID of the application",
11+
},
12+
timeFrame: {
13+
type: "string",
14+
label: "Time Frame",
15+
description: "Optional time frame to specify data collection period (YYYY-MM-DDTHH:MM:SS)",
16+
optional: true,
17+
},
18+
},
519
methods: {
6-
// this.$auth contains connected account data
20+
_baseUrl() {
21+
return "https://endpoint.drimify.com/api";
22+
},
23+
async _makeRequest(opts = {}) {
24+
const {
25+
$ = this, method = "GET", path = "/", headers, ...otherOpts
26+
} = opts;
27+
return axios($, {
28+
...otherOpts,
29+
method,
30+
url: this._baseUrl() + path,
31+
headers: {
32+
...headers,
33+
"Authorization": `Bearer ${this.$auth.apiKey}`,
34+
},
35+
});
36+
},
37+
async listAppDataCollections({
38+
applicationId, page = 1, createdSince,
39+
} = {}) {
40+
const params = {
41+
app: applicationId,
42+
page,
43+
};
44+
if (createdSince) {
45+
params.createdSince = createdSince;
46+
}
47+
return this._makeRequest({
48+
path: "/appdatacollection",
49+
params,
50+
});
51+
},
52+
async paginate(fn, ...opts) {
53+
const results = [];
54+
let page = 1;
55+
while (true) {
56+
const response = await fn({
57+
...opts,
58+
page,
59+
});
60+
if (response.length === 0) break;
61+
results.push(...response);
62+
page++;
63+
}
64+
return results;
65+
},
66+
emitNewEvent(applicationId, timeFrame) {
67+
console.log(`New event collected for application ID: ${applicationId}, within time frame: ${timeFrame}`);
68+
},
769
authKeys() {
870
console.log(Object.keys(this.$auth));
971
},
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { axios } from "@pipedream/platform";
2+
import drimify from "../../drimify.app.mjs";
3+
4+
export default {
5+
key: "drimify-new-application-data",
6+
name: "New Application Data Collected",
7+
description: "Emit new event when application data has been collected. [See the documentation](https://endpoint.drimify.com/api/docs?ui=re_doc)",
8+
version: "0.0.{{ts}}",
9+
type: "source",
10+
dedupe: "unique",
11+
props: {
12+
drimify,
13+
db: "$.service.db",
14+
timer: {
15+
type: "$.interface.timer",
16+
default: {
17+
intervalSeconds: 60 * 15,
18+
},
19+
},
20+
applicationId: {
21+
propDefinition: [
22+
drimify,
23+
"applicationId",
24+
],
25+
},
26+
timeFrame: {
27+
propDefinition: [
28+
drimify,
29+
"timeFrame",
30+
],
31+
optional: true,
32+
},
33+
},
34+
hooks: {
35+
async deploy() {
36+
const applicationData = await this.drimify.paginate(this.drimify.listAppDataCollections, {
37+
applicationId: this.applicationId,
38+
createdSince: this.timeFrame || undefined,
39+
});
40+
41+
for (const data of applicationData.slice(0, 50).reverse()) {
42+
this.$emit(data, {
43+
id: data.idunic,
44+
summary: `New Data Collected: ${data.username || data.email || data.idunic}`,
45+
ts: new Date(data.updatedAt).getTime(),
46+
});
47+
}
48+
49+
const lastTimestamp = applicationData[0]?.updatedAtTimestamp || 0;
50+
this._setLastTimestamp(lastTimestamp);
51+
},
52+
async activate() {
53+
// Implement if needed
54+
},
55+
async deactivate() {
56+
// Implement if needed
57+
},
58+
},
59+
methods: {
60+
_getLastTimestamp() {
61+
return this.db.get("lastTimestamp") || 0;
62+
},
63+
_setLastTimestamp(timestamp) {
64+
this.db.set("lastTimestamp", timestamp);
65+
},
66+
},
67+
async run() {
68+
const lastTimestamp = this._getLastTimestamp();
69+
const applicationData = await this.drimify.paginate(this.drimify.listAppDataCollections, {
70+
applicationId: this.applicationId,
71+
createdSince: new Date(lastTimestamp * 1000).toISOString(),
72+
});
73+
74+
for (const data of applicationData) {
75+
if (Date.parse(data.updatedAt) > lastTimestamp) {
76+
this.$emit(data, {
77+
id: data.idunic,
78+
summary: `New Data Collected: ${data.username || data.email || data.idunic}`,
79+
ts: new Date(data.updatedAt).getTime(),
80+
});
81+
}
82+
}
83+
84+
if (applicationData.length > 0) {
85+
this._setLastTimestamp(applicationData[0].updatedAtTimestamp || 0);
86+
}
87+
},
88+
};

0 commit comments

Comments
 (0)