log_search.py
changeset 66 090ed78ec8fa
parent 65 8b50694f841e
child 67 13975aa16b4c
--- a/log_search.py	Mon Feb 09 11:46:17 2009 +0200
+++ b/log_search.py	Mon Feb 09 12:07:01 2009 +0200
@@ -122,32 +122,76 @@
             source      = doc.attr('@source')
             data        = doc.cat_texts().decode('utf8')
 
-            # build+yield to (channel_id, LogLine) tuple
-            yield (channel_id, log_line.LogLine(None, type, timestamp, source, data))
+            # build+yield to as LogLine
+            # XXX: ignore channel_id for now
+            yield log_line.LogLine(None, type, timestamp, source, data)
+    
+    def search (self, options=None, channel=None, phrase=None, order=None, max=None, skip=None) :
+        """
+            Search with flexible parameters
 
-    def search_simple (self, channel, query) :
-        """
-            Search for lines from the given channel for the given simple query
+                options     - bitmask of hype.Condition.*
+                channel     - LogChannel object
+                phrase      - the search query phrase
+                order       - order attribute expression
+                max         - number of results to return
+                skip        - number of results to skip
         """
 
         # build condition
         cond = hype.Condition()
-
-        # simplified phrase
-        cond.set_options(hype.Condition.SIMPLE)
-
-        # add channel attribute
-        cond.add_attr("@channel STREQ %s" % (channel.id, ))
+        
+        if options :
+            # set options
+            cond.set_options(options)
+        
+        if channel :
+            # add channel attribute
+            cond.add_attr("@channel STREQ %s" % (channel.id, ))
+        
+        if phrase :
+            # add phrase
+            cond.set_phrase(phrase)
+        
+        if order :
+            # set order
+            cond.set_order(order)
+        
+        if max :
+            # set max
+            cond.set_max(max)
 
-        # add phrase
-        cond.set_phrase(query)
-
-        # set order
-        cond.set_order("@timestamp NUMA")
+        if skip :
+            # set skip
+            cond.set_skip(skip)
 
-        # search with cond
-        for channel_id, line in self.search_cond(cond) :
-            assert channel_id == channel.id
+        # execute
+        return self.search_cond(cond)
 
-            yield line
+    def search_simple (self, channel, query, count=None, offset=None) :
+        """
+            Search for lines from the given channel for the given simple query
+        """
+        
+        # use search(), backwards
+        results = list(self.search(
+            # simplified phrase
+            options     = hype.Condition.SIMPLE,
 
+            # specific channel
+            channel     = channel,
+
+            # given phrase
+            phrase      = query,
+
+            # order by timestamp
+            order       = "@timestamp NUMD",
+
+            # count/offset
+            max         = count,
+            skip        = offset,
+        ))
+        
+        # reverse
+        return reversed(results)
+