function amendMetric()

in wasa/index.js [144:243]


function amendMetric(metric_list,allMetrics) {
    var params = {
      TableName: METRIC_DETAILS_TABLE,
      KeyConditionExpression: 'MetricType = :hkey and EventTimestamp = :rkey',
      ExpressionAttributeValues: {
        ':hkey': metric_list[0].METRICTYPE,
        ':rkey': metric_list[0].EVENTTIMESTAMP
      }
    };

    // Get the existing data from METRIC_DETAILS_TABLE.
    docClient.query(params, (err, itemToAmend) => {
        if (!err) {
            var detailsToAmend = itemToAmend.Items[0].MetricDetails;
            var metricIndex = getMetricIndex(allMetrics,metric_list[0].METRICTYPE);
            // If metric is not found, don't do anything.
            if (metricIndex === -1) {
                return
            }
            var amendmentStrategy = allMetrics[metricIndex].AmendmentStrategy;
            var isWholeNumberMetric = allMetrics[metricIndex].IsWholeNumber;
            var isSet = allMetrics[metricIndex].IsSet;
            //console.log('metric:', allMetrics[metricIndex]);
            //console.log('amendmentStrategy: %s', amendmentStrategy);
            if(isSet == true){
                switch (amendmentStrategy) {
                    case 'add':
                        // For each item, find a match and add the values or add a new item.
                        metric_list.map( (item) => {
                            var detailIndex = getMetricDetailIndex(detailsToAmend, item.METRICITEM);
                            // Same metric exists in existing set.
                            if (detailIndex > -1) {
                                if (isWholeNumberMetric){
                                    detailsToAmend[detailIndex].UNITVALUEINT = detailsToAmend[detailIndex].UNITVALUEINT + item.UNITVALUEINT;
                                } else {
                                    detailsToAmend[detailIndex].UNITVALUEFLOAT = detailsToAmend[detailIndex].UNITVALUEFLOAT + item.UNITVALUEFLOAT;
                                }
                            } else {
                                detailsToAmend.push(item);
                            }
                        });
                    // If it exists, replace with updated value, if it is new, append it.
                    case 'replace_existing':
                        // For each item, find a match.
                        metric_list.map( (item) => {
                            var detailIndex = getMetricDetailIndex(detailsToAmend, item.METRICITEM);
                            // Same metric exists in existing set.
                            if (detailIndex > -1) {
                                detailsToAmend[detailIndex] = item;
                            } else {
                                detailsToAmend.push(item);
                            }
                        });
                        break;
                    case 'replace':
                        detailsToAmend = metric_list;
                        break;
                    default:
                        console.error('Unexpected amemdment strategy \'' + amendmentStrategy + '\'');
                }
            } else {
                switch (amendmentStrategy) {
                    case 'add':
                        if (isWholeNumberMetric){
                            detailsToAmend[0].UNITVALUEINT = detailsToAmend[0].UNITVALUEINT + metric_list[0].UNITVALUEINT;
                        } else {
                            detailsToAmend[0].UNITVALUEFLOAT = detailsToAmend[0].UNITVALUEFLOAT + metric_list[0].UNITVALUEFLOAT;
                        }
                        break;
                    case 'replace':
                    case 'replace_existing':
                        detailsToAmend = metric_list;
                        break;
                    default:
                        console.error('Unexpected amemdment strategy \'' + amendmentStrategy + '\'');                        
                }
            }
            if (detailsToAmend) {
                var ExpireTime = metric_list[0].EVENTTIMESTAMP + EXPIRE_TIME;
                var amendedParams = {
                    TableName : METRIC_DETAILS_TABLE,
                    Item : {
                        MetricType : metric_list[0].METRICTYPE,
                        EventTimestamp : metric_list[0].EVENTTIMESTAMP,
                        ExpireTime : ExpireTime,
                        MetricDetails : detailsToAmend
                    }
                };
                docClient.put(amendedParams, (err,data) => {
                    if (err) {
                        console.error('Error amending record:' + err + ' data ='  + JSON.stringify(data,null,2));
                    }
                });
            }
        } else {
            // Could not get details.
            console.error('Could not get expected results from the details table.', err);
        }
    });
};