-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathconcept.js
More file actions
1058 lines (867 loc) · 31.2 KB
/
concept.js
File metadata and controls
1058 lines (867 loc) · 31.2 KB
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 camelcaseKeys from 'camelcase-keys'
import dasherize from 'dasherize'
import snakeCaseKeys from 'snakecase-keys'
import get from 'lodash/get'
import pick from 'lodash/pick'
import snakeCase from 'lodash/snakeCase'
import { v4 as uuidv4 } from 'uuid'
import { CONCEPT_TYPES } from '../../constants'
import { cmrDelete } from '../../utils/cmrDelete'
import { cmrIngest } from '../../utils/cmrIngest'
import { cmrQuery } from '../../utils/cmrQuery'
import { downcaseKeys } from '../../utils/downcaseKeys'
import { mergeParams } from '../../utils/mergeParams'
import { parseError } from '../../utils/parseError'
import { pickIgnoringCase } from '../../utils/pickIgnoringCase'
export default class Concept {
/**
* Instantiates a Concept object
* @param {String} conceptType The CMR concept type to query against
* @param {Object} headers HTTP headers provided by the query
* @param {Object} requestInfo Parsed data pertaining to the Graph query
* @param {Object} params GraphQL query parameters
*/
constructor(conceptType, headers = {}, requestInfo = {}, params = {}) {
// Set properties for data available during instantiation
this.conceptType = conceptType
// Make a copy of headers so that new additional headers do not get propogated to any sub query calls
this.headers = { ...headers }
this.requestInfo = requestInfo
// Defaults the result set to an empty object
this.items = {}
// Initialize format specific properties
this.jsonItemCount = 0
this.jsonSearchAfterIdentifier = undefined
this.ummItemCount = 0
this.ummSearchAfterIdentifier = undefined
this.params = params
this.arrayifiableKeys = {
collectionConceptIds: 'collectionConceptId',
collectionProgresses: 'collectionProgress',
conceptIds: 'conceptId',
dataCenters: 'dataCenter',
providers: 'provider',
providerIds: 'providerId',
permittedGroups: 'permittedGroup',
shortNames: 'shortName'
}
}
/**
* If a plural key is provided it will take the value but 'convert' the key
* to singular but keep the array of values. This is done so that we can offer
* two different keys (singular and plural) within the schema.
* @param {Object} searchParams All provided search parameters requested
*/
arrayifyParams(searchParams) {
const arrayified = searchParams
Object.keys(this.arrayifiableKeys).forEach((pluralKey) => {
const { [pluralKey]: providedValues = [] } = arrayified
// If a value exists
if (providedValues.length > 0) {
arrayified[this.arrayifiableKeys[pluralKey]] = providedValues
}
// Delete the plural key because its not supported by CMR
delete arrayified[pluralKey]
})
return arrayified
}
/**
* Set a specific property on an object within the result set of an ingest operation
* @param {String} key The key to set within the result
* @param {Any} value They value to assign to the key within the result
*/
setIngestValue(key, value) {
this.items[key] = value
}
/**
* Set a specific property on an object within the result set of a search operation
* @param {String} id Concept ID to set a value for within the result set
* @param {String} key The key to set within the result
* @param {Any} value They value to assign to the key within the result
*/
setItemValue(id, key, value) {
// Ensure that our object knows about the item by creating it if it doesn't exist
this.getItem(id)
// Set the value
this.items[id][key] = value
}
/**
* Set the total number of records available for a given search to the json endpoint
* @param {Integer} itemCount The total number of records available for a given search
*/
setJsonItemCount(itemCount) {
this.jsonItemCount = parseInt(itemCount, 10)
}
/**
* Set the total number of records available for a given search to the json endpoint
* @param {Integer} itemCount The total number of records available for a given search
*/
setUmmItemCount(itemCount) {
this.ummItemCount = parseInt(itemCount, 10)
}
/**
* Set the search after identifier for a search against against the json endpoint
* @param {Float} searchAfterIdentifier The search after identifier provided by CMR
*/
setJsonSearchAfter(searchAfterIdentifier) {
this.jsonSearchAfterIdentifier = searchAfterIdentifier
}
/**
* Set the search after identifier for a search against against the umm endpoint
* @param {Float} searchAfterIdentifier The search after identifier provided by CMR
*/
setUmmSearchAfter(searchAfterIdentifier) {
this.ummSearchAfterIdentifier = searchAfterIdentifier
}
/**
* Set a value in the result set that a query has not requested but is necessary for other functionality
* @param {String} id Concept ID to set a value for within the result set
* @param {Object} item The item returned from the CMR json endpoint
*/
setEssentialJsonValues(id, item) {
const { associations, association_details: associationDetails } = item
const formattedAssociationDetails = camelcaseKeys(associationDetails, { deep: true })
if (associationDetails) {
this.setItemValue(id, 'associationDetails', formattedAssociationDetails)
}
if (associations) {
this.setItemValue(id, 'associations', associations)
}
}
/**
* Set a value in the result set that a query has not requested but is necessary for other functionality
* @param {String} id Concept ID to set a value for within the result set
* @param {Object} item The item returned from the CMR json endpoint
*/
setEssentialUmmValues(id, item) {
const { meta } = item
const { associations, 'association-details': associationDetails } = meta
const formattedAssociationDetails = camelcaseKeys(associationDetails, { deep: true })
if (associationDetails) {
this.setItemValue(id, 'associationDetails', formattedAssociationDetails)
}
if (associations) {
this.setItemValue(id, 'associations', associations)
}
}
/**
* Get the total number of records available for a given search across all endpoints. Also
* ensure that the value is the same if a given search spans multiple endpoints
*/
getItemCount() {
const { jsonKeys = [], ummKeys = [] } = this.requestInfo
if (jsonKeys.length) {
if (ummKeys.length) {
// Both endpoints returned the same value, return either value here
return this.ummItemCount
}
// Only json keys were requested, return the json item count
return this.jsonItemCount
}
if (ummKeys.length) {
// Only umm keys were requested, return the umm item count
return this.ummItemCount
}
return 0
}
/**
* Fetches missing items for a umm or json endpoint with retry logic
* @param {Array} missingIds - Array of concept IDs for missing items
* @param {Array} keys - Array of requested keys to fetch for each item (ummKeys or jsonKeys)
* @param {Function} fetchFunction - Function to fetch data from CMR (fetchUmm or fetchJson)
* @param {Function} parseFunction - Function to parse the fetched data
* @param {Integer} retryCount - Current retry attempt count
*/
async fetchWithRetry(missingIds, keys, fetchFunction, parseFunction, retryCount = 0) {
const {
maxRetries: maxRetriesEnv,
retryDelay: retryDelayEnv
} = process.env
const maxRetries = parseInt(maxRetriesEnv, 10)
const retryDelay = parseInt(retryDelayEnv, 10)
const response = await fetchFunction(missingIds, keys)
const fetchedItems = parseFunction(response)
if (missingIds.length === fetchedItems.length) {
return { fetchedItems }
}
if (retryCount < maxRetries) {
console.log(`Retry ${retryCount + 1}: ${missingIds} were missing. Retrying...`)
await new Promise((resolve) => { setTimeout(resolve, retryDelay) })
return this.fetchWithRetry(missingIds, keys, fetchFunction, parseFunction, retryCount + 1)
}
throw new Error(`Inconsistent data prevented GraphQL from correctly parsing results (JSON Hits: ${this.jsonItemCount}, UMM Hits: ${this.ummItemCount})`)
}
/**
* Validates the response from CMR and handles inconsistencies between JSON and UMM data
*/
async validateResponse() {
const response = await this.getResponse()
const { jsonKeys, ummKeys, ummKeyMappings } = this.requestInfo
const [jsonResponse, ummResponse] = response
// If both json and umm keys are being requested ensure that each endpoint
// returned the same number of results
if (jsonKeys.length && ummKeys.length) {
if (this.jsonItemCount === this.ummItemCount) {
return true
}
const parsedJsonResponse = this.parseJsonBody(jsonResponse)
const parsedUmmResponse = this.parseUmmBody(ummResponse)
const jsonIds = parsedJsonResponse.map((item) => item.concept_id)
const ummIds = parsedUmmResponse.map((item) => item.meta['concept-id'])
// If the number of concept ids in umm and json match, it mean the same number of items from both endpoint.
// This suggests that the data is consistent, so we can consider the response is valid
if (ummIds.length === jsonIds.length) {
return true
}
// Handles the case where umm data has missing items.
// Attempts to fetch the missing UMM data and add the missing items to existing items
if (ummIds.length < jsonIds.length) {
const missingUmmIds = jsonIds.filter((id) => !ummIds.includes(id))
const { fetchedItems } = await this.fetchWithRetry(
missingUmmIds,
ummKeys,
(params, keys) => this.fetchUmm(params, keys),
(parsedResponse) => this.parseUmmBody(parsedResponse)
)
const currentItems = this.getItems()
this.setUmmItemCount(ummIds.length + fetchedItems.length)
fetchedItems.forEach((item) => {
const { meta } = item
const conceptId = meta['concept-id']
const normalizedItem = this.normalizeUmmItem(item)
const itemKey = Object.keys(currentItems).find((key) => key.startsWith(conceptId))
this.setEssentialUmmValues(itemKey, normalizedItem)
this.setUmmItems(item, itemKey, ummKeys, ummKeyMappings)
})
}
// Handles the case where JSON data has missing items.
// Attempts to fetch the missing JSON data and add the missing items to existing items
if (jsonIds.length < ummIds.length) {
const missingJsonIds = ummIds.filter((id) => !jsonIds.includes(id))
const { fetchedItems } = await this.fetchWithRetry(
missingJsonIds,
jsonKeys,
(params, keys) => this.fetchJson(params, keys),
(parsedResponse) => this.parseJsonBody(parsedResponse)
)
const currentItems = this.getItems()
this.setJsonItemCount(jsonIds.length + fetchedItems.length)
fetchedItems.forEach((item) => {
const normalizedItem = this.normalizeJsonItem(item)
// Find the corresponding item key in the current items
// The key starts with the concept ID (e.g., 'G100000-EDSC-0')
const itemKey = Object.keys(currentItems).find(
(key) => key.startsWith(normalizedItem.concept_id)
)
this.setJsonItems(itemKey, jsonKeys, normalizedItem)
})
}
}
return true
}
/**
* Get the CMR concept type of this object
*/
getConceptType() {
return this.conceptType
}
/**
* Retrieve a single item from the result set
* @param {String} id The concept id of an item to lookup in the result set
*/
getItem(id) {
// If an item with the provided id does not exist, create one
if (Object.keys(this.items).indexOf(id) === -1) {
this.items[id] = {}
}
// Fetch the item from the result set
const { [id]: item } = this.items
return item
}
/**
* Return the raw result set
*/
getItems() {
return this.items
}
/**
* Return the ingest result set formatted for the graphql json response
*/
getFormattedIngestResponse() {
// Retrieve the result set regardless of whether or not the query is a list or not
const items = this.getItems()
return items
}
/**
* Return the delete result set formatted for the graphql json response
*/
getFormattedDeleteResponse() {
// Retrieve the result set regardless of whether or not the query is a list or not
const items = this.getItems()
return items
}
/**
* Return the result set formatted for the graphql json response
*/
getFormattedResponse() {
// Determine if the query was a list or not, list queries return meta
// keys using a slightly different format
const {
isList
} = this.requestInfo
// Retrieve the result set regardless of whether or not the query is a list or not
const items = this.getItems()
if (isList) {
const count = this.getItemCount()
const cursor = this.encodeCursor()
return {
count,
cursor,
items: Object.values(items)
}
}
return Object.values(items)
}
/**
* Returns an array of keys representing supported search params for the json endpoint
*/
getPermittedJsonSearchParams() {
return [
'all_revisions',
'concept_id',
'include_full_acl',
'offset',
'originator_id',
'page_size',
'permitted_user',
'provider_id',
'sort_key',
'tag_key',
'target'
]
}
/**
* Returns an array of keys representing supported search params for the umm endpoint
*/
getPermittedUmmSearchParams() {
return [
'all_revisions',
'concept_id',
'offset',
'page_size',
'provider_id',
'sort_key'
]
}
/**
* Returns an array of keys that should not be indexed when sent to CMR
*/
getNonIndexedKeys() {
return [
'concept_id',
'include_full_acl',
'offset',
'page_size',
'permitted_user',
'provider_id',
'sort_key',
'tag_key',
'target'
]
}
/**
* Retrieve the request id header from the request
* @param {Object} headers The provided headers from the query
* @return {String} Request ID defined in the headers
*/
getRequestId() {
const {
'CMR-Request-Id': requestId
} = this.headers
return requestId
}
/**
* Retrieve the client id header from the request
* @param {Object} headers The provided headers from the query
* @return {String} Client ID defined in the headers
*/
getClientId() {
const {
'Client-Id': clientId
} = this.headers
return clientId
}
/**
* Return the response from the query to CMR
*/
async getResponse() {
return this.response
}
/**
* Decodes and returns a base64 hashed version of the json and umm search after identifier
* @param {String} cursor A base64 hashed object containing search after identifier from CMR
*/
decodeCursor(cursor) {
if (cursor) return JSON.parse(Buffer.from(cursor, 'base64').toString())
return {}
}
/**
* Encode and return a base64 hashed version of the json and umm search after identifier
*/
encodeCursor() {
if (this.jsonSearchAfterIdentifier || this.ummSearchAfterIdentifier) {
return Buffer.from(
JSON.stringify({
json: this.jsonSearchAfterIdentifier,
umm: this.ummSearchAfterIdentifier
})
).toString('base64')
}
return null
}
/**
* Mutate the provided values from the user to meet expectations from CMR
* @param {Object} params Parameters provided by the client
* @returns The payload to send to CMR
*/
mutateIngestParameters(params) {
return params
}
/**
* Mutate the provided values from the user to meet expectations from CMR
* @param {Object} params Parameters provided by the client
* @returns The payload to send to CMR
*/
mutateDeleteParameters(params) {
return params
}
/**
* Merge provided and default headers and ensure they are permitted
* @param {Object} providedHeaders Headers provided by the client
* @returns An object holding acceptable headers and their values
*/
ingestHeaders(providedHeaders) {
return pickIgnoringCase({
Accept: 'application/json',
...providedHeaders
}, [
'Accept',
'Authorization',
'Client-Id',
'Content-Type',
'CMR-Request-Id'
])
}
/**
* Ingest the provided object into the CMR
* @param {Object} data Parameters provided by the query
* @param {Array} requestedKeys Keys requested by the query
* @param {Object} providedHeaders Headers requested by the query
*/
ingest(data, requestedKeys, providedHeaders, options) {
const params = mergeParams(data)
const {
nativeId = uuidv4(),
providerId,
...filteredParams
} = params
this.logKeyRequest(requestedKeys, 'ingest')
const preparedParameters = this.mutateIngestParameters(filteredParams)
const preparedHeaders = this.ingestHeaders(providedHeaders)
// Construct the promise that will ingest data into CMR
this.response = cmrIngest({
conceptType: this.getConceptType(),
data: preparedParameters,
headers: preparedHeaders,
options: {
path: `ingest/providers/${providerId}/${this.getConceptType()}/${encodeURIComponent(nativeId)}`,
...options
}
})
}
/**
* Delete the provided object from CMR
* @param {Object} data Parameters provided by the query
* @param {Array} requestedKeys Keys requested by the query
* @param {Object} providedHeaders Headers requested by the query
*/
delete(data, requestedKeys, providedHeaders, options) {
const params = mergeParams(data)
this.logKeyRequest(requestedKeys, 'ingest')
const preparedParameters = this.mutateDeleteParameters(params)
// Construct the promise that will delete data from CMR
this.response = cmrDelete({
conceptType: this.getConceptType(),
data: preparedParameters,
headers: providedHeaders,
options
})
}
/**
* Log requested keys for metrics and debugging
* @param {Array} keys List of keys being requested
* @param {String} format Format of the request (json, umm, meta)
*/
logKeyRequest(keys, format) {
// Prevent logging concept types, their meta keys are logged above
const filteredKeys = keys.filter((field) => CONCEPT_TYPES.indexOf(field) === -1)
filteredKeys.forEach((key) => {
console.log(`Request ${this.getRequestId()} from ${this.getClientId()} to [concept: ${this.getConceptType()}] requested [format: ${format}, key: ${key}]`)
})
}
/**
* Query the CMR JSON API endpoint to retrieve requested data
* @param {Object} searchParams Parameters provided by the query
* @param {Array} requestedKeys Keys requested by the query
* @param {Object} providedHeaders Headers requested by the query
*/
fetchJson(searchParams, requestedKeys, providedHeaders) {
this.logKeyRequest(requestedKeys, 'json')
// Construct the promise that will request data from the json endpoint
return cmrQuery({
conceptType: this.getConceptType(),
params: pick(snakeCaseKeys(searchParams), this.getPermittedJsonSearchParams()),
nonIndexedKeys: this.getNonIndexedKeys(),
headers: providedHeaders
})
}
/**
* Query the CMR UMM API endpoint to retrieve requested data
* @param {Object} searchParams Parameters provided by the query
* @param {Array} requestedKeys Keys requested by the query
* @param {Object} providedHeaders Headers requested by the query
*/
fetchUmm(searchParams, requestedKeys, providedHeaders) {
this.logKeyRequest(requestedKeys, 'umm')
const { revisionId } = this.requestInfo
const params = pick(snakeCaseKeys(searchParams), this.getPermittedUmmSearchParams())
// Add all_revisions to params if revisionId is present and all_revisions is not set
if (revisionId && !params.all_revisions) {
params.all_revisions = true
}
// Construct the promise that will request data from the umm endpoint
return cmrQuery({
conceptType: this.getConceptType(),
params,
nonIndexedKeys: this.getNonIndexedKeys(),
headers: providedHeaders,
options: {
format: 'umm_json'
}
})
}
/**
* Query the CMR API
* @param {Object} searchParams Parameters provided by the query
*/
fetch(searchParams) {
const params = mergeParams(searchParams)
// Default an array to hold the promises we need to make depending on the requested fields
const promises = []
const {
jsonKeys,
metaKeys,
ummKeys
} = this.requestInfo
const {
cursor
} = params
if (cursor) {
delete params.cursor
}
const {
json: jsonSearchAfterIdentifier,
umm: ummSearchAfterIdentifier
} = this.decodeCursor(cursor)
this.logKeyRequest(metaKeys, 'meta')
if (jsonKeys.length > 0) {
const jsonHeaders = this.headers
if (jsonSearchAfterIdentifier) {
jsonHeaders['CMR-Search-After'] = jsonSearchAfterIdentifier
}
promises.push(
this.fetchJson(this.arrayifyParams(params), jsonKeys, jsonHeaders)
)
} else {
// Push a null promise to the array so that the umm promise always exists as
// the second element of the promise array
promises.push(
// eslint-disable-next-line no-promise-executor-return
new Promise((resolve) => resolve(null))
)
}
// If any requested keys are umm keys, we need to make an additional request to cmr
if (ummKeys.length > 0) {
const ummHeaders = this.headers
if (ummSearchAfterIdentifier) {
ummHeaders['CMR-Search-After'] = ummSearchAfterIdentifier
}
// Construct the promise that will request data from the umm endpoint
promises.push(
this.fetchUmm(this.arrayifyParams(params), ummKeys, ummHeaders)
)
} else {
promises.push(
// eslint-disable-next-line no-promise-executor-return
new Promise((resolve) => resolve(null))
)
}
this.response = Promise.all(promises)
}
/**
* Rename fields, add fields, modify fields, etc
* @param {Object} item The item returned from the CMR json endpoint
*/
normalizeJsonItem(item) {
return item
}
/**
* Rename fields, add fields, modify fields, etc
* @param {Object} item The item returned from the CMR umm endpoint
*/
normalizeUmmItem(item) {
return item
}
/**
* Parse and return the body of an ingest operation
* @param {Object} ingestResponse HTTP response from the CMR endpoint
*/
parseIngestBody(ingestResponse, ingestKeys) {
const { data } = ingestResponse
ingestKeys.forEach((key) => {
const cmrKey = dasherize(key)
const { [cmrKey]: keyValue } = data
this.setIngestValue(key, keyValue)
})
}
/**
* Parse and return the body of an ingest operation
* @param {Object} ingestResponse HTTP response from the CMR endpoint
*/
parseDeleteBody(ingestResponse, ingestKeys) {
const { data } = ingestResponse
ingestKeys.forEach((key) => {
const cmrKey = dasherize(key)
const { [cmrKey]: keyValue } = data
this.setIngestValue(key, keyValue)
})
}
/**
* Parse and return the array of data from the nested response body
* @param {Object} jsonResponse HTTP response from the CMR endpoint
*/
parseJsonBody(jsonResponse) {
const { data } = jsonResponse
const { feed } = data
const { entry } = feed
return entry
}
/**
* Parse and return the array of data from the nested response body
* @param {Object} ummResponse HTTP response from the CMR endpoint
*/
parseUmmBody(ummResponse) {
const { data } = ummResponse
const { items } = data
return items
}
/**
* Creates unique item keys regardless of whether or not a user calls for data with similar conceptIds (as is the case with revisions)
* @param {Int} itemIndex This method is called in a loop, this is the index of the loop
* @param {Object} normalizedItem The item to generate a key for, after it's been through our normalization method
* @returns A unique key representing this item
*/
generateJsonItemKey(itemIndex, normalizedItem) {
const { concept_id: conceptId } = normalizedItem
return `${conceptId}-${itemIndex}`
}
/**
* Creates unique item keys regardless of whether or not a user calls for data with similar conceptIds (as is the case with revisions)
* @param {Int} itemIndex This method is called in a loop, this is the index of the loop
* @param {Object} normalizedItem The item to generate a key for, after it's been through our normalization method
* @returns A unique key representing this item
*/
generateUmmItemKey(itemIndex, normalizedItem) {
const { meta } = normalizedItem
const { 'concept-id': conceptId } = meta
return `${conceptId}-${itemIndex}`
}
/**
* Sets JSON item values in the result set based on requested keys
* @param {String} itemKey - Unique identifier for the item being processed
* @param {Array} jsonKeys - Array of JSON keys requested in the GraphQL query
* @param {Object} item - Raw item data received from the CMR JSON endpoint
*/
setJsonItems(itemKey, jsonKeys, item) {
jsonKeys.forEach((jsonKey) => {
const cmrKey = snakeCase(jsonKey)
const { [cmrKey]: keyValue } = item
// Snake case the key requested and any children of that key
this.setItemValue(itemKey, jsonKey, keyValue)
})
}
/**
* Parses the response from the json endpoint
* @param {Object} jsonResponse HTTP response from the CMR endpoint
* @param {Array} jsonKeys Array of the keys requested in the query
*/
async parseJson(jsonResponse, jsonKeys) {
const { headers } = jsonResponse
const {
'cmr-hits': cmrHits,
'cmr-search-after': jsonSearchAfterIdentifier
} = downcaseKeys(headers)
this.setJsonItemCount(cmrHits)
this.setJsonSearchAfter(jsonSearchAfterIdentifier)
const items = this.parseJsonBody(jsonResponse)
items.forEach((item, itemIndex) => {
const normalizedItem = this.normalizeJsonItem(item)
const itemKey = this.generateJsonItemKey(itemIndex, normalizedItem)
this.setEssentialJsonValues(itemKey, normalizedItem)
this.setJsonItems(itemKey, jsonKeys, normalizedItem)
})
}
/**
* Sets UMM item values to the result set
* @param {Object} item - Raw item data received from the CMR UMM endpoint
* @param {String} itemKey - Unique identifier for the item being processed
* @param {Array} ummKeys - Array of UMM keys requested in the GraphQL query
* @param {Object} ummKeyMappings - Object mapping UMM keys to their paths in the CMR response
*/
setUmmItems(item, itemKey, ummKeys, ummKeyMappings) {
ummKeys.forEach((ummKey) => {
// Use lodash.get to retrieve a value from the umm response given the
// path we've defined above
let keyValue = get(item, ummKeyMappings[ummKey])
// If the raw `ummMetadata` was requested return that value unaltered
if (ummKey === 'ummMetadata') {
this.setItemValue(
itemKey,
ummKey,
keyValue
)
return
}
// If the UMM Key is `previewMetadata`, we need to combine the `meta` and `umm` fields
// This ensures all the keys are available for the PreviewMetadata union type
if (ummKey === 'previewMetadata') {
keyValue = {
...item.umm,
...item.meta
}
}
if (keyValue != null) {
const camelCasedObject = camelcaseKeys({ [ummKey]: keyValue }, {
deep: true,
exclude: ['RelatedURLs']
})
// CamelcaseKey converts RelatedURLs to relatedUrLs, so excluding RelatedURLs above.
// This will remove RelatedURLs and create a new
// key called relatedUrls and assign the value to it so MMT and graphql response matches.
if (ummKey === 'previewMetadata') {
const { previewMetadata } = camelCasedObject
camelCasedObject.previewMetadata = {
...previewMetadata,
relatedUrls: previewMetadata.RelatedURLs
}
delete camelCasedObject.previewMetadata.RelatedURLs
}
// Camel case all of the keys of this object (ummKey is already camel cased)
const { [ummKey]: camelCasedValue } = camelCasedObject
this.setItemValue(
itemKey,
ummKey,
camelCasedValue
)
}
})
}
/**
* Parses the response from the umm endpoint
* @param {Object} ummResponse HTTP response from the CMR endpoint
* @param {Array} ummKeys Array of the keys requested in the query
*/
async parseUmm(ummResponse, ummKeys) {
const { revisionId } = this.requestInfo
// Pull out the key mappings so we can retrieve the values below
const { ummKeyMappings } = this.requestInfo
const { headers } = ummResponse
const {
'cmr-hits': cmrHits,
'cmr-search-after': ummSearchAfterIdentifier
} = downcaseKeys(headers)
this.setUmmItemCount(cmrHits)
this.setUmmSearchAfter(ummSearchAfterIdentifier)
const items = this.parseUmmBody(ummResponse)
items.forEach((item, itemIndex) => {
const normalizedItem = this.normalizeUmmItem(item)
// Filter out items that don't match the requested revisionId
// This is necessary because the umm endpoint returns multiple revisions
if (revisionId && normalizedItem.meta['revision-id'].toString() !== revisionId.toString()) {
return
}
// Creates unique item keys regardless of whether or not
// a user calls for data with similar conceptIds (as is the case with revisions)
const itemKey = this.generateUmmItemKey(itemIndex, normalizedItem)
this.setEssentialUmmValues(itemKey, normalizedItem)
this.setUmmItems(item, itemKey, ummKeys, ummKeyMappings)
})
// Update the item count if we filtered out items
if (revisionId) {
const filteredItemCount = Object.keys(this.items).length
this.setUmmItemCount(filteredItemCount)
}
}
/**
* Parses the response from an ingest
* @param {Object} requestInfo Parsed data pertaining to the ingest operation
*/
async parseIngest(requestInfo) {