Skip to content

Commit

Permalink
Merge branch 'feature/add-query-logs-endpoints' into test
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Jan 21, 2025
2 parents cffc996 + ab75004 commit ec20134
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ GIT

GIT
remote: https://github.com/ontoportal-lirmm/sparql-client.git
revision: d1b90df22ce8f9fa1b87d9483f7e833a19eaa86e
revision: 9d987098b7f27fb7e3b5e22e5c59d31a9c1b8311
branch: development
specs:
sparql-client (3.2.2)
Expand Down
6 changes: 5 additions & 1 deletion config/environments/config.rb.sample
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ LinkedData.config do |config|
config.repository_folder = REPOSITORY_FOLDER.to_s
# config.enable_notifications = false

# SPARQL logging
config.log_file = './sparql.log'
config.logging = false

config.interportal_hash = {
"agroportal" => {
"api" => "http://data.agroportal.lirmm.fr",
Expand Down Expand Up @@ -138,4 +142,4 @@ NcboCron.config do |config|
config.redis_host = REDIS_PERSISTENT_HOST.to_s
config.redis_port = REDIS_PORT.to_i
config.ontology_report_path = REPORT_PATH
end
end
18 changes: 15 additions & 3 deletions controllers/logging_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,22 @@ class LoggingController < ApplicationController
end
}

get '/latest_query_logs' do
get '/latest_day_query_logs' do
logs = Goo.logger.get_logs
logs = logs.map { |log| MultiJson.load(log) }
reply logs
reply 200, paginate_logs(logs)
end

get '/last_n_s_query_logs' do
sec = params[:seconds] || 10
logs = Goo.logger.queries_last_n_seconds(sec.to_i)
reply 200, paginate_logs(logs)
end

def paginate_logs(logs)
page, size = page_params
start = (page - 1) * size
page_end = [start + size - 1, logs.size].min
page_object(logs[start..page_end] || [], logs.size)
end

end
Expand Down
35 changes: 33 additions & 2 deletions test/controllers/test_logging_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def setup
Goo.redis_client.flushdb
Goo.add_query_logger(enabled: true, file: "./queries.log")
end

def teardown
Goo.add_query_logger(enabled: false, file: nil)
File.delete("./queries.log") if File.exist?("./queries.log")
Expand All @@ -16,10 +17,40 @@ def teardown
end

def test_logging_endpoint
(1..10).each do |_i|
LinkedData::Models::Ontology.where.include(:acronym).all
end
get '/admin/latest_day_query_logs?page=1&pagesize=10'
assert last_response.ok?
logs = MultiJson.load(last_response.body)
assert_equal 10, logs['collection'].size

get '/admin/latest_day_query_logs?page=2&pagesize=10'
assert last_response.ok?
logs = MultiJson.load(last_response.body)
assert_equal 1, logs['collection'].size

get '/admin/latest_day_query_logs?page=3&pagesize=10'
assert last_response.ok?
logs = MultiJson.load(last_response.body)
assert_empty logs['collection']
end

def test_n_last_seconds_logs
(1..10).each do |_i|
LinkedData::Models::Ontology.where.include(:acronym).all
end
get '/admin/last_n_s_query_logs?seconds=2&page=1&pagesize=10'
assert last_response.ok?
logs = MultiJson.load(last_response.body)
assert_equal 10, logs['collection'].size

sleep 1
LinkedData::Models::Ontology.where.include(:acronym).all
get '/admin/latest_query_logs'
get '/admin/last_n_s_query_logs?seconds=1&page=1&pagesize=10'
assert last_response.ok?
logs = MultiJson.load(last_response.body)
assert logs
assert_equal 1, logs['collection'].size
binding.pry
end
end

0 comments on commit ec20134

Please sign in to comment.