generated from magda-io/magda-function-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegistryEventTransformStream.ts
183 lines (167 loc) · 5.45 KB
/
RegistryEventTransformStream.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
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
import { Transform } from "stream";
import { Event } from "./RegistryEventStream";
import signJwtToken from "./signJwtToken";
const DEFAULT_HIGH_WATER_MARK = 50;
const userNameCache = {} as {
[key: string]: string;
};
async function getUserName(
authApiUrl: string,
userId: string
): Promise<string> {
try {
if (typeof userNameCache[userId] !== "undefined") {
return userNameCache[userId];
}
const tenantId = process.env.tenantId;
const headers = {
"X-Magda-Tenant-Id": tenantId ? tenantId : "0"
} as any;
const res = await fetch(`${authApiUrl}/public/users/${userId}`, {
headers
});
if (!res.ok) {
userNameCache[userId] = "N/A";
} else {
userNameCache[userId] = (await res.json())["displayName"];
}
return userNameCache[userId];
} catch (e) {
return "N/A";
}
}
const recordNameCache = {} as {
[key: string]: string;
};
async function getRecordName(
registryApiUrl: string,
recordId: string,
userId: string | null
): Promise<string> {
try {
if (typeof recordNameCache[recordId] !== "undefined") {
return recordNameCache[recordId];
}
const jwtToken = userId ? await signJwtToken(userId) : null;
const tenantId = process.env.tenantId;
const headers = {
"X-Magda-Tenant-Id": tenantId ? tenantId : "0"
} as any;
if (jwtToken) {
headers["X-Magda-Session"] = jwtToken;
}
const res = await fetch(
`${registryApiUrl}/records/${encodeURIComponent(recordId)}`,
{
headers
}
);
if (!res.ok) {
recordNameCache[userId] = "N/A";
} else {
recordNameCache[userId] = (await res.json())["name"];
recordNameCache[userId] = recordNameCache[userId]
? recordNameCache[userId]
: "N/A";
}
return recordNameCache[userId];
} catch (e) {
return "N/A";
}
}
export default class RegistryEventTransformStream extends Transform {
private authApiUrl: string;
private registryApiUrl: string;
private userId: string | null;
constructor(options: {
registryApiUrl: string;
authApiUrl: string;
highwaterMark?: number;
userId: string | null;
}) {
const highwaterMark = options.highwaterMark
? options.highwaterMark
: DEFAULT_HIGH_WATER_MARK;
super({
readableObjectMode: true,
writableObjectMode: true,
readableHighWaterMark: highwaterMark,
// this transform likely generate more records than input
writableHighWaterMark: highwaterMark * 2
});
this.registryApiUrl = options.registryApiUrl;
if (!this.registryApiUrl) {
throw new Error(
"RegistryEventTransformStream: registryApiUrl cannot be empty!"
);
}
this.authApiUrl = options.authApiUrl;
if (!this.authApiUrl) {
throw new Error(
"RegistryEventTransformStream: authApiUrl cannot be empty!"
);
}
this.userId = options.userId;
}
async _transform(
event: Event,
encoding: string,
callback: (e?: Error, data?: Event) => void
) {
try {
const row = {
"Event id": event.id,
"User id": event.userId,
"User Name": await getUserName(this.authApiUrl, event.userId),
Time: event.eventTime,
"Record Id": event?.data?.recordId,
"Record Name": await getRecordName(
this.registryApiUrl,
event?.data?.recordId,
this.userId
),
"Event type": event.eventType,
"Aspect Id": event?.data?.aspectId ? event.data.aspectId : ""
} as any;
let jsonData: string = "";
if (
event.eventType !== "DeleteRecord" &&
event.eventType !== "DeleteRecordAspect"
) {
jsonData = JSON.stringify(
event?.data?.aspect ? event.data.aspect : event.data
);
}
const jsonPatch = (event?.data?.patch
? event.data.patch
: []) as any[];
if (!jsonPatch.length) {
this.push({
...row,
"JSON Patch Operation": "",
"JSON Path": "",
"JSON Path Value": "",
"JSON Value": jsonData
});
} else {
jsonPatch.forEach(p =>
this.push({
...row,
"JSON Patch Operation": p?.op ? p.op : "",
"JSON Path": p?.path ? p.path : "",
"JSON Path Value":
typeof p?.value !== "undefined"
? typeof p.value === "string"
? p.value
: JSON.stringify(p.value)
: "",
"JSON Value": ""
})
);
}
callback();
} catch (e) {
callback(e as Error);
}
}
}