38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/forge/app/models/dispatch.rb', line 38
def chart_data
days = (self.sent_at.to_date..Date.today).to_a
unsubscribes = DispatchUnsubscribe.find_by_sql("SELECT DATE(created_at) as date, COUNT(*) as total FROM dispatch_unsubscribes WHERE dispatch_id = #{self.id} GROUP BY DATE(created_at)").map { |u| {u.date => u.total} }
opens = DispatchOpen.find_by_sql("SELECT DATE(created_at) as date, COUNT(*) as total FROM dispatch_opens WHERE dispatch_id = #{self.id} GROUP BY DATE(created_at)").map { |u| {u.date => u.total} }
clicks = DispatchLinkClick.find_by_sql("SELECT DATE(dlc.created_at) as date, COUNT(*) as total FROM dispatch_link_clicks dlc, dispatch_links dl WHERE dlc.dispatch_link_id = dl.id AND dl.dispatch_id = #{self.id} GROUP BY DATE(dlc.created_at)").map { |u| {u.date => u.total} }
[unsubscribes, opens, clicks].each do |items|
(days - items.map{|i| i.keys[0]}).each {|day| items << {day => 0}}
end
return {
:unsubscribes_over_time => unsubscribes.sort_by {|i| i.keys[0]},
:opens_over_time => opens.sort_by {|i| i.keys[0]},
:clicks_over_time => clicks.sort_by {|i| i.keys[0]},
:sent_messages => self.sent_messages.count,
:opened_messages => self.opens.count,
:total_messages => self.messages.count,
:days => days
}
end
|