function upsert()

in source/wasa/index.js [133:167]


function upsert(metricTypeSet, allMetrics) {
    var firstItem = metricTypeSet[0];
    var ExpireTime = firstItem.EVENTTIMESTAMP + EXPIRE_TIME;
    var metricDetailParams = {
        TableName : METRIC_DETAILS_TABLE,
        Item : {
            MetricType : firstItem.METRICTYPE,
            EventTimestamp : firstItem.EVENTTIMESTAMP,
            ExpireTime : ExpireTime,
            MetricDetails : metricTypeSet
        },
        ConditionExpression : 'attribute_not_exists(MetricType)'
    };

    try {
        if (firstItem.METRICTYPE == 'hourly_events') {
            console.log('try to put a new hourly_events item for ' + firstItem.EVENTTIMESTAMP);
        }

        docClient.put(metricDetailParams, function (err, data) {
            if (err) {
                if (err.code == "ConditionalCheckFailedException") {
                    if (firstItem.METRICTYPE == 'hourly_events') {
                        console.log('There is already a record there for hourly_events, ' + firstItem.EVENTTIMESTAMP);
                    }
                    amendMetric(metricTypeSet,allMetrics);
                } else {
                    console.error('Error updating metric detail table: ' + JSON.stringify(err,null,2));
                }
            }
        });
    } catch (err) {
        console.error('Unable to save records to DynamoDB: ', err);
    }
};