Browse Source

Start to add statistics on website

stats
Norore 3 months ago
committed by aeris
parent
commit
4914bf1764
  1. 2
      Procfile
  2. 32
      app/controllers/statistics_controller.rb
  3. 2
      app/helpers/statistics_helper.rb
  4. 42
      app/javascript/js/stats/grades.js
  5. 5
      app/javascript/packs/application.js
  6. 7
      app/views/statistics/grade.html.erb
  7. 7
      app/views/statistics/index.html.erb
  8. 3
      config/routes.rb
  9. 1
      package.json
  10. 7
      test/controllers/statistics_controller_test.rb

2
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

32
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

2
app/helpers/statistics_helper.rb

@ -0,0 +1,2 @@
module StatisticsHelper
end

42
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()
});

5
app/javascript/packs/application.js

@ -1 +1,6 @@
import 'css/application'
import Chart from 'chart.js/auto'
global.Chart = Chart
import 'js/stats/grades'

7
app/views/statistics/grade.html.erb

@ -0,0 +1,7 @@
<div>
Nombre de recherche <%= params[:service] %> : <%= @services.size %>
</div>
<div>
<canvas id="gradesChart"></canvas>
</div>

7
app/views/statistics/index.html.erb

@ -0,0 +1,7 @@
<h2>Statistiques par service</h2>
<ul class="list-unstyled">
<% %i[https smtp xmpp tls ssh].each do |service| %>
<li><%= link_to service, stats_grade_path(service) %> (<%= Analysis.where(service: service).count %> recherches)</li>
<% end %>
</ul>

3
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?

1
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"

7
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
Loading…
Cancel
Save