diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 7419b6c434..ee455ae432 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -78,31 +78,59 @@ def notes export_as_json(@start, @end, 'note') end - def maps + def wikis time - export_as_json(@start, @end, 'map') + export_as_json(@start, @end, 'page') end - def wikis + def users time - export_as_json(@start, @end, 'page') + data = User.where(created_at: @start..@end).where(status: 1) + respond_to do |format| + format.csv { send_data data.to_csv } + format.json { send_data data.to_json, :type => 'application/json; header=present', :disposition => "attachment; filename=user.json" } + end end - def comments + def questions time - data = Comment.select(%i(status timestamp)).where(status: 1, timestamp: @start.to_i...@end.to_i).all.to_json - send_data data, :type => 'application/json; header=present', :disposition => "attachment; filename=comment.json" + data = Node.published.questions.where(created: @start.to_i..@end.to_i).all + respond_to do |format| + format.csv { send_data data.to_csv } + format.json { send_data data.to_json, :type => 'application/json; header=present', :disposition => "attachment; filename=questions.json" } + end end - private + def answers + time + data = Answer.where(created_at: @start..@end).all + respond_to do |format| + format.csv { send_data data.to_csv } + format.json { send_data data.to_json, :type => 'application/json; header=present', :disposition => "attachment; filename=answers.json" } + end + end + + def comments + time + data = Comment.select(%i(status timestamp)).where(status: 1, timestamp: @start.to_i...@end.to_i).all + respond_to do |format| + format.csv { send_data data.to_csv } + format.json { send_data data.to_json, :type => 'application/json; header=present', :disposition => "attachment; filename=comment.json" } + end + end def export_as_json(starting, ending, type) - data = Node.select(%i(status created type)) - .where(type: type, status: 1, created: starting.to_i..ending.to_i) - .all.to_json - send_data data, :type => 'application/json; header=present', :disposition => "attachment; filename=#{type}.json" + data = Node.published.select(%i(created type)) + .where(type: type, created: starting.to_i..ending.to_i) + .all + respond_to do |format| + format.csv { send_data data.to_csv } + format.json { send_data data.to_json, :type => 'application/json; header=present', :disposition => "attachment; filename=#{type}.json" } + end end + private + def time @start = params[:start] ? Time.parse(params[:start].to_s) : Time.now - 1.month @end = params[:end] ? Time.parse(params[:end].to_s) : Time.now diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 10a4cba84d..cf513eb57a 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -1,3 +1,13 @@ class ApplicationRecord < ActiveRecord::Base + require 'csv' self.abstract_class = true + + def self.to_csv(options = {}) + CSV.generate(options) do |csv| + csv << column_names + all.each do |object| + csv << object.attributes.values_at(*column_names) + end + end + end end diff --git a/app/models/node.rb b/app/models/node.rb index 17fe818b7a..9bb99b6239 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -165,6 +165,15 @@ def setup save end + def self.to_csv(options = {}) + CSV.generate(options) do |csv| + csv << column_names + all.each do |object| + csv << object.attributes.values_at(*column_names) + end + end + end + public is_impressionable counter_cache: true, column_name: :views diff --git a/app/models/user.rb b/app/models/user.rb index 8337a9c375..c5f2a4b10b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -434,4 +434,13 @@ def self.count_all_time_contributor revisions = Revision.where(status: 1).pluck(:uid) contributors = (notes + answers + questions + comments + revisions).compact.uniq.length end + + def self.to_csv(options = {}) + CSV.generate(options) do |csv| + csv << column_names + all.each do |object| + csv << object.attributes.values_at(*column_names) + end + end + end end diff --git a/app/views/stats/raw_stats.html.erb b/app/views/stats/raw_stats.html.erb index 13cdf92d92..28855abc15 100644 --- a/app/views/stats/raw_stats.html.erb +++ b/app/views/stats/raw_stats.html.erb @@ -36,16 +36,22 @@
@@ -57,8 +63,23 @@ Toggle Dropdown diff --git a/config/routes.rb b/config/routes.rb index 73b0c5948a..74f62cddf6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -251,6 +251,12 @@ get 'stats/comments/:start/:end' => 'stats#comments' get 'stats/maps' => 'stats#maps' get 'stats/maps/:start/:end' => 'stats#maps' + get 'stats/users' => 'stats#users' + get 'stats/users/:start/:end' => 'stats#users' + get 'stats/questions' => 'stats#questions' + get 'stats/questions/:start/:end' => 'stats#questions' + get 'stats/answers' => 'stats#answers' + get 'stats/answers/:start/:end' => 'stats#answers' get 'stats/raw' => 'stats#raw_stats' get 'feed' => 'notes#rss' get 'rss.xml' => 'legacy#rss'