-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.ts
140 lines (127 loc) · 4.16 KB
/
transform.ts
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
import fs from "node:fs/promises";
import path from "node:path";
import { homedir } from "node:os";
import pMap, { pMapIterable } from "p-map";
import groupBy from "object.groupby";
import { glob } from "glob";
import type { SerializedCourse } from "./serialized-course";
import type { StudiesTree } from "./studies-tree";
type TreeNode = {
name: string;
iconName: string;
children: Record<number, TreeNode>;
credits?: number;
courseTypeDto?: string;
};
async function transformDir(dir: string) {
const courseFiles = (
await glob("course*.json", {
withFileTypes: true,
cwd: dir,
})
).filter((f) => f.isFile());
console.log("#CourseFiles:", courseFiles.length);
let progress = 0;
const studies = [];
for await (const studyInfos of await pMapIterable(
courseFiles,
async (file) => {
const content = await fs.readFile(file.fullpath(), "utf-8");
process.stdout.write(`\r${++progress}`);
return JSON.parse(content) as SerializedCourse;
},
{ concurrency: 10 }
)) {
for (const {
coCurriculumPositionDto: studyInfo,
} of studyInfos.curriculumPositions) {
studies.push({
studyNameInfo: studyInfo.studyNameInfoDto,
curriculumPositionPath: [
...studyInfo.curriculumPositionPathDto.path,
{
elementId: studyInfos.courseDetail.cpCourseDto.id,
designation: "",
name: {
value: studyInfos.courseDetail.cpCourseDto.courseTitle.value,
},
description: {
value: studyInfos.courseDetail.cpCourseDto.courseTitle.value,
},
iconName: "stp_empty" as "stp_0", // TODO create icon for this
credits:
studyInfo.creditsDto?.value ??
studyInfos.courseDetail.cpCourseDto.ectsCredits,
courseTypeDto:
studyInfos.courseDetail.cpCourseDto.courseTypeDto.courseTypeName
.value,
} satisfies (typeof studyInfo.curriculumPositionPathDto.path)[number] & {
credits: number | undefined;
courseTypeDto: string;
},
],
});
}
}
process.stdout.write("\n");
console.log("#StudyInfos:", Object.keys(studies).length);
function objectMap<T, U>(
obj: Record<string, T>,
fn: (value: T, key: string, index: number) => U
): Record<string, U> {
return Object.fromEntries(
Object.entries(obj).map(([k, v], i) => [k, fn(v, k, i)])
);
}
type PathEntry = (typeof studies)[number]["curriculumPositionPath"][number];
function arrayToTree(data: PathEntry[][]): Record<number, TreeNode> {
const root: TreeNode = { name: "root", iconName: "", children: {} };
for (const path of data) {
let node = root;
try {
for (const entry of path) {
const { elementId, name, iconName } = entry;
if (!node.children.hasOwnProperty(elementId)) {
node.children[elementId] = {
name: name.value,
iconName,
children: {},
};
if ("credits" in entry) {
node.children[elementId].credits = entry.credits;
node.children[elementId].courseTypeDto = entry.courseTypeDto;
}
}
node = node.children[elementId];
}
} catch (e) {
console.error(path, e);
process.exit(1);
}
}
return root.children;
}
const studiesTree = objectMap(
groupBy(studies, (s) => s.studyNameInfo.curriculumVersionId),
(group) => ({
studyNameInfo: group[0].studyNameInfo,
currics: arrayToTree(group.map((s) => s.curriculumPositionPath)),
})
) satisfies StudiesTree;
console.log("#Studies:", Object.keys(studiesTree).length);
await fs.writeFile(
path.join(dir, "studiesTree.json"),
JSON.stringify(studiesTree, null, 2)
);
}
async function main() {
const semesters = await fs.readFile(
path.join(homedir(), ".cache/campusoffline/semesters.json"),
"utf-8"
);
const semesterDirs = (JSON.parse(semesters) as { id: number }[]).map((s) =>
path.join(homedir(), ".cache/campusoffline/", s.id.toString())
);
await pMap(semesterDirs, transformDir);
}
await main();