From 896664af07387abda90c129cf9a7be288e243237 Mon Sep 17 00:00:00 2001 From: Norore Date: Mon, 18 Apr 2022 19:03:12 +0200 Subject: [PATCH] SVG and DIV solutions to display grades infos --- app/controllers/statistics_controller.rb | 2 +- app/javascript/js/stats/index.js | 1 + app/views/statistics/index.html.erb | 78 +++++++++++++++++++++--- bin/stats | 5 +- db/schema.rb | 4 +- 5 files changed, 77 insertions(+), 13 deletions(-) diff --git a/app/controllers/statistics_controller.rb b/app/controllers/statistics_controller.rb index c58797c..e609185 100644 --- a/app/controllers/statistics_controller.rb +++ b/app/controllers/statistics_controller.rb @@ -5,7 +5,7 @@ class StatisticsController < ApplicationController service = params.fetch :id respond_to do |format| format.json do - json = Stat.where(name: "grades_for_#{service}").order(date: :desc).first.dataset + json = Stat.where(name: "grades_for_#{service}").order(date: :desc).first.data render json: json, status: :ok end end diff --git a/app/javascript/js/stats/index.js b/app/javascript/js/stats/index.js index 9a2c95b..0a4541a 100644 --- a/app/javascript/js/stats/index.js +++ b/app/javascript/js/stats/index.js @@ -54,6 +54,7 @@ document.addEventListener("DOMContentLoaded", () => { fetch(`/statistics/${service}.json`).then((response) => { if (response.status === 200) { response.json().then((data) => { + console.info(data) const labels = ["A+", "A", "B+", "B", "C+", "C", "D", "E", "F", "G"] const dataset = JSON.parse(JSON.stringify(data, labels, 0)) chart.data.labels = labels diff --git a/app/views/statistics/index.html.erb b/app/views/statistics/index.html.erb index 6791ee8..b6bbe06 100644 --- a/app/views/statistics/index.html.erb +++ b/app/views/statistics/index.html.erb @@ -1,15 +1,79 @@ -
+<% colors = { + "A+" => '#5cb85c', + "A" => '#5cb85c', + "B+" => '#8db457', + "B" => '#8db457', + "C+" => '#beb052', + "C" => '#beb052', + "D" => '#6c757d', + "E" => '#f0ad4e', + "F" => '#e4804e', + "G" => '#d9534f' } %> - <% %i[https smtp tls xmpp].each do |s| %> -
-

Grades for service <%= s.to_s.upcase %>

+<% %i[https smtp tls xmpp].each do |service| %> +
+

Grades for service <%= service.to_s.upcase %>

- " aria-label="Bar chart for number of grades for service <%= s.to_s.upcase %>" role="img"> + + <% grades = Stat.where(name: "grades_for_#{service}").order(date: :desc).first + total = grades.data.collect { _2 }.sum %> + + Over <%= total %> URL tested with a grade.
+ + + SVG solution +
+ "> + ">Last grades for service <%= service %> + "> + This graphic represents the percentage of different grades obtained for the last analysis of + service <%= service %> requested. The obtained grades are: + <% grades.data.sort_by(&:first).each do |grade, number| %> + <% unless %w(T V).include?(grade) + percent = (number.to_f / total.to_f) * 100.0 %> + <%= "#{grade}: #{percent.round(2)} (#{number} requests)" %>; + <% end + end %> + Exotic grades like T or V are excluded from this graphic. + + <% y = 0 + x = 0 + grades.data.sort_by(&:first).each do |grade, number| + %> + <% unless %w(T V).include?(grade) %> + <% percent = (number.to_f / total.to_f) * 100.0 + color = colors[grade] %> + " + height="40" style="<%= "fill:#{color};" %>" + data-service="<%= service %>" + data-grade="<%= grade %>" + data-percent="<%= "#{percent}%" %>" + data-number="<%= number %>" + /> + <%= "#{grade}: #{percent.round}% (#{number})" %> + <% x += percent.round %> + <% end + end %> + +
+ + + DIV solution +
+ <% grades.data.sort_by(&:first).each do |grade, number| %> + <% unless %w(T V).include?(grade) + percent = (number.to_f / total.to_f) * 100.0 + color = colors[grade] %> +
+ <%= "#{grade}: #{percent.round}% (#{number})" %> +
+ <% end + end %>
- <% end %> -
\ No newline at end of file +
+<% end %> \ No newline at end of file diff --git a/bin/stats b/bin/stats index ab24fc6..e2cef65 100755 --- a/bin/stats +++ b/bin/stats @@ -18,7 +18,7 @@ class Analysis return :ssl if %i[SSLv2 SSLv3].any? { protocols.include? _1 } return :tls unless protocols.include? :TLSv1_2 return :tls1_2 unless protocols == %i[TLSv1_2] - return :tls1_2_only + :tls1_2_only end def ciphers @@ -49,14 +49,13 @@ class Analysis return :no_pfs unless ciphers.include? true return :pfs unless ciphers == [true] - return :pfs_only + :pfs_only end end services = Analysis.group(:service).count Stat.create! :request_per_service, services -# grade per service for https, smtp, tls and xmpp %i[https smtp tls xmpp].each do |service| services = Analysis.where service: service, pending: false diff --git a/db/schema.rb b/db/schema.rb index 0a3c6fc..1e64ebd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -15,7 +15,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_03_26_181216) do enable_extension "pgcrypto" enable_extension "plpgsql" - create_table "analyses", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t| + create_table "analyses", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.string "service", null: false t.string "host", null: false t.boolean "pending", default: true, null: false @@ -30,7 +30,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_03_26_181216) do t.string "name" t.date "date" t.jsonb "data" - t.index ["name", "date"], name: "index_stats_on_name_and_date", unique: true + t.index ["name"], name: "index_stats_on_name" end end