From 4914bf176409fdb31e10c7966d4365abe4fa3ef2 Mon Sep 17 00:00:00 2001 From: Norore Date: Sat, 26 Mar 2022 17:48:03 +0100 Subject: [PATCH] Start to add statistics on website --- Procfile | 2 +- app/controllers/statistics_controller.rb | 32 ++++++++++++++ app/helpers/statistics_helper.rb | 2 + app/javascript/js/stats/grades.js | 42 +++++++++++++++++++ app/javascript/packs/application.js | 5 +++ app/views/statistics/grade.html.erb | 7 ++++ app/views/statistics/index.html.erb | 7 ++++ config/routes.rb | 3 ++ package.json | 1 + .../controllers/statistics_controller_test.rb | 7 ++++ 10 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 app/controllers/statistics_controller.rb create mode 100644 app/helpers/statistics_helper.rb create mode 100644 app/javascript/js/stats/grades.js create mode 100644 app/views/statistics/grade.html.erb create mode 100644 app/views/statistics/index.html.erb create mode 100644 test/controllers/statistics_controller_test.rb diff --git a/Procfile b/Procfile index cd299bb..e0926a4 100644 --- a/Procfile +++ b/Procfile @@ -1,4 +1,4 @@ web: bundle exec guard -i -webpack: bundle exec webpack-dev-server +webpack: bundle exec bin/webpack-dev-server sidekiq: bundle exec sidekiq -q default sidekiq_1_0: BUNDLE_GEMFILE=Gemfile-2.3 bin/sidekiq 1.0 -q tls_1_0 diff --git a/app/controllers/statistics_controller.rb b/app/controllers/statistics_controller.rb new file mode 100644 index 0000000..814d707 --- /dev/null +++ b/app/controllers/statistics_controller.rb @@ -0,0 +1,32 @@ +class StatisticsController < ApplicationController + def index; end + + def grade + @services = Analysis.where(service: params.fetch(:service)) + + respond_to do |format| + format.html + format.json do + grades = Hash.new 0 + + @services.each do |service| + if (result = service.result) + result.each do |r| + next unless (grade = r['grade']) + grades[grade] += 1 + end + end + end + + json = { + grades: { + labels: grades.keys, + dataset: grades + } + } + + render json: json, status: :ok + end + end + end +end diff --git a/app/helpers/statistics_helper.rb b/app/helpers/statistics_helper.rb new file mode 100644 index 0000000..2d25d41 --- /dev/null +++ b/app/helpers/statistics_helper.rb @@ -0,0 +1,2 @@ +module StatisticsHelper +end diff --git a/app/javascript/js/stats/grades.js b/app/javascript/js/stats/grades.js new file mode 100644 index 0000000..a96f8e1 --- /dev/null +++ b/app/javascript/js/stats/grades.js @@ -0,0 +1,42 @@ +document.addEventListener("DOMContentLoaded", () => { + let gradesChart = document.getElementById('gradesChart').getContext('2d') + + let background = [ + 'rgba(255, 99, 132, 1)', + 'rgba(54, 162, 235, 1)', + 'rgba(255, 206, 86, 1)', + 'rgba(75, 192, 192, 1)', + 'rgba(153, 102, 255, 1)', + 'rgba(255, 159, 64, 1)' + ] + + let createGradesChart = new Chart(gradesChart, { + type: 'bar' + }) + + let generateGraphs = function () { + fetch(window.location.href, { + method: "GET", + headers: { + "Accept": "application/json", + "Content-Type": "application/json", + }, + }).then((response) => { + response.json().then((data) => { + if (response.status === 200) { + let grades = data["grades"]["dataset"] + + createGradesChart.data.labels = data["grades"]["labels"] + createGradesChart.data.datasets = [{ + label: 'Count', + data: grades, + backgroundColor: background + }] + createGradesChart.update() + } + }) + }); + } + + generateGraphs() +}); diff --git a/app/javascript/packs/application.js b/app/javascript/packs/application.js index ab97118..e83440b 100644 --- a/app/javascript/packs/application.js +++ b/app/javascript/packs/application.js @@ -1 +1,6 @@ import 'css/application' + +import Chart from 'chart.js/auto' +global.Chart = Chart + +import 'js/stats/grades' \ No newline at end of file diff --git a/app/views/statistics/grade.html.erb b/app/views/statistics/grade.html.erb new file mode 100644 index 0000000..3964c5b --- /dev/null +++ b/app/views/statistics/grade.html.erb @@ -0,0 +1,7 @@ +
+ Nombre de recherche <%= params[:service] %> : <%= @services.size %> +
+ +
+ +
\ No newline at end of file diff --git a/app/views/statistics/index.html.erb b/app/views/statistics/index.html.erb new file mode 100644 index 0000000..0e8c95e --- /dev/null +++ b/app/views/statistics/index.html.erb @@ -0,0 +1,7 @@ +

Statistiques par service

+ + \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 98b4dce..60651b1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,6 +16,9 @@ Rails.application.routes.draw do root 'site#index' post '/' => 'site#check' + get 'statistics/', to: 'statistics#index', as: 'statisticts' + get 'statistics/grade/:service', to: 'statistics#grade', as: 'stats_grade' + get 'sites' => 'site#sites' if Rails.env.development? diff --git a/package.json b/package.json index 76422be..f6d30a0 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "dependencies": { "@rails/webpacker": "5.4.3", "bootstrap": "^5.1.3", + "chart.js": "^3.7.1", "font-awesome": "^4.7.0", "webpack": "^4.46.0", "webpack-cli": "^3.3.12" diff --git a/test/controllers/statistics_controller_test.rb b/test/controllers/statistics_controller_test.rb new file mode 100644 index 0000000..d70a747 --- /dev/null +++ b/test/controllers/statistics_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class StatisticsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end