From cfba7c109a58a46e76ef6cd23da5409b67077d4d Mon Sep 17 00:00:00 2001
From: ArtOfCode-
Date: Mon, 22 Jan 2018 22:47:55 +0000
Subject: [PATCH] Pretty graphs
---
app/controllers/graphs_controller.rb | 51 ++++++++++++++++++++++
app/views/flag_conditions/sandbox.html.erb | 13 ++++++
config/routes.rb | 2 +
lib/queries/fp_counter.sql | 6 +++
lib/queries/post_counter.sql | 5 +++
lib/queries/tp_counter.sql | 6 +++
6 files changed, 83 insertions(+)
create mode 100644 lib/queries/fp_counter.sql
create mode 100644 lib/queries/post_counter.sql
create mode 100644 lib/queries/tp_counter.sql
diff --git a/app/controllers/graphs_controller.rb b/app/controllers/graphs_controller.rb
index 6afcb5add..ce5d3e533 100644
--- a/app/controllers/graphs_controller.rb
+++ b/app/controllers/graphs_controller.rb
@@ -138,6 +138,57 @@ def reports
]
end
+ def af_posts
+ data = Rails.cache.fetch :af_detail_data, expires_in: 1.hour do
+ total_query = ActiveRecord::Base.connection.execute File.read(Rails.root.join('lib/queries/post_counter.sql'))
+ tp_query = ActiveRecord::Base.connection.execute File.read(Rails.root.join('lib/queries/tp_counter.sql'))
+ fp_query = ActiveRecord::Base.connection.execute File.read(Rails.root.join('lib/queries/fp_counter.sql'))
+
+ total_counts = total_query.map(&:last)
+ totals = total_query.map.with_index { |e, i| [e[0], total_counts[i..-1].sum] }.select { |x| x[0] <= 400 }
+
+ tp_counts = tp_query.map(&:last)
+ tps = tp_query.map.with_index { |e, i| [e[0], tp_counts[i..-1].sum] }.select { |x| x[0] <= 400 }
+
+ fp_counts = fp_query.map(&:last)
+ fps = fp_query.map.with_index { |e, i| [e[0], fp_counts[i..-1].sum] }.select { |x| x[0] <= 400 }
+
+ { totals: totals, tps: tps, fps: fps }
+ end
+
+ render json: [
+ { name: 'Total posts', data: data[:totals] },
+ { name: 'TPs', data: data[:tps] }
+ ]
+ end
+
+ def af_accuracy
+ data = Rails.cache.fetch :af_detail_data, expires_in: 1.hour do
+ total_query = ActiveRecord::Base.connection.execute File.read(Rails.root.join('lib/queries/post_counter.sql'))
+ tp_query = ActiveRecord::Base.connection.execute File.read(Rails.root.join('lib/queries/tp_counter.sql'))
+ fp_query = ActiveRecord::Base.connection.execute File.read(Rails.root.join('lib/queries/fp_counter.sql'))
+
+ total_counts = total_query.map(&:last)
+ totals = total_query.map.with_index { |e, i| [e[0], total_counts[i..-1].sum] }.select { |x| x[0] <= 400 }
+
+ tp_counts = tp_query.map(&:last)
+ tps = tp_query.map.with_index { |e, i| [e[0], tp_counts[i..-1].sum] }.select { |x| x[0] <= 400 }
+
+ fp_counts = fp_query.map(&:last)
+ fps = fp_query.map.with_index { |e, i| [e[0], fp_counts[i..-1].sum] }.select { |x| x[0] <= 400 }
+
+ { totals: totals, tps: tps, fps: fps }
+ end
+
+ maxlen = [data[:totals].size, data[:tps].size, data[:fps].size].max - 1
+ range = 0..maxlen
+ # BEHOLD. MAGIC.
+ # Okay fine. Calculates accuracy for each weight 10-group, providing a value (0) for fps if it's not there, and clamping to 0.9..1.0.
+ acc_data = range.map { |i| [data[:tps][i][0], ([data[:tps][i][1].to_f / (data[:tps][i][1] + (data[:fps][i] || [0, 0])[1]), 0.90].max * 100).round(2)] }
+
+ render json: [{ name: 'Accuracy', data: acc_data }]
+ end
+
private
def cached_query(cache_key, **opts)
diff --git a/app/views/flag_conditions/sandbox.html.erb b/app/views/flag_conditions/sandbox.html.erb
index 680e209b1..ca314dc94 100644
--- a/app/views/flag_conditions/sandbox.html.erb
+++ b/app/views/flag_conditions/sandbox.html.erb
@@ -3,3 +3,16 @@
database - please don't abuse it.
<%= render 'form', :exclude_save => true %>
+
+
+
+ <%= line_chart af_posts_path, library: { tooltip: { shared: true }, yAxis: { gridLineWidth: 1, minorGridLineWidth: 1, minRange: 100 },
+ xAxis: { gridLineWidth: 1, minorGridLineWidth: 1, minRange: 10 }, chart: { zoomType: 'xy' } },
+ height: '500px', colors: ['#555', 'green'] %>
+
+
+ <%= line_chart af_accuracy_path, library: { yAxis: { ceiling: 100, floor: 90, gridLineWidth: 1, minorGridLineWidth: 1, minRange: 1 },
+ xAxis: { gridLineWidth: 1, minorGridLineWidth: 1, minRange: 10 }, chart: { zoomType: 'xy' } },
+ height: '500px', colors: ['gold'] %>
+
+
diff --git a/config/routes.rb b/config/routes.rb
index 701922791..9b8152c16 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -71,6 +71,8 @@
get 'dttd', to: 'graphs#detailed_ttd'
get 'monthly_ttd', to: 'graphs#monthly_ttd', as: :monthly_ttd_graph
get 'reports', to: 'graphs#reports', as: :reports_graph
+ get 'af_posts', to: 'graphs#af_posts', as: :af_posts
+ get 'af_accuracy', to: 'graphs#af_accuracy', as: :af_accuracy
end
get 'status', to: 'status#index', as: :status
diff --git a/lib/queries/fp_counter.sql b/lib/queries/fp_counter.sql
new file mode 100644
index 000000000..bf7021aec
--- /dev/null
+++ b/lib/queries/fp_counter.sql
@@ -0,0 +1,6 @@
+SELECT TRUNCATE(weight, -1), COUNT(id) FROM
+(SELECT posts.id, posts.is_tp, SUM(reasons.weight) AS weight FROM posts INNER JOIN posts_reasons ON posts.id = posts_reasons.post_id
+INNER JOIN reasons ON posts_reasons.reason_id = reasons.id GROUP BY posts.id) AS posts_weights
+WHERE is_tp = 0
+GROUP BY TRUNCATE(weight, -1)
+ORDER BY TRUNCATE(weight, -1) ASC;
\ No newline at end of file
diff --git a/lib/queries/post_counter.sql b/lib/queries/post_counter.sql
new file mode 100644
index 000000000..005cd7caf
--- /dev/null
+++ b/lib/queries/post_counter.sql
@@ -0,0 +1,5 @@
+SELECT TRUNCATE(weight, -1), COUNT(id) FROM
+(SELECT posts.id, posts.is_tp, SUM(reasons.weight) AS weight FROM posts INNER JOIN posts_reasons ON posts.id = posts_reasons.post_id
+INNER JOIN reasons ON posts_reasons.reason_id = reasons.id GROUP BY posts.id) AS posts_weights
+GROUP BY TRUNCATE(weight, -1)
+ORDER BY TRUNCATE(weight, -1) ASC;
\ No newline at end of file
diff --git a/lib/queries/tp_counter.sql b/lib/queries/tp_counter.sql
new file mode 100644
index 000000000..608499c1e
--- /dev/null
+++ b/lib/queries/tp_counter.sql
@@ -0,0 +1,6 @@
+SELECT TRUNCATE(weight, -1), COUNT(id) FROM
+(SELECT posts.id, posts.is_tp, SUM(reasons.weight) AS weight FROM posts INNER JOIN posts_reasons ON posts.id = posts_reasons.post_id
+INNER JOIN reasons ON posts_reasons.reason_id = reasons.id GROUP BY posts.id) AS posts_weights
+WHERE is_tp = 1
+GROUP BY TRUNCATE(weight, -1)
+ORDER BY TRUNCATE(weight, -1) ASC;
\ No newline at end of file