-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Habitify - new components #19416
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
Merged
Merged
Habitify - new components #19416
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { axios } from "@pipedream/platform"; | ||
|
|
||
| export default { | ||
| type: "app", | ||
| app: "habitify", | ||
| propDefinitions: { | ||
| habitIds: { | ||
| type: "string[]", | ||
| label: "Habit IDs", | ||
| description: "The IDs of the habits to watch", | ||
| async options() { | ||
| const { data } = await this.getHabits(); | ||
| return data?.map(({ | ||
| id, name, | ||
| }) => ({ | ||
| label: name, | ||
| value: id, | ||
| })) || []; | ||
| }, | ||
| }, | ||
| }, | ||
| methods: { | ||
| _apiKey() { | ||
| return this.$auth.api_key; | ||
| }, | ||
| _apiUrl() { | ||
| return "https://api.habitify.me"; | ||
| }, | ||
| async _makeRequest({ | ||
| $ = this, path, ...args | ||
| }) { | ||
| return axios($, { | ||
| url: `${this._apiUrl()}${path}`, | ||
| headers: { | ||
| Authorization: this._apiKey(), | ||
| }, | ||
| ...args, | ||
| }); | ||
| }, | ||
| getHabits(args = {}) { | ||
| return this._makeRequest({ | ||
| path: "/habits", | ||
| ...args, | ||
| }); | ||
| }, | ||
| getHabitStatus({ | ||
| habitId, ...args | ||
| }) { | ||
| return this._makeRequest({ | ||
| path: `/status/${habitId}`, | ||
| ...args, | ||
| }); | ||
| }, | ||
| getHabitLogs({ | ||
| habitId, ...args | ||
| }) { | ||
| return this._makeRequest({ | ||
| path: `/logs/${habitId}`, | ||
| ...args, | ||
| }); | ||
| }, | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import habitify from "../../habitify.app.mjs"; | ||
| import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; | ||
|
|
||
| export default { | ||
| props: { | ||
| habitify, | ||
| db: "$.service.db", | ||
| timer: { | ||
| type: "$.interface.timer", | ||
| default: { | ||
| intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, | ||
| }, | ||
| }, | ||
| }, | ||
| methods: { | ||
| getCurrentDateTime() { | ||
| return new Date().toISOString() | ||
| .replace("Z", this.getTimeZoneOffset()); | ||
| }, | ||
| getTimeZoneOffset() { | ||
| const offset = new Date().getTimezoneOffset(); | ||
| const sign = offset > 0 | ||
| ? "-" | ||
| : "+"; | ||
| const abs = Math.abs(offset); | ||
| const hours = String(Math.floor(abs / 60)).padStart(2, "0"); | ||
| const minutes = String(abs % 60).padStart(2, "0"); | ||
| return `${sign}${hours}:${minutes}`; | ||
| }, | ||
| convertToUTCOffset(dateString) { | ||
| const date = new Date(dateString); | ||
| const iso = | ||
| date.getUTCFullYear() + | ||
| "-" + String(date.getUTCMonth() + 1).padStart(2, "0") + | ||
| "-" + String(date.getUTCDate()).padStart(2, "0") + | ||
| "T" + String(date.getUTCHours()).padStart(2, "0") + | ||
| ":" + String(date.getUTCMinutes()).padStart(2, "0") + | ||
| ":" + String(date.getUTCSeconds()).padStart(2, "0"); | ||
| return iso + "+00:00"; | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import common from "../common/base-polling.mjs"; | ||
| import sampleEmit from "./test-event.mjs"; | ||
|
|
||
| export default { | ||
| ...common, | ||
| key: "habitify-habit-logged", | ||
| name: "Habit Logged", | ||
| description: "Emit new event when a new log is created for the selected habit(s). [See the documentation](https://docs.habitify.me/core-resources/habits/logs)", | ||
| version: "0.0.1", | ||
| type: "source", | ||
| dedupe: "unique", | ||
| props: { | ||
| ...common.props, | ||
| habitIds: { | ||
| propDefinition: [ | ||
| common.props.habitify, | ||
| "habitIds", | ||
| ], | ||
| }, | ||
| }, | ||
| methods: { | ||
| ...common.methods, | ||
| _getLastTs() { | ||
| return this.db.get("lastTs") || this.convertToUTCOffset(this.getCurrentDateTime()); | ||
| }, | ||
| _setLastTs(ts) { | ||
| this.db.set("lastTs", ts); | ||
| }, | ||
| generateMeta(log) { | ||
| return { | ||
| id: log.id, | ||
| summary: `New log with ID ${log.id} created for habit ${log.habit_id}`, | ||
| ts: Date.parse(log.created_date), | ||
| }; | ||
| }, | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| async run() { | ||
| const lastTs = this._getLastTs(); | ||
| let maxTs = lastTs; | ||
| const params = { | ||
| from: lastTs, | ||
| to: this.convertToUTCOffset(this.getCurrentDateTime()), | ||
| }; | ||
| const logs = []; | ||
| for (const habitId of this.habitIds) { | ||
| const { data } = await this.habitify.getHabitLogs({ | ||
| habitId, | ||
| params, | ||
| }); | ||
| if (!data.length) { | ||
| continue; | ||
| } | ||
| logs.push(...data); | ||
| if (Date.parse(data[data.length - 1].created_date) > Date.parse(maxTs)) { | ||
| maxTs = this.convertToUTCOffset(data[data.length - 1].created_date); | ||
| } | ||
| } | ||
| this._setLastTs(maxTs); | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| logs.sort((a, b) => Date.parse(a.created_date) - Date.parse(b.created_date)); | ||
| logs.forEach((log) => { | ||
| this.$emit(log, this.generateMeta(log)); | ||
| }); | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| sampleEmit, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export default { | ||
| "id": "-Og94QB1QHNBaFW1Q12v", | ||
| "value": 1, | ||
| "created_date": "2025-12-10T21:05:52.838Z", | ||
| "unit_type": "rep", | ||
| "habit_id": "C0D4CD24-EF3E-4EAB-9D88-17BBDE801FC1" | ||
| } |
87 changes: 87 additions & 0 deletions
87
components/habitify/sources/habit-status-updated/habit-status-updated.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import common from "../common/base-polling.mjs"; | ||
| import sampleEmit from "./test-event.mjs"; | ||
|
|
||
| export default { | ||
| ...common, | ||
| key: "habitify-habit-status-updated", | ||
| name: "Habit Status Updated", | ||
| description: "Emit new event when the status of a habit is updated. [See the documentation](https://docs.habitify.me/core-resources/habits/status)", | ||
| version: "0.0.1", | ||
| type: "source", | ||
| dedupe: "unique", | ||
| props: { | ||
| ...common.props, | ||
| habitIds: { | ||
| propDefinition: [ | ||
| common.props.habitify, | ||
| "habitIds", | ||
| ], | ||
| }, | ||
| statuses: { | ||
| type: "string[]", | ||
| label: "Statuses", | ||
| description: "The statuses to watch for. [See the documentation](https://docs.habitify.me/core-resources/habits/status)", | ||
| options: [ | ||
| "in_progress", | ||
| "completed", | ||
| "skipped", | ||
| "failed", | ||
| ], | ||
| optional: true, | ||
| }, | ||
| }, | ||
| hooks: { | ||
| async deploy() { | ||
| const previousStatuses = {}; | ||
| for (const habitId of this.habitIds) { | ||
| const { data } = await this.habitify.getHabitStatus({ | ||
| habitId, | ||
| params: { | ||
| target_date: this.convertToUTCOffset(this.getCurrentDateTime()), | ||
| }, | ||
| }); | ||
| previousStatuses[habitId] = data.status; | ||
| } | ||
| this._setPreviousStatuses(previousStatuses); | ||
| }, | ||
| }, | ||
| methods: { | ||
| ...common.methods, | ||
| _getPreviousStatuses() { | ||
| return this.db.get("previousStatuses") || {}; | ||
| }, | ||
| _setPreviousStatuses(statuses) { | ||
| this.db.set("previousStatuses", statuses); | ||
| }, | ||
| generateMeta(status) { | ||
| return { | ||
| id: `${status.habit_id}-${status.progress.reference_date}`, | ||
| summary: `New Status ${status.status} for habit ${status.habit_id}`, | ||
| ts: Date.parse(status.progress.reference_date), | ||
| }; | ||
| }, | ||
| }, | ||
| async run() { | ||
| const previousStatuses = this._getPreviousStatuses(); | ||
| const currentStatuses = {}; | ||
| const targetDate = this.convertToUTCOffset(this.getCurrentDateTime()); | ||
| for (const habitId of this.habitIds) { | ||
| const { data } = await this.habitify.getHabitStatus({ | ||
| habitId, | ||
| params: { | ||
| target_date: targetDate, | ||
| }, | ||
| }); | ||
| currentStatuses[habitId] = data.status; | ||
| if ( | ||
| previousStatuses[habitId] !== data.status | ||
| && (!this.statuses || this.statuses.includes(data.status)) | ||
| ) { | ||
| data.habit_id = habitId; | ||
| this.$emit(data, this.generateMeta(data)); | ||
| } | ||
| } | ||
| this._setPreviousStatuses(currentStatuses); | ||
| }, | ||
| sampleEmit, | ||
| }; | ||
10 changes: 10 additions & 0 deletions
10
components/habitify/sources/habit-status-updated/test-event.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| export default { | ||
| "status": "completed", | ||
| "progress": { | ||
| "current_value": 8, | ||
| "target_value": 8, | ||
| "unit_type": "rep", | ||
| "periodicity": "daily", | ||
| "reference_date": "2025-12-10T00:00:00.000Z" | ||
| } | ||
| } | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
components/habitify/sources/new-habit-created/test-event.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| export default { | ||
| "id": "C0D4CD24-EF3E-4EAB-9D88-17BBDE801FC1", | ||
| "name": "Drink Water", | ||
| "is_archived": false, | ||
| "start_date": "2025-12-10T19:57:16.566Z", | ||
| "time_of_day": [ | ||
| "any_time" | ||
| ], | ||
| "goal": { | ||
| "unit_type": "rep", | ||
| "value": 8, | ||
| "periodicity": "daily" | ||
| }, | ||
| "goal_history_items": [ | ||
| { | ||
| "unit_type": "rep", | ||
| "value": 8, | ||
| "periodicity": "daily" | ||
| } | ||
| ], | ||
| "log_method": "manual", | ||
| "recurrence": "DTSTART:20251210T195716Z\nRRULE:FREQ=DAILY", | ||
| "remind": [ | ||
| "6:30" | ||
| ], | ||
| "area": null, | ||
| "created_date": "2025-12-10T19:57:16.566Z", | ||
| "priority": 0 | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.