Skip to content

Commit

Permalink
add statistics page
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Dec 31, 2023
1 parent 880359a commit caa510c
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
14 changes: 14 additions & 0 deletions app/controllers/statistics_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class StatisticsController < ApplicationController
include StatisticsHelper, ComponentsHelper

layout :determine_layout

def index
projects = LinkedData::Client::Models::Project.all({include: 'created'})
users = LinkedData::Client::Models::User.all({include: 'created'})
year_month_count, @year_month_visits = ontologies_by_year_month
@merged_data = merge_time_evolution_data([group_by_year_month(users),
group_by_year_month(projects),
year_month_count])
end
end
60 changes: 60 additions & 0 deletions app/helpers/statistics_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module StatisticsHelper

def ontologies_by_year_month
data = LinkedData::Client::Analytics.all.to_h
data.delete(:links)
data.delete(:context)
year_month_count = {}
year_month_visits = {}
acronyms = []
data.each do |acronym, ont|
ont.each do |year, months|
next if year.eql?(:links) || year.eql?(:context)
months.each do |month, count|
next if month.eql?(:links) || month.eql?(:context)
year_month_count[[year.to_s.to_i, month.to_s.to_i]] ||= []
year_month_visits[[year.to_s.to_i, month.to_s.to_i]] = count + (year_month_visits[[year.to_s.to_i, month.to_s.to_i]] || 0)

if !count.zero? && !acronyms.include?(acronym)
year_month_count[[year.to_s.to_i, month.to_s.to_i]] << acronym
acronyms << acronym
end
end
end
end
year_month_visits = year_month_visits.sort_by { |(year, month), _| [year, month] }.to_h
[year_month_count, year_month_visits]
end

def string_year_month(year, month)
DateTime.parse("#{year}/#{month}").strftime("%b %Y")
end
def group_by_year_month(data)
data.group_by{|x| [Date.parse(x.created).year, Date.parse(x.created).month] }.sort_by { |(year, month), _| [year, month] }.to_h
end

def merge_time_evolution_data(data)
min_year = data.map{|x| x.keys.first.first}.min
old = data.size.times.map { |x| 0 }

visits_data = { visits: data.size.times.map { |x| [] }, labels: [] }

(min_year..Date.today.year).each do |year|
(1..12).each do |month|
data.each_with_index do |x , i|
old[i] += x[[year, month]]&.size || 0
end

next if old.sum.zero?

data.each_index do |i|
visits_data[:visits][i] << old[i]
end

visits_data[:labels] << string_year_month(year, month)
end
end
visits_data
end

end
2 changes: 1 addition & 1 deletion app/views/admin/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
%div
%div.mx-1
- sections = ['Analytics', 'Site Administration','Ontology Administration', 'Licensing', 'Users', 'Metadata Administration', 'Groups', 'Categories', 'Persons & Organizations', 'SPARQL']
- selected = params[:section] || sections.first
- selected = params[:section] || sections.first.downcase
= render TabsContainerComponent.new do |t|
- sections.each do |section_title|
- t.item(title: section_title,
Expand Down
33 changes: 33 additions & 0 deletions app/views/statistics/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
%div.px-3.py-3.pt-md-5.pb-md-4.text-center
%h1.display-4
#{portal_name} Statistics
%p.lead (last #{Date.today.year - Date.parse(@merged_data[:labels].first).year} years)

%div.container
%div
= chart_component(title: nil, type: 'line', show_legend: true,
labels: @merged_data[:labels],
datasets: visits_chart_dataset_array({ 'Ontologies': @merged_data[:visits][2] , 'Users': @merged_data[:visits][0], 'Projects': @merged_data[:visits][1]}, fill: false))

%div.pb-3.pb-md-4
- size = @merged_data[:labels].size - 1
- visits = @year_month_visits
= render TableComponent.new(id: 'statistics_table') do |t|
- t.header do |h|
- h.th {'Date'}
- h.th {'Ontologies'}
- h.th {'Users'}
- h.th {'Projects'}
- h.th {'Ontology Visits'}

- @merged_data[:labels].reverse.each_with_index do |year_month, i|
- t.row do |r|
- r.td {year_month}
- r.td {@merged_data[:visits][2][size - i].to_s}
- r.td {@merged_data[:visits][0][size - i].to_s}
- r.td {@merged_data[:visits][1][size - i].to_s}
- r.td do
- year = Date.parse(year_month).year
- month = Date.parse(year_month).month
= visits[[year, month]] || 0

1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
get '/site_config' => 'home#site_config'
post '/annotator_recommender_form' => 'home#annotator_recommender_form'
match '/visits', to: 'visits#index', via: :get
get 'statistics/index'

# Error pages
match "/404", to: "errors#not_found", via: :all
Expand Down

0 comments on commit caa510c

Please sign in to comment.