function amendMetric()

in source/wasa/index.js [169:270]


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 other 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;

            console.log('metric:', allMetrics[metricIndex]);
            console.log('amendmentStrategy: %s', amendmentStrategy);

            if (itemToAmend.Items[0].MetricType == 'hourly_events') {
                console.log('item to amend:\n' + JSON.stringify(itemToAmend,null,2) + '\nWITH\n' + JSON.stringify(metric_list,null,2));
            }
            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 + '\'');
            }

            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
                    }
                };

                if (metric_list[0].METRICTYPE == 'hourly_events') {
                    console.log('new details = \n' + JSON.stringify(amendedParams,null,2));
                };

                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);

        }
    });
};