Skip to content

Commit

Permalink
Add uptime to /status endpoint (#4281)
Browse files Browse the repository at this point in the history
* Add uptime to /status endpoint

* Tweak the output

* Even better

* Tweak output format
  • Loading branch information
hanssv committed Mar 7, 2024
1 parent f5bb0d2 commit 37a228b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apps/aehttp/priv/oas3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3066,6 +3066,8 @@ components:
format: float
minimum: 0
maximum: 100
uptime:
type: string
listening:
type: boolean
protocols:
Expand Down Expand Up @@ -3096,6 +3098,7 @@ components:
- difficulty
- hashrate
- syncing
- uptime
- listening
- protocols
- node_version
Expand Down
3 changes: 3 additions & 0 deletions apps/aehttp/priv/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2934,6 +2934,8 @@ definitions:
format: float
minimum: 0.0
maximum: 100.0
uptime:
type: string
listening:
type: boolean
protocols:
Expand Down Expand Up @@ -2964,6 +2966,7 @@ definitions:
- difficulty
- hashrate
- syncing
- uptime
- listening
- protocols
- node_version
Expand Down
21 changes: 21 additions & 0 deletions apps/aehttp/src/aehttp_dispatch_ext.erl
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ handle_request_('GetStatus', _Params, _Context) ->
Difficulty = difficulty(aec_blocks:difficulty(TopKeyBlock), Consensus),
HashRate = target_to_hashrate(aec_blocks:target(TopKeyBlock), Consensus),
{Syncing, SyncProgress, _, _} = aec_sync:sync_progress(),
Uptime = uptime(),
Listening = true, %% TODO
Protocols =
maps:fold(fun(Vsn, Height, Acc) ->
Expand Down Expand Up @@ -774,6 +775,7 @@ handle_request_('GetStatus', _Params, _Context) ->
<<"hashrate">> => HashRate,
<<"syncing">> => Syncing,
<<"sync_progress">> => SyncProgress,
<<"uptime">> => Uptime,
<<"listening">> => Listening,
<<"protocols">> => Protocols2,
<<"node_version">> => NodeVersion,
Expand Down Expand Up @@ -931,3 +933,22 @@ get_default_network_name(<<"ae_uat">>) ->
get_default_network_name(NetworkId) ->
NetworkId.

uptime() ->
{UptimeMs, _} = statistics(wall_clock),

Ts = UptimeMs div 1000,
Tms = UptimeMs rem 1000,

Res =
if Ts < 60 ->
io_lib:format("~ps.~p", [Ts, Tms]);
Ts < 60 * 60 ->
io_lib:format("~pm:~ps.~p", [Ts div 60, Ts rem 60, Tms]);
Ts < 60 * 60 * 24 ->
io_lib:format("~ph:~pm:~ps", [Ts div (60 * 60), (Ts rem (60 * 60)) div 60, Ts rem 60]);
true ->
io_lib:format("~pd:~ph:~pm:~ps", [Ts div (60 * 60 * 24), (Ts rem (60 * 60 * 24)) div (60 * 60),
(Ts rem (60 * 60)) div 60, Ts rem 60])
end,

iolist_to_binary(Res).

0 comments on commit 37a228b

Please sign in to comment.