aboutsummaryrefslogtreecommitdiff
path: root/units/ping.zsh
blob: f7e2508f21e83d66d59b9a60da658bbc32016505 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Copyright (C) 2024-2026 Maria Lisina
# Copyright (C) 2024-2026 Danil Lisin
# SPDX-License-Identifier: Apache-2.0

uptime_table=(
    86400 d
    3600 h
    60 m
    1 s
)

latency_sys=$(((latency_sysf - latency_sys) / 1000000))
latency_net=$(((latency_netf - latency_net) / 1000000))
latency_all=$((latency_sys + latency_net))

uptime_time=$(($(strftime %s) - startup_time + 1))

while [[ ${uptime_time} -gt 0 && ${#uptime_table} -ge 2 ]]
do
    uptime_unit=$((uptime_time / uptime_table[1]))

    if [[ ${uptime_unit} -eq 0 ]]
    then
        shift 2 uptime_table
        continue
    fi

    uptime_text="${uptime_text} ${uptime_unit}${uptime_table[2]}"

    uptime_time=$((uptime_time - (uptime_unit * uptime_table[1])))
    shift 2 uptime_table
done

output_text="$(printf "<b>System:</b> %ums" "${latency_sys}")"
output_text="$(printf "%s\n<b>Network:</b> %ums" "${output_text}" "${latency_net}")"
output_text="$(printf "%s\n<b>Overall:</b> %ums" "${output_text}" "${latency_all}")"
output_text="$(printf "%s\n\n<b>Uptime:</b>%s" "${output_text}" "${uptime_text}")"

keyboard_text1="Refresh"
keyboard_data1="ping"

reply_markup="$(
    jq --null-input --compact-output \
        --arg text1 "${keyboard_text1}" \
        --arg data1 "${keyboard_data1}" \
        '{"inline_keyboard": [[{"text": $text1, "callback_data": $data1}]]}'
)"