Skip to content

Commit

Permalink
Pretty graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtOfCode- committed Jan 22, 2018
1 parent cfb62cf commit cfba7c1
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
51 changes: 51 additions & 0 deletions app/controllers/graphs_controller.rb
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions app/views/flag_conditions/sandbox.html.erb
Expand Up @@ -3,3 +3,16 @@
database - <span class="text-danger">please don't abuse it</span>.</p>

<%= render 'form', :exclude_save => true %>

<div class="row clearfix">
<div class="col-md-6">
<%= 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'] %>
</div>
<div class="col-md-6">
<%= 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'] %>
</div>
</div>
2 changes: 2 additions & 0 deletions config/routes.rb
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions 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;
5 changes: 5 additions & 0 deletions 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;
6 changes: 6 additions & 0 deletions 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;

0 comments on commit cfba7c1

Please sign in to comment.