-
Notifications
You must be signed in to change notification settings - Fork 2
/
plan.ts
717 lines (631 loc) · 23.8 KB
/
plan.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
import type { Express, Request, Response } from 'express';
import rateLimit from 'express-rate-limit';
import multer from 'multer';
import { parse } from 'csv-parse';
import fetch from 'node-fetch';
import { Readable } from 'stream';
import { auth } from '../auth/middleware.js';
import { parseJSONFile } from '../../util/fileParser.js';
import { convertDateToDoy, getTimeDifference } from '../../util/time.js';
import { HasuraError } from '../../types/hasura.js';
import type {
ActivityDirective,
ActivityDirectiveInsertInput,
ImportPlanPayload,
PlanInsertInput,
PlanSchema,
PlanTagsInsertInput,
PlanTransfer,
Tag,
} from '../../types/plan.js';
import {
ProfileSegment,
ProfileSet,
ProfileSets,
UploadPlanDatasetJSON,
UploadPlanDatasetPayload,
} from '../../types/dataset.js';
import gql from './gql.js';
import getLogger from '../../logger.js';
import { getEnv } from '../../env.js';
const upload = multer();
const logger = getLogger('packages/plan/plan');
const { RATE_LIMITER_LOGIN_MAX, HASURA_API_URL } = getEnv();
const GQL_API_URL = `${HASURA_API_URL}/v1/graphql`;
// Limit imposed by Jetty server
const EXTERNAL_DATASET_MAX_SIZE = 1024;
const refreshLimiter = rateLimit({
legacyHeaders: false,
max: RATE_LIMITER_LOGIN_MAX,
standardHeaders: true,
windowMs: 15 * 60 * 1000, // 15 minutes
});
const timeColumnKey = 'time_utc';
async function importPlan(req: Request, res: Response) {
const authorizationHeader = req.get('authorization');
const {
headers: { 'x-hasura-role': roleHeader, 'x-hasura-user-id': userHeader },
} = req;
const { body, file } = req;
const { name, model_id, start_time, duration, simulation_template_id, tags } = body as ImportPlanPayload;
logger.info(`POST /importPlan: Importing plan: ${name}`);
const headers: HeadersInit = {
Authorization: authorizationHeader ?? '',
'Content-Type': 'application/json',
'x-hasura-role': roleHeader ? `${roleHeader}` : '',
'x-hasura-user-id': userHeader ? `${userHeader}` : '',
};
let createdPlan: PlanSchema | null = null;
let createdTags: Tag[] = [];
try {
const { activities, simulation_arguments }: PlanTransfer = await parseJSONFile<PlanTransfer>(file);
// create the new plan first
logger.info(`POST /importPlan: Creating new plan: ${name}`);
const planInsertInput: PlanInsertInput = {
duration,
model_id,
name,
start_time,
};
const planCreationResponse = await fetch(GQL_API_URL, {
body: JSON.stringify({ query: gql.CREATE_PLAN, variables: { plan: planInsertInput } }),
headers,
method: 'POST',
});
const planCreationResponseJSON = (await planCreationResponse.json()) as {
data: {
createPlan: any;
};
};
if (planCreationResponseJSON != null && planCreationResponseJSON.data != null) {
createdPlan = planCreationResponseJSON.data.createPlan;
if (createdPlan) {
// associate specified simulation parameters to new plan
logger.info(`POST /importPlan: Associating simulation parameters: ${name}`);
const simulationInput = {
arguments: simulation_arguments,
simulation_template_id,
};
await fetch(GQL_API_URL, {
body: JSON.stringify({
query: gql.UPDATE_SIMULATION,
variables: { plan_id: createdPlan.id, simulation: simulationInput },
}),
headers,
method: 'POST',
});
// insert all the imported activities into the plan
logger.info(`POST /importPlan: Importing activities: ${name}`);
const tagsResponse = await fetch(GQL_API_URL, {
body: JSON.stringify({
query: gql.GET_TAGS,
}),
headers,
method: 'POST',
});
const tagsResponseJSON = (await tagsResponse.json()) as {
data: {
tags: Tag[];
};
};
let tagsMap: Record<string, Tag> = {};
if (tagsResponseJSON != null && tagsResponseJSON.data != null) {
const {
data: { tags },
} = tagsResponseJSON;
tagsMap = tags.reduce((prevTagsMap: Record<string, Tag>, tag) => {
return {
...prevTagsMap,
[tag.name]: tag,
};
}, {});
}
// derive a map of uniquely named tags from the list of activities that doesn't already exist in the database
const activityTags = activities.reduce(
(prevActivitiesTagsMap: Record<string, Pick<Tag, 'color' | 'name'>>, { tags }) => {
const currentTagsMap =
tags?.reduce(
(prevTagsMap: Record<string, Pick<Tag, 'color' | 'name'>>, { tag: { name: tagName, color } }) => {
// If the tag doesn't exist already, add it
if (tagsMap[tagName] === undefined) {
return {
...prevTagsMap,
[tagName]: {
color,
name: tagName,
},
};
}
return prevTagsMap;
},
{},
) ?? {};
return {
...prevActivitiesTagsMap,
...currentTagsMap,
};
},
{},
);
const createdTagsResponse = await fetch(GQL_API_URL, {
body: JSON.stringify({
query: gql.CREATE_TAGS,
variables: { tags: Object.values(activityTags) },
}),
headers,
method: 'POST',
});
const { data } = (await createdTagsResponse.json()) as {
data: {
insert_tags: { returning: Tag[] };
};
};
if (data && data.insert_tags && data.insert_tags.returning.length) {
// track the newly created tags for cleanup if an error occurs during plan import
createdTags = data.insert_tags.returning;
}
// add the newly created tags to the `tagsMap`
tagsMap = createdTags.reduce(
(prevTagsMap: Record<string, Tag>, tag) => ({
...prevTagsMap,
[tag.name]: tag,
}),
tagsMap,
);
const activityRemap: Record<number, number> = {};
const activityDirectivesInsertInput = activities.map(
({
anchored_to_start: anchoredToStart,
arguments: activityArguments,
metadata,
name: activityName,
start_offset: startOffset,
tags,
type,
}) => {
const activityDirectiveInsertInput: ActivityDirectiveInsertInput = {
anchor_id: null,
anchored_to_start: anchoredToStart,
arguments: activityArguments,
metadata,
name: activityName,
plan_id: (createdPlan as PlanSchema).id,
start_offset: startOffset,
tags: {
data:
tags?.map(({ tag: { name } }) => ({
tag_id: tagsMap[name].id,
})) ?? [],
},
type,
};
return activityDirectiveInsertInput;
},
);
const createdActivitiesResponse = await fetch(GQL_API_URL, {
body: JSON.stringify({
query: gql.CREATE_ACTIVITY_DIRECTIVES,
variables: {
activityDirectivesInsertInput,
},
}),
headers,
method: 'POST',
});
const createdActivityDirectivesData = (await createdActivitiesResponse.json()) as {
data: {
insert_activity_directive: {
returning: ActivityDirective[];
};
};
} | null;
if (createdActivityDirectivesData) {
const {
data: {
insert_activity_directive: { returning: createdActivityDirectives },
},
} = createdActivityDirectivesData;
if (createdActivityDirectives.length === activities.length) {
createdActivityDirectives.forEach((createdActivityDirective, index) => {
const { id } = activities[index];
activityRemap[id] = createdActivityDirective.id;
});
} else {
throw new Error('Activity insertion failed.');
}
}
// remap all the anchor ids to the newly created activity directives
logger.info(`POST /importPlan: Re-assigning anchors: ${name}`);
const activityDirectivesSetInput = activities
.filter(({ anchor_id: anchorId }) => anchorId !== null)
.map(({ anchor_id: anchorId, id }) => ({
_set: { anchor_id: activityRemap[anchorId as number] },
where: { id: { _eq: activityRemap[id] }, plan_id: { _eq: (createdPlan as PlanSchema).id } },
}));
await fetch(GQL_API_URL, {
body: JSON.stringify({
query: gql.UPDATE_ACTIVITY_DIRECTIVES,
variables: {
updates: activityDirectivesSetInput,
},
}),
headers,
method: 'POST',
});
// associate the tags with the newly created plan
logger.info(`POST /importPlan: Importing plan tags: ${name}`);
const parsedTags: number[] = JSON.parse(tags);
const tagsInsert: PlanTagsInsertInput[] = parsedTags.map(tagId => ({
plan_id: (createdPlan as PlanSchema).id,
tag_id: tagId,
}));
await fetch(GQL_API_URL, {
body: JSON.stringify({ query: gql.CREATE_PLAN_TAGS, variables: { tags: tagsInsert } }),
headers,
method: 'POST',
});
logger.info(`POST /importPlan: Imported plan: ${name}`);
}
res.json(createdPlan);
} else {
throw Error('Plan creation unsuccessful.');
}
} catch (error) {
logger.error(`POST /importPlan: Error occurred during plan ${name} import`);
logger.error(error);
// cleanup the imported plan if it failed along the way
if (createdPlan) {
// delete the plan - activities associated to the plan will be automatically cleaned up
await fetch(GQL_API_URL, {
body: JSON.stringify({ query: gql.DELETE_PLAN, variables: { id: createdPlan.id } }),
headers,
method: 'POST',
});
// if any activity tags were created as a result of this import, remove them
await fetch(GQL_API_URL, {
body: JSON.stringify({ query: gql.DELETE_TAGS, variables: { tagIds: createdTags.map(({ id }) => id) } }),
headers,
method: 'POST',
});
}
res.status(500);
res.send((error as Error).message);
}
}
function profileHasSegments(profileSets: ProfileSets): boolean {
const profileKeys = Object.keys(profileSets);
for (let i = 0; i < profileKeys.length; i++) {
if (profileSets[profileKeys[i]].segments.length) {
return true;
}
}
return false;
}
function getSegmentByteSize(segment: ProfileSegment): number {
return Buffer.byteLength(JSON.stringify(segment));
}
async function uploadDataset(req: Request, res: Response) {
const authorizationHeader = req.get('authorization');
const {
headers: { 'x-hasura-role': roleHeader, 'x-hasura-user-id': userHeader },
} = req;
const { body, file } = req;
const { plan_id: planIdString, simulation_dataset_id: simulationDatasetIdString } = body as UploadPlanDatasetPayload;
const headers: HeadersInit = {
Authorization: authorizationHeader ?? '',
'Content-Type': 'application/json',
'x-hasura-role': roleHeader ? `${roleHeader}` : '',
'x-hasura-user-id': userHeader ? `${userHeader}` : '',
};
let createdDatasetId: number | undefined;
try {
const planId: number = parseInt(planIdString);
const simulationDatasetId: number | undefined =
simulationDatasetIdString != null ? parseInt(simulationDatasetIdString) : undefined;
const matches = file?.originalname?.match(/\.(?<extension>\w+)$/);
if (file && matches != null) {
const { groups: { extension = '' } = {} } = matches;
logger.info(`POST /uploadDataset: Uploading plan dataset`);
let uploadedPlanDataset: UploadPlanDatasetJSON;
switch (extension) {
case 'json':
uploadedPlanDataset = await parseJSONFile<UploadPlanDatasetJSON>(file);
break;
case 'csv':
case 'txt': {
const parsedCSV: string[][] = [];
await new Promise((resolve, reject) => {
const parser = parse({
delimiter: ',',
});
parser.on('readable', () => {
let record;
while ((record = parser.read()) !== null) {
parsedCSV.push(record);
}
});
parser.on('error', error => {
reject(error);
});
parser.on('end', () => {
resolve(parsedCSV);
});
const fileStream = Readable.from(file.buffer);
fileStream.pipe(parser);
});
// Keep track of the time column's index separately since the name of the column is static
let timeColumnIndex = -1;
// Create a lookup for the profile name's index in each CSV row
const headerIndexMap: Record<string, number> = parsedCSV[0].reduce(
(prevHeaderIndexMap: Record<string, number>, header: string, headerIndex: number) => {
if (new RegExp(timeColumnKey).test(header)) {
timeColumnIndex = headerIndex;
return prevHeaderIndexMap;
} else {
return {
...prevHeaderIndexMap,
[header]: headerIndex,
};
}
},
{},
);
if (timeColumnIndex === -1) {
throw new Error(`CSV file does not contain a "${timeColumnKey}" column.`);
}
const parsedSegments: string[][] = parsedCSV.slice(1);
// Use the first entry's time value in the CSV as the dataset start time
const startTime = convertDateToDoy(parsedSegments[0][timeColumnIndex]);
const parsedProfiles: ProfileSets = Object.keys(headerIndexMap).reduce(
(previousProfileSet: ProfileSets, header) => {
return {
...previousProfileSet,
[header]: {
// default CSV profile schemas to `real` and type `discrete`
schema: { type: 'real' },
segments: [],
type: 'discrete',
},
};
},
{},
);
uploadedPlanDataset = parsedSegments.reduce(
(
previousPlanDataset: UploadPlanDatasetJSON,
parsedSegment: string[],
parsedSegmentIndex,
parsedSegmentsArray,
) => {
const nextParsedSegment = parsedSegmentsArray[parsedSegmentIndex + 1];
// Only process entries that have an entry after it.
// The last entry is ignored on purpose as it is only used to get the duration of the previous entry
if (nextParsedSegment) {
const duration = getTimeDifference(parsedSegment[timeColumnIndex], nextParsedSegment[timeColumnIndex]);
if (duration) {
const profileSet: ProfileSets = Object.entries(headerIndexMap).reduce(
(previousProfileSet: ProfileSets, [header, index]) => {
const previousSegments = previousProfileSet[header].segments;
const value = parsedSegment[index];
return {
...previousProfileSet,
[header]: {
...previousProfileSet[header],
segments: [
...previousSegments,
{ duration, ...(value !== undefined ? { dynamics: parseFloat(value) } : {}) },
],
},
};
},
previousPlanDataset.profileSet,
);
return {
...previousPlanDataset,
profileSet,
} as UploadPlanDatasetJSON;
}
}
return previousPlanDataset;
},
{ datasetStart: startTime, profileSet: parsedProfiles } as UploadPlanDatasetJSON,
);
break;
}
default:
throw new Error('File extension not supported');
}
const { datasetStart, profileSet } = uploadedPlanDataset;
const profileNames = Object.keys(profileSet);
// Insert an initial set of profiles that have empty segments
const initialProfileSet: ProfileSets = profileNames.reduce(
(currentProfileSet: ProfileSets, profileName: string) => {
return {
...currentProfileSet,
[profileName]: {
...profileSet[profileName],
segments: [],
},
};
},
{},
);
const response = await fetch(GQL_API_URL, {
body: JSON.stringify({
query: gql.ADD_EXTERNAL_DATASET,
variables: { datasetStart, planId, profileSet: initialProfileSet, simulationDatasetId },
}),
headers,
method: 'POST',
});
type AddExternalDatasetResponse = { data: { addExternalDataset: { datasetId: number } | null } };
const jsonResponse = await response.json();
const addExternalDatasetResponse = jsonResponse as AddExternalDatasetResponse | HasuraError;
// If the initial insert was successful, follow-up with multiple inserts to add the segments to each profile
if ((addExternalDatasetResponse as AddExternalDatasetResponse).data?.addExternalDataset != null) {
logger.info(`POST /uploadDataset: Uploaded initial plan dataset`);
createdDatasetId = (addExternalDatasetResponse as AddExternalDatasetResponse).data.addExternalDataset
?.datasetId;
// Repeat as long as the is at least one profile with a segment left
while (profileHasSegments(profileSet)) {
// Initialize profile payload
let currentProfileSet: ProfileSets = initialProfileSet;
// Get the initial profile payload byte size
let currentProfileSize: number = Buffer.byteLength(JSON.stringify(currentProfileSet));
let isMaxSizeReached: boolean = false;
// Repeat until the maximum payload size is reached or there are no more segments left within the profile to send
while (profileHasSegments(profileSet) && !isMaxSizeReached) {
for (let i = 0; i < profileNames.length; i++) {
const profileName = profileNames[i];
const profileSegments = profileSet[profileName].segments;
const nextProfileSegment = profileSegments[0];
const nextProfileSegmentSize = nextProfileSegment ? getSegmentByteSize(nextProfileSegment) : 0;
if (nextProfileSegment !== undefined) {
// Check to see if including the next segment will be under the maximum payload size
if (currentProfileSize + nextProfileSegmentSize < EXTERNAL_DATASET_MAX_SIZE) {
// Add the next segment to the current profile set
currentProfileSet = {
...currentProfileSet,
[profileName]: {
...currentProfileSet[profileName],
segments: [...currentProfileSet[profileName].segments, nextProfileSegment],
} as ProfileSet,
};
// Mutate the array to remove the segment that we just copied
profileSegments.shift();
currentProfileSize += nextProfileSegmentSize;
} else {
isMaxSizeReached = true;
break;
}
}
}
}
logger.info(`POST /uploadDataset: Uploading extended plan dataset to dataset: ${createdDatasetId}`);
await fetch(GQL_API_URL, {
body: JSON.stringify({
query: gql.EXTEND_EXTERNAL_DATASET,
variables: { datasetId: createdDatasetId, profileSet: currentProfileSet },
}),
headers,
method: 'POST',
});
logger.info(`POST /uploadDataset: Uploaded extended plan dataset to dataset: ${createdDatasetId}`);
}
res.json(createdDatasetId);
} else if ((addExternalDatasetResponse as HasuraError).errors) {
throw new Error(JSON.stringify((addExternalDatasetResponse as HasuraError).errors));
} else {
throw new Error('Plan dataset upload unsuccessful.');
}
} else {
throw new Error('File extension not supported');
}
} catch (error) {
logger.error(`POST /uploadDataset: Error occurred during plan dataset upload`);
logger.error(error);
// cleanup the plan dataset if it failed along the way
if (createdDatasetId !== undefined) {
// delete the dataset - profiles associated to the plan will be automatically cleaned up
await fetch(GQL_API_URL, {
body: JSON.stringify({ query: gql.DELETE_EXTERNAL_DATASET, variables: { id: createdDatasetId } }),
headers,
method: 'POST',
});
}
res.status(500);
res.send((error as Error).message);
}
}
export default (app: Express) => {
/**
* @swagger
* /importPlan:
* post:
* security:
* - bearerAuth: []
* consumes:
* - multipart/form-data
* produces:
* - application/json
* parameters:
* - in: header
* name: x-hasura-role
* schema:
* type: string
* required: false
* requestBody:
* content:
* multipart/form-data:
* schema:
* type: object
* properties:
* plan_file:
* format: binary
* type: string
* name:
* type: string
* model_id:
* type: integer
* start_time:
* type: string
* duration:
* type: string
* sim_id:
* type: integer
* tags:
* type: string
* responses:
* 200:
* description: ImportResponse
* 403:
* description: Unauthorized error
* 401:
* description: Unauthenticated error
* summary: Import a plan JSON file
* tags:
* - Hasura
*/
app.post('/importPlan', upload.single('plan_file'), refreshLimiter, auth, importPlan);
/**
* @swagger
* /uploadDataset:
* post:
* security:
* - bearerAuth: []
* consumes:
* - multipart/form-data
* produces:
* - application/json
* parameters:
* - in: header
* name: x-hasura-role
* schema:
* type: string
* required: false
* requestBody:
* content:
* multipart/form-data:
* schema:
* type: object
* properties:
* external_dataset:
* format: binary
* type: string
* plan_id:
* type: long
* simulation_dataset_id:
* type: integer
* responses:
* 200:
* description: ImportResponse
* 403:
* description: Unauthorized error
* 401:
* description: Unauthenticated error
* summary: Upload an external dataset to a plan
* tags:
* - Hasura
*/
app.post('/uploadDataset', upload.single('external_dataset'), refreshLimiter, auth, uploadDataset);
};