async function processS3ObjectData()

in source/external-integrations-handler/index.ts [183:253]


async function processS3ObjectData(machineId: string, s3ObjData: any): Promise<any> {
  if (!machineId || machineId.trim() === '') {
    throw new Error('Machine ID was not supplied');
  }

  if (!s3ObjData.hasOwnProperty('prediction')) {
    throw new Error('Anomaly data did not contain a prediction score');
  }

  if (s3ObjData.prediction === 0) {
    logger.log(LogLevel.INFO, 'No anomaly detected');
  }

  if (!s3ObjData.diagnostics) {
    throw new Error('Anomaly data did not contain diagnostic information');
  }

  logger.log(LogLevel.INFO, `Using machine ID (${machineId}) to look up device`);
  const device = await getDevice(machineId);
  if (!device) {
    throw new Error(`Unable to match machine ID (${machineId}) to a Device in Amazon Virtual Andon`);
  }

  const station = await getDataHierarchyItemByIdAndType(device.parentId, 'STATION');
  if (!station) {
    throw new Error(`Unable to find station by ID (${device.parentId})`);
  }

  const area = await getDataHierarchyItemByIdAndType(station.parentId, 'AREA');
  if (!area) {
    throw new Error(`Unable to find area by ID (${station.parentId})`);
  }

  const site = await getDataHierarchyItemByIdAndType(area.parentId, 'SITE');
  if (!site) {
    throw new Error(`Unable to find site by ID (${area.parentId})`);
  }

  const processes = await getDataHierarchyItemsByTypeAndParentId('PROCESS', area.id);
  if (processes.length === 0) {
    throw new Error(`Unable to find any processes under Area (${area.name}: ${area.description})`);
  }

  const events: Ddb.DocumentClient.AttributeMap[] = [];
  for (const process of processes) {
    events.push(...await getDataHierarchyItemsByTypeAndParentId('EVENT', process.id));
  }

  const automatedEvent = events.find(e => e.eventType && e.eventType.trim().toLowerCase() === 'automated');
  if (!automatedEvent) {
    throw new Error(`Unable to find any automated events under Area (${area.name}: ${area.description})`);
  }

  if (await hasUnresolvedIssueForEventAndDevice(automatedEvent.id, device.name)) {
    throw new Error('An unresolved issue exists for this event on this device');
  }

  await publishToIssuesTopic({
    eventId: automatedEvent.id,
    eventDescription: automatedEvent.name,
    priority: automatedEvent.priority,
    deviceName: device.name,
    stationName: station.name,
    areaName: area.name,
    siteName: site.name,
    processName: (processes.find(p => p.id === automatedEvent.parentId)).name,
    issueSource: 's3File',
    createdBy: 'automatic-issue-detection',
    additionalDetails: JSON.stringify(s3ObjData)
  });
}