incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mathias Leppich <mlepp...@muhqu.de>
Subject Re: Reporting aggregated data using reduce function
Date Wed, 02 Feb 2011 07:47:18 GMT
Maybe related: a typical reduce function I use to sum on objects:

function (keys, values, rereduce) {
  var sums = {};
  for (var i in values) {
    for (var k in values[i]) {
      sums[k] = (sums[k]||0)+values[i][k];
    }
  }
  return sums;
}

which sums emited keys as follows:
emit("somekey",{"A":1});
emit("somekey",{"B":2});
emit("somekey",{"A":1,"C":1});
emit("somekey",{"A":1,"B":2});

reduced output:
"somekey": {"A":3,"B":4,"C":1}

but, I guess the array approach is more efficient, as it uses less space by using indexes
instead of keys...

- Mathias

On 31.01.2011, at 12:07, John wrote:

> That's perfect, thanks Robert. Funny how something so simple can be so confusing if the
concept is new to you.......
> 
> For anyone who searches for how to do a reduce over an array in the future, here's the
code:
> 
> function(keys, values, rereduce){
> 
> 	var total = [0,0];
> 	values.forEach(function(value){
> 		total[0] += value[0];
> 		total[1] += value[1];
> 	}
> 		
> 	)
> 	return total;
> }
> 
> Looks like I might get to add reporting to my successful use cases for Couchdb!
> 
> John
> 
> 
> On 31 Jan 2011, at 09:01, Robert Newson wrote:
> 
>> in 1.1, _sum will work for arrays of numbers too (rather than
>> concatenating them as above). for now, just loop over the array of
>> arrays and do the sum yourself.
>> 
>> On Mon, Jan 31, 2011 at 1:02 AM, Keith Gable <ziggythehamster@gmail.com> wrote:
>>> It sounds like you need a new view for each piece of data.
>>> 
>>> by_answered, by_busy, by_time_to_answer, etc.
>>> 
>>> Then you'd query each view to get the reduction, and the reduce would be as
>>> simple as _sum.
>>> 
>>> 
>>> 
>>> On Jan 30, 2011, at 5:55 PM, John <john.logsdon@netdev.co.uk> wrote:
>>> 
>>>> Hi
>>>> 
>>>> I'm looking to extend our usage of couchdb  by replacing our mysql
>>>> reporting db.
>>>> Whilst using couchdb successfully for a number of varied use cases I've
>>>> never had to do much with reduce so I'm unsure on how to use it to reduce
an
>>>> array of values.
>>>> 
>>>> Basically I want to be able to search a database using a composite key and
>>>> retrieving some aggregated information about number of calls, call status,
>>>> avg time to answer and avg duration
>>>> 
>>>> 
>>>> The following view shows how I'd like it to work:
>>>> 
>>>> Key = <Application, Account, Subscription>
>>>> Value = <1, answered, busy, noreply, time to answer, duration>
>>>> 
>>>> e.g.
>>>> 
>>>> ["NTS", "NetDev", "MySub1"], [1,1,0,0,100,200]
>>>> ["NTS", "NetDev", "MySub1"], [1,1,0,0,150,400]
>>>> ["NTS", "NetDev", "MySub1"], [1,1,0,0,170,500]
>>>> ["NTS", "NetDev", "MySub1"], [1,0,1,0,0,0]
>>>> ["NTS", "NetDev", "MySub1"], [1,0,1,0,0,0]
>>>> ["NTS", "NetDev", "MySub1"], [1,0,0,2,0,0]
>>>> ["NTS", "NetDev", "MySub1"], [1,0,0,2,0,0]
>>>> 
>>>> My Reduced output should look like this:
>>>> 
>>>> [7,3,2,2,420,1100]
>>>> i.e. 7 calls in total, 3 answered, 2 busy, 2 no reply, the total time for
>>>> time to answer is 420 and the total time for call duration is 1100.
>>>> 
>>>> I can then compute the two averages after getting the data back from couch
>>>> i.e. 420/no. of answered calls(3) and 1100/no. of answered calls(3)
>>>> 
>>>> I thought that sum(values) would do this for me but it just upsets couch:
>>>> 
>>>> Reduce output must shrink more rapidly: Current output:
>>>> '["001,11,11,11,11,11,11,11,11,11,11,101,11,11,11,11,11,11,11,11,11,11,11,101,11,11,11,11,11,11,11,11'...
>>>> (first 100 of 277 bytes)
>>>> 
>>>> What should my reduce function look like?
>>>> 
>>>> Thanks
>>>> 
>>>> John
>>> 
> 


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message