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.
Merge branch 'development' into stage
- Loading branch information
Showing
28 changed files
with
873 additions
and
88 deletions.
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
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
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
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
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
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,67 @@ | ||
module SparqlHelper | ||
def change_from_clause(query, graph) | ||
unless graph.blank? | ||
graph = graph.gsub($REST_URL, 'http://data.bioontology.org') | ||
|
||
if query.match?(/FROM <[^>]+>/i) | ||
# Use a regular expression to replace all instances of FROM <uri> | ||
query = query.gsub(/FROM <[^>]+>/i, "") | ||
else | ||
query = query.gsub("WHERE", "FROM <#{graph}> WHERE") | ||
end | ||
|
||
query = query.gsub(/GRAPH <[^>]+>/i, "") | ||
end | ||
query | ||
end | ||
def ontology_sparql_query(query, graph = '') | ||
query = change_from_clause(query, graph) | ||
sparql_query(query) | ||
end | ||
def is_allowed_query?(sparql_query) | ||
forbidden_operations = [ | ||
'INSERT DATA', | ||
'DELETE DATA', | ||
'DELETE/INSERT', | ||
'DELETE', | ||
'INSERT', | ||
'DELETE WHERE', | ||
'LOAD', | ||
'CLEAR', | ||
'CREATE', | ||
'DROP', | ||
'COPY', | ||
'MOVE', | ||
'ADD' | ||
] | ||
|
||
# Define a regular expression to match SELECT queries | ||
select_query_regex = /\A\s*SELECT\b/m | ||
|
||
# Check if the query contains any forbidden operations outside SELECT queries | ||
return false if forbidden_operations.any? { |op| sparql_query.upcase.include?(op) && !sparql_query.match(select_query_regex) } | ||
|
||
true | ||
end | ||
|
||
def sparql_query(query) | ||
return 'No SPARQL endpoint configured' if $SPARQL_URL.blank? | ||
return 'INSERT Queries not permitted' unless is_allowed_query?(query) | ||
endpoint = $SPARQL_URL.gsub('test', 'sparql') | ||
begin | ||
conn = Faraday.new do |conn| | ||
conn.options.timeout = 60 | ||
end | ||
response = conn.get("#{endpoint}?query=#{encode_param(query)}") | ||
response.body.force_encoding('ISO-8859-1').encode('UTF-8') | ||
rescue | ||
"Query timeout" | ||
end | ||
end | ||
def sparql_query_container(graph: nil) | ||
content_tag(:div, '', data: {controller: 'sparql', | ||
'sparql-proxy-value': '/sparql_proxy/', | ||
'sparql-graph-value': graph}) | ||
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
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,25 @@ | ||
import { Controller } from '@hotwired/stimulus' | ||
import { getYasgui } from '../mixins/useYasgui' | ||
|
||
// Connects to data-controller="sparql" | ||
export default class extends Controller { | ||
static values = { | ||
proxy: String, | ||
graph: String, | ||
} | ||
connect () { | ||
localStorage.removeItem('yagui__config'); | ||
this.yasgui = getYasgui(this.element, | ||
{ | ||
corsProxy: this.proxyValue, | ||
copyEndpointOnNewTab: true, | ||
requestConfig: { | ||
endpoint: this.proxyValue, | ||
acceptHeaderGraph: false, | ||
acceptHeaderUpdate: false, | ||
namedGraphs: [this.graphValue], | ||
} | ||
}) | ||
|
||
} | ||
} |
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,6 @@ | ||
import Yasgui from "@triply/yasgui"; | ||
|
||
export const getYasgui = (elem, config) => { | ||
return new Yasgui(elem, config) | ||
} | ||
|
Oops, something went wrong.