Skip to content

Commit

Permalink
add user API usage graph in the admin analytics section
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Jan 21, 2025
1 parent 04a131a commit b921d17
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
60 changes: 28 additions & 32 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class AdminController < ApplicationController
include TurboHelper, HomeHelper, SparqlHelper
include AdminHelper
include SparqlHelper
include HomeHelper
include TurboHelper
layout :determine_layout
before_action :cache_setup

Expand All @@ -16,29 +19,30 @@ def sparql_endpoint
user_name = params["username"]

unless user_name.blank?
user = LinkedData::Client::Models::User.find(user_name, {include: 'all', apikey: apikey})
user = LinkedData::Client::Models::User.find(user_name, { include: 'all', apikey: apikey })
render(inline: 'Query not permitted') && return if user.nil?
end

render(inline: 'Query not permitted') && return if graph.blank? && !user&.admin?

unless graph.blank?
acronym = graph.split('/')[-3]
@ontology = LinkedData::Client::Models::Ontology.find_by_acronym(acronym, {apikey: apikey}).first
render(inline: t('admin.query_not_permitted')) && return if @ontology.nil? || @ontology.errors
@ontology = LinkedData::Client::Models::Ontology.find_by_acronym(acronym, { apikey: apikey }).first
render(inline: t('admin.query_not_permitted')) && return if @ontology.nil? || @ontology.errors
end

response = helpers.ontology_sparql_query(params[:query], graph)

render inline: response
render inline: response
end

def index
@users = LinkedData::Client::Models::User.all
@ontology_visits = ontology_visits_data
@users_visits = user_visits_data
@page_visits = page_visits_data
@ontologies_problems_count = _ontologies_report[:ontologies]&.select{|a,v| v[:problem]}&.size || 0
@users_count = user_count
@ontologies_problems_count = _ontologies_report[:ontologies]&.select { |a, v| v[:problem] }&.size || 0

if session[:user].nil? || !session[:user].admin?
redirect_to :controller => 'login', :action => 'index', :redirect => '/admin'
Expand All @@ -47,12 +51,11 @@ def index
end
end


def update_check_enabled
enabled = LinkedData::Client::HTTP.get("#{ADMIN_URL}update_check_enabled", {}, raw: false)

if enabled
response = {update_info: Hash.new, errors: nil, success: '', notices: ''}
response = { update_info: Hash.new, errors: nil, success: '', notices: '' }
json = LinkedData::Client::HTTP.get("#{ADMIN_URL}update_info", params, raw: true)

begin
Expand All @@ -74,22 +77,19 @@ def update_check_enabled
else
output = []

output << response[:update_info]["notes"] if response[:update_info]["update_available"]
output << response[:update_info]["notes"] if response[:update_info]["update_available"]

output << t('admin.current_version', version: response[:update_info]['current_version'])
output << t('admin.appliance_id', id: response[:update_info]['appliance_id'])


render_turbo_stream *output.map{|message| alert(id: 'update_check_frame', type: 'info') {message} }

render_turbo_stream *output.map { |message| alert(id: 'update_check_frame', type: 'info') { message } }

end
else
render_turbo_stream alert(id: 'update_check_frame', type: 'info') { 'not enabled' }
end
end


def parse_log
@acronym = params["acronym"]
@parse_log = LinkedData::Client::HTTP.get(PARSE_LOG_URL.call(params["acronym"]), {}, raw: false)
Expand All @@ -108,7 +108,7 @@ def parse_log
end

def clearcache
response = {errors: nil, success: ''}
response = { errors: nil, success: '' }

if @cache.respond_to?(:flush_all)
begin
Expand All @@ -134,7 +134,7 @@ def clearcache
end

def resetcache
response = {errors: nil, success: ''}
response = { errors: nil, success: '' }

if @cache.respond_to?(:reset)
begin
Expand All @@ -144,7 +144,7 @@ def resetcache
response[:errors] = t('admin.cache_reset_error', message: e.message)
end
else
response[:errors] = t('admin.no_reset_command')
response[:errors] = t('admin.no_reset_command')
end

respond_to do |format|
Expand All @@ -159,7 +159,7 @@ def resetcache
end

def clear_goo_cache
response = {errors: nil, success: ''}
response = { errors: nil, success: '' }

begin
response_raw = LinkedData::Client::HTTP.post("#{ADMIN_URL}clear_goo_cache", params, raw: true)
Expand All @@ -181,7 +181,7 @@ def clear_goo_cache
end

def clear_http_cache
response = {errors: nil, success: ''}
response = { errors: nil, success: '' }

begin
response_raw = LinkedData::Client::HTTP.post("#{ADMIN_URL}clear_http_cache", params, raw: true)
Expand All @@ -207,7 +207,7 @@ def ontologies_report
end

def refresh_ontologies_report
response = {errors: '', success: ''}
response = { errors: '', success: '' }

begin
response_raw = LinkedData::Client::HTTP.post(ONTOLOGIES_URL, params, raw: true)
Expand All @@ -221,7 +221,7 @@ def refresh_ontologies_report
if params["ontologies"].nil? || params["ontologies"].empty?
response[:success] = t('admin.refresh_report_without_ontologies')
else
ontologies = params["ontologies"].split(",").map {|o| o.strip}
ontologies = params["ontologies"].split(",").map { |o| o.strip }
response[:success] = t('admin.refresh_report_with_ontologies', ontologies: ontologies.join(", "))
end
end
Expand All @@ -232,7 +232,6 @@ def refresh_ontologies_report
render :json => response
end


def process_ontologies
_process_ontologies('enqued for processing', 'processing', :_process_ontology)
end
Expand Down Expand Up @@ -266,12 +265,12 @@ def delete_submission
response[:errors] << t('admin.ontology_not_found', ont: ont)
end
rescue Exception => e
response[:errors] << t('admin.problem_deleting_submission', id: params["id"], ont: ont, class: e.class, message: e.message )
response[:errors] << t('admin.problem_deleting_submission', id: params["id"], ont: ont, class: e.class, message: e.message)
end

if params[:turbo_stream]
if response[:errors].empty?
render_turbo_stream( alert_success { response[:success] }, remove('submission_' + submission_id.to_s))
render_turbo_stream(alert_success { response[:success] }, remove('submission_' + submission_id.to_s))

else
render_turbo_stream alert_error { response[:errors] }
Expand All @@ -282,15 +281,14 @@ def delete_submission

end


private

def cache_setup
@cache = Rails.cache.instance_variable_get("@data")
end

def _ontologies_report
response = {ontologies: Hash.new, report_date_generated: REPORT_NEVER_GENERATED, errors: '', success: ''}
response = { ontologies: Hash.new, report_date_generated: REPORT_NEVER_GENERATED, errors: '', success: '' }
start = Time.now

begin
Expand All @@ -310,7 +308,7 @@ def _ontologies_report
response
end

def _process_errors(errors, response, remove_trailing_comma=true)
def _process_errors(errors, response, remove_trailing_comma = true)
if errors.is_a?(Hash)
errors.each do |_, v|
if v.kind_of?(Array)
Expand All @@ -321,7 +319,7 @@ def _process_errors(errors, response, remove_trailing_comma=true)
end
end
elsif errors.kind_of?(Array)
errors.each {|err| response[:errors] << "#{err}, "}
errors.each { |err| response[:errors] << "#{err}, " }
end
response[:errors] = response[:errors][0...-2] if remove_trailing_comma
end
Expand All @@ -336,12 +334,12 @@ def _process_ontology(ontology, params)
end

def _process_ontologies(success_keyword, error_keyword, process_proc)
response = {errors: '', success: ''}
response = { errors: '', success: '' }

if params["ontologies"].nil? || params["ontologies"].empty?
response[:errors] = t('admin.no_ontologies_parameter_passed')
else
ontologies = params["ontologies"].split(",").map {|o| o.strip}
ontologies = params["ontologies"].split(",").map { |o| o.strip }

ontologies.each do |ont|
begin
Expand All @@ -368,7 +366,6 @@ def _process_ontologies(success_keyword, error_keyword, process_proc)
render :json => response
end


def user_visits_data
begin
analytics = JSON.parse(LinkedData::Client::HTTP.get("#{rest_url}/data/analytics/users", {}, raw: true))
Expand Down Expand Up @@ -403,7 +400,7 @@ def ontology_visits_data
aggregated_data = {}
analytics.each do |acronym, years_data|
current_year_count = 0
previous_year_count = 0
previous_year_count = 0
years_data.each do |year, months_data|
previous_year_count += current_year_count
current_year_count = 0
Expand All @@ -423,7 +420,6 @@ def ontology_visits_data
end
end


aggregated_data.sort.each do |year, year_data|
year_data.each do |month, value|
visits_data[:visits] << value
Expand Down
12 changes: 10 additions & 2 deletions app/views/admin/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@
= render Layout::VerticalTabsComponent.new(header: t('admin.index.administration_console'), titles: sections, selected: selected, url_parameter: 'section') do |t|
- t.item_content do
- if @ontology_visits[:visits].empty?
%div.d-flex.justify-content-center
%div.d-flex.justify-content-center.card.p-2.text-center
Google analytics not enabled
- else
- else
= render 'analytics'

%div
%div.my-2
%div.card.p-2
= chart_component(title: 'Users API usage the last 30 days', type: 'bar',
labels: @users_count.keys,
datasets: visits_chart_dataset_array({'SPARQL Calls': @users_count.values}),
index_axis: 'y')
- t.item_content do
= render 'main'
- t.item_content do
Expand Down

0 comments on commit b921d17

Please sign in to comment.