[Example] es-find -r/--range "start_time=2021-1-1 00:00:00,2021-1-1 23:59:59;..."
Signed-off-by: Lu Weitao luweitaobe@163.com --- sbin/es-find | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)
diff --git a/sbin/es-find b/sbin/es-find index 17eefe6..8348554 100755 --- a/sbin/es-find +++ b/sbin/es-find @@ -5,6 +5,7 @@
# Usage: # es-find key=val ... +# es-find -r "start_time=2021-1-1 00:00:00,2021-1-1 23:59:59;..." # if given multi keys, they create AND conditions # if given val1,val2,... for a key, they create OR conditions # Eg: query (testbox is xxx or yyy) and (suite is aaa) and (user is bbb). @@ -29,7 +30,42 @@ def parse_argv items end
+# output: +# eg: +# { +# start_time => { +# gte: '2020-09-10 01:50:00', +# lte: '2020-09-10 01:53:00' +# }, +# ... +# } +def prase_range(range) + range_hash = {} + keyword_list = range.split(';') + puts "keyword_list ==> #{keyword_list}" + keyword_list.each do |keyword| + key, value = keyword.split('=') + next unless key && value + + assign_range(range_hash, key, value) + end + + range_hash +end + +def assign_range(range_hash, key, value) + range_hash[key] = {} + value_list = value.split(',') + if value_list.length > 1 + range_hash[key][:gte] = value_list[0] + range_hash[key][:lte] = value_list[1] + else + range_hash[key][:gte] = value + end +end + options = { nr_jobs: 10, desc_keyword: nil } +range = nil
opt_parser = OptionParser.new do |opts| opts.banner = 'Usage: es-find [options] search_key1=val1[,val2..] ..' @@ -49,6 +85,10 @@ opt_parser = OptionParser.new do |opts| options[:desc_keyword] = d end
+ opts.on('-r', '--range KEYWORD', 'range by keyword: start_time|end_time|...') do |r| + range = r + end + opts.on_tail('-h', '--help', 'show this message') do puts opts exit @@ -57,6 +97,8 @@ end
opt_parser.parse!(ARGV) items = parse_argv +items[:range] = prase_range(range) if range + es = ESQuery.new(ES_HOST, ES_PORT) query_result = es.multi_field_query(items, size: options[:nr_jobs], desc_keyword: options[:desc_keyword]) json_string = JSON.pretty_generate(query_result)