Note that there are some explanatory texts on larger screens.

plurals
  1. POCouchDB - filter latest log per logged instance from a list
    primarykey
    data
    text
    <p>i could use some help filtering distinct values from a couchdb view. I have a database that stores logs with information about computers. Periodically new logs for a computer are written to the db.</p> <p>A bit simplified i store entries like these:</p> <pre><code>{ "name": "NAS", "os": "Linux", "timestamp": "2011-03-03T16:26:39Z", } { "name": "Server1", "os": "Windows", "timestamp": "2011-02-03T19:31:31Z", } { "name": "NAS", "os": "Linux", "timestamp": "2011-02-03T18:21:29Z", } </code></pre> <p>So far i am struggling to filter this list by distinct entries. What i'd like to receive is the latest logfile for each device.</p> <p>I have a view like this:</p> <pre><code>function(doc) { emit([doc.timestamp,doc.name], doc); } </code></pre> <p>Im querying this view with python (couchdbkit) and the best solution i came up with so far looks like this:</p> <pre><code>def get_latest_logs(cls): unique_names = [] unique = [] for log in cls.view("logs/timestamp", descending=True): if log.name not in unique_names: unique_names.append(log.name) unique.append(log) return unique </code></pre> <p>Okay ... this works. But i have the strong feeling, that this is not the best solution as python needs to iterate the whole list of logfiles (which could become quite long).</p> <p>I guess i need a reduce function but i couldn't really find any examples or explanations that i could adapt to my problem.</p> <p>So, what i am looking for is a (pure couchdb) view, that only spits out the latest log for a given device.</p> <p>Thanks, Andreas</p>
    singulars
    1. This table or related slice is empty.
    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