Skip to content

Commit e0db198

Browse files
committed
Adjusting Sources
1 parent 7e28250 commit e0db198

File tree

3 files changed

+87
-135
lines changed

3 files changed

+87
-135
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import app from "../the_magic_drip.app.mjs";
2+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
3+
4+
export default {
5+
props: {
6+
app,
7+
timer: {
8+
type: "$.interface.timer",
9+
default: {
10+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
11+
},
12+
},
13+
db: "$.service.db",
14+
},
15+
methods: {
16+
_getSavedIds() {
17+
return this.db.get("savedIds") || [];
18+
},
19+
_setSavedIds(value) {
20+
this.db.set("savedIds", value);
21+
},
22+
getItemId(item) {
23+
return item.id;
24+
},
25+
getItemMetadata() {
26+
return {
27+
summary: "New event",
28+
ts: Date.now(),
29+
};
30+
},
31+
async getAndProcessData(maxEmits = 0) {
32+
const savedIds = this._getSavedIds();
33+
const items = await this.getItems();
34+
35+
items?.filter?.((item) => !savedIds.includes(this.getItemId(item))).forEach((item, index) => {
36+
if ((!maxEmits) || (index < maxEmits)) {
37+
this.$emit(item, {
38+
id: this.getItemId(item),
39+
...this.getItemMetadata(item),
40+
});
41+
}
42+
savedIds.push(this.getItemId(item));
43+
});
44+
45+
this._setSavedIds(savedIds);
46+
},
47+
},
48+
hooks: {
49+
async deploy() {
50+
await this.getAndProcessData(5);
51+
},
52+
},
53+
async run() {
54+
await this.getAndProcessData();
55+
},
56+
};
Lines changed: 16 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,27 @@
1-
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2-
import app from "../../the_magic_drip.app.mjs";
1+
import common from "../common.mjs";
32

43
export default {
4+
...common,
55
key: "the_magic_drip-new-campaign-created",
66
name: "New Campaign Created",
7-
description: "Emit new event when a campaign is created. [See the documentation]()",
8-
version: "0.0.{{ts}}",
7+
description: "Emit new event when a campaign is created. [See the documentation](https://docs.themagicdrip.com/api-reference/endpoint/get-v1campaign)",
8+
version: "0.0.1",
99
type: "source",
1010
dedupe: "unique",
11-
props: {
12-
app,
13-
db: "$.service.db",
14-
timer: {
15-
type: "$.interface.timer",
16-
default: {
17-
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
18-
},
11+
methods: {
12+
...common.methods,
13+
async getItems() {
14+
const { campaigns } = await this.app.listCampaigns();
15+
return campaigns;
1916
},
20-
},
21-
hooks: {
22-
async deploy() {
23-
const lastRunAt = new Date(0).toISOString();
24-
const newCampaigns = await this.app.pollNewCampaigns(lastRunAt);
25-
const sortedCampaigns = newCampaigns.sort(
26-
(a, b) => new Date(b.createdAt) - new Date(a.createdAt),
27-
).slice(0, 50);
28-
29-
for (const campaign of sortedCampaigns) {
30-
this.$emit(
31-
campaign,
32-
{
33-
id: campaign.id || campaign.createdAt,
34-
summary: `New Campaign: ${campaign.name}`,
35-
ts: Date.parse(campaign.createdAt),
36-
},
37-
);
38-
}
39-
40-
const latestCreatedAt = sortedCampaigns.length
41-
? sortedCampaigns[0].createdAt
42-
: lastRunAt;
43-
await this.db.set("lastRunAt", latestCreatedAt);
44-
},
45-
async activate() {
46-
// No action needed on activate for polling source
17+
getItemId(item) {
18+
return item.workflowId;
4719
},
48-
async deactivate() {
49-
// No action needed on deactivate for polling source
20+
getItemMetadata(item) {
21+
return {
22+
summary: `New Campaign: ${item.name}`,
23+
ts: item.createdAt,
24+
};
5025
},
5126
},
52-
async run() {
53-
const lastRunAt = (await this.db.get("lastRunAt")) || new Date(0).toISOString();
54-
const newCampaigns = await this.app.pollNewCampaigns(lastRunAt);
55-
const sortedCampaigns = newCampaigns.sort(
56-
(a, b) => new Date(b.createdAt) - new Date(a.createdAt),
57-
);
58-
59-
for (const campaign of sortedCampaigns) {
60-
this.$emit(
61-
campaign,
62-
{
63-
id: campaign.id || campaign.createdAt,
64-
summary: `New Campaign: ${campaign.name}`,
65-
ts: Date.parse(campaign.createdAt),
66-
},
67-
);
68-
}
69-
70-
if (newCampaigns.length) {
71-
const latestCreatedAt = newCampaigns.reduce((latest, campaign) => {
72-
return new Date(campaign.createdAt) > new Date(latest)
73-
? campaign.createdAt
74-
: latest;
75-
}, lastRunAt);
76-
await this.db.set("lastRunAt", latestCreatedAt);
77-
}
78-
},
7927
};
Lines changed: 15 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,27 @@
1-
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2-
import app from "../../the_magic_drip.app.mjs";
1+
import common from "../common.mjs";
32

43
export default {
4+
...common,
55
key: "the_magic_drip-new-template-created",
66
name: "New Template Created",
7-
description: "Emits a new event when a template is created. [See the documentation]()",
8-
version: "0.0.{{ts}}",
7+
description: "Emit new event when a template is created. [See the documentation](https://docs.themagicdrip.com/api-reference/endpoint/get-v1templates)",
8+
version: "0.0.1",
99
type: "source",
1010
dedupe: "unique",
11-
props: {
12-
app,
13-
db: "$.service.db",
14-
timer: {
15-
type: "$.interface.timer",
16-
default: {
17-
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
18-
},
19-
},
20-
},
2111
methods: {
22-
_getLastRunAt() {
23-
return this.db.get("lastRunAt") || new Date(0).toISOString();
24-
},
25-
_setLastRunAt(timestamp) {
26-
return this.db.set("lastRunAt", timestamp);
12+
...common.methods,
13+
async getItems() {
14+
const { templates } = await this.app.listTemplates();
15+
return templates;
2716
},
28-
},
29-
hooks: {
30-
async deploy() {
31-
const lastRunAt = new Date(0).toISOString();
32-
const newTemplates = await this.app.pollNewTemplates(lastRunAt);
33-
const sortedTemplates = newTemplates.sort(
34-
(a, b) => new Date(b.createdAt) - new Date(a.createdAt),
35-
);
36-
const latest50Templates = sortedTemplates.slice(0, 50);
37-
for (const template of latest50Templates) {
38-
this.$emit(template, {
39-
id: template.templateId || template.createdAt,
40-
summary: `New Template: ${template.name}`,
41-
ts: Date.parse(template.createdAt) || Date.now(),
42-
});
43-
}
44-
const newLastRunAt =
45-
latest50Templates.length > 0
46-
? latest50Templates[0].createdAt
47-
: lastRunAt;
48-
await this._setLastRunAt(newLastRunAt);
49-
},
50-
async activate() {
51-
// No webhook setup required
17+
getItemId(item) {
18+
return item.templateId;
5219
},
53-
async deactivate() {
54-
// No webhook teardown required
20+
getItemMetadata(item) {
21+
return {
22+
summary: `New Template: ${item.name}`,
23+
ts: item.createdAt,
24+
};
5525
},
5626
},
57-
async run() {
58-
const lastRunAt = await this._getLastRunAt();
59-
const newTemplates = await this.app.pollNewTemplates(lastRunAt);
60-
const sortedNewTemplates = newTemplates.sort(
61-
(a, b) => new Date(a.createdAt) - new Date(b.createdAt),
62-
);
63-
for (const template of sortedNewTemplates) {
64-
this.$emit(template, {
65-
id: template.templateId || template.createdAt,
66-
summary: `New Template: ${template.name}`,
67-
ts: Date.parse(template.createdAt) || Date.now(),
68-
});
69-
}
70-
if (newTemplates.length > 0) {
71-
const latestCreatedAt = newTemplates.reduce((max, t) =>
72-
new Date(t.createdAt) > new Date(max)
73-
? t.createdAt
74-
: max,
75-
lastRunAt);
76-
await this._setLastRunAt(latestCreatedAt);
77-
}
78-
},
7927
};

0 commit comments

Comments
 (0)