incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matthieu Rakotojaona <matthieu.rakotoja...@gmail.com>
Subject Re: Summary view for Pie Charting
Date Thu, 20 Sep 2012 12:00:38 GMT
On Thu, Sep 20, 2012 at 1:20 PM, Carl Bourne <carl.bourne@me.com> wrote:
> Thanks,
>
> So I'm assuming the only way to to summarise this information is to do 3 separate queries
and aggregate the results?
>
>> It's this;
>>
>> map:
>> function(doc) {
>>  emit(doc.date.expire, null);
>> }
>
> If I query against the above without a reduce (_count) function it returns all documents
according to the ?startkey="2012-10-19" and not the count which I need to build the pie chart!
>
> Am I understanding you correctly?
>
> Carl

With this map function, you can query your db like that :

GET /path/to/view?startkey=<now date>&endkey=<now date + 30 days>
GET /path/to/view?startkey=<now date + 30 days>&endkey=<now date + 60 days>
GET /path/to/view?startkey=<now date + 60 days>&endkey=<now date + 90 days>

With a _count as a reduce, you will only get the numbers for each.

You can also use a _list function, if you're okay with couchdb-side
processing (http://wiki.apache.org/couchdb/Formatting_with_Show_and_List)
:

function(head, req) {
  nowDate = req.query.date;
  in30days = new Date();
  in30days.setDate(nowDate.getDate() + 30);

  in60days = new Date();
  in60days.setDate(nowDate.getDate() + 60);

  in60days = new Date();
  in60days.setDate(nowDate.getDate() + 60);

  out["now"] = 0;
  out["in30days"] = 0;
  out[in60days"] = 0;

  while(row = getRow()) {
    if (nowDate <= row.value.expire  && row.value.expire <= in30days) {
      out["now"] += 1;
    } else if (in30days <= row.value.expire  && row.value.expire <= in60days)
{
      out["in30days"] += 1;
    } else if (in30days <= row.value.expire  && row.value.expire <= in60days)
{
      out["in60days"] += 1;
    }
  }

  send(out["now"] + "-" + out["in30days"] + "-" + out["in60days"]);
}

You would query such a _list with

GET /path/to/_list?date=<date of now>&reduce=false

This raises a question I do not know the answer for : when we use such
a _list function, are we sure we will not take into account a doc that
will be added/modified _after_ having launched the function ?

-- 
Matthieu RAKOTOJAONA

Mime
View raw message