lib/tasks/site_statistics.rake in drg_cms-0.4.39 vs lib/tasks/site_statistics.rake in drg_cms-0.4.53
- old
+ new
@@ -44,26 +44,43 @@
visits = DcVisit.only(:session_id, :time).where(:time.gt => @date_from, :time.lt => @date_to).map_reduce(map, reduce).out(inline: 1).to_a
visits.each do |stat|
p stat
end
=end
- totals = {}
- visits = DcVisit.only(:session_id, :time).where(:time.gt => @date_from, :time.lt => @date_to).order_by('time asc')
+ p 'Reading visits collection ...'
+ totals = {}
+ totals_ip = {}
+ visits = DcVisit.only(:session_id, :time,:ip).where(:time.gt => @date_from, :time.lt => @date_to).order_by('time asc')
visits.each do |visit|
- key = visit.time.strftime('%Y%m%d') + '_' + visit.session_id
+ key = visit.time.strftime('%Y%m%d') + '_' + visit.session_id.to_s
totals[key] ||= 0
totals[key] += 1
+
+ key = visit.time.strftime('%Y%m%d') + '_' + visit.ip.to_s
+ totals_ip[key] ||= 0
+ totals_ip[key] += 1
end
- output,old_date,unique,count = '','',0,0
- totals.to_a.sort! {|x,y| x <=> y}.each do |e|
- if e.first[0,8] != old_date
- output << "#{old_date}\t#{unique}\t#{count}\n" unless old_date == ''
- old_date,unique,count = e.first[0,8], 0, 0
- end
- unique += 1
- count += e.last
+ p 'Calculating totals ....'
+# Totals by unique
+ date_totals = {}
+ totals.each do |total|
+ date = total.first[0,8]
+ date_totals[date] ||= [0,0,0]
+ date_totals[date][0] += total.last
+ date_totals[date][1] += 1
end
- output << "#{old_date}\t#{unique}\t#{count}\n"
+# Totals by ip
+ totals_ip.each do |total|
+ date = total.first[0,8]
+ date_totals[date][2] += 1
+ end
+# Create output file
+ p 'Creating output ....'
+ output = "date\tunique\tunique_by_ip\tvisits\n"
+ old_date,unique,count = 0,0
+ date_totals.to_a.sort! {|x,y| x <=> y}.each do |e|
+ output << [e.first, e.last[1], e.last[2], e.last[0] ].join("\t") + "\n"
+ end
filename = Rails.root.join('tmp/','statistics.txt')
File.open(filename,'w') {|f| f.write(output) }
p "Statistics saved to #{filename}."
end
\ No newline at end of file