Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
query by timespent
Browse files Browse the repository at this point in the history
  • Loading branch information
wuminzhe committed Mar 15, 2024
1 parent b5aa2ed commit 9301338
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9301338

Please sign in to comment.