From f9abde82e5578f8e386a5a43bc33ea8bc13a3d8e Mon Sep 17 00:00:00 2001 From: keshav sethi Date: Wed, 12 Aug 2020 02:45:47 +0530 Subject: [PATCH 1/6] insights for spam2 --- app/controllers/spam2_controller.rb | 14 ++ app/models/node.rb | 15 ++ app/models/tag.rb | 7 + app/views/spam2/_insights.html.erb | 214 ++++++++++++++++++++++++++++ app/views/spam2/_spam.html.erb | 15 +- config/routes.rb | 1 + 6 files changed, 260 insertions(+), 6 deletions(-) create mode 100644 app/views/spam2/_insights.html.erb diff --git a/app/controllers/spam2_controller.rb b/app/controllers/spam2_controller.rb index 94d7f29509..494820f5cf 100644 --- a/app/controllers/spam2_controller.rb +++ b/app/controllers/spam2_controller.rb @@ -104,6 +104,20 @@ def _spam_revisions end end + def _spam_insights + if logged_in_as(%w(admin moderator)) + @graph_spammed = Node.spam_graph_making(0) + @graph_unmoderated = Node.spam_graph_making(4) + @graph_flagged = Node.where('flag > ?', 0).spam_graph_making(1) + @moderator_tag = Tag.tag_frequency(30) + @popular_tags = Tag.tag_frequency(10) + render template: 'spam2/_spam' + else + flash[:error] = 'Only moderators and admins can moderate this.' + redirect_to '/dashboard' + end + end + def _spam_comments if logged_in_as(%w(moderator admin)) @comments = Comment.paginate(page: params[:page], per_page: params[:pagination]) diff --git a/app/models/node.rb b/app/models/node.rb index 3a453a93c8..eff85a1420 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -1128,6 +1128,21 @@ def comments_viewable_by(user) comments.where(status: 1) end end + + def self.spam_graph_making(status) + start = Time.now - 1.year + fin = Time.now + time_hash = {} + week = start.to_date.step(fin.to_date, 7).count + while week >= 1 + months = (fin - (week * 7 - 1).days) + range = (fin.to_i - week.weeks.to_i)..(fin.to_i - (week - 1).weeks.to_i) + nodes = Node.where(created: range).where(status: status).select(:created).size + time_hash[months.to_f * 1000] = nodes + week -= 1 + end + time_hash + end def notify_callout_users # notify mentioned users diff --git a/app/models/tag.rb b/app/models/tag.rb index c533bbfd09..7aed058d1a 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -443,4 +443,11 @@ def span(start, fin) def range(fin, week) (fin.to_i - week.weeks.to_i).to_s..(fin.to_i - (week - 1).weeks.to_i).to_s end + + def self.tag_frequency(limit) + uids = User.where('rusers.role = ?', 'moderator').or(User.where('rusers.role = ?', 'admin')).collect(&:uid) + tids = TagSelection.where(following: true, user_id: uids).collect(&:tid) + hash = tids.uniq.map { |id| p (Tag.find id).name, tids.count(id) }.to_h + hash.sort_by { |_, v| v }.reverse.first(limit).to_h + end end diff --git a/app/views/spam2/_insights.html.erb b/app/views/spam2/_insights.html.erb new file mode 100644 index 0000000000..3489e62d97 --- /dev/null +++ b/app/views/spam2/_insights.html.erb @@ -0,0 +1,214 @@ +
+
+
+
Unmoderated
+
    +
  • <%= Node.where(status: 4).count%> Nodes
  • +
  • <%= Comment.where(status: 4).count %> Comments
  • +
+
+
+
+
+
Spammed
+
    +
  • <%= Node.where(status: 0).count%> Nodes
  • +
  • <%= Comment.where(status: 0).count %> Comments
  • +
+
+
+
+
+
Flagged
+
    +
  • <%= Node.where('flag > ?', 0).count%> Nodes
  • +
  • <%= Comment.where('flag > ?', 0).count %> Comments
  • +
+
+
+
+
+
Published
+
    +
  • <%= Node.where(status: 1).count%> Nodes
  • +
  • <%= Comment.where(status: 1).count %> Comments
  • +
+
+
+
+ +
+ +
+
+
+
Tags followed by moderators and admins
+ <% if @popular_tags.present? %> +
+ <% end %> +
+
+ +
+ + + + + diff --git a/app/views/spam2/_spam.html.erb b/app/views/spam2/_spam.html.erb index 31eb7ee837..beb038f9b6 100644 --- a/app/views/spam2/_spam.html.erb +++ b/app/views/spam2/_spam.html.erb @@ -17,6 +17,8 @@ Spam2 / Wiki <% elsif params[:action] == "_spam_queue"%> Spam2 / Queue + <% elsif params[:action] == "_spam_insights"%> + Spam2 / Insights <% elsif params[:action] == "_spam_revisions"%> Spam2 / Revisions <% elsif params[:action] == "_spam_comments"%> @@ -35,10 +37,10 @@