Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is what I do. This is borderline CouchDB abuse however I have had much success.</p> <p>Usually, <code>reduce</code> will compute a sum, or a count, or something like that. However, think of reduce as an elimination tournament. Many values go in. Only one comes out. A reduction! Repeat over and over and you have the ultimate winner (a re-reduction). In this case, the log with the latest timestamp is the winner.</p> <p>Of course, welterweights can't fight heavyweights. There have to be leagues and weight classes. It only makes sense for <em>certain</em> documents to do battle with <em>certain other</em> similar documents. That is exactly what the reduce <strong>group</strong> parameter will do. It will ensure that only evenly-matched gladiators enter the steel cage in our bloodsport. (Coffee is kicking in.)</p> <p>First, emit all logs keyed by device. The <code>value</code> emitted is simply a copy of the document.</p> <pre><code>function(doc) { emit(doc.name, doc); } </code></pre> <p>Next, write a reduce function to return the latest timestamp of all given values. If you see a fight between two gladiators from different leagues (two logs from different systems), stop the fight! Something went wrong (somebody queried without the correct <code>group</code> value).</p> <pre><code>function(keys, vals, re) { var challenger, winner = null; for(var a = 0; a &lt; vals.length; a++) { challenger = vals[a]; if(!winner) { // The title is unchallenged. This value is the winner. winner = challenger; } else { // Fight! if(winner.name !== challenger.name) { // Stop the fight! He's gonna kill him! return null; // With a grouping query, this will never happen. } else if(winner.timestamp &gt; challenger.timestamp) { // The champ wins! (Nothing to do.) } else { // The challenger wins! winner = challenger; } } } // Today's champion lives to fight another day. return winner; } </code></pre> <p>(Note, the timestamp comparison is probably wrong. You will have to convert to a <code>Date</code> probably.)</p> <p>Now, when you query a view with <code>?group=true</code>, then CouchDB will only reduce (find the winner between) values with the same <code>key</code>, which is your machine name.</p> <p>(You can also emit an array as a key, which gives a bit more flexibility. You could <code>emit([doc.name, doc.timestamp], doc)</code> instead. So you can see all logs by system with a query like <code>?reduce=false&amp;startkey=["NAS", null]&amp;endkey=["NAS", {}]</code> or you could see latest logs by system with <code>?group_level=1</code>.</p> <p>Finally, the "stop the fight" stuff is optional. You could simply always return the document with the latest timestamp. However, I prefer to keep it there because in similar situations, I want to see if I am map-reducing incorrectly, and a null reduce output is my big clue.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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