async function populateSourcePlate()

in main/src/image-management/image-management.ts [191:291]


async function populateSourcePlate(inputBucket: any, inputKey: any) {
  const data = await s3
    .getObject({ Bucket: inputBucket, Key: inputKey })
    .promise();
  if (!data) {
    throw new Error("sourcePlateInfo object null");
  }
  const sourcePlateInfoStr = data.Body.toString("utf-8");
  const sourcePlateInfo = JSON.parse(sourcePlateInfoStr);

  const plateSourceId = sourcePlateInfo["plateSourceId"];
  if (!("images" in sourcePlateInfo)) {
    throw new Error("images required");
  }
  const plateId = su.generate();
  const plateKey = "plate#" + plateId;
  const plateEntry = {
    [PARTITION_KEY_IMGID]: plateKey,
    [SORT_KEY_TRNID]: ORIGIN,
  };
  const params = {
    TableName: TABLE_NAME,
    Item: plateEntry,
  };
  await db.put(params).promise();
  const plateMessageId = await createPlateMessageId(plateId);
  console.log("plateMessageId=");
  console.log(plateMessageId);

  const images: any[] = sourcePlateInfo["images"];
  const wellDict: Map<string, string> = new Map();
  const fields: any[] = [
    "wellSourceId",
    "imageSourceId",
    "sourceBucket",
    "sourceKey",
  ];
  const timestamp = Date.now().toString();
  const p: any[] = [];

  for (const image of images) {
    const imageId = su.generate();
    for (const field of fields) {
      if (!(field in image)) {
        throw new Error(
          `field ${field} required in sourcePlateId ${plateSourceId}`
        );
      }
    }

    const wellSourceId = image["wellSourceId"];
    const imageSourceId = image["imageSourceId"];
    const sourceBucket = image["sourceBucket"];
    const sourceKey = image["sourceKey"];
    var wellId: string = "";
    if (wellDict.has(wellSourceId)) {
      wellId = wellDict.get(wellSourceId)!;
    } else {
      wellId = su.generate();
      wellDict.set(wellSourceId, wellId);
    }

    const messageId = await createMessage(`Creation of imageId=${imageId}`);

    const imageEntry = {
      [PARTITION_KEY_IMGID]: imageId,
      [SORT_KEY_TRNID]: ORIGIN,
      [PLATE_ID_ATTRIBUTE]: plateId,
      [WELL_ID_ATTRIBUTE]: wellId,
      [PLATE_SOURCE_ID_ATTRIBUTE]: plateSourceId,
      [WELL_SOURCE_ID_ATTRIBUTE]: wellSourceId,
      [IMAGE_SOURCE_ID_ATTRIBUTE]: imageSourceId,
      [CREATE_TIMESTAMP_ATTRIBUTE]: timestamp,
      [MESSAGE_ID_ATTRIBUTE]: messageId,
      [BUCKET_ATTRIBUTE]: sourceBucket,
      [KEY_ATTRIBUTE]: sourceKey,
      ...("channelKeys" in image && {
        [CHANNEL_KEYS_ATTRIBUTE]: image["channelKeys"],
      }),
      ...("category" in image && {
        [TRAIN_CATEGORY_ATTRIBUTE]: image["category"],
      }),
      ...("label" in image && { [TRAIN_LABEL_ATTRIBUTE]: image["label"] }),
      ...("subclassType" in image && { [TRAIN_SUBCLASS_TYPE_ATTRIBUTE]: image["subclassType"] }),
      ...("subclass" in image && { [TRAIN_SUBCLASS_ATTRIBUTE]: image["subclass"] }),
      ...("experiment" in image && {
        [EXPERIMENT_ATTRIBUTE]: image["experiment"],
      }),
    };

    const params = {
      TableName: TABLE_NAME,
      Item: imageEntry,
    };

    await db.put(params).promise();
    //    p.push(db.put(params).promise());
  }
  //  await Promise.all(p);
  return { plateId: plateId };
}