Skip to content

Commit

Permalink
Merge branch 'feature/add-graphs-count-admin-page' into stage
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Jan 24, 2025
2 parents 10a3a1d + d46f0a2 commit 6c0f4a5
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 70 deletions.
2 changes: 1 addition & 1 deletion app/components/text_area_field_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class TextAreaFieldComponent < ViewComponent::Base
include InternationalisationHelper

def initialize(value: , see_more_text: t('components.see_more') , see_less_text: t('components.see_less'))
def initialize(value: nil, see_more_text: t('components.see_more') , see_less_text: t('components.see_less'))
super
@value = value
@see_more_text = see_more_text
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%div{data:{controller:"text-truncate", 'text-truncate-more-text-value': @see_more_text , 'text-truncate-less-text-value': @see_less_text}}
%span.text-content{'data-text-truncate-target': 'content'}
= @value
= @value || content
%span.see_more_text{data:{'text-truncate-target': 'button', 'action':"click->text-truncate#toggle"}}
= @see_more_text
= @see_more_text
16 changes: 10 additions & 6 deletions app/controllers/admin/graphs_controller.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
require 'multi_json'

class Admin::GraphsController < ApplicationController

include TurboHelper

layout :determine_layout
before_action :authorize_admin

GRAPHS_URL = "#{LinkedData::Client.settings.rest_url}/admin/graphs"
GRAPHS_URL = "#{LinkedData::Client.settings.rest_url}/admin/graphs".freeze

def index
@graphs = LinkedData::Client::HTTP.get("#{GRAPHS_URL}", { raw: true }, { raw: true })
@graphs = LinkedData::Client::HTTP.get(GRAPHS_URL.dup, { raw: true }, { raw: true })
@graphs = MultiJson.load(@graphs)
@zombie_graphs = @graphs.select { |_, v| v[1] }
all_onts = LinkedData::Client::Models::Ontology.all
@empty_ontologies = all_onts.select { |ont| !@graphs.any? { |k, _| k.include?(ont.acronym) } }
end

def create

message = 'Graphs counts created successfully'
# response = LinkedData::Client::HTTP.post(GRAPHS_URL, {})
# message = response.status == 200 ? response.message : 'Error creating graphs counts'
redirect_to admin_index_path(section: 'graphs'), notice: message
end

def destroy

end
end
19 changes: 19 additions & 0 deletions app/controllers/admin/logs_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'multi_json'

class Admin::LogsController < ApplicationController

include TurboHelper

layout :determine_layout
before_action :authorize_admin

ALL_LOGS = "#{LinkedData::Client.settings.rest_url}/admin/latest_day_query_logs".freeze
LATEST_LOGS = "#{LinkedData::Client.settings.rest_url}/admin/last_n_s_query_logs?seconds=10".freeze
USERS_QUERY_COUNT = "#{LinkedData::Client.settings.rest_url}/admin/user_query_count".freeze
def index
page = (params[:page] || 1).to_i
page_size = (params[:page_size] || 100).to_i
@logs = LinkedData::Client::HTTP.get(ALL_LOGS.dup, { page: page, pagesize: page_size })
end

end
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
13 changes: 11 additions & 2 deletions app/helpers/admin_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ def selected_admin_section?(section_title)
current_section.eql?(section_title)
end


def new_ontologies_created_title
content_tag(:div,
t('admin.new_ontologies_created_title', count: @new_ontologies_count.join(', ')),
Expand All @@ -19,7 +18,17 @@ def visits_evolution

def action_button(name, link, method: :post, class_style: 'btn btn-link')
button_to name, link, method: method, class: class_style,
form: {data: { turbo: true, turbo_confirm: t('admin.turbo_confirm', name: name), turbo_frame: '_top'}}
form: { data: { turbo: true, turbo_confirm: t('admin.turbo_confirm', name: name), turbo_frame: '_top' } }

end

def user_count
user_count = LinkedData::Client::HTTP.get(Admin::LogsController::USERS_QUERY_COUNT.dup, {}, raw: true)
user_count = MultiJson.load(user_count)
return [] if user_count.is_a?(Hash) && user_count['errors']

user_count.sort_by! { |uc| uc['count'] }.reverse!
user_count.map! { |uc| [uc['user'].split('/').last, uc['count']] }.to_h
end

end
4 changes: 2 additions & 2 deletions app/helpers/components_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ def properties_dropdown(id, title, tooltip, properties, is_open: false, &block)
end
end

def regular_button(id, value, variant: "secondary", state: "regular", size: "slim", href: nil, &block)
render Buttons::RegularButtonComponent.new(id:id, value: value, variant: variant, state: state, size: size, href: href) do |btn|
def regular_button(id, value, variant: "secondary", state: "regular", size: "slim", href: nil, type: 'button', &block)
render Buttons::RegularButtonComponent.new(id:id, value: value, variant: variant, state: state, type: type, size: size, href: href) do |btn|
capture(btn, &block) if block_given?
end
end
Expand Down
Loading

0 comments on commit 6c0f4a5

Please sign in to comment.