log_search.py
changeset 66 090ed78ec8fa
parent 65 8b50694f841e
child 67 13975aa16b4c
equal deleted inserted replaced
65:8b50694f841e 66:090ed78ec8fa
   120             type        = int(doc.attr('@type'))
   120             type        = int(doc.attr('@type'))
   121             timestamp   = datetime.datetime.fromtimestamp(int(doc.attr('@timestamp')), pytz.utc)
   121             timestamp   = datetime.datetime.fromtimestamp(int(doc.attr('@timestamp')), pytz.utc)
   122             source      = doc.attr('@source')
   122             source      = doc.attr('@source')
   123             data        = doc.cat_texts().decode('utf8')
   123             data        = doc.cat_texts().decode('utf8')
   124 
   124 
   125             # build+yield to (channel_id, LogLine) tuple
   125             # build+yield to as LogLine
   126             yield (channel_id, log_line.LogLine(None, type, timestamp, source, data))
   126             # XXX: ignore channel_id for now
       
   127             yield log_line.LogLine(None, type, timestamp, source, data)
       
   128     
       
   129     def search (self, options=None, channel=None, phrase=None, order=None, max=None, skip=None) :
       
   130         """
       
   131             Search with flexible parameters
   127 
   132 
   128     def search_simple (self, channel, query) :
   133                 options     - bitmask of hype.Condition.*
   129         """
   134                 channel     - LogChannel object
   130             Search for lines from the given channel for the given simple query
   135                 phrase      - the search query phrase
       
   136                 order       - order attribute expression
       
   137                 max         - number of results to return
       
   138                 skip        - number of results to skip
   131         """
   139         """
   132 
   140 
   133         # build condition
   141         # build condition
   134         cond = hype.Condition()
   142         cond = hype.Condition()
       
   143         
       
   144         if options :
       
   145             # set options
       
   146             cond.set_options(options)
       
   147         
       
   148         if channel :
       
   149             # add channel attribute
       
   150             cond.add_attr("@channel STREQ %s" % (channel.id, ))
       
   151         
       
   152         if phrase :
       
   153             # add phrase
       
   154             cond.set_phrase(phrase)
       
   155         
       
   156         if order :
       
   157             # set order
       
   158             cond.set_order(order)
       
   159         
       
   160         if max :
       
   161             # set max
       
   162             cond.set_max(max)
   135 
   163 
   136         # simplified phrase
   164         if skip :
   137         cond.set_options(hype.Condition.SIMPLE)
   165             # set skip
       
   166             cond.set_skip(skip)
   138 
   167 
   139         # add channel attribute
   168         # execute
   140         cond.add_attr("@channel STREQ %s" % (channel.id, ))
   169         return self.search_cond(cond)
   141 
   170 
   142         # add phrase
   171     def search_simple (self, channel, query, count=None, offset=None) :
   143         cond.set_phrase(query)
   172         """
       
   173             Search for lines from the given channel for the given simple query
       
   174         """
       
   175         
       
   176         # use search(), backwards
       
   177         results = list(self.search(
       
   178             # simplified phrase
       
   179             options     = hype.Condition.SIMPLE,
   144 
   180 
   145         # set order
   181             # specific channel
   146         cond.set_order("@timestamp NUMA")
   182             channel     = channel,
   147 
   183 
   148         # search with cond
   184             # given phrase
   149         for channel_id, line in self.search_cond(cond) :
   185             phrase      = query,
   150             assert channel_id == channel.id
       
   151 
   186 
   152             yield line
   187             # order by timestamp
       
   188             order       = "@timestamp NUMD",
   153 
   189 
       
   190             # count/offset
       
   191             max         = count,
       
   192             skip        = offset,
       
   193         ))
       
   194         
       
   195         # reverse
       
   196         return reversed(results)
       
   197