.net not showing all values
I'm sure that I'm just doing something wrong since I haven't been playing with this for very long and its all new to me. I've got documents that look like this:
{
"key": 2185335,
"metric": "CLR_AUTO_EVENT_wait_time_ms",
"sample_dt": "2013-03-07T22:29:18",
"value": 0
}My map function just spits out the key, the metric name, and the value:
function (doc, meta) { emit([doc.key, doc.metric], doc.value) }
My reduce function returns the total, count, average, and eightieth percentile.
function(key, values, rereduce) { values.sort(); var result = {total: 0, count: 0, eightieth: 0, average: 0}; for(i=0; i < values.length; i++) { if (rereduce) { result.total = result.total + values[i].total; result.count = result.count + values[i].count; result.average = result.total / result.count; } else { result.total = sum(values); result.count = values.length; result.average = result.total / result.count; } } result.eightieth = values[Math.round(values.length * 0.8)]; return(result); }
If I view the results via the webGUI, all the values show up. If I do it via .NET, the eightyth key is missing. All I'm doing is iterating over the rows from this:
CouchbaseClient db = new CouchbaseClient(); var rows = db.GetView("test", "all_metrics").GroupAt(2);
Thats great! How did you find that out?
Trial and error... I've debugged my fair share of views now. I take an approach similar to debugging web page JavaScript (i.e., conosole.log). It took me 4 steps to debug the result.eightieth issue.
1. Verify that the out-of-loop assignments were working, which they were
result.foo = ""; result.eightieth = values[Math.floor(values.length * 0.8)];
2. Eliminate the right hand side of the assignment as a source of a bug
result.eightieth = "foo"; //foo was in the value
3. Test the indexing of the values array as a source of the bug
result.eightieth = Math.round(values.length * 0.8); //no error, but I saw with 2 docs in my bucket, the indexes were 1 and 2
4. Test the indexing of the values with a lower bound index
result.eightieth = values[Math.floor(values.length * 0.8)]; //no error
I ran the sample view after creating some test records based on the JSON you provided. I found that Math.round(values.length * 0.8) was resulting in an index out of bounds error in the JavaScript. Math.floor seems to work though - both in the console and in .NET.