forked from ontoportal/ontoportal_web_ui
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
880359a
commit caa510c
Showing
5 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters