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

Notion - updated-page source improvements #14045

Merged
merged 6 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion components/notion/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/notion",
"version": "0.1.21",
"version": "0.1.22",
"description": "Pipedream Notion Components",
"main": "notion.app.mjs",
"keywords": [
Expand All @@ -17,6 +17,7 @@
"delayed-stream": "^1.0.0",
"form-data": "^3.0.1",
"lodash-es": "^4.17.21",
"md5": "^2.3.0",
"mime-db": "^1.52.0",
"mime-types": "^2.1.35",
"node-fetch": "^2.6.7",
Expand Down
40 changes: 33 additions & 7 deletions components/notion/sources/updated-page/updated-page.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import notion from "../../notion.app.mjs";
import sampleEmit from "./test-event.mjs";
import base from "../common/base.mjs";
import constants from "../common/constants.mjs";
import md5 from "md5";

export default {
...base,
key: "notion-updated-page",
name: "Updated Page in Database", /* eslint-disable-line pipedream/source-name */
description: "Emit new event when a page in a database is updated. To select a specific page, use `Updated Page ID` instead",
version: "0.0.16",
version: "0.0.17",
type: "source",
dedupe: "unique",
props: {
Expand Down Expand Up @@ -42,14 +43,26 @@ export default {
async deploy() {
const properties = await this.getProperties();
const propertyValues = {};
const pagesStream = this.notion.getPages(this.databaseId);
const params = this.lastUpdatedSortParam();
const pagesStream = this.notion.getPages(this.databaseId, params);
let count = 0;
let lastUpdatedTimestamp = 0;
for await (const page of pagesStream) {
propertyValues[page.id] = {};
for (const property of properties) {
propertyValues[page.id][property] = JSON.stringify(page.properties[property]);
propertyValues[page.id][property] = md5(JSON.stringify(page.properties[property]));
}
lastUpdatedTimestamp = Math.max(
lastUpdatedTimestamp,
Date.parse(page?.last_edited_time),
);
if (count < 25) {
this.emitEvent(page);
}
count++;
}
this._setPropertyValues(propertyValues);
this.setLastUpdatedTimestamp(lastUpdatedTimestamp);
},
},
methods: {
Expand All @@ -70,20 +83,30 @@ export default {
generateMeta(obj, summary) {
const { id } = obj;
const title = this.notion.extractPageTitle(obj);
const ts = Date.now();
const ts = Date.parse(obj.last_edited_time);
return {
id: `${id}-${ts}`,
summary: `${summary}: ${title} - ${id}`,
ts,
};
},
emitEvent(page) {
const meta = this.generateMeta(page, constants.summaries.PAGE_UPDATED);
this.$emit(page, meta);
},
},
async run() {
const lastCheckedTimestamp = this.getLastUpdatedTimestamp();
const propertyValues = this._getPropertyValues();

const params = {
...this.lastUpdatedSortParam(),
filter: {
timestamp: "last_edited_time",
last_edited_time: {
on_or_after: new Date(lastCheckedTimestamp).toISOString(),
},
},
};
let newLastUpdatedTimestamp = lastCheckedTimestamp;
const properties = await this.getProperties();
Expand All @@ -97,7 +120,7 @@ export default {

let propertyChangeFound = false;
for (const property of properties) {
const currentProperty = JSON.stringify(page.properties[property]);
const currentProperty = md5(JSON.stringify(page.properties[property]));
if (!propertyValues[page.id] || currentProperty !== propertyValues[page.id][property]) {
propertyChangeFound = true;
propertyValues[page.id] = {
Expand All @@ -114,8 +137,11 @@ export default {
continue;
}

const meta = this.generateMeta(page, constants.summaries.PAGE_UPDATED);
this.$emit(page, meta);
this.emitEvent(page);

if (Date.parse(page?.last_edited_time) < lastCheckedTimestamp) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does it make sense to add back the HTTP request param to filter out pages where last_edited_time < lastCheckedTimestamp:

{
  filter: {
    timestamp: "last_edited_time",
    last_edited_time: {
      on_or_after: new Date(lastCheckedTimestamp).toISOString()
    }
  }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@andrewjschuang Ah, I didn't notice there's an on-or-after filter in addition to after. I added the filter back in, but I'm still hesitant to be using last_edited_time (either for filtering or for exitng the loop) since that was the reason the source was skipping events to begin with. The last_edited_time is not necessarily updated when a page is updated. I guess in this case it's a choice between possibly skipping events and having to loop through a large amount of pages.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks, based on my tests the Notion API was reliably updating the last_edited_time. The issues the user was seeing were most likely due to performance issues due to a huge number of pages in the database. I tested with a fairly large database, and with the improvements from this PR, I did not have any issues

break;
}
}

this.setLastUpdatedTimestamp(newLastUpdatedTimestamp);
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading