here are the docs I have. you will see I have 2 docs per time (2nd key)
{"id":"server_host177.lss.emc.com_2014-05-15_11:39:48","key":["SRMSuite_3.0.2_test1","2014-05-14 11:00:00"],"value":20},
{"id":"server_host180.lss.emc.com_2014-05-15_11:39:48","key":["SRMSuite_3.0.2_test1","2014-05-14 11:00:00"],"value":20},
{"id":"server_host090.lss.emc.com_2014-05-15_11:39:55","key":["SRMSuite_3.0.2_test1","2014-05-14 12:00:00"],"value":22},
{"id":"server_host091.lss.emc.com_2014-05-15_11:39:55","key":["SRMSuite_3.0.2_test1","2014-05-14 12:00:00"],"value":20},
{"id":"server_host177.lss.emc.com_2014-05-15_11:39:48","key":["SRMSuite_3.0.2_test1","2014-05-14 13:00:00"],"value":26},
{"id":"server_host180.lss.emc.com_2014-05-15_11:39:48","key":["SRMSuite_3.0.2_test1","2014-05-14 13:00:00"],"value":20},
{"id":"server_host090.lss.emc.com_2014-05-15_11:39:55","key":["SRMSuite_3.0.2_test1","2014-05-14 14:00:00"],"value":22},
{"id":"server_host091.lss.emc.com_2014-05-15_11:39:55","key":["SRMSuite_3.0.2_test1","2014-05-14 14:00:00"],"value":20}
I want to get the avg value for each time. Here is my reduce function:
"maxcpu": {
"map": "function(doc) { if ((doc.type == 'performance_stats')) emit([doc.test_id, doc.start_time], doc.CPU) }",
"reduce": "function(keys, values) "
"{ "
"avg = Math.round(sum(values)/values.length);"
"return(avg)"
" }"
}
So, you will have 4 rows:
http:yourhostip:5984/longevity/_design/perfstats/_view/maxcpu?group=true
{"rows":[
{"key":["SRMSuite_3.0.2_test1","2014-05-14 11:00:00"],"value":20},
{"key":["SRMSuite_3.0.2_test1","2014-05-14 12:00:00"],"value":21},
{"key":["SRMSuite_3.0.2_test1","2014-05-14 13:00:00"],"value":23},
{"key":["SRMSuite_3.0.2_test1","2014-05-14 14:00:00"],"value":21}}
Now to report just the max value which is 23, we need to use list function. Got it from- http://geekiriki.blogspot.com/2010/08/couchdb-using-list-functions-to-sort.html
"lists":{
"sort":"function(head, req) {"
"var row;"
"var rows=[];"
"while(row = getRow()) {"
"rows.push(row)"
"};"
"rows.sort(function(a,b) {"
"return b.value-a.value"
"});"
"send(JSON.stringify({\"rows\" : rows[0]}))"
"}"
Then this gets you what you need-
http://yourhostip:5984/longevity/_design/perfstats/_list/sort/maxcpu?group=true
{"rows":{"key":["SRMSuite_3.0.2_test1","2014-05-14 13:00:00"],"value":87}}