async function doCommand({
  dryRun: isDryRun = false,
  limit = false,
}) {
  if (isDryRun) {
    console.log(DRY_RUN_MSG);
  }

  const activityDocs = await Activity.find(
    {
      'migration.location': { $ne: true },
    },
    null,
    limit ? { limit } : null,
  );

  if (!activityDocs.length) {
    console.log('No activities found without activityLocations field');
    return;
  }

  const migrateActivityFormat = async (doc) => {
    const activityLocations = [{
      location: MIGRATE_LOCATION,
      laps: doc.laps,
      segment_efforts: doc.segment_efforts,
    }];
    doc.set({
      activityLocations,
      app_version: 'v2',
      migration: {
        ...(doc.migration || {}),
        location: true,
      },
    });
    doc.markModified('migration');
    doc.markModified('activityLocations');
    if (!isDryRun) {
      await doc.save();
    }
    return doc.id;
  };

  const progressBar = new cliProgress.SingleBar(
    {},
    cliProgress.Presets.shades_classic,
  );
  progressBar.start(activityDocs.length, 0);

  const iterable = makeArrayAsyncIterable(
    activityDocs,
    migrateActivityFormat,
  );

  console.log(`Migrating stats for ${activityDocs.length} activities`);

  const results = {
    success: [],
    error: [],
  };

  // eslint-disable-next-line
  for await (const id of iterable) {
    // Check that it worked
    if (isDryRun) {
      results.success.push(id);
    } else {
      const checked = await Activity.findById(
        id,
        'activityLocations laps segment_efforts',
        { lean: true },
      );

      const {
        activityLocations = [],
        laps,
        segment_efforts,
      } = checked;

      const nextNumSegmentEfforts = activityLocations[0].segment_efforts.length;
      const prevNumSegmentEfforts = segment_efforts.length;
      if (
        activityLocations.length === 1
        && activityLocations[0].location === MIGRATE_LOCATION
        && activityLocations[0].laps === laps
        && nextNumSegmentEfforts === prevNumSegmentEfforts
      ) {
        results.success.push(id);
      } else {
        results.error.push(id);
      }
    }
    progressBar.increment();
  }

  progressBar.stop();

  console.table({
    Success: results.success.length,
    Error: results.error.length,
  });

  if (isDryRun) {
    console.log(DRY_RUN_MSG);
  }
}
/**
 * Create v2 stats format activityLocations for legacy prospectpark activities
 */
Create v2 stats format activityLocations for legacy prospectpark activities
Create v2 stats format activityLocations for legacy prospectpark activities
async function doCommand({
  dryRun: isDryRun = false,
  limit = false,
}) {
  if (isDryRun) {
    console.log(DRY_RUN_MSG);
  }
  const activityDocs = await Activity.find(
    {
      'migration.location': { $ne: true },
    },
    null,
    limit ? { limit } : null,
  );
  if (!activityDocs.length) {
    console.log('No activities found without activityLocations field');
    return;
  }
  const migrateActivityFormat = async (doc) => {
    const activityLocations = [{
      location: MIGRATE_LOCATION,
      laps: doc.laps,
      segment_efforts: doc.segment_efforts,
    }];
    doc.set({
      activityLocations,
      app_version: 'v2',
      migration: {
        ...(doc.migration || {}),
        location: true,
      },
    });
    doc.markModified('migration');
    doc.markModified('activityLocations');
    if (!isDryRun) {
      await doc.save();
    }
    return doc.id;
  };
  const progressBar = new cliProgress.SingleBar(
    {},
    cliProgress.Presets.shades_classic,
  );
  progressBar.start(activityDocs.length, 0);
  const iterable = makeArrayAsyncIterable(
    activityDocs,
    migrateActivityFormat,
  );
  console.log(`Migrating stats for ${activityDocs.length} activities`);
  const results = {
    success: [],
    error: [],
  };
  for await (const id of iterable) {
    if (isDryRun) {
      results.success.push(id);
    } else {
      const checked = await Activity.findById(
        id,
        'activityLocations laps segment_efforts',
        { lean: true },
      );
      const {
        activityLocations = [],
        laps,
        segment_efforts,
      } = checked;
      const nextNumSegmentEfforts = activityLocations[0].segment_efforts.length;
      const prevNumSegmentEfforts = segment_efforts.length;
      if (
        activityLocations.length === 1
        && activityLocations[0].location === MIGRATE_LOCATION
        && activityLocations[0].laps === laps
        && nextNumSegmentEfforts === prevNumSegmentEfforts
      ) {
        results.success.push(id);
      } else {
        results.error.push(id);
      }
    }
    progressBar.increment();
  }
  progressBar.stop();
  console.table({
    Success: results.success.length,
    Error: results.error.length,
  });
  if (isDryRun) {
    console.log(DRY_RUN_MSG);
  }
}
async function doCommand ( { dryRun : isDryRun = false , limit = false , } ) { if ( isDryRun ) { console . log ( DRY_RUN_MSG ) ; } const activityDocs = await Activity . find ( { 'migration.location' : { $ne : true } , } , null , limit ? { limit } : null , ) ; if ( ! activityDocs . length ) { console . log ( 'No activities found without activityLocations field' ) ; return ; } const migrateActivityFormat = async ( doc ) => { const activityLocations = [ { location : MIGRATE_LOCATION , laps : doc . laps , segment_efforts : doc . segment_efforts , } ] ; doc . set ( { activityLocations , app_version : 'v2' , migration : { ... ( doc . migration || { } ) , location : true , } , } ) ; doc . markModified ( 'migration' ) ; doc . markModified ( 'activityLocations' ) ; if ( ! isDryRun ) { await doc . save ( ) ; } return doc . id ; } ; const progressBar = new cliProgress . SingleBar ( { } , cliProgress . Presets . shades_classic , ) ; progressBar . start ( activityDocs . length , 0 ) ; const iterable = makeArrayAsyncIterable ( activityDocs , migrateActivityFormat , ) ; console . log ( ` ${ activityDocs . length } ` ) ; const results = { success : [ ] , error : [ ] , } ; for await ( const id of iterable ) { if ( isDryRun ) { results . success . push ( id ) ; } else { const checked = await Activity . findById ( id , 'activityLocations laps segment_efforts' , { lean : true } , ) ; const { activityLocations = [ ] , laps , segment_efforts , } = checked ; const nextNumSegmentEfforts = activityLocations [ 0 ] . segment_efforts . length ; const prevNumSegmentEfforts = segment_efforts . length ; if ( activityLocations . length === 1 && activityLocations [ 0 ] . location === MIGRATE_LOCATION && activityLocations [ 0 ] . laps === laps && nextNumSegmentEfforts === prevNumSegmentEfforts ) { results . success . push ( id ) ; } else { results . error . push ( id ) ; } } progressBar . increment ( ) ; } progressBar . stop ( ) ; console . table ( { Success : results . success . length , Error : results . error . length , } ) ; if ( isDryRun ) { console . log ( DRY_RUN_MSG ) ; } }
Create v2 stats format activityLocations for legacy prospectpark activities
Create v2 stats format activityLocations for legacy prospectpark activities
// eslint-disable-next-line // Check that it worked