Skip to content

Commit

Permalink
Merge pull request #1064 from brianlong/188705398-ping-time-stats-api
Browse files Browse the repository at this point in the history
188705398 ping time stats api
  • Loading branch information
kbiala authored Dec 30, 2024
2 parents 41d3a58 + 944ff65 commit e45843d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/controllers/api/v1/ping_times_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def ping_times
render json: { 'status' => e.message }, status: 500
end

def ping_time_stats
limit = [(ping_time_params[:limit] || 100).to_i, 9999].min

render json: PingTimeStat.where(network: ping_time_params[:network])
.order('created_at desc')
.limit(limit).to_json, status: 200
end

def collector
params[:payload] = params[:payload]&.to_json
@collector = ::Collector.new(
Expand Down
30 changes: 30 additions & 0 deletions app/views/public/api_documentation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<li><a href="#auhentication">Authentication</a></li>
<li><a href="#ping">Server Ping</a></li>
<li><a href="#ping-times">Ping Times</a></li>
<li><a href="#ping-time-stats">Ping Time Stats</a></li>
<li><a href="#validators-list">Validators List</a></li>
<li><a href="#validator-detail">Validator Details</a></li>
<li><a href="#validator-block">Validator Block Production History</a></li>
Expand Down Expand Up @@ -109,6 +110,35 @@

<hr />

<h2 class="h4" id="ping-time-stats">Ping Time Stats</h2>
<p>
The ping-time-stats endpoint will return overall stats for the ping times as reported by the participating validators. By default the most recent 100 ping time stats will be used.
</p>
<h3 class="h6">Request:</h3>
<pre>curl -H "Token: secret-api-token" '<%= @api_path %>/ping-time-stats/:network.json'</pre>
<h3 class="h6">Parameter Options:</h3>
<p><strong class="text-muted">`limit=NN`</strong> will limit the results to NN entries, defaults to 100 (max 9_999).</p>

<h3 class="h6">Response:</h3>
<p>An Array of ping time stats.</p>
<pre>
[
{
"batch_uuid": "baf5c581-7ca3-471f-9a86-17a4f3c904a3",
"overall_min_time": 101.778,
"overall_max_time": 101.872,
"overall_average_time": 101.803,
"observed_at": "2024-12-18 10:34:06.984608000 +0000",
"created_at": "2024-12-18 10:34:26.984608000 +0000",
"updated_at": "2024-12-18 10:34:26.984608000 +0000",
"network": "testnet"
},
...
]
</pre>

<hr />

<h2 class="h4" id="validators-list">Validators List</h2>
<p>
The Validators List endpoint will return a list of validators for the requested network. In the example below,
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
# TODO to remove - endpoint no longer in use
# api_v1_ping_times GET /api/v1/ping_times
get 'ping-times/:network', to: 'ping_times#ping_times', as: 'ping_times'
get 'ping-time-stats/:network', to: 'ping_times#ping_time_stats', as: 'ping_time_stats'

# POST /api/v1/ping-thing/
post 'ping-thing/:network', to: 'ping_things#create', as: 'ping_thing'
Expand Down
57 changes: 57 additions & 0 deletions test/controllers/api/v1/ping_times_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,63 @@ def setup
assert_equal 1, collector.payload_version
assert_equal '{"test_key":"test_value"}', collector.payload
end

# ping time stats
test 'GET api_v1_ping_time_stats without token should get error' do
get api_v1_ping_time_stats_path(network: 'testnet')
assert_response 401
expected_response = { 'error' => 'Unauthorized' }
assert_equal expected_response, response_to_json(@response.body)
end

test 'GET api_v1_ping_time_stats with valid token should succeed' do
create_list(:ping_time_stat, 10, network: 'testnet')
get api_v1_ping_time_stats_path(network: 'testnet'),
headers: { 'Token' => @user.api_token }
assert_response 200
json = response_to_json(@response.body)
assert_equal 10, json.size
end

test 'GET api_v1_ping_time_stats with limit should return limited results' do
create_list(:ping_time_stat, 10, network: 'testnet')
get api_v1_ping_time_stats_path(network: 'testnet', limit: 5),
headers: { 'Token' => @user.api_token }
assert_response 200
json = response_to_json(@response.body)
assert_equal 5, json.size
end

# ping times
test 'GET api_v1_ping_times with valid token should succeed' do
create_list(:ping_time, 10, network: 'testnet')

get api_v1_ping_times_path(network: 'testnet'),
headers: { 'Token' => @user.api_token }
assert_response 200
json = response_to_json(@response.body)
assert_equal 10, json.size
end

test 'GET api_v1_ping_times with limit should return limited results' do
create_list(:ping_time, 10, network: 'testnet')

get api_v1_ping_times_path(network: 'testnet', limit: 5),
headers: { 'Token' => @user.api_token }
assert_response 200
json = response_to_json(@response.body)
assert_equal 5, json.size
end

test 'GET api_v1_ping_times with invalid limit should return limited results' do
create_list(:ping_time, 10, network: 'testnet')

get api_v1_ping_times_path(network: 'testnet', limit: 100_000),
headers: { 'Token' => @user.api_token }
assert_response 200
json = response_to_json(@response.body)
assert_equal 10, json.size
end
end
end
end

0 comments on commit e45843d

Please sign in to comment.