Class: Statlysis::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/statlysis/configuration.rb

Constant Summary collapse

DelegateMethods =
[
  :sequel, :set_database, :check_set_database,
  :default_time_zone,
  :set_tablename_default_pre, :tablename_default_pre
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#database_optsObject

variables



13
14
15
# File 'lib/statlysis/configuration.rb', line 13

def database_opts
  @database_opts
end

#default_time_columnsObject

variables



13
14
15
# File 'lib/statlysis/configuration.rb', line 13

def default_time_columns
  @default_time_columns
end

#default_time_zoneObject

variables



13
14
15
# File 'lib/statlysis/configuration.rb', line 13

def default_time_zone
  @default_time_zone
end

#is_skip_database_indexObject

Returns the value of attribute is_skip_database_index.



14
15
16
# File 'lib/statlysis/configuration.rb', line 14

def is_skip_database_index
  @is_skip_database_index
end

#sequelObject

variables



13
14
15
# File 'lib/statlysis/configuration.rb', line 13

def sequel
  @sequel
end

#tablename_default_preObject

variables



13
14
15
# File 'lib/statlysis/configuration.rb', line 13

def tablename_default_pre
  @tablename_default_pre
end

Instance Method Details

#always(source, opts = {}) ⇒ Object

IMPORTANT set :time_unit to false



71
# File 'lib/statlysis/configuration.rb', line 71

def always source, opts = {}; timely source, {:time_unit => false, :time_column => false}.merge(opts) end

#check_set_databaseObject



67
# File 'lib/statlysis/configuration.rb', line 67

def check_set_database; raise "Please setup database first" if sequel.nil?  end

#daily(source, opts = {}) ⇒ Object



69
# File 'lib/statlysis/configuration.rb', line 69

def daily  source, opts = {}; timely source, {:time_unit => :day}.merge(opts) end

#hotest_items(key, id_to_score_and_time_hash = {}) ⇒ Object

TODO 为什么一层proc的话会直接执行的



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/statlysis/configuration.rb', line 81

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



70
# File 'lib/statlysis/configuration.rb', line 70

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, …



74
75
76
77
78
# File 'lib/statlysis/configuration.rb', line 74

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(obj) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/statlysis/configuration.rb', line 34

def set_database obj
  self.database_opts = case obj
                       when Hash
                         obj
                       when Symbol, String
                         YAML.load_file(Rails.root.join("config/database.yml"))[Rails.env].merge('database' => obj.to_s)
                       else
                         raise "Statlysis#set_database only support symbol or hash params"
                       end

  raise "database_opts should not be blank" if self.database_opts.blank?

  # sqlite dont support regular creating database in mysql style
  self.sequel = if (self.database_opts['adapter'].match(/sqlite/) && self.database_opts['database'].match(/\A:memory:\Z/)) # only for test envrionment
    Sequel.sqlite
  else
    # create database, copied from http://stackoverflow.com/a/14435522/595618
    require 'mysql2'
    mysql2_client = Mysql2::Client.new(self.database_opts.except('database'))
    mysql2_client.query("CREATE DATABASE IF NOT EXISTS #{self.database_opts['database']}")
    Sequel.connect(self.database_opts)
  end

  # 初始化键值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



65
# File 'lib/statlysis/configuration.rb', line 65

def set_default_time_zone zone; self.default_time_zone = zone; return self; end

#set_tablename_default_pre(str) ⇒ Object



66
# File 'lib/statlysis/configuration.rb', line 66

def set_tablename_default_pre str; self.tablename_default_pre = str.to_s; return self end

#similar_items(model_name, id_to_text_hash = {}) ⇒ Object

TODO support mongoid



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/statlysis/configuration.rb', line 96

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

#update_time_columns(*columns) ⇒ Object

会在自动拼接统计数据库表名时去除这些时间字段



28
29
30
31
32
# File 'lib/statlysis/configuration.rb', line 28

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