Module: Platform

Defined in:
lib/athergin/platform.rb

Overview

todo: module Athergin

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/athergin/platform.rb', line 4

def config
  @config
end

.connectionsObject (readonly)

Returns the value of attribute connections.



4
5
6
# File 'lib/athergin/platform.rb', line 4

def connections
  @connections
end

.eval_queueObject (readonly)

Returns the value of attribute eval_queue.



4
5
6
# File 'lib/athergin/platform.rb', line 4

def eval_queue
  @eval_queue
end

.namespacesObject (readonly)

Returns the value of attribute namespaces.



4
5
6
# File 'lib/athergin/platform.rb', line 4

def namespaces
  @namespaces
end

Class Method Details

.connect!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/athergin/platform.rb', line 10

def connect!
  @connections = {}
  config.environments.each do |env|
    database = config.database_config env
    begin
      pool_size, pool_timeout = database.pool_size || 50, database.pool_timeout || 60
      if database.hosts.present?
        @connections[env] = Mongo::ReplSetConnection.new database.hosts, pool_size: pool_size, pool_timeout: pool_timeout
      else
        @connections[env] = Mongo::Connection.new database.hostname, database.port, pool_size: pool_size, pool_timeout: pool_timeout
      end
    rescue Mongo::ConnectionFailure => e
      warn "Warning (could not connect to the #{ env } environment, skipping): #{ e.message }"
    end
  end
end

.connected_environmentsObject



77
78
79
80
# File 'lib/athergin/platform.rb', line 77

def connected_environments
  # todo: reverse is kind of ugly here
  @connections.keys.reverse rescue []
end

.connectionObject



82
83
84
# File 'lib/athergin/platform.rb', line 82

def connection
  @connections[environment]
end

.cookiesObject



64
65
66
# File 'lib/athergin/platform.rb', line 64

def cookies
  Thread.current[:cookies] || {}
end

.current_namespaceObject



94
95
96
# File 'lib/athergin/platform.rb', line 94

def current_namespace
  current_report.try(:namespace)
end

.current_queryObject



103
104
105
106
# File 'lib/athergin/platform.rb', line 103

def current_query
  return unless params[:type].eql? 'queries'
  Query.find_by_name params[:name]
end

.current_reportObject



98
99
100
101
# File 'lib/athergin/platform.rb', line 98

def current_report
  return unless params[:type].eql? 'reports'
  Report.find_by_name params[:name]
end

.database(name) ⇒ Object



90
91
92
# File 'lib/athergin/platform.rb', line 90

def database(name)
  Platform.connection[database_name(name)]
end

.database_name(name) ⇒ Object



86
87
88
# File 'lib/athergin/platform.rb', line 86

def database_name(name)
  database_name = Platform.config.database_override.try(name) || name
end

.environmentObject



68
69
70
# File 'lib/athergin/platform.rb', line 68

def environment
  @environment || cookies['environment'].try(:to_sym) || ENV['REPORTS_ENV'].try(:to_sym) || :development
end

.exact_match?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/athergin/platform.rb', line 52

def exact_match?
  params[:exact_match].present?
end

.load_config!(config_file = 'config/environment.yml') ⇒ Object



6
7
8
# File 'lib/athergin/platform.rb', line 6

def load_config!(config_file='config/environment.yml')
  @config = Configuration.new config_file
end

.namespace(name, opts = {}, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/athergin/platform.rb', line 27

def namespace(name, opts={}, &block)
  file_execution_order = namespace_display_order = opts[:display_order] || 10_000

  @namespaces = {} if @namespaces.nil?
  @namespaces[name] = Namespace.new(name, namespace_display_order) if @namespaces[name].nil?

  @eval_queue = [] if @eval_queue.nil?
  @eval_queue << [file_execution_order, @namespaces[name], block]
end

.paramsObject



44
45
46
# File 'lib/athergin/platform.rb', line 44

def params
  Thread.current[:params]
end

.query_limitObject



56
57
58
# File 'lib/athergin/platform.rb', line 56

def query_limit
  params[:limit]
end

.query_offsetObject



60
61
62
# File 'lib/athergin/platform.rb', line 60

def query_offset
  params[:offset]
end

.run_eval_queue!Object

execute the files in the right order as specified in the reports directory (for display order in index page and menu)



38
39
40
41
42
# File 'lib/athergin/platform.rb', line 38

def run_eval_queue!
  eval_queue.sort { |a,b| a.first <=> b.first }.each do |file_execution_order,namespace,block|
    namespace.instance_eval &block
  end
end

.search_paramsObject



48
49
50
# File 'lib/athergin/platform.rb', line 48

def search_params
  (params[:search] || {}).reject { |param,value| value.blank? }
end

.set_environment!(env) ⇒ Object



72
73
74
75
# File 'lib/athergin/platform.rb', line 72

def set_environment!(env)
  puts "Setting environment as #{ env }"
  @environment = env.to_sym
end