Skip to content

Commit 7ec7798

Browse files
Feat: Add Airdrop loading documentation (#201)
* Add loading documentation --------- Co-authored-by: Atul-Butola <[email protected]>
1 parent a53672b commit 7ec7798

File tree

5 files changed

+141
-10
lines changed

5 files changed

+141
-10
lines changed

fern/docs/pages/airdrop/extraction-phases.mdx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ function that iterates over events and invokes workers per extraction phase.
44
The SDK library exports `processTask` to structure the work within each phase, and `onTimeout` function
55
to handle timeouts.
66

7-
The Airdrop snap-in extraction lifecycle consists of four phases: External Sync Units Extraction,
8-
Metadata Extraction, Data Extraction and Attachments Extraction. Each phase is defined in a
9-
separate file and is responsible for fetching the respective data.
7+
The Airdrop snap-in extraction lifecycle consists of four phases:
8+
* External sync units extraction
9+
* Metadata extraction
10+
* Data extraction
11+
* Attachments extraction
12+
13+
Each phase is defined in a separate file and is responsible for fetching the respective data.
1014

1115
The SDK library provides a repository management system to handle artifacts in batches.
1216
The `initializeRepos` function initializes the repositories, and the `push` function uploads the
@@ -59,9 +63,6 @@ const run = async (events: AirdropEvent[]) => {
5963
event,
6064
initialState,
6165
workerPath: file,
62-
63-
// TODO: If needed you can pass additional options to the spawn function.
64-
// For example timeout of the lambda, batch size, etc.
6566
// options: {},
6667
});
6768
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
In the load attachments phase, the snap-in saves each attachment to the external system.
2+
3+
## Triggering event
4+
5+
Airdrop initiates the load attachments phase by starting the snap-in with a message containing an event of type `START_LOADING_ATTACHMENTS`.
6+
7+
## Implementation
8+
9+
This phase is defined in [load-attachments.ts](https://github.com/devrev/airdrop-template/blob/main/code/src/functions/loading/workers/load-attachments.ts).
10+
11+
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 should return the `id` and optionally `modifiedDate` of the record in the external system or indicates a rate-limiting back-off or logs errors if the attachment could not be created.
12+
13+
```typescript
14+
processTask<LoaderState>({
15+
task: async ({ adapter }) => {
16+
const { reports, processed_files } = await adapter.loadAttachments({
17+
create: createAttachment,
18+
});
19+
20+
await adapter.emit(LoaderEventType.AttachmentLoadingDone, {
21+
reports,
22+
processed_files,
23+
});
24+
},
25+
onTimeout: async ({ adapter }) => {
26+
await adapter.emit(LoaderEventType.AttachmentLoadingProgress, {
27+
reports: adapter.reports,
28+
processed_files: adapter.processedFiles,
29+
});
30+
},
31+
});
32+
```
33+
34+
35+

fern/docs/pages/airdrop/load-data.mdx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
The load data phase manages the creation and updating of items in the external system.
2+
3+
## Triggering event
4+
5+
Airdrop initiates data loading by starting the snap-in with a message containing an event of type `START_LOADING_DATA`.
6+
7+
## Implementation
8+
9+
This phase is defined in [load-data.ts](https://github.com/devrev/airdrop-template/blob/main/code/src/functions/loading/workers/load-data.ts).
10+
11+
Loading is performed by providing a list of item types to load (`itemTypesToLoad`), ordered in the sequence they should be loaded.
12+
13+
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 should return an `id` of the record in the external system and optionally also `modifiedDate`. If a record cannot be created or updated, they indicate the rate-limiting offset or errors.
14+
15+
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.
16+
17+
```typescript
18+
processTask<LoaderState>({
19+
task: async ({ adapter }) => {
20+
const { reports, processed_files } = await adapter.loadItemTypes({
21+
itemTypesToLoad: [
22+
{
23+
itemType: 'todos',
24+
create: createTodo,
25+
update: updateTodo,
26+
},
27+
],
28+
});
29+
30+
await adapter.emit(LoaderEventType.DataLoadingDone, {
31+
reports,
32+
processed_files,
33+
});
34+
},
35+
onTimeout: async ({ adapter }) => {
36+
await adapter.emit(LoaderEventType.DataLoadingProgress, {
37+
reports: adapter.reports,
38+
processed_files: adapter.processedFiles,
39+
});
40+
},
41+
});
42+
```
Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,52 @@
1-
# Loading
1+
Loading is the process of exporting data from DevRev back to the external system.
2+
This process includes creating new items in the external system and updating them with any changes made in DevRev.
23

3-
To be added.
4+
The snap-in manages two phases for loading:
5+
* Load data
6+
* Load attachments
7+
8+
Each phase is defined in a separate file and is responsible for loading the corresponding data.
9+
10+
```typescript
11+
import { AirdropEvent, EventType, spawn } from '@devrev/ts-adaas';
12+
13+
export interface LoaderState {}
14+
15+
export const initialLoaderState: LoaderState = {};
16+
17+
function getWorkerPerLoadingPhase(event: AirdropEvent) {
18+
let path;
19+
switch (event.payload.event_type) {
20+
case EventType.StartLoadingData:
21+
case EventType.ContinueLoadingData:
22+
path = __dirname + '/workers/load-data';
23+
break;
24+
case EventType.StartLoadingAttachments:
25+
case EventType.ContinueLoadingAttachments:
26+
path = __dirname + '/workers/load-attachments';
27+
break;
28+
}
29+
return path;
30+
}
31+
32+
const run = async (events: AirdropEvent[]) => {
33+
for (const event of events) {
34+
const file = getWorkerPerLoadingPhase(event);
35+
await spawn<LoaderState>({
36+
event,
37+
initialState: initialLoaderState,
38+
workerPath: file,
39+
// options: {},
40+
});
41+
}
42+
};
43+
44+
export default run;
45+
```
46+
47+
## State handling
48+
49+
Loading phases run as separate runtime instances, similar to extraction phases, with a maximum execution time of 12 minutes.
50+
These phases share a `state`, defined in the `LoaderState` interface.
51+
It is important to note that the loader state is separate from the extractor state.
52+
Access to the `state` is available through the SDK's `adapter` object.

fern/versions/public.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ navigation:
166166
slug: metadata-extraction
167167
path: ../docs/pages/airdrop/metadata-extraction.mdx
168168
- page: "Initial domain mapping"
169-
hidden: false
170169
slug: initial-domain-mapping
171170
path: ../docs/pages/airdrop/initial-domain-mapping.mdx
172171
- page: "Data extraction"
@@ -177,8 +176,13 @@ navigation:
177176
path: ../docs/pages/airdrop/attachments-extraction.mdx
178177
- page: "Loading phases"
179178
slug: loading-phases
180-
hidden: true
181179
path: ../docs/pages/airdrop/loading-phases.mdx
180+
- page: "Load data"
181+
slug: load-data
182+
path: ../docs/pages/airdrop/load-data.mdx
183+
- page: "Load attachments"
184+
slug: load-attachments
185+
path: ../docs/pages/airdrop/load-attachments.mdx
182186
- page: "Data and attachments deletion"
183187
slug: data-attachments-deletion
184188
hidden: true

0 commit comments

Comments
 (0)