-
Notifications
You must be signed in to change notification settings - Fork 3
Feat: Add Airdrop loading documentation #201
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
Changes from 1 commit
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
There are no files selected for viewing
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,35 @@ | ||
In the load attachments phase, the snap-in saves each attachment to the external system. | ||
|
||
## Triggering event | ||
|
||
Airdrop initiates the load attachments phase by starting the snap-in with a message containing an event of type `START_LOADING_ATTACHMENTS`. | ||
|
||
## Implementation | ||
|
||
This phase is defined in [load-attachments.ts](https://github.com/devrev/airdrop-template/blob/main/code/src/functions/loading/workers/load-attachments.ts). | ||
|
||
The loading process involves providing the `create` function to add attachments to the external system. The `create` function is responsible for making API calls to the external system to create the attachments, as well as handling errors and the external system's rate limiting. The function returns the ID and modified date of the record in the external system or indicates a rate-limiting back-off or logs errors if the attachment could not be created. | ||
|
||
```typescript | ||
processTask<LoaderState>({ | ||
task: async ({ adapter }) => { | ||
const { reports, processed_files } = await adapter.loadAttachments({ | ||
create: createAttachment, | ||
}); | ||
|
||
await adapter.emit(LoaderEventType.AttachmentLoadingDone, { | ||
reports, | ||
processed_files, | ||
}); | ||
}, | ||
onTimeout: async ({ adapter }) => { | ||
await adapter.emit(LoaderEventType.AttachmentLoadingProgress, { | ||
reports: adapter.reports, | ||
processed_files: adapter.processedFiles, | ||
}); | ||
}, | ||
}); | ||
``` | ||
|
||
|
||
|
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,43 @@ | ||
The load data phase manages the creation and updating of items in the external system. | ||
|
||
## Triggering event | ||
|
||
Airdrop initiates data loading by starting the snap-in with a message containing an event | ||
of type `START_LOADING_DATA`, which, in the template, can be found under `EventType.StartLoadingData`. | ||
patricijabrecko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Implementation | ||
|
||
This phase is defined in [load-data.ts](https://github.com/devrev/airdrop-template/blob/main/code/src/functions/loading/workers/load-data.ts). | ||
|
||
Loading is performed by providing an ordered list of item types to load (`itemTypesToLoad`). | ||
patricijabrecko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Each item type must provide `create` and `update` functions, which handle the denormalization of records to the schema of the external system and facilitate HTTP calls to the external system. Both loading functions must manage rate limiting for the external system and handle errors. The `create` and `update` functions return an ID of the record in the external system. If a record cannot be created or updated, they indicate the rate-limiting offset or errors. | ||
patricijabrecko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Same as with extraction, the SDK library exports the `processTask` function to structure the work within each phase and the `onTimeout` function to handle timeouts. | ||
|
||
```typescript | ||
processTask<LoaderState>({ | ||
task: async ({ adapter }) => { | ||
const { reports, processed_files } = await adapter.loadItemTypes({ | ||
itemTypesToLoad: [ | ||
{ | ||
itemType: 'todos', | ||
create: createTodo, | ||
update: updateTodo, | ||
}, | ||
], | ||
}); | ||
|
||
await adapter.emit(LoaderEventType.DataLoadingDone, { | ||
reports, | ||
processed_files, | ||
}); | ||
}, | ||
onTimeout: async ({ adapter }) => { | ||
await adapter.emit(LoaderEventType.DataLoadingProgress, { | ||
reports: adapter.reports, | ||
processed_files: adapter.processedFiles, | ||
}); | ||
}, | ||
}); | ||
``` |
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 |
---|---|---|
@@ -1,3 +1,52 @@ | ||
# Loading | ||
Loading is the process of exporting data from DevRev back to the external system. | ||
This process includes creating new items in the external system and updating them with any changes made in DevRev. | ||
|
||
To be added. | ||
The snap-in manages two phases for loading: | ||
* Load Data and | ||
* Load Attachments. | ||
|
||
Each phase is defined in a separate file and is responsible for loading the corresponding data. | ||
|
||
```typescript | ||
import { AirdropEvent, EventType, spawn } from '@devrev/ts-adaas'; | ||
|
||
export interface LoaderState {} | ||
|
||
export const initialLoaderState: LoaderState = {}; | ||
|
||
function getWorkerPerLoadingPhase(event: AirdropEvent) { | ||
let path; | ||
switch (event.payload.event_type) { | ||
case EventType.StartLoadingData: | ||
case EventType.ContinueLoadingData: | ||
path = __dirname + '/workers/load-data'; | ||
break; | ||
case EventType.StartLoadingAttachments: | ||
case EventType.ContinueLoadingAttachments: | ||
path = __dirname + '/workers/load-attachments'; | ||
break; | ||
} | ||
return path; | ||
} | ||
|
||
const run = async (events: AirdropEvent[]) => { | ||
for (const event of events) { | ||
const file = getWorkerPerLoadingPhase(event); | ||
await spawn<LoaderState>({ | ||
event, | ||
initialState: initialLoaderState, | ||
workerPath: file, | ||
// options: {}, | ||
}); | ||
} | ||
}; | ||
|
||
export default run; | ||
``` | ||
|
||
## State handling | ||
|
||
Loading phases run as separate runtime instances, similar to extraction phases, with a maximum execution time of 12 minutes. | ||
These phases share a `state`, defined in the `LoaderState` interface. | ||
It is important to note that the loader state is separate from the extraction state. | ||
Access to the `state` is available through the SDK's `adapter` object. | ||
patricijabrecko 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
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.