/**!
 *
 *  Copyright 2015 Netflix, Inc.
 *
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 *
 */

 (function () {
     'use strict';

    /**
    * @name NetworkBytesMetricDataModel
    * @desc
    */
    function NetworkBytesMetricDataModel(WidgetDataModel, MetricListService, VectorService) {
        var DataModel = function () {
            return this;
        };

        DataModel.prototype = Object.create(WidgetDataModel.prototype);

        DataModel.prototype.init = function () {
            WidgetDataModel.prototype.init.call(this);

            this.name = this.dataModelOptions ? this.dataModelOptions.name : 'metric_' + VectorService.getGuid();

            // create create base metrics
            var inMetric = MetricListService.getOrCreateCumulativeMetric('network.interface.in.bytes'),
                outMetric = MetricListService.getOrCreateCumulativeMetric('network.interface.out.bytes'),
                derivedFunction;

            // create derived function
            derivedFunction = function () {
                var returnValues = [],
                    lastValue;

                angular.forEach(inMetric.data, function (instance) {
                    if (instance.values.length > 0) {
                        lastValue = instance.values[instance.values.length - 1];
                        returnValues.push({
                            timestamp: lastValue.x,
                            key: instance.key + ' in',
                            value: lastValue.y / 1024
                        });
                    }
                });

                angular.forEach(outMetric.data, function (instance) {
                    if (instance.values.length > 0) {
                        lastValue = instance.values[instance.values.length - 1];
                        returnValues.push({
                            timestamp: lastValue.x,
                            key: instance.key + ' out',
                            value: lastValue.y / 1024
                        });
                    }
                });

                return returnValues;
            };

            // create derived metric
            this.metric = MetricListService.getOrCreateDerivedMetric(this.name, derivedFunction);

            this.updateScope(this.metric.data);
        };

        DataModel.prototype.destroy = function () {
            // remove subscribers and delete derived metric
            MetricListService.destroyDerivedMetric(this.name);

            // remove subscribers and delete base metrics
            MetricListService.destroyMetric('network.interface.in.bytes');
            MetricListService.destroyMetric('network.interface.out.bytes');

            WidgetDataModel.prototype.destroy.call(this);
        };

        return DataModel;
    }

    angular
        .module('app.datamodels')
        .factory('NetworkBytesMetricDataModel', NetworkBytesMetricDataModel);
 })();

@@ -1661,38 +1661,30 @@
     
-angular.forEach(inMetric.data,
+var pushReturnValues =
  fun
@@ -1680,33 +1680,32 @@
 alues = function
- 
 (instance) %7B%0A   
@@ -1689,32 +1689,44 @@
 unction(instance
+, metricName
 ) %7B%0A            
@@ -1996,13 +1996,18 @@
 y + 
-' in'
+metricName
 ,%0A  
@@ -2117,34 +2117,32 @@
                %7D
-);
 %0A%0A              
@@ -2159,19 +2159,18 @@
 forEach(
-out
+in
 Metric.d
@@ -2220,102 +2220,33 @@
     
-if (instance.values.length %3E 0) %7B%0A                        lastValue = instance.v
+pushReturnV
 alues
-%5B
+(
 instance
 .val
@@ -2245,284 +2245,164 @@
 ance
-.values.length - 1%5D;%0A                        returnValues.push(%7B%0A                            timestamp: lastValue.x,%0A                            key: instance.key + ' out',%0A                            value: lastValue.y / 1024%0A                        %7D);%0A                    %7D
+, ' in');%0A                %7D);%0A%0A                angular.forEach(outMetric.data, function (instance) %7B%0A                    pushReturnValues(instance, ' out');
 %0A   

Refactor duplicated function expressions for pushing return values