scripts/search-index
changeset 89 2dc6de43f317
parent 88 0b8e2ba5f76f
child 93 48fca00689e3
equal deleted inserted replaced
88:0b8e2ba5f76f 89:2dc6de43f317
    58         count = index.insert(channel, lines)
    58         count = index.insert(channel, lines)
    59 
    59 
    60         if not options.quiet :
    60         if not options.quiet :
    61             print "OK: %d lines" % count
    61             print "OK: %d lines" % count
    62 
    62 
       
    63 
       
    64 def _parse_date (options, date_str, tz=None, fmt='%Y-%m-%d') :
       
    65     """
       
    66         Parse the given datetime, using the given timezone(defaults to options.tz) and format
       
    67     """
       
    68 
       
    69     # default tz
       
    70     if not tz :
       
    71         tz = options.tz
       
    72 
       
    73     try :
       
    74         # parse
       
    75         return datetime.datetime.strptime(date_str, fmt).replace(tzinfo=tz)
       
    76 
       
    77     except Exception, e :
       
    78         raise CommandError("[ERROR] Invalid date: %s: %s" % (date_str, e))
       
    79 
       
    80 def _output_lines (options, lines) :
       
    81     """
       
    82         Display the formatted LogLines
       
    83     """
       
    84 
       
    85     # display as plaintext
       
    86     for line, txt_data in options.formatter.format_txt(lines, full_timestamps=True) :
       
    87         print txt_data
       
    88 
    63 class CommandError (Exception) :
    89 class CommandError (Exception) :
    64     """
    90     """
    65         Error with command-line arguments
    91         Error with command-line arguments
    66     """
    92     """
    67 
    93 
    85 
   111 
    86     # open index/channel
   112     # open index/channel
    87     index, channel = _open_index_and_channel(options, channel_name, 'c' if options.create else 'a')
   113     index, channel = _open_index_and_channel(options, channel_name, 'c' if options.create else 'a')
    88     
   114     
    89     # handle each date
   115     # handle each date
    90     for date_name in dates :
   116     for date_str in dates :
       
   117         # prase date
    91         try :
   118         try :
    92             # parse date
   119             date = _parse_date(options, date_str, channel.source.tz)
    93             date = datetime.datetime.strptime(date_name, '%Y-%m-%d').replace(tzinfo=channel.source.tz)
   120         
    94 
   121         # handle errors
    95         except Exception, e :
   122         except CommandError, e :
    96             print "[ERROR] Invalid date: %s: %s" % (date_name, e)
       
    97 
       
    98             if options.skip_missing :
   123             if options.skip_missing :
    99                 continue
   124                 print "[ERROR] %s" % (date_name, e)
   100 
   125 
   101             else :
   126             else :
   102                 raise
   127                 raise
   103         
   128         
   104         # load
   129         # otherwise, load
   105         _load_channel_date(index, options, channel, date)
   130         else :        
       
   131             _load_channel_date(index, options, channel, date)
   106 
   132 
   107 def cmd_load_month (options, channel_name, *months) :
   133 def cmd_load_month (options, channel_name, *months) :
   108     """
   134     """
   109         Loads the logs for a specific channel for the given months (in terms of the channel's timezone) into the index
   135         Loads the logs for a specific channel for the given months (in terms of the channel's timezone) into the index
   110     """
   136     """
   111 
   137 
   112     # open index/channel
   138     # open index/channel
   113     index, channel = _open_index_and_channel(options, channel_name, 'c' if options.create else 'a')
   139     index, channel = _open_index_and_channel(options, channel_name, 'c' if options.create else 'a')
   114     
   140     
   115     # handle each date
   141     # handle each date
   116     for month_name in months :
   142     for month_str in months :
       
   143         # prase date
   117         try :
   144         try :
   118             # parse date
   145             month = _parse_date(options, month_str, channel.source.tz, '%Y-%m')
   119             month = datetime.datetime.strptime(month_name, '%Y-%m').replace(tzinfo=channel.source.tz)
   146         
   120 
   147         # handle errors
   121         except Exception, e :
   148         except CommandError, e :
   122             print "[ERROR] Invalid date: %s: %s" % (month_name, e)
   149             # skip?
   123 
       
   124             if options.skip_missing :
   150             if options.skip_missing :
       
   151                 print "[ERROR] %s" % (date_name, e)
   125                 continue
   152                 continue
   126 
   153 
   127             else :
   154             else :
   128                 raise
   155                 raise
   129         
   156         
   144     """
   171     """
   145         Search the index for events on a specific channel with the given query
   172         Search the index for events on a specific channel with the given query
   146     """
   173     """
   147     
   174     
   148     # sanity-check
   175     # sanity-check
   149     if options.create_index :
   176     if options.create :
   150         raise Exception("--create doesn't make sense for 'search'")
   177         raise Exception("--create doesn't make sense for 'search'")
   151     
   178     
   152     # open index/channel
   179     # open index/channel
   153     index, channel = _open_index_and_channel(options, channel_name, 'r')
   180     index, channel = _open_index_and_channel(options, channel_name, 'r')
   154     
   181     
   155     # search
   182     # search
   156     lines = index.search_simple(channel, query)
   183     lines = index.search_simple(channel, query)
   157     
   184     
   158     # display as plaintext
   185     # display
   159     for line in options.formatter.format_txt(lines) :
   186     _output_lines(options, lines)
   160         print line
   187 
       
   188 def cmd_list (options, channel_name, *dates) :
       
   189     """
       
   190         List the indexed events for a specific date
       
   191     """
       
   192 
       
   193     # sanity-check
       
   194     if options.create :
       
   195         raise Exception("--create doesn't make sense for 'search'")
       
   196     
       
   197     # open index/channel
       
   198     index, channel = _open_index_and_channel(options, channel_name, 'r')
       
   199 
       
   200     # ...for each date
       
   201     for date_str in dates :
       
   202         # parse date
       
   203         date = _parse_date(options, date_str)
       
   204 
       
   205         # list
       
   206         lines = index.list(channel, date)
       
   207         
       
   208         # display
       
   209         _output_lines(options, lines)
   161 
   210 
   162 def cmd_help (options, *args) :
   211 def cmd_help (options, *args) :
   163     """
   212     """
   164         Help about commands
   213         Help about commands
   165     """
   214     """