{"size": 2318, "ext": "js", "max_stars_repo_path": "source/layers/core-lib/lib/rekogHelper.js", "max_stars_repo_name": "chriscoombs/amazon-rekognition-custom-brand-detection", "max_stars_repo_stars_event_min_datetime": "2020-10-16T10:31:03.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-04T21:00:48.000Z", "max_issues_repo_path": "source/layers/core-lib/lib/rekogHelper.js", "max_issues_repo_name": "chriscoombs/amazon-rekognition-custom-brand-detection", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "source/layers/core-lib/lib/rekogHelper.js", "max_forks_repo_name": "chriscoombs/amazon-rekognition-custom-brand-detection", "max_forks_repo_forks_event_min_datetime": "2020-10-27T05:27:19.000Z", "max_forks_repo_forks_event_max_datetime": "2021-05-07T03:52:31.000Z"}
{"max_stars_count": 15, "max_issues_count": null, "max_forks_count": 4, "avg_line_length": 26.6436781609, "max_line_length": 78, "alphanum_fraction": 0.6294219154}
const AWS = require('aws-sdk');
const Retry = require('./retry');
const ModelTimerTable = require('./modelTimerTable');

class RekogHelper {
  static getInstance() {
    return new AWS.Rekognition({
      apiVersion: '2016-06-27',
    });
  }

  static async describeProjectVersion(projectArn, projectVersionArn) {
    const params = {
      ProjectArn: projectArn,
      MaxResults: 1,
      VersionNames: [
        projectVersionArn.split('/')[3],
      ],
    };
    const rekog = RekogHelper.getInstance();
    const fn = rekog.describeProjectVersions.bind(rekog);
    const response = await Retry.run(fn, params, 4);

    while (response.ProjectVersionDescriptions.length) {
      const item = response.ProjectVersionDescriptions.shift();
      if (item.ProjectVersionArn === projectVersionArn) {
        return {
          status: item.Status,
          inferenceUnits: item.MinInferenceUnits,
        };
      }
    }
    return {
      status: 'UNKNOWN',
    };
  }

  static async detectCustomLabels(params) {
    const rekog = RekogHelper.getInstance();
    const fn = rekog.detectCustomLabels.bind(rekog);
    return Retry.run(fn, params, 3);
  }

  static async startProjectVersion(params) {
    const rekog = RekogHelper.getInstance();
    const fn = rekog.startProjectVersion.bind(rekog);
    return Retry.run(fn, params, 3);
  }

  static async updateProjectVersionTTL(projectVersionArn, ttl) {
    const createParams = RekogHelper.makeCreateParams(projectVersionArn, ttl);
    await ModelTimerTable.createItem(createParams)
      .catch((e) => {
        if (e.code === 'ConditionalCheckFailedException') {
          return undefined;
        }
        throw e;
      });
  }

  /**
   * create db item if not exists OR
   * update the item is ttl is less than new TTL.
   */
  static makeCreateParams(projectVersionArn, ttl) {
    const table = ModelTimerTable.getTable();
    return {
      TableName: table.name,
      Item: {
        [table.partition]: projectVersionArn,
        ttl,
      },
      ExpressionAttributeNames: {
        '#k0': 'ttl',
        '#k1': table.partition,
      },
      ExpressionAttributeValues: {
        ':v0': ttl,
      },
      ConditionExpression: 'attribute_not_exists(#k1) OR (#k0 <= :v0)',
      ReturnValues: 'NONE',
    };
  }
}

module.exports = RekogHelper;
