Skip to content

Commit

Permalink
Feature: add queries_last_n_seconds query logs method
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Jan 21, 2025
1 parent fe45cb7 commit 630a517
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions lib/sparql/client/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def initialize(redis:, redis_key: 'query_logs', redis_expiry: REDIS_EXPIRY, logg
@enabled = !logger.nil?
end

def log(query, id: SecureRandom.uuid, cached: nil, user: nil ,&block)
def log(query, id: SecureRandom.uuid, cached: nil, user: nil, &block)
return block.call unless @enabled

time = Benchmark.realtime do
Expand All @@ -27,7 +27,6 @@ def log(query, id: SecureRandom.uuid, cached: nil, user: nil ,&block)
info(query, id: id, cached: cached, user: user, execution_time: time)
end


def info(query, id: SecureRandom.uuid, cached: 'null', user: 'null', execution_time: 0)
timestamp = Time.now.iso8601
entry = {
Expand All @@ -39,7 +38,6 @@ def info(query, id: SecureRandom.uuid, cached: 'null', user: 'null', execution_t
execution_time: execution_time.round(3).to_s
}


@logger&.info("SPARQL: #{query} (#{execution_time}s) | Cached: #{cached} | User: #{user}")
return if @redis.nil?

Expand All @@ -56,12 +54,36 @@ def get_logs
keys.map { |key| Marshal.load(@redis.get(key)) }
end

def queries_last_n_seconds(seconds)
current_time = Time.now
filtered_logs = []
cursor = '0'
loop do
cursor, keys = @redis.scan(cursor, match: "#{@redis_key}-*", count: 100)
keys.each do |key|
timestamp = key.split('-').last
next unless timestamp && timestamp.include?('T')

log_time = Time.parse(timestamp)
if (current_time - log_time) <= seconds
log = JSON.parse(Marshal.load(@redis.get(key)))
filtered_logs << log
end
end

break if cursor == '0' # Exit loop when scan cursor is back to 0
end

filtered_logs.sort_by { |log| Time.parse(log['timestamp']) }
end

def logger=(logger)
@logger = logger
@enabled = !logger.nil?
end

private

def encode_data(entry)
data = Marshal.dump(entry.to_json)
if data.length > 50e6 # 50MB of marshal object
Expand Down

0 comments on commit 630a517

Please sign in to comment.