Class: Appstats::AppstatsQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/appstats/appstats_query.rb

Constant Summary collapse

@@default_query =
"select 0 as num"
@@action_to_available_contexts =
{
  "appstats_queries" => [ "action", "contexts", "group_by" ],
  "booms" => []
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#queryObject

Returns the value of attribute query.



5
6
7
# File 'lib/appstats/appstats_query.rb', line 5

def query
  @query
end

Class Method Details

.available_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/appstats/appstats_query.rb', line 50

def self.available_action?(action)
  return false if action.blank?
  return @@action_to_available_contexts.keys.include?(action.downcase.pluralize)
end

Instance Method Details

#db_connectionObject



46
47
48
# File 'lib/appstats/appstats_query.rb', line 46

def db_connection
  Appstats.connection
end

#group_query_to_sqlObject



20
21
22
23
# File 'lib/appstats/appstats_query.rb', line 20

def group_query_to_sql
  return nil if query.nil?
  query.group_query_to_sql
end

#process_queryObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/appstats/appstats_query.rb', line 25

def process_query
  return if query.nil?
  query.query_to_sql = @@default_query
  query.group_query_to_sql = nil
  return if query.action.blank?
  
  action = query.action.pluralize.downcase
  case action
    when "appstats_queries"
      count_filter = "COUNT(*)"
      query.query_to_sql = "select #{count_filter} as num from appstats_results#{build_where_clause}"
    when "booms"
      query.query_to_sql = "invalid sql"
  end
  query.group_query_to_sql = query.query_to_sql.sub("#{count_filter} as num","#{context_key_filter_name(action)} as context_key_filter, #{context_value_filter_name(action)} as context_value_filter, COUNT(*) as num") + " group by context_value_filter" unless query.group_by.empty?
end

#query_to_sqlObject



15
16
17
18
# File 'lib/appstats/appstats_query.rb', line 15

def query_to_sql
  return @@default_query if query.nil?
  query.query_to_sql
end

#runObject



42
43
44
# File 'lib/appstats/appstats_query.rb', line 42

def run
  query.run
end