Class: Appstats::Result

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/appstats/result.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.calculate_count_to_s(raw_count, data = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/appstats/result.rb', line 79

def self.calculate_count_to_s(raw_count,data = {})
  return "--" if raw_count.nil?
  if data[:format] == :short_hand
    lookups = { 1000.0 => 'thousand', 1000000.0 => 'million', 1000000000.0 => 'billion', 1000000000000.0 => 'trillion' }
    lookups.keys.sort.reverse.each do |v|
      next if v > raw_count
      short_hand = (raw_count / v * 10).round / 10.0
      short_hand = short_hand.round if short_hand.round == short_hand
      return "#{add_commas(short_hand)} #{lookups[v]}"
    end
  end
  add_commas(raw_count)
end

.fix_all_is_latestObject



71
72
73
74
75
76
77
# File 'lib/appstats/result.rb', line 71

def self.fix_all_is_latest
  connection.update('update appstats_results set is_latest = false')
  all = connection.select_all("select concat(id,' ',max(updated_at)) as id_and_date from appstats_results group by query")
  return if all.empty?
  ids = all.each.collect { |e| e["id_and_date"].split[0] }.compact
  connection.update("update appstats_results set is_latest = '1' where id in (#{ids.join(',')})")
end

Instance Method Details

#==(o) ⇒ Object Also known as: eql?



66
67
68
# File 'lib/appstats/result.rb', line 66

def ==(o)
   o.class == self.class && o.send(:state) == state
end

#count_to_s(data = {}) ⇒ Object



62
63
64
# File 'lib/appstats/result.rb', line 62

def count_to_s(data = {})
  Appstats::Result.calculate_count_to_s(count,data)
end

#date_to_sObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/appstats/result.rb', line 14

def date_to_s
  return "" if from_date.nil? && to_date.nil?
  
  from_s = nil
  to_s = nil
  
  if !from_date.nil? && to_date.nil? && created_at.nil?
    from_s = from_date_to_s
    to_s = "present"
  elsif !from_date.nil? && to_date.nil? && !created_at.nil?
    from_s = from_date_to_s
    to_s = created_at.strftime('%Y-%m-%d')
  elsif from_date.nil? && !to_date.nil?
    from_s = "up"
    to_s = to_date_to_s
  else
    from_s = from_date_to_s
    to_s = to_date_to_s
  end
  
  return from_s if from_s == to_s
  "#{from_s} to #{to_s}"
end

#from_date_to_sObject



38
39
40
41
# File 'lib/appstats/result.rb', line 38

def from_date_to_s
  return "" if from_date.nil?
  from_date.strftime('%Y-%m-%d')
end

#group_query_duration_to_sObject



58
59
60
# File 'lib/appstats/result.rb', line 58

def group_query_duration_to_s
  FriendlyTimer.calculate_duration_to_s(group_query_duration_in_seconds)
end

#host_to_sObject



48
49
50
51
52
# File 'lib/appstats/result.rb', line 48

def host_to_s
  return host if (db_host.blank? || host == db_host)
  return db_host if host.blank?
  "#{host} (host), #{db_host} (db_host)"
end

#query_duration_to_sObject



54
55
56
# File 'lib/appstats/result.rb', line 54

def query_duration_to_s
  FriendlyTimer.calculate_duration_to_s(query_duration_in_seconds)
end

#to_date_to_sObject



43
44
45
46
# File 'lib/appstats/result.rb', line 43

def to_date_to_s
  return "" if to_date.nil?
  to_date.strftime('%Y-%m-%d')
end