-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheventhandlers.js
120 lines (110 loc) · 3.93 KB
/
eventhandlers.js
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
/*
Copyright 2025 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const consts = require("./consts");
const config = require("./config");
const { DicomInMemory } = require("./dicomtojson");
const bq = require("./bigquery");
const gcs = require("./gcs");
const hcapi = require("./hcapi");
const { deepAssign, DEBUG_MODE } = require("./utils");
// TODO: Include the metaHash for all files
async function getMetadata(buffer, uriPath) {
const configProvidedOptions = config.get().jsonOutputOptions;
const bulkDataRoot = configProvidedOptions.explicitBulkDataRoot ? uriPath : "";
const outputOptions = deepAssign({}, configProvidedOptions, { bulkDataRoot });
const reader = new DicomInMemory(buffer);
const metadata = reader.toJson(outputOptions);
return JSON.stringify(metadata);
}
async function handleGcsPubSubUnwrap(ctx, perfCtx) {
const { eventType, bucketId, objectId } = ctx.message.attributes;
const msgData = JSON.parse(Buffer.from(ctx.message.data, "base64").toString());
const writeObj = {
timestamp: new Date(),
path: `${msgData.bucket}/${msgData.name}`,
version: msgData.generation,
};
switch (eventType) {
// The object is no longer current
case consts.GCS_OBJ_ARCHIVE:
// The object has been removed
case consts.GCS_OBJ_DELETE: {
writeObj.info = JSON.stringify({
event: eventType,
storage: { type: consts.STORAGE_TYPE_GCS },
});
await bq.insert(writeObj);
perfCtx.addRef("afterBqInsert");
break;
}
// The object has been replaced with a new version
case consts.GCS_OBJ_FINALIZE: {
// Use memory to read, avoiding volume mount in container
const buffer = await gcs.downloadToMemory(bucketId, objectId);
perfCtx.addRef("afterGcsDownloadToMemory");
writeObj.info = JSON.stringify({
event: eventType,
storage: { size: buffer.length, type: consts.STORAGE_TYPE_GCS },
});
const uriPath = gcs.createUriPath(bucketId, objectId);
writeObj.metadata = await getMetadata(buffer, uriPath);
perfCtx.addRef("afterGetMetadata");
await bq.insert(writeObj);
perfCtx.addRef("afterBqInsert");
break;
}
// Metadata has been updated on the object
case consts.GCS_OBJ_METADATA_UPDATE: {
// Do nothing
break;
}
}
if (DEBUG_MODE) {
console.log(JSON.stringify(writeObj));
}
}
async function handleHcapiPubSubUnwrap(ctx, perfCtx) {
const dicomWebPath = Buffer.from(ctx.message.data, "base64").toString();
const uriPath = hcapi.createUriPath(dicomWebPath);
const buffer = await hcapi.downloadToMemory(uriPath);
perfCtx.addRef("afterHcapiDownloadToMemory");
const writeObj = {
timestamp: new Date(),
path: dicomWebPath,
version: null, // TODO: Fix when HCAPI supports versions
info: JSON.stringify({
event: consts.HCAPI_FINALIZE,
storage: { size: buffer.length, type: consts.STORAGE_TYPE_DICOMWEB },
}),
metadata: await getMetadata(buffer, uriPath),
};
perfCtx.addRef("afterGetMetadata");
await bq.insert(writeObj);
perfCtx.addRef("afterBqInsert");
if (DEBUG_MODE) {
console.log(JSON.stringify(writeObj));
}
}
async function handleEvent(name, req, res) {
switch (name) {
case consts.GCS_PUBSUB_UNWRAP: {
await handleGcsPubSubUnwrap(req.body, res.perfCtx);
break;
}
case consts.HCAPI_PUBSUB_UNWRAP: {
await handleHcapiPubSubUnwrap(req.body, res.perfCtx);
break;
}
}
}
module.exports = { handleEvent };