-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse_report_export.ts
More file actions
52 lines (43 loc) · 1.79 KB
/
Copy pathcourse_report_export.ts
File metadata and controls
52 lines (43 loc) · 1.79 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
import { infrai } from "./infrai_storage.ts";
type LearnerProgress = {
learner: string;
course: string;
completedLessons: number;
assignedLessons: number;
};
const BUCKET = "learning-report-exports";
function csvCell(value: string | number): string {
const text = String(value);
return /[",\n]/.test(text) ? `"${text.replaceAll("\"", "\"\"")}"` : text;
}
function progressCsv(rows: LearnerProgress[]): string {
const header = "learner,course,completed_lessons,assigned_lessons";
const entries = rows.map((row) => [
csvCell(row.learner),
csvCell(row.course),
csvCell(row.completedLessons),
csvCell(row.assignedLessons),
].join(","));
return [header, ...entries].join("\n");
}
async function exportCourseReport(): Promise<void> {
const reportDate = "2026-07-31";
const key = `course-progress/${reportDate}.csv`;
const csv = progressCsv([
{ learner: "Asha Patel", course: "Fraud Review Foundations", completedLessons: 8, assignedLessons: 8 },
{ learner: "Noah Kim", course: "Fraud Review Foundations", completedLessons: 6, assignedLessons: 8 },
]);
// Create the export bucket as part of the normal setup before object operations.
await infrai.storage.bucket.create({ bucket: BUCKET });
// A stable key means a retried PUT replaces this report rather than creating another export.
await infrai.storage.object.put(BUCKET, key, {
data_base64: Buffer.from(csv, "utf8").toString("base64"),
});
const request = { op: "get" as const, bucket: BUCKET, key, expires_in: 900 };
const signed = await infrai.storage.object.presign(BUCKET, key, request);
console.log(`Download the course-progress CSV: ${signed.url}`);
}
exportCourseReport().catch((error: unknown) => {
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
});