-
Notifications
You must be signed in to change notification settings - Fork 15
/
ExportManager.ts
1161 lines (999 loc) · 38.3 KB
/
ExportManager.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import crypto from "crypto"
import moment from "moment"
import fs from "fs"
import base64url from "base64-url"
import zlib from "zlib"
import config from "./config"
import * as lib from "./lib";
import QueryBuilder from "./QueryBuilder"
import getDB from "./db"
import toNdjson from "./transforms/dbRowToNdjson"
import toCSV from "./transforms/dbRowToCSV"
import fhirStream from "./FhirStream"
import translator from "./transforms/dbRowTranslator"
import { ExportManifest, RequestWithSim } from "./types"
import { OperationOutcome, ParametersParameter } from "fhir/r4"
import { NextFunction, Request, Response } from "express"
const supportedFormats = {
"application/fhir+ndjson" : "ndjson",
"application/ndjson" : "ndjson",
"ndjson" : "ndjson",
"text/csv" : "csv",
"csv" : "csv"
};
const exportTypes = {
ndjson: {
fileExtension : "ndjson",
contentType : "application/fhir+ndjson",
transform : toNdjson
},
csv: {
fileExtension : "csv",
contentType : "text/csv; charset=UTF-8; header=present",
transform : toCSV
}
};
function getExportParam(req: Request, name: string)
{
if (req.method == "GET") {
return req.query[name];
}
if (req.method == "POST") {
const out: any[] = [];
(req.body.parameter || []).forEach((x: ParametersParameter) => {
if (x.name === name) {
const valueX = Object.keys(x).find(key => key.startsWith("value"));
if (valueX) {
// @ts-ignore
out.push(x[valueX]);
}
}
});
if (out.length) {
return out.length === 1 ? out[0] : out;
}
}
return null;
}
function isFile(path: string)
{
try {
const stat = fs.statSync(path);
return stat.isFile();
} catch {
return false;
}
}
function deleteFileIfExists(path: string)
{
try {
if (isFile(path)) {
fs.unlinkSync(path);
}
} catch (ex) {
console.error(ex);
return false;
}
return true;
}
const ABORT_CONTROLLERS = new Map();
interface JobState {
createdAt : number
simulatedError ?: string
simulatedExportDuration?: number
databaseMultiplier ?: number
stu ?: number
resourcesPerFile ?: number
accessTokenLifeTime ?: number
resourceTypes ?: string[]
fhirElements ?: string[]
id ?: string
requestStart ?: number
secure ?: boolean
outputFormat ?: string
group ?: string
request ?: string
since ?: string
systemLevel ?: boolean
patients ?: string[]
fileError ?: string
jobStatus ?: string
extended ?: boolean
ignoreTransientError ?: boolean
simulateDeletedPct ?: number
kickoffErrors ?: OperationOutcome[]
typeFilter ?: string
progress ?: number
statusMessage ?: string
manifest ?: ExportManifest
tooManyFiles ?: boolean
};
class ExportManager
{
/**
* Simulated error (if any)
*/
simulatedError = "";
/**
* Simulated export duration
*/
simulatedExportDuration = 10;
/**
* Database size multiplier
*/
databaseMultiplier = 1;
/**
* FHIR version as integer (2|3|4)
*/
stu = 4;
/**
* How many FHIR resources to include in one file
*/
resourcesPerFile = config.defaultPageSize;
/**
* Access Token LifeTime in minutes
*/
accessTokenLifeTime = config.defaultTokenLifeTime;
/**
* An array of resourceTypes (from the _type parameter)
*/
resourceTypes: string[] = [];
/**
* An array of FHIR element paths (from the _elements parameter)
*/
fhirElements: string[] = [];
/**
* Unique ID for this job
*/
id: string = "";
/**
* When was the export started? JS timestamp.
*/
requestStart: number = 0;
/**
* True if an authorization header has been passed to kick-off
*/
secure: boolean = false;
/**
* File extension (ndjson|csv). Based on the _outputFormat parameter.
*/
outputFormat: keyof typeof exportTypes = "ndjson";
/**
* The group ID (if any)
*/
group: string = "";
/**
* The kick-off request URL
*/
request: string = "";
/**
* The modified since FHIR instant (the _since parameter)
*/
since: string = "";
/**
* The _typeFilter parameter
*/
typeFilter: URLSearchParams = new URLSearchParams();
/**
* true for system-level exports and false otherwise
*/
systemLevel: boolean = false;
/**
* Array of patient IDs to filter by (from the patient parameter)
*/
patients: string[] = [];
/**
* Generated file download error (if any)
*/
fileError: string = "";
/**
* The status of this job
*/
jobStatus = "UNDEFINED";
/**
* When a _since timestamp is supplied in the export request, a portion of
* the resources (expressed as percentage here) will be reported as deleted
* using the deleted field in the output JSON.
*/
simulateDeletedPct = 0;
extended = false;
/**
* An array to hold kickoff errors that should be included in the errors
* payload property if lenient handling is preferred
*/
kickoffErrors: OperationOutcome[] = [];
/**
* Percent complete (0 to 100)
*/
progress = 0;
statusMessage = "Please wait...";
manifest?: ExportManifest;
tooManyFiles = false;
createdAt: number = 0;
ignoreTransientError?: boolean;
getAbortController() {
let ctl = ABORT_CONTROLLERS.get(this.id)
if (!ctl) {
ctl = new AbortController()
ABORT_CONTROLLERS.set(this.id, ctl)
}
return ctl
}
static createKickOffHandler(system = false)
{
return function(req: Request, res: Response) {
const sim = (req as RequestWithSim).sim
ExportManager.create(
{
simulatedError : sim.err,
simulatedExportDuration: sim.dur,
databaseMultiplier : sim.m,
stu : sim.stu,
resourcesPerFile : sim.page,
accessTokenLifeTime : sim.tlt,
fileError : sim.fileError,
simulateDeletedPct : sim.del
}
).then(
job => job.kickOff(req, res, system),
err => lib.operationOutcome(res, err.message, { httpCode: 400 })
);
}
}
static createCancelHandler()
{
return function cancelFlow(req: Request, res: Response) {
return ExportManager.load(req.params.id).then(
job => job.cancel(res),
() => lib.outcomes.cancelNotFound(res)
);
};
}
static createStatusHandler()
{
return function handleStatus(req: Request, res: Response) {
return ExportManager.load(req.params.id).then(
job => job.handleStatus(req, res),
(err: any) => {
if (err.code === "ENOENT") {
lib.operationOutcome(res, "The export was not found", { httpCode: 404 })
} else {
lib.operationOutcome(res, err.message, { httpCode: 400 })
}
}
);
}
};
static createDownloadHandler()
{
return function handleFileDownload(req: Request, res: Response, next: NextFunction) {
const sim = (req as RequestWithSim).sim
ExportManager.load(sim.id).then(
job => job.download(req, res).catch(next),
() => lib.outcomes.exportDeleted(res)
);
}
}
static async cleanUp()
{
return lib.forEachFile({
dir: config.jobsPath,
filter: path => path.endsWith(".json")
}, path => lib.readJSON<JobState>(path).then(state => {
if (state && Date.now() - state.createdAt > config.maxExportAge * 60000) {
return fs.promises.unlink(path);
}
})).then(() => {
/* istanbul ignore if */
if (process.env.NODE_ENV != "test") {
setTimeout(ExportManager.cleanUp, 5000).unref();
}
});
}
// ========================================================================
static async create(options: Partial<JobState>) {
const instance = new ExportManager()
instance.id = crypto.randomBytes(16).toString("hex");
instance.createdAt = Date.now();
instance.setOptions(options);
await instance.save()
return instance;
}
static async load(id: string) {
const options = await lib.readJSON<JobState>(`${config.jobsPath}/${id}.json`);
const instance = new ExportManager();
instance.setOptions(options);
return instance;
}
setOptions(options: Partial<JobState>) {
this.kickoffErrors = options.kickoffErrors || [];
this.setSimulatedError(options.simulatedError)
.setSimulatedExportDuration(options.simulatedExportDuration)
.setDatabaseMultiplier(options.databaseMultiplier)
.setSTU(options.stu)
.setResourcesPerFile(options.resourcesPerFile)
.setAccessTokenLifeTime(options.accessTokenLifeTime)
.setSystemLevel(options.systemLevel)
.setGroup(options.group)
// .setPatients(options.patients)
.setSimulateDeletedPct(options.simulateDeletedPct)
.setSince(options.since)
.setTypeFilter(options.typeFilter);
["resourceTypes", "fhirElements", "id", "requestStart", "secure",
"patients", "outputFormat", "request", "fileError","jobStatus",
"extended", "createdAt", "ignoreTransientError", "progress",
"statusMessage", "manifest", "tooManyFiles"].forEach(key => {
if (key in options) {
// @ts-ignore
this[key] = options[key];
}
});
}
// ========================================================================
async save()
{
// DO NOT RE-CREATE THE FILE IF ALREADY ABORTED
if (!ABORT_CONTROLLERS.has(this.id)) {
return true;
}
const path = `${config.jobsPath}/${this.id}.json`;
const data = JSON.stringify(this.toJSON(), null, 4);
fs.writeFileSync(path, data, "utf8");
return true;
}
delete()
{
const path = `${config.jobsPath}/${this.id}.json`;
this.getAbortController().abort();
deleteFileIfExists(path);
ABORT_CONTROLLERS.delete(this.id);
}
toJSON(): JobState
{
return {
simulatedError : this.simulatedError,
simulatedExportDuration: this.simulatedExportDuration,
databaseMultiplier : this.databaseMultiplier,
stu : this.stu,
resourcesPerFile : this.resourcesPerFile,
accessTokenLifeTime : this.accessTokenLifeTime,
resourceTypes : this.resourceTypes,
fhirElements : this.fhirElements,
id : this.id,
requestStart : this.requestStart,
secure : this.secure,
outputFormat : this.outputFormat,
group : this.group,
request : this.request,
since : this.since,
systemLevel : this.systemLevel,
patients : this.patients,
fileError : this.fileError,
jobStatus : this.jobStatus,
extended : this.extended,
createdAt : this.createdAt,
ignoreTransientError : this.ignoreTransientError,
simulateDeletedPct : this.simulateDeletedPct,
kickoffErrors : this.kickoffErrors,
typeFilter : this.typeFilter.toString(),
progress : this.progress,
statusMessage : this.statusMessage,
manifest : this.manifest,
tooManyFiles : this.tooManyFiles
};
}
async kickOff(req: Request, res: Response, system: boolean)
{
const sim = (req as RequestWithSim).sim
const isLenient = !!String(req.headers.prefer || "").match(/\bhandling\s*=\s*lenient\b/i);
// Verify that the POST body contains a Parameters resource ------------
if (req.method == "POST" && req.body.resourceType !== "Parameters") {
return lib.operationOutcome(res, "The POST body should be a Parameters resource", { httpCode: 400 });
}
// Validate the accept header ------------------------------------------
// let accept = req.headers.accept;
// if (!accept || accept == "*/*") {
// accept = "application/fhir+ndjson"
// }
// if (accept != "application/fhir+ndjson" && accept != "application/fhir+json") {
// return lib.outcomes.invalidAccept(res, accept);
// }
// Simulate file_generation_failed error if requested ------------------
if (this.simulatedError == "file_generation_failed") {
return lib.outcomes.fileGenerationFailed(res);
}
try {
this.setSTU(sim.stu);
} catch (ex) {
return lib.operationOutcome(res, (ex as Error).message, { httpCode: 400 });
}
this.setGroup(req.params.groupId);
this.setSystemLevel(system);
this.extended = !!sim.extended;
const _type = getExportParam(req, "_type") || "";
const _patient = getExportParam(req, "patient") || "";
const _since = getExportParam(req, "_since") || "";
const _outputFormat = getExportParam(req, "_outputFormat") || "application/fhir+ndjson";
const _elements = getExportParam(req, "_elements") || "";
const _typeFilter = getExportParam(req, "_typeFilter") || "";
const _includeAssociatedData = getExportParam(req, "_includeAssociatedData") || "";
if (_includeAssociatedData) {
const outcome = lib.createOperationOutcome(`The "_includeAssociatedData" parameter is not supported by this server`);
if (!isLenient) {
this.delete();
return res.status(400).json(outcome);
}
this.kickoffErrors.push(outcome);
}
if (_patient && req.method != "POST") {
return lib.operationOutcome(res, `The "patient" parameter is only available in POST requests`, { httpCode: 400 });
}
try {
this.setTypeFilter(_typeFilter)
} catch (ex) {
const outcome = lib.createOperationOutcome((ex as Error).message);
if (!isLenient) {
this.delete();
return res.status(400).json(outcome);
}
this.kickoffErrors.push(outcome);
}
try {
await this.setResourceTypes(_type);
} catch (ex) {
return lib.operationOutcome(res, (ex as Error).message, { httpCode: 400 });
}
try {
this.setPatients(_patient);
} catch (ex) {
return lib.operationOutcome(res, (ex as Error).message, { httpCode: 400 });
}
try {
this.setSince(_since);
} catch (ex) {
return lib.outcomes.invalidSinceParameter(res, _since);
}
try {
await this.setFHIRElements(_elements);
} catch (ex) {
return lib.operationOutcome(res, (ex as Error).message, { httpCode: 400 });
}
if (!supportedFormats.hasOwnProperty(_outputFormat)) {
return lib.operationOutcome(res, `The "${_outputFormat}" _outputFormat is not supported`, { httpCode: 400 });
}
this.outputFormat = supportedFormats[_outputFormat as keyof typeof supportedFormats] as keyof typeof exportTypes;
this.requestStart = Date.now();
this.secure = !!req.headers.authorization;
const proto = req.headers["x-forwarded-proto"] || req.protocol;
this.request = proto + "://" + req.headers.host + req.originalUrl;
// Prepare the status URL
let url = config.baseUrl + req.originalUrl.split("?").shift()!.replace(
/(\/[^/]+)?\/fhir\/.*/,
`/fhir/bulkstatus/${this.id}`
);
this.jobStatus = "STARTED";
const abortSignal = this.getAbortController().signal;
await this.save();
lib.abortablePromise(this.buildManifest(abortSignal), abortSignal).catch(e => {
if (!(e instanceof lib.AbortError)) {
console.error(e)
}
});
// Instead of generating the response, and then returning it, the server
// returns a 202 Accepted header, and a Content-Location at which the
// client can use to access the response.
// HTTP/1.1 202 Accepted
res.set("Content-Location", url);
lib.outcomes.exportAccepted(res, url);
}
/**
* Finds all the resource types included in this export and their count
*/
async getResourceTypes(): Promise<{ fhir_type: string, rowCount: number }[]> {
const builder = new QueryBuilder({
type : this.resourceTypes,
patients : this.patients,
group : this.group,
systemLevel: this.systemLevel,
start : this.since
});
const { sql, params } = builder.compileCount();
const DB = getDB(this.stu);
return DB.promise("all", sql, params);
}
/**
* Creates and returns a ReadableStream of JSON FHIR resources
*/
getStreamForResource(resourceType: string, limit: number) {
return new fhirStream({
types : [resourceType],
stu : this.stu,
databaseMultiplier: this.databaseMultiplier,
extended : this.extended,
group : this.group,
since : this.since,
systemLevel: this.systemLevel,
patients : this.patients,
offset : 0,
filter : this.typeFilter.get("_filter"),
limit
});
}
async getCountsForResourceType(resourceType: string, limit: number): Promise<number> {
return new Promise((resolve, reject) => {
let input = this.getStreamForResource(resourceType, limit);
input.init().then(() => {
let count = 0;
input.once("error", reject)
input.once("close", () => resolve(count))
input.on("data", () => { count += 1; })
}, reject);
});
}
async buildManifest(signal: AbortSignal) {
let requestStart = moment(this.requestStart);
const manifest: ExportManifest = {
// a FHIR instant type that indicates the server's time when the
// query is run. No resources that have a modified data after this
// instant should be in the response.
"transactionTime": requestStart + "",
// the full url of the original bulk data kick-off request
"request" : this.request,
// boolean value indicating whether downloading the generated files
// will require an authentication token. Note: This may be false in
// the case of signed S3 urls or an internal file server within an
// organization's firewall.
"requiresAccessToken": this.secure,
// array of bulk data file items with one entry for each generated
// file. Note: If no data is returned from the kick-off request,
// the server should return an empty array.
"output" : [],
// When a _since timestamp is supplied in the export request,
// this array SHALL be populated with output files containing
// FHIR Transaction Bundles that indicate which FHIR resources
// would have been returned, but have been deleted subsequent to
// that date. If no resources have been deleted or the _since
// parameter was not supplied, the server MAY omit this key
// or MAY return an empty array.
"deleted": [],
// If no errors occurred, the server should return an empty array
"error": []
};
try {
var resourceTypes = await lib.abortablePromise(this.getResourceTypes(), signal);
} catch (e) {
if (!(e instanceof lib.AbortError)) {
console.error(e)
}
return null
}
const totalResourceCount = resourceTypes.reduce((prev, cur) => prev + cur.rowCount, 0) * this.databaseMultiplier;
const addDeleted = (index: number, resourceType: string, count: number, offset: number, filteredCount: number) => {
manifest.deleted!.push({
type: "Bundle",
count: filteredCount,
url: lib.buildUrlPath(
config.baseUrl,
base64url.encode(JSON.stringify({
id : this.id,
limit : count,
del : 1,
secure: this.secure,
offset
})),
"/fhir/bulkfiles/",
`${index + 1}.${resourceType}.${this.outputFormat}`
)
});
}
const addError = (index: number, resourceType: string, error: string) => {
manifest.error.push({
type : "OperationOutcome",
url: lib.buildUrlPath(
config.baseUrl,
base64url.encode(JSON.stringify({
id: this.id,
secure: this.secure,
fileError: error
})),
"/fhir/bulkfiles/",
`${index + 1}.${resourceType}.${this.outputFormat}`
)
});
};
const addFile = (index: number, resourceType: string, count: number, offset: number, filteredCount: number) => {
manifest.output.push({
type: resourceType,
count: filteredCount,
url: lib.buildUrlPath(
config.baseUrl,
base64url.encode(JSON.stringify({
id : this.id,
offset,
limit : count,
secure: this.secure,
})),
"/fhir/bulkfiles/",
`${index + 1}.${resourceType}.${this.outputFormat}`
)
});
};
for (const { fhir_type, rowCount } of resourceTypes) {
if (signal.aborted) {
return null;
}
this.statusMessage = `currently processing ${fhir_type} resources`;
await this.save()
let resourceCount = rowCount * this.databaseMultiplier;
let filteredCount = resourceCount;
// If a filter is used we need to actually loop tru, filter and count how many
// resources would remain after the filter
if (this.typeFilter.get("_filter")) {
try {
filteredCount = await lib.abortablePromise<number>(this.getCountsForResourceType(fhir_type, resourceCount), signal);
} catch (e) {
if (!(e instanceof lib.AbortError)) {
console.error(e)
return null
}
}
}
if (filteredCount > 0) {
const numFiles = Math.ceil(filteredCount / this.resourcesPerFile);
for (let i = 0; i < numFiles; i++) {
// ~ half of the links might fail if such error is requested
if (this.simulatedError == "some_file_generation_failed" && i % 2) {
addError(i, fhir_type, `Failed to export ${i + 1}.${fhir_type}.${this.outputFormat}`);
}
// Add normal download link
else {
let offset = this.resourcesPerFile * i;
let count = Math.min(this.resourcesPerFile, filteredCount - offset);
// Here we know we have a list of {count} resources that
// we can put into a file by generating the proper link
// to it. However, if {this.simulateDeletedPct} is set,
// certain percentage of them should go into the
// "deleted" array instead!
if (this.simulateDeletedPct && this.since) {
let cnt = Math.round(count/100 * this.simulateDeletedPct);
if (cnt) {
addDeleted(
i,
fhir_type,
cnt,
offset,
Math.min(this.resourcesPerFile, Math.abs(filteredCount - offset))
);
count -= cnt;
offset += cnt;
}
}
addFile(
i,
fhir_type,
count,
offset,
Math.max(Math.min(this.resourcesPerFile, filteredCount - offset), 0)
);
}
// Limit the manifest size based on total number of file links
if (manifest.output.length + manifest.error.length + manifest.deleted!.length > config.maxFiles) {
this.tooManyFiles = true;
await this.save();
return null;
}
}
}
this.progress += (rowCount * this.databaseMultiplier / totalResourceCount * 100)
// console.log(`Progress =======> ${this.progress}%`);
await this.save()
}
this.manifest = manifest
await this.save()
return manifest
}
async handleStatus(req: Request, res: Response) {
if (this.secure && !req.headers.authorization) {
return lib.operationOutcome(res, "Not authorized", { httpCode: 401 });
}
if (this.tooManyFiles) {
return res.status(413).send("Too many files");
}
// const signal = this.getAbortController().signal
if (!this.ignoreTransientError && this.simulatedError == "transient_error") {
this.ignoreTransientError = true;
await this.save();
return lib.operationOutcome(
res,
"An unknown error ocurred (transient_error). Please try again.",
{
httpCode : 500,
issueCode: "transient"
}
);
}
if (this.jobStatus === "EXPORTED") {
return lib.outcomes.cancelCompleted(res);
}
// ensure requestStart param is present
// let requestStart = moment(this.requestStart);
// If waiting - show progress and exit
if (this.typeFilter.get("_filter") || this.simulatedExportDuration <= 0) {
if (Math.round(this.progress) < 100) {
return res.set({
"X-Progress" : Math.round(this.progress) + "% complete, " + this.statusMessage,
"Retry-After": 1
}).status(202).end();
}
}
else {
// check if the user should (continue to) wait
let requestStart = moment(this.requestStart);
let endTime = moment(requestStart).add(this.simulatedExportDuration, "seconds");
let now = moment();
// If waiting - show progress and exit
if (endTime.isAfter(now, "second")) {
let diff = (+now - +requestStart)/1000;
let pct = Math.round((diff / this.simulatedExportDuration) * 100);
return res.set({
"X-Progress" : pct + "% complete, " + this.statusMessage,
"Retry-After": 2//Math.ceil(this.simulatedExportDuration - diff)
}).status(202).end();
}
}
if (!this.manifest) {
return lib.operationOutcome(res, "Failed to build export manifest.", {
httpCode : 400,
// issueCode: "transient"
});
}
this.jobStatus = "EXPORTED";
await this.save()
res.set({
"Expires": new Date(this.createdAt + config.maxExportAge * 60000).toUTCString()
}).json(this.manifest);
};
async download(req: Request, res: Response)
{
const sim = (req as RequestWithSim).sim
if (this.secure && !req.headers.authorization) {
return lib.operationOutcome(res, "Not authorized", { httpCode: 401 });
}
if (this.secure) {
const grantedScopes = lib.getGrantedScopes(req)
const resourceType = req.params.file.split(".")[1]
const hasAccess = grantedScopes.scopes.some(scope => {
return (scope.level === "*" || scope.level === "system") &&
(scope.resource === "*" || scope.resource === resourceType) &&
scope.actions.has("read");
})
if (!hasAccess) {
return lib.operationOutcome(res, "Permission denied", { httpCode: 403 });
}
}
if (this.jobStatus !== "EXPORTED") {
return lib.outcomes.exportNotCompleted(res);
}
// console.log(req.sim, this)
const fileError = sim.fileError;
// early exit in case simulated errors
if (this.simulatedError == "file_expired") {
return lib.outcomes.fileExpired(res);
}
// early exit in case simulated file errors
if (fileError) {
return res.set({ "Content-Type": "application/fhir+ndjson" })
.end(JSON.stringify(lib.createOperationOutcome(sim.fileError!)));
}
const acceptEncoding = String(req.headers["accept-encoding"] || "");
const shouldDeflate = (/\bdeflate\b/.test(acceptEncoding));
const shouldGzip = (/\bgzip\b/.test(acceptEncoding));
// set the response headers
res.set({
"Content-Type": exportTypes[this.outputFormat].contentType,
"Content-Disposition": "attachment"
});
/* istanbul ignore else */
if (shouldGzip) {
res.set({ "Content-Encoding": "gzip" });
}
else if (shouldDeflate) {
res.set({ "Content-Encoding": "deflate" });
}
let input = new fhirStream({
types : [req.params.file.split(".")[1]],
stu : this.stu,
databaseMultiplier: this.databaseMultiplier,
extended : this.extended,
group : this.group,
limit : sim.limit,
offset : sim.offset,
since : this.since,
systemLevel: this.systemLevel,
patients : this.patients,
filter : this.typeFilter.get("_filter")
});
input.on("error", error => {
console.error(error);
return res.status(500).end();
});
input.init().then(() => {
let pipeline = input.pipe(translator({
_elements : this.fhirElements,
err : this.fileError,
deleted : !!sim.del,
secure : this.secure
}));
const transform = exportTypes[this.outputFormat].transform;
if (transform) {
pipeline = pipeline.pipe(transform({ extended: this.extended }));
}
/* istanbul ignore else */
if (shouldGzip) {
pipeline = pipeline.pipe(zlib.createGzip())
}
else if (shouldDeflate) {
pipeline = pipeline.pipe(zlib.createDeflate())
}
pipeline.pipe(res);
});
}
/**
* After a bulk data request has been started, a client MAY send a DELETE
* request to the URL provided in the Content-Location header to cancel the
* request.
*
* If the request has been completed, a server MAY use the request as a
* signal that a client is done retrieving files and that it is safe for the
* sever to remove those from storage.
*
* Following the delete request, when subsequent requests are made to the
* polling location, the server SHALL return a 404 error and an associated
* FHIR OperationOutcome in JSON format.
*/
cancel(res: Response)
{
this.delete();
return lib.outcomes.cancelAccepted(res);