-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.ts
More file actions
239 lines (203 loc) · 7.27 KB
/
main.ts
File metadata and controls
239 lines (203 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import "dotenv/config";
import { task } from "@renderinc/sdk/workflows";
import { readFileSync, existsSync } from "node:fs";
import { resolve } from "node:path";
interface Record {
id?: string;
name?: string;
email?: string;
age?: string;
country?: string;
[key: string]: string | undefined;
}
interface ValidatedRecord {
id: string | undefined;
name: string;
email: string | null;
age: number | null;
country: string;
is_valid: boolean;
errors: string[];
warnings: string[];
}
const retry = {
maxRetries: 3,
waitDurationMs: 1000,
backoffScaling: 1.5,
};
// Chained task: extract rows from a CSV file
const extractCsvData = task(
{ name: "extractCsvData", retry },
function extractCsvData(filePath: string): Record[] {
console.log(`[EXTRACT] Reading CSV file: ${filePath}`);
const fullPath = resolve(filePath);
if (!existsSync(fullPath)) {
console.warn("[EXTRACT] File not found, using sample data");
return [
{ id: "1", name: "Alice", email: "alice@example.com", age: "28", country: "USA" },
{ id: "2", name: "Bob", email: "bob@example.com", age: "34", country: "Canada" },
{ id: "3", name: "Charlie", email: "invalid-email", age: "invalid", country: "UK" },
];
}
const content = readFileSync(fullPath, "utf-8");
const lines = content.trim().split("\n");
if (lines.length < 2) return [];
const headers = lines[0].split(",").map((h) => h.trim());
const records: Record[] = [];
for (let i = 1; i < lines.length; i++) {
const values = lines[i].split(",").map((v) => v.trim());
const record: Record = {};
headers.forEach((h, idx) => {
record[h] = values[idx];
});
records.push(record);
}
console.log(`[EXTRACT] Successfully extracted ${records.length} records`);
return records;
},
);
// Chained task: validate and clean a single record
const validateRecord = task(
{ name: "validateRecord", retry },
function validateRecord(record: Record): ValidatedRecord {
console.log(`[TRANSFORM] Validating record ID: ${record.id ?? "unknown"}`);
const errors: string[] = [];
const warnings: string[] = [];
if (!record.name) errors.push("Missing name");
if (!record.email) errors.push("Missing email");
const email = record.email ?? "";
if (email && !email.includes("@")) errors.push("Invalid email format");
let age: number | null = null;
try {
age = parseInt(record.age ?? "0", 10);
if (isNaN(age) || age < 0 || age > 120) {
errors.push(`Invalid age: ${record.age}`);
age = null;
}
} catch {
errors.push(`Age must be a number: ${record.age}`);
}
const cleaned: ValidatedRecord = {
id: record.id,
name: (record.name ?? "").trim(),
email: email ? email.toLowerCase().trim() : null,
age,
country: (record.country ?? "").trim(),
is_valid: errors.length === 0,
errors,
warnings,
};
const status = cleaned.is_valid ? "VALID" : "INVALID";
console.log(`[TRANSFORM] Record ${record.id}: ${status}`);
return cleaned;
},
);
// Chained task: validate a batch of records by calling validateRecord for each
const transformBatch = task(
{ name: "transformBatch", retry },
async function transformBatch(records: Record[]) {
console.log(`[TRANSFORM] Starting batch transformation of ${records.length} records`);
const validRecords: ValidatedRecord[] = [];
const invalidRecords: ValidatedRecord[] = [];
for (let i = 0; i < records.length; i++) {
console.log(`[TRANSFORM] Processing record ${i + 1}/${records.length}`);
const validated = await validateRecord(records[i]);
if (validated.is_valid) {
validRecords.push(validated);
} else {
invalidRecords.push(validated);
}
}
const result = {
valid_records: validRecords,
invalid_records: invalidRecords,
total_processed: records.length,
valid_count: validRecords.length,
invalid_count: invalidRecords.length,
success_rate: records.length > 0 ? validRecords.length / records.length : 0,
};
console.log(
`[TRANSFORM] Batch complete: ${result.valid_count} valid, ${result.invalid_count} invalid`,
);
return result;
},
);
// Chained task: compute statistics from validated records
const computeStatistics = task(
{ name: "computeStatistics", retry },
function computeStatistics(validRecords: ValidatedRecord[]) {
console.log(`[LOAD] Computing statistics for ${validRecords.length} records`);
if (validRecords.length === 0) {
console.warn("[LOAD] No valid records to analyze");
return { total_records: 0, country_distribution: {}, age_stats: {} };
}
const countryCounts: { [key: string]: number } = {};
for (const record of validRecords) {
const country = record.country || "Unknown";
countryCounts[country] = (countryCounts[country] ?? 0) + 1;
}
const ages = validRecords
.filter((r) => r.age !== null)
.map((r) => r.age as number);
const ageStats =
ages.length > 0
? {
min: Math.min(...ages),
max: Math.max(...ages),
average: ages.reduce((a, b) => a + b, 0) / ages.length,
count: ages.length,
}
: {};
const statistics = {
total_records: validRecords.length,
country_distribution: countryCounts,
age_stats: ageStats,
timestamp: new Date().toISOString(),
};
console.log("[LOAD] Statistics computed successfully");
return statistics;
},
);
// Root task: orchestrates the full ETL pipeline
task(
{ name: "runEtlPipeline", retry, timeoutSeconds: 300 },
async function runEtlPipeline(sourceFile: string) {
console.log("=".repeat(80));
console.log("[PIPELINE] Starting ETL Pipeline");
console.log(`[PIPELINE] Source: ${sourceFile}`);
console.log("=".repeat(80));
console.log("[PIPELINE] Stage 1/3: EXTRACT");
const rawRecords = await extractCsvData(sourceFile);
console.log(`[PIPELINE] Extracted ${rawRecords.length} records`);
console.log("[PIPELINE] Stage 2/3: TRANSFORM");
const transformResult = await transformBatch(rawRecords);
console.log(
`[PIPELINE] Transformation complete: ${(transformResult.success_rate * 100).toFixed(1)}% success rate`,
);
console.log("[PIPELINE] Stage 3/3: LOAD");
const statistics = await computeStatistics(transformResult.valid_records);
console.log("[PIPELINE] Statistics computed");
const pipelineResult = {
status: "success",
extract: {
records_extracted: rawRecords.length,
source: sourceFile,
},
transform: {
valid_count: transformResult.valid_count,
invalid_count: transformResult.invalid_count,
success_rate: transformResult.success_rate,
invalid_records: transformResult.invalid_records,
},
load: { statistics },
completed_at: new Date().toISOString(),
};
console.log("=".repeat(80));
console.log("[PIPELINE] ETL Pipeline Complete!");
console.log(`[PIPELINE] Processed: ${rawRecords.length} records`);
console.log(`[PIPELINE] Valid: ${transformResult.valid_count} records`);
console.log(`[PIPELINE] Invalid: ${transformResult.invalid_count} records`);
console.log("=".repeat(80));
return pipelineResult;
},
);