From a9a078a7f4a7a0a9c4e57c02f66722f4d6c1cec6 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 21 Oct 2023 16:22:01 -0400 Subject: Added quotes stats page --- app/controllers/quotes_controller.rb | 76 ++++++++++++++++++++++++++++++++++++ app/views/layouts/quotes.html.haml | 1 + app/views/quotes/stats.html.haml | 41 +++++++++++++++++++ config/routes.rb | 1 + 4 files changed, 119 insertions(+) create mode 100644 app/views/quotes/stats.html.haml diff --git a/app/controllers/quotes_controller.rb b/app/controllers/quotes_controller.rb index be931a0..92a424e 100644 --- a/app/controllers/quotes_controller.rb +++ b/app/controllers/quotes_controller.rb @@ -47,6 +47,82 @@ class QuotesController < ApplicationController end end + def stats + @months = [] + 0.upto(11) do |i| + month = i.months.ago.month + year = i.months.ago.year + beginning = i.months.ago.beginning_of_month.beginning_of_day + endtime = i.months.ago.end_of_month.end_of_day + quotes = Quote.where("created_at >= :start_date AND created_at <= :end_date", { :start_date => beginning, :end_date => endtime }).all + @months[11-i] = { :name => Date::MONTHNAMES[month], :number => quotes.count } + end + + #@months2 = [] + #i = 0 + #while true + # month = i.months.ago.month + # year = i.months.ago.year + # beginning = i.months.ago.beginning_of_month.beginning_of_day + # endtime = i.months.ago.end_of_month.end_of_day + # quotes = Quote.where("created_at <= :end_date", { :end_date => endtime }).all + # @months2[i] = { :name => i.months.ago.strftime("%Y-%m"), :number => quotes.count } + # + # break if month == 4 and year == 2008 + # + # i=i+1 + #end + # + #@months2.reverse! + # + #@upvotes = Vote.where(upvote: 1, votable_type: :quote).count + #@downvotes = Vote.where(upvote: 0, votable_type: :quote).count + + hardcoded_aliases = { + "hatkirby": "Hatkirby", + "Gryphic": "Drifty", + "DriftyBeyond": "Drifty", + "Starla": "Hatkirby", + "BleuM937": "Bluemonkey", + "BlueM937": "Bluemonkey", + "Drifty1": "Drifty", + "Starla Insigna": "Hatkirby", + "Starla Alice Insigna": "Hatkirby", + "tamasys": "Tamasys", + "Student 2": "Student", + "Старла Эппрет": "Hatkirby", + "Girl Without Slipper": "Hatkirby", + "Actias": "Drifty", + "Salaboy123": "Tamasys", + "TaMACsys": "Tamasys", + "RealityCheck": "Drifty", + "Student 3": "Student" + } + + person_count = {} + Quote.all.each do |quote| + person_in_quote = {} + + quote.content.split("\n").each do |line| + check = line.chomp + hi = /([\(\[][^\]\)]*[\]\)] )?([^:]*): /.match(check) + if hi and hi[2] then + person = hi[2] + if hardcoded_aliases.has_key? person.intern + person = hardcoded_aliases[person.intern] + end + unless person_in_quote.has_key? person + person_count[person] ||= 0 + person_count[person] += 1 + person_in_quote[person] = 1 + end + end + end + end + + @by_speaker = person_count.to_a.sort_by {|hi| hi[1]}.reverse.take(20) + end + def show @quote = Quote.published.find(params[:id]) diff --git a/app/views/layouts/quotes.html.haml b/app/views/layouts/quotes.html.haml index 68607f6..f42eee0 100644 --- a/app/views/layouts/quotes.html.haml +++ b/app/views/layouts/quotes.html.haml @@ -23,6 +23,7 @@ - if user_signed_in? %li= link_to_unless_current "Submit", new_quote_url %li= link_to_unless_current "Tags", tags_quotes_url + %li= link_to_unless_current "Stats", stats_quotes_url %li= link_to_unless_current "Feed", latest_quotes_url(:atom) .cleardiv #page-body diff --git a/app/views/quotes/stats.html.haml b/app/views/quotes/stats.html.haml new file mode 100644 index 0000000..d7a8140 --- /dev/null +++ b/app/views/quotes/stats.html.haml @@ -0,0 +1,41 @@ +%script{ :type => "text/javascript", :src => "https://www.gstatic.com/charts/loader.js" } +%script{ :type => "text/javascript" } + google.charts.load('visualization', '1.0', {'packages':['corechart']}); + google.charts.setOnLoadCallback(drawChart); + function drawChart() + { + var data = new google.visualization.DataTable(); + data.addColumn('string', 'Month'); + data.addColumn('number', 'Quotes Submitted'); + data.addRows([ + - @months.each do |month| + ['#{month[:name]}', #{month[:number]}], + ]); + var options = {'title':'Quotes Submitted in the Last 12 Months', 'width':1000, 'height':500, 'legend':'none', 'hAxis.maxAlternation':1}; + var chart = new google.visualization.ColumnChart(document.getElementById('chart_div')); + chart.draw(data, options); + //var data2 = new google.visualization.DataTable(); + //data2.addColumn('string', 'Month'); + //data2.addColumn('number', 'Quotes'); + //data2.addRows([ + //- @months2.each do |month| + // ['\#{month[:name]}', \#{month[:number]}], + //]); + //var options2 = {'title':'Number of Quotes', 'width':1000, 'height':500, 'legend':'none', 'hAxis.maxAlternation':1}; + //var chart2 = new google.visualization.AreaChart(document.getElementById('chart2_div')); + //chart2.draw(data2, options2); + var data3 = new google.visualization.DataTable(); + data3.addColumn('string', 'Speaker'); + data3.addColumn('number', 'Quotes'); + data3.addRows([ + - @by_speaker.each do |speaker| + ['#{speaker[0]}', #{speaker[1]}], + ]); + var options3 = {'title':'Number of Quotes per Speaker', 'width':1000, 'height':500}; + var chart3 = new google.visualization.ColumnChart(document.getElementById('chart3_div')); + chart3.draw(data3, options3); + } +%p.normal Because charts are fun, we've compiled a few charts here containing statistics about the Four Island Quotes DB. +#chart2_div +#chart_div +#chart3_div diff --git a/config/routes.rb b/config/routes.rb index 7e22946..59589aa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -72,6 +72,7 @@ Rails.application.routes.draw do get 'random' get 'tags' get 'tags/:id', :action => "tag", :as => "tag" + get 'stats' end member do -- cgit 1.4.1