aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/usr/bin/.drive-compute-speed-eta
blob: a1d13773d616a208a63e838b849d5f1366ef6419 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
###################################################################################################
# .drive-compute-speed-eta
###################################################################################################
# Copyright 2017-2023 Jean-Christophe Manciot <jcmanciot@sdxlive.com>
#
# Licensed under a GPLv3 License; you may not use this file except in compliance with the License. 
# You may obtain a copy of the License at
#
#    https://www.gnu.org/licenses/gpl-3.0.md
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Attribution — You must give appropriate credit, provide a link to the license, and indicate if 
#               changes were made. You may do so in any reasonable manner, but not in any way that 
#               suggests the licensor endorses you or your use.
###################################################################################################

###################################################################################################
#
# Compute the current download/upload speed alongside estimated time of arrival.
#
###################################################################################################
#
# Requirements:
# ------------
#
# - We assume that:
#       + bc & coreutils packages are already installed.
#
###################################################################################################
#
# Parameters:
# ----------
#
# - <current_size>:     current size of pulled/pushed objects so far in bytes
# - <total_size>:       total size of all objects to be pulled/pushed in bytes
# - <elapsed_time>:     elapsed time since the operation has started in seconds
#
###################################################################################################
#
# Returned value:
# --------------
#
# - <speed_eta>: array containing:
#       + speed of the operation in human-readable format
#       + estimated time left until the completion of the operation expressed in hh:mn:ss
#
###################################################################################################
#
# Usage example:
# -------------
#
# readarray -t speed_eta < <(.drive-compute-speed-eta $current_size $total_size $elapsed_time)
# printf '%s\n' "${speed_eta[@]}"
#
###################################################################################################
if [[ $LOG_DRIVE_COMPUTE_SPEED_ETA == ON ]]; then
        # Getting the last existing file descriptor of ~/.drive-google/logs/drive-google.log
        fd=$(ls -al /proc/$$/fd | grep ~/.drive-google/logs/drive-google.log | sed -E 's|^.* ([0-9]+) -> .*$|\1|g' | sort -V | tail -n 1)

        # Case where fd is unset, i.e the file ~/.drive-google/logs/drive-google.log has not yet been opened
        if [[ -z $fd ]]; then
                lowest_unused_fd () 
                {
                        local fd=0
                        while [ -e /proc/$$/fd/${fd} ]; do
                                fd=$((fd+1))
                        done
                        echo $fd
                }
                fd=$(lowest_unused_fd)

                # Opening ~/.drive-google/logs/drive-google.log as file descriptor $fd for appending
                eval "exec $fd>> ~/.drive-google/logs/drive-google.log"
        fi

        # Writing the trace output generated when set -x is enabled to file descriptor $fd 
        eval "BASH_XTRACEFD=$fd"
        # Logging line numbers - We could also use ${0} for ${BASH_SOURCE}
        BASH_SOURCE_BASENAME=$(basename ${BASH_SOURCE})
        export PS4='${BASH_SOURCE_BASENAME}.${LINENO}+ '
        # Expanding all variables and prints the full commands before output of the command
        set -x
fi

# Preventing globbing (pathname expansion)
set -o noglob
# Allowing extended globbing
shopt -s extglob

current_size=$1
total_size=$2
elapsed_time=$3

# Initializing:
# - speed of the operation in b/s
# - speed of the operation in human-readable format
# - total size of objects left to be pulled
# - estimated time left until the completion of the operation expressed in seconds
# - estimated time left until the completion of the operation expressed in hh:mn:ss
speed=0
current_speed=0
size_left=0
time_left=0
eta=""

if [[ $elapsed_time -ne 0 ]]; then
        # Computing the current speed of pulled objects in bits/s
        speed=$(echo "scale=4; (${current_size}/${elapsed_time})*8" | bc)
fi

# Rounding speed to the nearest integer in bits/s
round_speed=$(LC_ALL=C printf '%.0f' "$speed")

# Converting speed to human-readable number
current_speed=$(numfmt --to=iec --format="%.2f" --round=nearest --suffix=b $speed)

# In bytes
size_left=$(( total_size - current_size ))

if [[ $round_speed -ne 0 ]]; then
        # in seconds
        time_left=$(echo "scale=4; (${size_left}/${round_speed})*8" | bc)
fi

# Rounding time_left to the nearest integer
round_time_left=$(LC_ALL=C printf '%.0f' "$time_left")

# Converting seconds to hh:mn:ss
if [[ $round_time_left -ne 0 ]]; then
        hours=$(($round_time_left/3600))
        minutes=$(($round_time_left%3600/60))
        seconds=$(($round_time_left%60))
        if [[ $hours -ne 0 ]]; then
                eta="$(printf '%02dh:%02dm:%02ds\n' $hours $minutes $seconds)"
        elif [[ $minutes -ne 0 ]]; then
                eta="$(printf '%02dm:%02ds\n' $minutes $seconds)"
        elif [[ $seconds -ne 0 ]]; then
                eta="$(printf '%02ds\n' $seconds)"
        fi
else
        eta="$(printf '%02ds\n' 0)"
fi

# Returning the speed in human-readable format
echo "${current_speed}"

# Returning the estimated time left until the completion of the operation expressed in hh:mn:ss
echo "${eta}"