From fa06aeeea4998b7ec5c55e208e0e9f73f2821c8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kocy=C5=82a?= Date: Mon, 23 Dec 2024 12:22:03 +0100 Subject: [PATCH 1/2] add method and tests --- .../api/v1/ping_times_controller.rb | 8 +++ config/routes.rb | 1 + .../api/v1/ping_times_controller_test.rb | 57 +++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/app/controllers/api/v1/ping_times_controller.rb b/app/controllers/api/v1/ping_times_controller.rb index b51362920..1ddbdc604 100644 --- a/app/controllers/api/v1/ping_times_controller.rb +++ b/app/controllers/api/v1/ping_times_controller.rb @@ -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( diff --git a/config/routes.rb b/config/routes.rb index d02ac5d60..aa1702fea 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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' diff --git a/test/controllers/api/v1/ping_times_controller_test.rb b/test/controllers/api/v1/ping_times_controller_test.rb index 206614c3a..acb80e7dc 100644 --- a/test/controllers/api/v1/ping_times_controller_test.rb +++ b/test/controllers/api/v1/ping_times_controller_test.rb @@ -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 From 944ff656ff8643a3d32214b8bf5868561a14f2b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kocy=C5=82a?= Date: Mon, 23 Dec 2024 12:39:55 +0100 Subject: [PATCH 2/2] update docs --- app/views/public/api_documentation.html.erb | 30 +++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/app/views/public/api_documentation.html.erb b/app/views/public/api_documentation.html.erb index 251981405..d81524230 100644 --- a/app/views/public/api_documentation.html.erb +++ b/app/views/public/api_documentation.html.erb @@ -14,6 +14,7 @@
  • Authentication
  • Server Ping
  • Ping Times
  • +
  • Ping Time Stats
  • Validators List
  • Validator Details
  • Validator Block Production History
  • @@ -109,6 +110,35 @@
    +

    Ping Time Stats

    +

    + 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. +

    +

    Request:

    +
    curl -H "Token: secret-api-token" '<%= @api_path %>/ping-time-stats/:network.json'
    +

    Parameter Options:

    +

    `limit=NN` will limit the results to NN entries, defaults to 100 (max 9_999).

    + +

    Response:

    +

    An Array of ping time stats.

    +
    +[
    +  {
    +    "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"
    +  },
    +  ...
    +]
    +
    + +
    +

    Validators List

    The Validators List endpoint will return a list of validators for the requested network. In the example below,