Class: AuditRails::Audit

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
DBExtension
Defined in:
app/models/audit_rails/audit.rb

Class Method Summary collapse

Methods included from DBExtension

adapter_type, hourly

Class Method Details

.analysis(range_begin, range_end) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/audit_rails/audit.rb', line 27

def self.analysis(range_begin, range_end)
  range_scope = in_range(range_begin, range_end)
  {
    :by_user_name      => range_scope.analysis_by_user_name,
    :by_page_views     => range_scope.analysis_by_page_views,
    :per_user_by_page_views => range_scope.analysis_per_user_by_page_views,
    :by_hourly_views   => range_scope.analysis_by_hourly_views,
    :total             => range_scope.count,
    :count_by_day      => count_by_day(range_begin, range_end)
  }
end

.analysis_by_hourly_viewsObject



71
72
73
74
75
76
77
78
79
80
# File 'app/models/audit_rails/audit.rb', line 71

def self.analysis_by_hourly_views
  hourly_counts = Hash[group_by_hours.count.map{|k,v| [k.to_i, v]}]
  hourly_series = Array.new(24, 0)

  hourly_series.each.with_index do |a, i|
    hourly_series[i] = hourly_counts[i] if hourly_counts[i]
  end

  hourly_series.map.with_index{|v,i| {'hour' => i.to_s.rjust(2,'0')+":00", 'count' => v}}.to_json
end

.analysis_by_page_viewsObject



50
51
52
# File 'app/models/audit_rails/audit.rb', line 50

def self.analysis_by_page_views
  group_by_controller_action.count.map{|k,v| {'page' => k.join('/'), 'count' => v}}.to_json
end

.analysis_by_user_nameObject



46
47
48
# File 'app/models/audit_rails/audit.rb', line 46

def self.analysis_by_user_name
  group_by_user_name.count.map{|k,v| {'user' => k, 'count' => v}}.to_json
end

.analysis_per_user_by_page_viewsObject



54
55
56
57
58
59
60
61
# File 'app/models/audit_rails/audit.rb', line 54

def self.analysis_per_user_by_page_views
  users = {}
  group_by_user_name.group_by_controller_action.count.map do |k, v|
    value = [{"page" => "#{k[1]}/#{k[2]}", "count" => v}]
    users[k[0]] = users[k[0]] ? users[k[0]] + value : value
  end
  users.to_json
end

.count_by_day(start_date, end_date) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/audit_rails/audit.rb', line 82

def self.count_by_day(start_date, end_date)
  start_date  = start_date.blank? ? 3.months.ago : start_date
  end_date    = end_date.blank? ? Time.now : end_date

  dates = start_date.to_date..end_date.to_date
  records = AuditRails::Audit.where(created_at: start_date.to_date.beginning_of_day..end_date.to_date.end_of_day).order('created_at')
  records = records.group_by{|audit| audit.created_at.to_date.strftime('%Y%m%d')}

  overall_counts = Hash.new(0)
  overall_counts['Date']='Page views'

  records.each do |date, audits|
    overall_counts[date] = audits.count
  end

  dates = dates.map{|d| d.strftime('%Y%m%d')}

  dates.each do |date|
    overall_counts[date] = "#{overall_counts[date]}".to_i
  end

  overall_counts.to_json
end

.needs_attr_accessible?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'app/models/audit_rails/audit.rb', line 5

def self.needs_attr_accessible?
  Rails::VERSION::MAJOR == 3
end

.no_audit_entry_for_today?(action_name, user_name) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'app/models/audit_rails/audit.rb', line 39

def self.no_audit_entry_for_today?(action_name, user_name)
  audits = where(action: action_name, user_name: user_name, 
    created_at: Time.now.to_date.beginning_of_day..Time.now.to_date.end_of_day)
  
  audits.blank?
end

.unique_visitor_countObject



63
64
65
# File 'app/models/audit_rails/audit.rb', line 63

def self.unique_visitor_count
  group_by_ip_address.count.values.size
end

.visitor_countObject



67
68
69
# File 'app/models/audit_rails/audit.rb', line 67

def self.visitor_count
  group_by_ip_address.count.values.sum
end