Note that there are some explanatory texts on larger screens.

plurals
  1. POCouchDB function to sample records at a given interval.
    primarykey
    data
    text
    <p>I have records with a time value and need to be able to query them for a span of time and return only records at a given interval. </p> <p>For example I may need all the records from 12:00 to 1:00 in 10 minute intervals giving me 12:00, 12:10, 12:20, 12:30, ... 12:50, 01:00. The interval needs to be a parameter and it may be any time value. 15 minutes, 47 seconds, 1.4 hours. </p> <p>I attempted to do this doing some kind of reduce but that is apparently the wrong place to do it. </p> <p>Here is what I have come up with. Comments are welcome. </p> <p>Created a view for the time field so I can query a range of times. The view outputs the id and the time.</p> <pre><code>function(doc) { emit([doc.rec_id, doc.time], [doc._id, doc.time]) } </code></pre> <p>Then I created a list function that accepts a param called interval. In the list function I work thru the rows and compare the current rows time to the last accepted time. If the span is greater or equal to the interval I add the row to the output and JSON-ify it. </p> <pre><code>function(head, req) { // default to 30000ms or 30 seconds. var interval = 30000; // get the interval from the request. if (req.query.interval) { interval = req.query.interval; } // setup var row; var rows = []; var lastTime = 0; // go thru the results... while (row = getRow()) { // if the time from view is more than the interval // from our last time then add it. if (row.value[1] - lastTime &gt; interval) { lastTime = row.value[1]; rows.push(row); } } // JSON-ify! send(JSON.stringify({'rows' : rows})); } </code></pre> <p>So far this is working well. I will test against some large data to see how the performance is. Any comments on how this could be done better or would this be the correct way with couch?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload