How to use @cumulus/cmrjs - 10 common examples

To help you get started, we’ve selected a few @cumulus/cmrjs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nasa / cumulus / example / spec / ingestGranule / IngestUMMGSuccessSpec.js View on Github external
it('has expected payload', () => {
      expect(granule.published).toBe(true);
      expect(granule.cmrLink).toEqual(`${getUrl('search')}granules.json?concept_id=${granule.cmrConceptId}`);

      // Set the expected CMR values since they're going to be different
      // every time this is run.
      const updatedExpectedPayload = cloneDeep(expectedPayload);
      updatedExpectedPayload.granules[0].cmrLink = granule.cmrLink;
      updatedExpectedPayload.granules[0].cmrConceptId = granule.cmrConceptId;

      expect(postToCmrOutput.payload).toEqual(updatedExpectedPayload);
    });
github nasa / cumulus / example / spec / ingestGranule / IngestUMMGSuccessSpec.js View on Github external
it('has expected payload', () => {
      expect(granule.published).toBe(true);
      expect(granule.cmrLink).toEqual(`${getUrl('search')}granules.json?concept_id=${granule.cmrConceptId}`);

      // Set the expected CMR values since they're going to be different
      // every time this is run.
      const updatedExpectedPayload = cloneDeep(expectedPayload);
      updatedExpectedPayload.granules[0].cmrLink = granule.cmrLink;
      updatedExpectedPayload.granules[0].cmrConceptId = granule.cmrConceptId;

      expect(postToCmrOutput.payload).toEqual(updatedExpectedPayload);
    });
github nasa / cumulus / packages / api / lambdas / create-reconciliation-report.js View on Github external
granuleInCmr.RelatedUrls.forEach((relatedUrl) => {
    // only check URL types for downloading granule files and related data (such as documents)
    if (cmrGetDataTypes.includes(relatedUrl.Type)
      || cmrRelatedDataTypes.includes(relatedUrl.Type)) {
      const urlFileName = relatedUrl.URL.split('/').pop();

      // filename in both cumulus and CMR
      if (granuleFiles[urlFileName] && bucketsConfig.key(granuleFiles[urlFileName].bucket)) {
        // not all files should be in CMR
        const accessUrl = constructOnlineAccessUrl({
          file: granuleFiles[urlFileName],
          distEndpoint: process.env.DISTRIBUTION_ENDPOINT,
          buckets: bucketsConfig
        });

        if (accessUrl && relatedUrl.URL === accessUrl.URL) {
          okCount += 1;
        } else if (cmrGetDataTypes.includes(relatedUrl.Type)) {
          // ignore any URL which is not for getting data
          // some files should not be in CMR such as private files
          onlyInCmr.push({
            URL: relatedUrl.URL,
            Type: relatedUrl.Type,
            GranuleUR: granuleInCmr.GranuleUR
          });
        }
github nasa / cumulus / packages / api / lambdas / create-reconciliation-report.js View on Github external
async function reconciliationReportForCollections() {
  // compare collection holdings:
  //   Get list of collections from CMR
  //   Get list of collections from CUMULUS
  //   Report collections only in CMR
  //   Report collections only in CUMULUS

  // get all collections from CMR and sort them, since CMR query doesn't support
  // 'Version' as sort_key
  const cmr = new CMR(process.env.cmr_provider, process.env.cmr_client_id);
  const cmrCollectionItems = await cmr.searchCollections({}, 'umm_json');
  const cmrCollectionIds = cmrCollectionItems.map((item) =>
    constructCollectionId(item.umm.ShortName, item.umm.Version)).sort();

  // get all collections from database and sort them, since the scan result is not ordered
  const dbCollectionsItems = await new Collection().getAllCollections();
  const dbCollectionIds = dbCollectionsItems.map((item) =>
    constructCollectionId(item.name, item.version)).sort();

  const okCollections = [];
  let collectionsOnlyInCumulus = [];
  let collectionsOnlyInCmr = [];

  let nextDbCollectionId = (dbCollectionIds.length !== 0) ? dbCollectionIds[0] : null;
  let nextCmrCollectionId = (cmrCollectionIds.length !== 0) ? cmrCollectionIds[0] : null;
github nasa / cumulus / packages / api / lambdas / ems-metadata-report.js View on Github external
async function getCmrCollections() {
  const collections = [];
  // get all collections from CMR and sort them, since CMR query doesn't support
  // 'version' as sort_key
  const cmrCollectionsIterator = new CMRSearchConceptQueue(
    process.env.cmr_provider, process.env.cmr_client_id, 'collections', [], 'echo10'
  );

  let nextCmrItem = await cmrCollectionsIterator.peek();

  while (nextCmrItem) {
    await cmrCollectionsIterator.shift(); // eslint-disable-line no-await-in-loop

    // evaluate each EMS field based on CMR collection and build EMS record
    const emsRecord = Object.entries(emsMapping)
      // eslint-disable-next-line no-loop-func
      .map(([field, value]) =>
        ({ [field]: (isFunction(value) ? value(nextCmrItem.Collection) : value) }))
      .reduce((returnObj, currentValue) => ({ ...returnObj, ...currentValue }), {});

    const collectionId = constructCollectionId(
github nasa / cumulus / packages / api / lambdas / create-reconciliation-report.js View on Github external
async function reconciliationReportForGranules(collectionId, bucketsConfig) {
  // compare granule holdings:
  //   Get CMR granules list (by PROVIDER, short_name, version, sort_key: ['granule_ur'])
  //   Get CUMULUS granules list (by collectionId order by granuleId)
  //   Report granules only in CMR
  //   Report granules only in CUMULUS
  const { name, version } = deconstructCollectionId(collectionId);
  const cmrGranulesIterator = new CMRSearchConceptQueue(
    process.env.cmr_provider, process.env.cmr_client_id, 'granules',
    { short_name: name, version: version, sort_key: ['granule_ur'] }, 'umm_json'
  );

  const dbGranulesIterator = new Granule().getGranulesForCollection(collectionId, 'completed');

  const granulesReport = {
    okCount: 0,
    onlyInCumulus: [],
    onlyInCmr: []
  };

  const filesReport = {
    okCount: 0,
    onlyInCumulus: [],
    onlyInCmr: []
github nasa / cumulus / packages / api / models / granules.js View on Github external
passphrase: process.env.launchpad_passphrase,
        certificate: process.env.launchpad_certificate
      };
      const token = await launchpad.getLaunchpadToken(config);
      params.token = token;
    } else {
      const secret = await commonAws.secretsManager().getSecretValue({
        SecretId: process.env.cmr_password_secret_name
      }).promise();

      params.username = process.env.cmr_username;
      params.password = secret.SecretString;
    }

    const cmr = new CMR(params);
    const metadata = await cmrjs.getMetadata(granule.cmrLink);

    // Use granule UR to delete from CMR
    await cmr.deleteGranule(metadata.title, granule.collectionId);
    await this.update({ granuleId: granule.granuleId }, { published: false }, ['cmrLink']);
  }
github nasa / cumulus / packages / api / models / granules.js View on Github external
error: exception,
          createdAt: get(payload, 'cumulus_meta.workflow_start_time'),
          timestamp: Date.now(),
          productVolume: getGranuleProductVolume(g.files),
          timeToPreprocess: get(payload, 'meta.sync_granule_duration', 0) / 1000,
          timeToArchive: get(payload, 'meta.post_to_cmr_duration', 0) / 1000,
          processingStartDateTime: extractDate(payload, 'meta.sync_granule_end_time'),
          processingEndDateTime: extractDate(payload, 'meta.post_to_cmr_start_time')
        };

        doc.published = get(g, 'published', false);
        // Duration is also used as timeToXfer for the EMS report
        doc.duration = (doc.timestamp - doc.createdAt) / 1000;

        if (g.cmrLink) {
          const metadata = await cmrjs.getMetadata(g.cmrLink);
          doc.beginningDateTime = metadata.time_start;
          doc.endingDateTime = metadata.time_end;
          doc.lastUpdateDateTime = metadata.updated;

          const fullMetadata = await cmrjs.getFullMetadata(g.cmrLink);
          if (fullMetadata && fullMetadata.DataGranule) {
            doc.productionDateTime = fullMetadata.DataGranule.ProductionDateTime;
          }
        }

        return this.create(doc);
      }
      return Promise.resolve();
    });
github nasa / cumulus / tasks / post-to-cmr / index.js View on Github external
async function postToCMR(event) {
  // get cmr files and metadata
  const cmrFiles = granulesToCmrFileObjects(event.input.granules);
  log.debug(`Found ${cmrFiles.length} CMR files.`);
  const updatedCMRFiles = await addMetadataObjects(cmrFiles);

  log.info(`Publishing ${updatedCMRFiles.length} CMR files.`);
  // post all meta files to CMR
  const results = await Promise.all(
    updatedCMRFiles.map(
      (cmrFile) =>
        publish2CMR(cmrFile, event.config.cmr, event.config.bucket, event.config.stack)
    )
  );

  return {
    process: event.config.process,
    granules: buildOutput(
      results,
github nasa / cumulus / tasks / move-granules / index.js View on Github external
async function moveGranules(event) {
  // we have to post the meta-xml file of all output granules
  // first we check if there is an output file
  const config = event.config;
  const bucketsConfig = new BucketsConfig(config.buckets);
  const moveStagedFiles = get(config, 'moveStagedFiles', true);
  const cmrGranuleUrlType = get(config, 'cmrGranuleUrlType', 'distribution');

  const duplicateHandling = duplicateHandlingType(event);

  const granulesInput = event.input.granules;
  const cmrFiles = granulesToCmrFileObjects(granulesInput);
  const granulesByGranuleId = keyBy(granulesInput, 'granuleId');

  let movedGranules;

  if (cmrGranuleUrlType === 'distribution' && !config.distribution_endpoint) {
    throw new Error('cmrGranuleUrlType is distribution, but no distribution endpoint is configured.');
  }

  // allows us to disable moving the files
  if (moveStagedFiles) {
    // update allGranules with aspirational metadata (where the file should end up after moving.)
    const granulesToMove = await updateGranuleMetadata(
      granulesByGranuleId, config.collection, cmrFiles, bucketsConfig
    );

    // move files from staging location to final location