Class: Statlysis::Configuration
- Inherits:
-
Object
- Object
- Statlysis::Configuration
- Includes:
- Singleton
- Defined in:
- lib/statlysis/configuration.rb
Instance Attribute Summary collapse
-
#database_opts ⇒ Object
Returns the value of attribute database_opts.
-
#default_time_columns ⇒ Object
Returns the value of attribute default_time_columns.
-
#default_time_zone ⇒ Object
Returns the value of attribute default_time_zone.
-
#is_skip_database_index ⇒ Object
Returns the value of attribute is_skip_database_index.
-
#sequel ⇒ Object
Returns the value of attribute sequel.
-
#tablename_default_pre ⇒ Object
Returns the value of attribute tablename_default_pre.
Instance Method Summary collapse
- #check_set_database ⇒ Object
- #daily(source, opts = {}) ⇒ Object
-
#hotest_items(key, id_to_score_and_time_hash = {}) ⇒ Object
TODO 为什么一层proc的话会直接执行的.
- #hourly(source, opts = {}) ⇒ Object
-
#lastest_visits(source, opts) ⇒ Object
the real requirement is to compute lastest items group by special pattens, like user_id, url prefix, …
- #set_database(sym_or_hash) ⇒ Object
- #set_default_time_zone(zone) ⇒ Object
- #set_tablename_default_pre(str) ⇒ Object
-
#similar_items(model_name, id_to_text_hash = {}) ⇒ Object
TODO support mongoid.
- #timely(source, opts) ⇒ Object
-
#update_time_columns(*columns) ⇒ Object
会在自动拼接统计数据库表名时去除这些时间字段.
Instance Attribute Details
#database_opts ⇒ Object
Returns the value of attribute database_opts.
12 13 14 |
# File 'lib/statlysis/configuration.rb', line 12 def database_opts @database_opts end |
#default_time_columns ⇒ Object
Returns the value of attribute default_time_columns.
12 13 14 |
# File 'lib/statlysis/configuration.rb', line 12 def default_time_columns @default_time_columns end |
#default_time_zone ⇒ Object
Returns the value of attribute default_time_zone.
12 13 14 |
# File 'lib/statlysis/configuration.rb', line 12 def default_time_zone @default_time_zone end |
#is_skip_database_index ⇒ Object
Returns the value of attribute is_skip_database_index.
13 14 15 |
# File 'lib/statlysis/configuration.rb', line 13 def is_skip_database_index @is_skip_database_index end |
#sequel ⇒ Object
Returns the value of attribute sequel.
12 13 14 |
# File 'lib/statlysis/configuration.rb', line 12 def sequel @sequel end |
#tablename_default_pre ⇒ Object
Returns the value of attribute tablename_default_pre.
12 13 14 |
# File 'lib/statlysis/configuration.rb', line 12 def tablename_default_pre @tablename_default_pre end |
Instance Method Details
#check_set_database ⇒ Object
58 |
# File 'lib/statlysis/configuration.rb', line 58 def check_set_database; raise "Please setup database first" if sequel.nil? end |
#daily(source, opts = {}) ⇒ Object
55 |
# File 'lib/statlysis/configuration.rb', line 55 def daily source, opts = {}; timely source, {:time_unit => :day }.merge(opts) end |
#hotest_items(key, id_to_score_and_time_hash = {}) ⇒ Object
TODO 为什么一层proc的话会直接执行的
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/statlysis/configuration.rb', line 75 def hotest_items key, id_to_score_and_time_hash = {} _p = proc { if block_given? (proc do id_to_score_and_time_hash = Hash.new yield id_to_score_and_time_hash id_to_score_and_time_hash end) else (proc { id_to_score_and_time_hash }) end} self.hotest_crons.push HotestItems.new(key, _p) end |
#hourly(source, opts = {}) ⇒ Object
56 |
# File 'lib/statlysis/configuration.rb', line 56 def hourly source, opts = {}; timely source, {:time_unit => :hour}.merge(opts) end |
#lastest_visits(source, opts) ⇒ Object
the real requirement is to compute lastest items group by special pattens, like user_id, url prefix, …
68 69 70 71 72 |
# File 'lib/statlysis/configuration.rb', line 68 def lastest_visits source, opts self.check_set_database opts.reverse_merge! :time_column => :created_at self.realtime_crons.push LastestVisits.new(source, opts) end |
#set_database(sym_or_hash) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/statlysis/configuration.rb', line 29 def set_database sym_or_hash self.database_opts = if sym_or_hash.is_a? Symbol YAML.load_file(Rails.root.join("config/database.yml"))[sym_or_hash.to_s] elsif Hash sym_or_hash else raise "Statlysis#set_database only support symbol or hash params" end self.sequel = Sequel.connect(self.database_opts) # 初始化键值model ["#{self.tablename_default_pre}_single_kvs", "#{self.tablename_default_pre}_single_kv_histories"].each do |tn| Utils.setup_pattern_table_and_model tn end return self end |
#set_default_time_zone(zone) ⇒ Object
46 47 48 49 |
# File 'lib/statlysis/configuration.rb', line 46 def set_default_time_zone zone self.default_time_zone = zone return self end |
#set_tablename_default_pre(str) ⇒ Object
51 52 53 |
# File 'lib/statlysis/configuration.rb', line 51 def set_tablename_default_pre str self.tablename_default_pre = str.to_s end |
#similar_items(model_name, id_to_text_hash = {}) ⇒ Object
TODO support mongoid
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/statlysis/configuration.rb', line 90 def similar_items model_name, id_to_text_hash = {} _p = if block_given? (proc do id_to_text_hash = Hash.new {|hash, key| hash[key] = "" } yield id_to_text_hash id_to_text_hash end) else (proc { id_to_text_hash }) end self.similar_crons.push Similar.new(model_name, _p) end |
#timely(source, opts) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/statlysis/configuration.rb', line 60 def timely source, opts self.check_set_database opts.reverse_merge! :time_column => :created_at, :time_unit => :day t = Timely.new source, opts self.send("#{opts[:time_unit]}_crons").push t end |
#update_time_columns(*columns) ⇒ Object
会在自动拼接统计数据库表名时去除这些时间字段
23 24 25 26 27 |
# File 'lib/statlysis/configuration.rb', line 23 def update_time_columns *columns self.default_time_columns ||= [:created_at, :updated_at] columns.each {|column| self.default_time_columns.push column } self.default_time_columns = self.default_time_columns.uniq end |