Class: Statlysis::Cron
Instance Attribute Summary collapse
-
#clock ⇒ Object
readonly
Returns the value of attribute clock.
-
#multiple_dataset ⇒ Object
readonly
Returns the value of attribute multiple_dataset.
-
#source_type ⇒ Object
readonly
Returns the value of attribute source_type.
-
#time_column ⇒ Object
readonly
Returns the value of attribute time_column.
-
#time_unit ⇒ Object
readonly
Returns the value of attribute time_unit.
-
#time_zone ⇒ Object
readonly
Returns the value of attribute time_zone.
Instance Method Summary collapse
- #_source ⇒ Object
- #group_by_columns? ⇒ Boolean
-
#initialize(s, opts = {}) ⇒ Cron
constructor
A new instance of Cron.
- #is_activerecord? ⇒ Boolean
- #is_mongoid? ⇒ Boolean
- #is_orm? ⇒ Boolean
-
#is_time_column_integer? ⇒ Boolean
兼容采用整数类型作时间字段.
- #output ⇒ Object
- #reoutput ⇒ Object
- #run ⇒ Object
- #setup_stat_model ⇒ Object
- #source_where_array ⇒ Object
- #time_column? ⇒ Boolean
-
#time_range ⇒ Object
automode or specify TIME_RANGE and TIME_UNIT in shell to run.
Methods included from Common
Constructor Details
#initialize(s, opts = {}) ⇒ Cron
Returns a new instance of Cron.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/statlysis/cron.rb', line 9 def initialize s, opts = {} # setup data type related @source_type = ({Utils.is_activerecord?(s) => :activerecord, Utils.is_mongoid?(s) => :mongoid}.detect {|k, v| k } || {})[1] || :unknown @time_column = opts[:time_column] @time_unit = opts[:time_unit] @time_zone = opts[:time_zone] || Statlysis.default_time_zone || Time.zone || Time.now.utc_offset # insert source as a dataset @multiple_dataset = (s.is_a?(ActiveRecordDataset) ? s : ActiveRecordDataset.new(cron).add_source(s)) if is_activerecord? @multiple_dataset = (s.is_a?(MongoidDataset) ? s : MongoidDataset.new(cron).add_source(s)) if is_mongoid? @multiple_dataset.instance_variable_set("@cron", cron) if is_orm? && @multiple_dataset.cron.nil? @stat_table_name = opts[:stat_table_name] if opts[:stat_table_name] cron end |
Instance Attribute Details
#clock ⇒ Object (readonly)
Returns the value of attribute clock.
6 7 8 |
# File 'lib/statlysis/cron.rb', line 6 def clock @clock end |
#multiple_dataset ⇒ Object (readonly)
Returns the value of attribute multiple_dataset.
5 6 7 |
# File 'lib/statlysis/cron.rb', line 5 def multiple_dataset @multiple_dataset end |
#source_type ⇒ Object (readonly)
Returns the value of attribute source_type.
5 6 7 |
# File 'lib/statlysis/cron.rb', line 5 def source_type @source_type end |
#time_column ⇒ Object (readonly)
Returns the value of attribute time_column.
5 6 7 |
# File 'lib/statlysis/cron.rb', line 5 def time_column @time_column end |
#time_unit ⇒ Object (readonly)
Returns the value of attribute time_unit.
5 6 7 |
# File 'lib/statlysis/cron.rb', line 5 def time_unit @time_unit end |
#time_zone ⇒ Object (readonly)
Returns the value of attribute time_zone.
5 6 7 |
# File 'lib/statlysis/cron.rb', line 5 def time_zone @time_zone end |
Instance Method Details
#_source ⇒ Object
33 |
# File 'lib/statlysis/cron.rb', line 33 def _source; cron.multiple_dataset.sources.first end |
#group_by_columns? ⇒ Boolean
75 |
# File 'lib/statlysis/cron.rb', line 75 def group_by_columns?; !!@group_by_columns.any? end |
#is_activerecord? ⇒ Boolean
30 |
# File 'lib/statlysis/cron.rb', line 30 def is_activerecord?; @source_type == :activerecord; end |
#is_mongoid? ⇒ Boolean
31 |
# File 'lib/statlysis/cron.rb', line 31 def is_mongoid?; @source_type == :mongoid; end |
#is_orm? ⇒ Boolean
32 |
# File 'lib/statlysis/cron.rb', line 32 def is_orm?; [:activerecord, :mongoid].include?(@source_type); end |
#is_time_column_integer? ⇒ Boolean
兼容采用整数类型作时间字段
67 68 69 70 71 72 73 |
# File 'lib/statlysis/cron.rb', line 67 def is_time_column_integer? if is_activerecord? _source.columns_hash[cron.time_column.to_s].type == :integer else false end end |
#output ⇒ Object
26 |
# File 'lib/statlysis/cron.rb', line 26 def output; raise DefaultNotImplementWrongMessage end |
#reoutput ⇒ Object
27 |
# File 'lib/statlysis/cron.rb', line 27 def reoutput; @output = nil; output end |
#run ⇒ Object
29 |
# File 'lib/statlysis/cron.rb', line 29 def run; raise DefaultNotImplementWrongMessage end |
#setup_stat_model ⇒ Object
28 |
# File 'lib/statlysis/cron.rb', line 28 def setup_stat_model; raise DefaultNotImplementWrongMessage end |
#source_where_array ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/statlysis/cron.rb', line 35 def source_where_array # TODO follow index seq a = _source.where("").where_values.map do |equality| # use full keyvalue index name equality.is_a?(String) ? equality.to_sym : "#{equality.operand1.name}#{equality.operand2}" end if is_activerecord? a = _source.all.selector.reject {|k, v| k == 't' } if is_mongoid? a.map {|s1| s1.to_s.split(//).select {|s2| s2.match(/[a-z0-9]/i) }.join }.sort.map(&:to_sym) end |
#time_column? ⇒ Boolean
74 |
# File 'lib/statlysis/cron.rb', line 74 def time_column?; !!@time_column end |
#time_range ⇒ Object
automode or specify TIME_RANGE and TIME_UNIT in shell to run
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/statlysis/cron.rb', line 48 def time_range return TimeSeries.parse(ENV['TIME_RANGE'], :unit => (ENV['TIME_UNIT'] || 'day'), :zone => cron.time_zone) if ENV['TIME_RANGE'] # 选择开始时间。取出统计表的最后时间,和数据表的最先时间对比,哪个在后就选择哪个 begin_day = DateTime.now.beginning_of_day st_timebegin = (a = cron.stat_model.order(:t).where("t >= ?", begin_day.yesterday).first) ? a[:t] : nil # TODO support multiple log cron.stat_model.where("t >= ?", begin_day.tomorrow).delete # 明天的数据没出来肯定统计不了 timebegin = (multiple_dataset.first_time != DateTime1970) ? multiple_dataset.first_time : (DateTime.now - 1.second) timebegin = Time.at(timebegin) if is_time_column_integer? timebegin = (st_timebegin > timebegin) ? st_timebegin : timebegin if st_timebegin timeend = DateTime.now logger.info "#{multiple_dataset.name}'s range #{timebegin..timeend}" # 把统计表的最后时间点也包含进去重新计算下 TimeSeries.parse(timebegin..timeend, :unit => cron.time_unit) end |