export async function push()

in src/push/index.ts [68:161]


export async function push(monitors: Monitor[], options: PushOptions) {
  if (parseInt(process.env.CHUNK_SIZE) > 250) {
    throw error(
      'Invalid CHUNK_SIZE. CHUNK_SIZE must be less than or equal to 250'
    );
  }
  const duplicates = trackDuplicates(monitors);
  if (duplicates.size > 0) {
    throw error(formatDuplicateError(duplicates));
  }
  progress(
    `Pushing monitors for '${options.id}' project in kibana '${options.space}' space`
  );

  /**
   * Legacy API for kibana which does not support bulk operations
   */
  if (!isBulkAPISupported(options.kibanaVersion)) {
    return await pushLegacy(monitors, options);
  }

  const { monitors: remote } = await bulkGetMonitors(options);

  progress(`preparing ${monitors.length} monitors`);
  const { schemas, sizes } = await buildMonitorSchema(monitors, true);
  const local = getLocalMonitors(schemas);

  const { newIDs, changedIDs, removedIDs, unchangedIDs } = diffMonitorHashIDs(
    local,
    remote
  );
  logDiff(newIDs, changedIDs, removedIDs, unchangedIDs);
  if (inDebugMode()) {
    logGroups(sizes, newIDs, changedIDs, removedIDs, unchangedIDs);
    // show bundle size for the whole project
    let totalSize = 0;
    for (const value of sizes.values()) {
      totalSize += value;
    }
    progress(
      `total size of the ${sizes.size} monitors payload is ` +
        printBytes(totalSize)
    );
  }

  if (options.dryRun) {
    progress('Running browser monitors in dry run mode');
    await runLocal(schemas);
    progress('Dry run completed');
    return;
  }

  const updatedMonitors = new Set<string>([...changedIDs, ...newIDs]);
  if (updatedMonitors.size > 0) {
    const updatedMonSchemas = schemas.filter(s => updatedMonitors.has(s.id));
    const batches = getSizedBatches(
      updatedMonSchemas,
      sizes,
      MAX_PAYLOAD_SIZE_KB,
      BATCH_SIZE
    );
    if (batches.length > 1) {
      progress(`Monitors will be pushed as ${batches.length} batches.`);
    }
    for (const batch of batches) {
      await liveProgress(
        bulkPutMonitors(options, batch),
        `creating or updating ${batch.length} monitors`
      );
    }
  }

  if (removedIDs.size > 0) {
    if (updatedMonitors.size === 0 && unchangedIDs.size === 0) {
      await confirmDelete(
        `Pushing without any monitors will delete all monitors associated with the project.\n Do you want to continue?`,
        options.yes
      );
    } else {
      await confirmDelete(
        `Deleting ${removedIDs.size} monitors. Do you want to continue?`,
        options.yes
      );
    }
    const batches = getBatches(Array.from(removedIDs), BATCH_SIZE);
    for (const batch of batches) {
      await liveProgress(
        bulkDeleteMonitors(options, batch),
        `deleting ${batch.length} monitors`
      );
    }
  }
  done(`Pushed: ${underline(getMonitorManagementURL(options.url))} `);
}