5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/ifd_tools/analytics/customer_tracking.rb', line 5
def self.included(dsl)
dsl.action_item only: [:show, :edit] do
link_to "Trends", [:trends, :admin, resource]
end
dsl.send(:member_action, :trends) do
@customer = Customer.find_by_url! params[:id]
@trends = IfdTools::Tracking::Event.where(customer_id: @customer.id).limit(75)
@registration_date = @customer.created_at
@application_launches = IfdTools::Tracking::ApplicationEvent.where(customer_id: @customer.id).count
end
dsl. :trend_info, only: :trends
dsl.send(:collection_action, :demographic_data) do
@total_activity = IfdTools::Tracking::Event.count
@activity = []
db_activity_per_state = IfdTools::Tracking::Event.includes(:customer).group("customers.state").count
users_per_state = Customer.group(:state).count
IfdTools::States::all.each do |state, long_name|
@activity << OpenStruct.new(name: long_name, activity: db_activity_per_state[state] || 0.00, users: users_per_state[state] || 0)
end
end
dsl.send(:collection_action, :filter_events, method: :post) do
cookies[:filter_events] = params[:q].to_yaml
respond_to do |format|
format.js
format.html { redirect_to admin_dashboard_url }
end
end
dsl.send(:collection_action, :events_data) do
filters = YAML.load(cookies[:filter_events]).symbolize_keys rescue {}
@color = IfdTools::Configure.dashboard_graph_color
@events = IfdTools::Tracking::Event.scoped
unless filters.empty?
@events = IfdTools::Tracking::Event.search(filters)
if filters[:timestamp_gte].present? and filters[:timestamp_lte].present?
@time_range = "Between #{filters[:timestamp_gte]} and #{filters[:timestamp_lte]}"
elsif filters[:timestamp_gte].present?
@time_range = "Since #{filters[:timestamp_gte]}"
elsif filters[:timestamp_lte].present?
@time_range = "Before #{filters[:timestamp_lte]}"
end
@filter_type = filters[:type_eq].gsub("IfdTools::Tracking::", "").titleize rescue nil
@filter_type = "Everything" if @filter_type.blank?
@caption = "Activity #{@time_range || 'for All Time'}"
@subcaption = "Type: #{@filter_type}"
@subcaption << " (Mac)" if filters[:platform_eq] == "mac"
@subcaption << " (PC)" if filters[:platform_eq] == "pc"
end
@events = @events.group("date(timestamp)")
distribution = @events.count
first_date = distribution.keys.first.to_date rescue Date.today
last_date = distribution.keys.last.to_date rescue Date.today
num_days_in_time_span = (first_date..last_date).count rescue 0
grouping = :days
if num_days_in_time_span > 450
@events = @events.group_by { |e| e.timestamp.beginning_of_year.to_date }
grouping = :years
elsif num_days_in_time_span > 175
@events = @events.group_by { |e| e.timestamp.beginning_of_month.to_date }
grouping = :months
elsif num_days_in_time_span > 25
@events = @events.group_by { |e| e.timestamp.beginning_of_week.to_date }
grouping = :weeks
else
@events = @events.count
end
unless grouping == :days
@events.delete_if { |k, v| v.nil? }.map { |date, arr| @events[date] = arr.count }
end
first_date = @events.keys.first.to_date rescue nil
last_date = @events.keys.last.to_date rescue nil
date_step =
case grouping
when :years
1.year
when :months
1.month
when :weeks
1.week
else
1.day
end
event_keys = []
unless first_date.nil?
curr_date = first_date.dup
until curr_date > last_date do
event_keys << curr_date.to_s
curr_date += date_step
end
end
@events = event_keys.inject({}) { |k, v| k[v] = 0; k }.merge(@events)
cookies.delete(:filter_events)
end
end
|