module BuilderApm class RequestDataController < ApplicationController def index cursor = params[:cursor].presence&.to_f || Time.now.to_f * 1000 limit = params[:limit].presence&.to_i || 50 type = params[:type].presence || 'timestamps' purge_old_keys @requests = retrieve_data_from_redis_since(cursor, limit, type) Rails.logger.info "#{@requests.size} results gathered" render json: @requests end private def purge_old_keys time_limit = (Time.now - 2.hour).to_f * 1000 redis_client.zremrangebyscore('builder_apm:timestamps', '-inf', time_limit) redis_client.zremrangebyscore('builder_apm:errors', '-inf', time_limit) redis_client.zremrangebyscore('builder_apm:n_plus_one', '-inf', time_limit) redis_client.zremrangebyscore('builder_apm:slow', '-inf', time_limit) redis_client.zremrangebyscore('builder_apm:dashboard', '-inf', time_limit) end def retrieve_data_from_redis_since(cursor, limit, type) data = redis_client.zrevrangebyscore("builder_apm:#{type}", cursor, "-inf", limit: [0, limit]) data.map{ |d| JSON.parse(d) } end end end