1

I'm plotting some metrics that I have stored in a backbone collection that is being updated every second. I'm not seeing the correct metrics being plotted on cubism. For each metric, I only get the one metric plotted (with an incorrect) value. I don't really see how I can debug this. Here is my code:

           var models = dataSet.models;

        //aggregate 'metrics'. Each component has a 'metric' which contains its stats over time
        for(model in models){
            var attributes = models[model].attributes;
            var metricData = metricCache.get(models[model].id);
            var metrics = metricData['attributes']['metrics'];

            if(!attributes['name'] || attributes['type']== "FLOW" || attributes['type'] == "SERVER"){
                continue;
            }
            if(attributes['name'] == null){
                continue;
            }
            var name = attributes['name'];
            var type = attributes['type'];
            var metName = name;

            /* GETTING METRIC. Using js closure to pass correct values to metric call */
            if(metricData != null){
                //var curContext = getMetric(metName, metrics);
                var curContext = (function(val1, val2){
                    return getMetric(val1, val2);
                })(metName,metrics);
            }

            statsList.push(curContext);

        }

'getMetric' takes the metric's name and 'metrics', which is an array of objects where each object has a timestamp and a value. I'm simply trying to plot each value with its corresponding timestamp on the horizon chart. Here is my code for 'getMetric'.

            function getMetric(name, metricData){
            var format = d3.time.format("%I-%M-%S");
            return context.metric(function(start, stop, step, callback){
                var statValues = [];

                    var lookup = {},
                    i = start.getTime();
                    //console.log('startTime', i);
                    var curStat = null;
                    for(stat in metricData){
                        curStat = metricData[stat];
                        curStat.value = +curStat.value;
                        var curDate = new Date(curStat.timestamp);
                        curDate = format(curDate);
                        lookup[curDate] = curStat;
                    }
                    var lastValue;
                    while((i+=step) < stop){
                        var key = format(new Date(i));
                        var curVal = key in lookup ? lookup[key].value : NaN;
                        console.log(name,curVal);
                        statValues.push(curVal);
                    }
                 //console.log(statValues);
                 callback(null,statValues);
                }, name);
        }

Here is a screenshot of what I see being plotted, which is each metric plotting the same values (which I verified is incorrect data). Output

capkutay
  • 183
  • 2
  • 11
  • Any chance of a quick demo on jsfiddle.net or jsbin.com? This would probably be a lot easier to figure out if we could watch it happening. – mu is too short Oct 23 '13 at 03:00
  • Hmm..I wish but the data is generated by an application that I deploy locally. I could try using a json file but that wouldn't have real-time data so I'm not sure it'd be solving the same problem. I was basically just hoping someone with heavy cubism.js experience would just notice some convention or syntax I missed that would cause the problem I'm running into... – capkutay Oct 23 '13 at 07:05
  • 1
    Smells like a closure or scoping problem to me. Something is in one scope but you think it is in another or there's a reference being shared when you think it is being cloned or something along those lines. Adding a few `console.log(JSON.stringify(x))` calls might help, the `JSON.stringify` will get you a snapshot in the console rather than a live reference. – mu is too short Oct 23 '13 at 07:46

0 Answers0