diff --git a/Gemfile b/Gemfile index 3eaebc6..bdefa38 100644 --- a/Gemfile +++ b/Gemfile @@ -14,3 +14,4 @@ gem 'chronic' gem 'kubeclient' gem 'pry', group: :development +gem 'dotenv', require: 'dotenv/load', group: :development diff --git a/Gemfile.lock b/Gemfile.lock index a2ffe7f..ea227d3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -28,6 +28,7 @@ GEM concurrent-ruby (1.0.5) domain_name (0.5.20161021) unf (>= 0.0.5, < 1.0.0) + dotenv (2.2.1) erubis (2.7.0) faraday (0.14.0) multipart-post (>= 1.2, < 3) @@ -103,6 +104,7 @@ DEPENDENCIES activesupport chronic clickhouse! + dotenv json kubeclient parslet diff --git a/lib/loghouse_query/clickhouse.rb b/lib/loghouse_query/clickhouse.rb index 2621346..04cbd29 100644 --- a/lib/loghouse_query/clickhouse.rb +++ b/lib/loghouse_query/clickhouse.rb @@ -4,6 +4,7 @@ class LoghouseQuery module Clickhouse extend ActiveSupport::Concern + MAX_GREEDY_SEARCH_PERIODS = 2 def result @result ||= begin @@ -37,7 +38,9 @@ def to_clickhouse(table, from, to, lim = nil) def result_older(start_time, lim, stop_at = nil) result = [] time = start_time - while lim.positive? && (stop_at.blank? || time >= stop_at) + stop_at ||= start_time - LogsTables::PARTITION_PERIOD.hours * MAX_GREEDY_SEARCH_PERIODS + + while lim.positive? && (time >= stop_at) table = LogsTables.partition_table_name(time) break unless ::Clickhouse.connection.exists_table(table) @@ -55,7 +58,10 @@ def result_older(start_time, lim, stop_at = nil) def result_newer(start_time, lim, stop_at = nil) result = [] time = start_time - while lim.positive? && (stop_at.blank? || time <= stop_at) + stop_at ||= start_time + LogsTables::PARTITION_PERIOD.hours * MAX_GREEDY_SEARCH_PERIODS + stop_at = Time.zone.now if stop_at > Time.zone.now + + while lim.positive? && (time <= stop_at) table = LogsTables.partition_table_name(time) break unless ::Clickhouse.connection.exists_table(table)