diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 3c4a875..25597f2 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -27,6 +27,35 @@ def message end end + def timespent + unit = params[:unit].singularize + if unit == 'second' + distance = params[:number].to_i + elsif unit == 'minute' + distance = params[:number].to_i * 60 + elsif unit == 'hour' + distance = params[:number].to_i * 60 * 60 + elsif unit == 'day' + distance = params[:number].to_i * 60 * 60 * 24 + else + raise 'Invalid unit' + end + + @messages = if params[:op] == 'gt' + Message.where( + 'dispatch_block_timestamp IS NOT NULL AND round(extract(epoch from(dispatch_block_timestamp - block_timestamp)))::int > ?', distance + ) + elsif params[:op] == 'lt' + Message.where( + 'dispatch_block_timestamp IS NOT NULL AND round(extract(epoch from(dispatch_block_timestamp - block_timestamp)))::int < ?', distance + ) + else + raise 'Invalid operator' + end + @messages = @messages.order(block_timestamp: :desc).page(params[:page]).per(25) + render :index + end + private # Use callbacks to share common setup or constraints between actions. diff --git a/config/routes.rb b/config/routes.rb index 3e77883..8278730 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,8 +10,13 @@ resources :messages, only: %i[index] get 'message' => 'messages#message', as: :message + + get 'messages/timespent/:op/:number/:unit' => 'messages#timespent', + as: :messages_timespent + get 'messages/:tx_or_hash' => 'messages#show', as: :message_by_tx_or_hash, constraints: { tx_or_hash: /0x[0-9a-fA-F]{64}/ } + get 'messages/:from_network(/:to_network)' => 'messages#index', as: :network_messages end