Skip to content
This repository has been archived by the owner on Dec 24, 2020. It is now read-only.

Add a graphs controller. #12

Merged
merged 11 commits into from
Apr 28, 2018
Merged
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ group :development do
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'annotate', '~> 2.7'
gem 'rails-erd', '~> 1.5', require: false
gem 'better_errors'
gem 'binding_of_caller'
end

group :test do
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ GEM
arel (9.0.0)
autoprefixer-rails (8.3.0)
execjs
better_errors (2.4.0)
coderay (>= 1.0.0)
erubi (>= 1.0.0)
rack (>= 0.9.0)
bindex (0.5.0)
binding_of_caller (0.8.0)
debug_inspector (>= 0.0.1)
bootsnap (1.3.0)
msgpack (~> 1.0)
bootstrap (4.1.0)
Expand All @@ -75,9 +81,11 @@ GEM
chromedriver-helper (1.2.0)
archive-zip (~> 0.10)
nokogiri (~> 1.8)
coderay (1.1.2)
concurrent-ruby (1.0.5)
crass (1.0.4)
database_cleaner (1.7.0)
debug_inspector (0.0.3)
erubi (1.7.1)
execjs (2.7.0)
ffi (1.9.23)
Expand Down Expand Up @@ -224,6 +232,8 @@ PLATFORMS

DEPENDENCIES
annotate (~> 2.7)
better_errors
binding_of_caller
bootsnap (>= 1.1.0)
bootstrap (~> 4.1.0)
byebug
Expand Down
2 changes: 2 additions & 0 deletions app/assets/javascripts/graphs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/graphs.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Graphs controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
27 changes: 27 additions & 0 deletions app/controllers/graphs_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class GraphsController < ApplicationController
def index
@browser_stats = Hash.new

@browsers = Rails.configuration.browsers

@browsers.keys.each do |browser|
stats = {
unknown: Feature.public_send("#{browser}_nil").count,
yes: Feature.public_send("#{browser}_true").count,
no: Feature.public_send("#{browser}_false").count,
no_data: Feature.public_send("#{browser}_no_data").count
}

@browser_stats[browser] = stats

total = 0
@browser_stats[browser].each do |key, value|
total += value
end

@browser_stats[browser][:total] = total
end

@feature_count = Feature.all.count
end
end
2 changes: 2 additions & 0 deletions app/helpers/graphs_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module GraphsHelper
end
37 changes: 28 additions & 9 deletions app/models/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,39 @@ class Feature < ApplicationRecord
scope :no_experimental_info, -> { where(experimental: nil) }

# Feature category scopes
scope :api, -> { where("name ~* ?", '^api.*') }
scope :css, -> { where("name ~* ?", '^css.*') }
scope :html, -> { where("name ~* ?", '^html.*') }
scope :http, -> { where("name ~* ?", '^http.*') }
scope :javascript, -> { where("name ~* ?", '^javascript.*') }
scope :mathml, -> { where("name ~* ?", '^mathml.*') }
scope :svg, -> { where("name ~* ?", '^svg.*') }
scope :webdriver, -> { where("name ~* ?", '^webdriver.*') }
scope :webextensions, -> { where("name ~* ?", '^webextensions.*') }
# Creates scopes like Feature.api, Feature.css, Feature.html, etc.
Rails.configuration.feature_categories.keys.each do |category|
scope "#{category}", -> { where("name ~* ?", "^#{category}.*") }
end

pg_search_scope :search,
against: [:name],
using: {
tsearch: { prefix: true },
trigram: { threshold: 0.3 }
}

Rails.configuration.browsers.keys.each do |browser|
# @> is an SQL operator that determines whether the left JSON value
# contains the right value.
# Does the browser hash contain version added key with the value 'false'?
# This doesn't properly handle support values which are arrays.
scope "#{browser}_false", -> { where( "#{browser} @> ?", {'version_added': false}.to_json) }

scope "#{browser}_nil", -> { where( "#{browser} @> ?", {'version_added': nil}.to_json) }

scope "#{browser}_no_data", -> { where("#{browser}": nil) }

# This tries to find all cases where the version_added value is either
# version number or true, since the version_added can only be true, false,
# null, or a version number we can use a regex to elimate all but the
# version numbers.
scope "#{browser}_true", -> {
where("#{browser} ->> :key ~ :regex",
key: "version_added",
regex: '^(?!true|false|null)'
)
.or(where( "#{browser} @> ?", {'version_added': true}.to_json))
}
end
end
47 changes: 47 additions & 0 deletions app/views/graphs/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<div class="container">
<div class="row py-4">
<div class="col-12">
<h1>Graphs</h1>
</div>
</div>

<div class="row">
<div class="col-12">
<div class="card-columns">
<% @browsers.keys.each do |browser| %>
<div class="card">
<div class="card-body">
<%= pie_chart(
{
"Unknown" => @browser_stats[browser][:unknown],
"True" => @browser_stats[browser][:yes],
"False" => @browser_stats[browser][:no],
"No data" => @browser_stats[browser][:no_data]
},
title: @browsers[browser],
donut: true
) %>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">
Unknown: <%= @browser_stats[browser][:unknown] %>
</li>
<li class="list-group-item">
True: <%= @browser_stats[browser][:yes] %>
</li>
<li class="list-group-item">
False: <%= @browser_stats[browser][:no] %>
</li>
<li class="list-group-item">
No data: <%= @browser_stats[browser][:no_data] %>
</li>
<li class="list-group-item">
Total: <%= @browser_stats[browser][:total] %>
</li>
</ul>
</div>
<% end %>
</div>
</div>
</div>
</div>
3 changes: 3 additions & 0 deletions app/views/shared/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<li class="nav-item">
<%= active_link_to "Features", features_path, class: "nav-link" %>
</li>
<li class="nav-item">
<%= active_link_to "Graphs", graphs_path, class: "nav-link" %>
</li>
</ul>
</div>
</nav>
65 changes: 12 additions & 53 deletions app/views/welcome/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,19 @@
</div>

<div class="row">
<div class="col-12">
<div class="card-columns">
<div class="card">
<div class="card-header">
Statistics
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">Features: <%= @feature_count %></li>
<li class="list-group-item">Features with an MDN link: <%= @features_with_mdn_count %></li>
<li class="list-group-item">Features with a description: <%= @features_with_description_count %></li>
<li class="list-group-item">Features on a standards track: <%= @standard_features %></li>
<li class="list-group-item">Features marked experimental: <%= @experimental_features %></li>
<li class="list-group-item">Features marked deprecated: <%= @deprecated_features %></li>
</ul>
</div>

<div class="card">
<div class="card-body">
<%= pie_chart(
{
"True" => @experimental_features,
"False" => @feature_count - @experimental_features
},
title: "Experimental features",
donut: true
) %>
</div>
</div>

<div class="card">
<div class="card-body">
<%= pie_chart(
{
"True" => @features_with_mdn_count,
"False" => @feature_count - @features_with_mdn_count
},
title: "Features with MDN Links",
donut: true
) %>
</div>
</div>

<div class="card">
<div class="card-body">
<%= pie_chart(
{
"True" => @features_with_description_count,
"False" => @feature_count - @features_with_description_count
},
title: "Features with a description",
donut: true
) %>
</div>
<div class="col-12 col-md-4">
<div class="card">
<div class="card-header">
Statistics
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">Features: <%= @feature_count %></li>
<li class="list-group-item">Features with an MDN link: <%= @features_with_mdn_count %></li>
<li class="list-group-item">Features with a description: <%= @features_with_description_count %></li>
<li class="list-group-item">Features on a standards track: <%= @standard_features %></li>
<li class="list-group-item">Features marked experimental: <%= @experimental_features %></li>
<li class="list-group-item">Features marked deprecated: <%= @deprecated_features %></li>
</ul>
</div>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

match 'features', to: 'features#index', via: :get
match 'browsers', to: 'browsers#index', via: :get
match 'graphs', to: 'graphs#index', via: :get

root 'welcome#index'
end
20 changes: 20 additions & 0 deletions db/migrate/20180426202826_change_browser_columns_default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class ChangeBrowserColumnsDefault < ActiveRecord::Migration[5.2]
def change
change_column_default :features, :chrome, from: '{}', to: nil
change_column_default :features, :chrome_android, from: '{}', to: nil
change_column_default :features, :edge, from: '{}', to: nil
change_column_default :features, :edge_mobile, from: '{}', to: nil
change_column_default :features, :firefox, from: '{}', to: nil
change_column_default :features, :firefox_android, from: '{}', to: nil
change_column_default :features, :ie, from: '{}', to: nil
change_column_default :features, :nodejs, from: '{}', to: nil
change_column_default :features, :opera, from: '{}', to: nil
change_column_default :features, :qq_android, from: '{}', to: nil
change_column_default :features, :safari, from: '{}', to: nil
change_column_default :features, :safari_ios, from: '{}', to: nil
change_column_default :features, :samsunginternet_android, from: '{}', to: nil
change_column_default :features, :uc_android, from: '{}', to: nil
change_column_default :features, :uc_chinese_android, from: '{}', to: nil
change_column_default :features, :webview_android, from: '{}', to: nil
end
end
8 changes: 4 additions & 4 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

puts "Seeding..."

if Rails.env.test?
if Rails.env.test?
puts "Rails environment is test"
end

Expand All @@ -24,7 +24,7 @@
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean

if Rails.env.test?
if Rails.env.test? || ENV['USE_TEST_DATA']
@data = File.read('public/data-test.json')
else
@data = File.read('public/data.json')
Expand All @@ -45,7 +45,7 @@
# webextensions: []
# }

if Rails.env.test?
if Rails.env.test? || ENV['USE_TEST_DATA']
@top_level_schema = {
css: []
}
Expand All @@ -63,7 +63,7 @@
}
end

if Rails.env.test?
if Rails.env.test? || ENV['USE_TEST_DATA']
@browser_names = {
firefox: "Firefox"
}
Expand Down
Loading