couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dave Cottlehuber <d...@muse.net.nz>
Subject Re: Count since
Date Wed, 25 May 2011 19:53:10 GMT
On 26 May 2011 07:31, Chris Stockton <chrisstocktonaz@gmail.com> wrote:
> Hello,
>
> On Wed, May 25, 2011 at 10:17 AM, Simon Woodhead
> <simon.woodhead@simwood.com> wrote:
>> Hi folks,
>>
>> I was wondering if someone could help me with a view.
>>
>> We're storing some Apache logs in CouchDB and I want to report on
>> requests by IP address in the last n of time. The ultimate goal is to
>> key on IP address, returning a count of requests within the last day.
>>
>> I've got the count by IP working fine with the following map/reduce:
>>
>> function(doc)
>> {
>>   emit(doc.request.REMOTE_ADDR,1);
>> }
>> function(keys, values, rereduce)
>> {
>>   return sum(values);
>> }
>>
>> Can anyone guide me as to how to emit only records where the date/time
>> (stored as REQUEST_TIME in seconds since epoch), e.g. 1306343401.
>> Effectively I want the map to emit records where that field is greater
>> than the current timestamp - n seconds.
>>
>> Any pointers greatly appreciated.
>>
>
> Something like:
> function(doc)
> {
>  if(doc.request.REQUEST_TIME > (new Date().getTime() - N)) {
>    emit(doc.request.REMOTE_ADDR,1);
>  }
> }
>

AFAIK the view is not run dynamically but is stored in the view
b-tree, so I'm not sure how this would work unless it was in a list
instead. But I'd be interested to find out what the result is.

Try simply sorting the view by request time (emit
doc.request.REQUEST_TIME, doc.request.REMOTE_ADDR) and then query with
start/end and perhaps descending=true.

Some more info in:
http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options
http://wiki.apache.org/couchdb/View_collation
http://guide.couchdb.org/draft/views.html which also has a great
example of how do use a single view to query across multiple date
types (year or month or day).

A+
Dave

Mime
View raw message