Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache results to prevent miscalculations #13

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions scripts/download_speed.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ sum_download_speed()
main()
{
# TODO make configurable
#local file=$(get_tmux_option $DOWNLOAD_FILE)
local file=$DOWNLOAD_FILE
local old_val=$(read_file $file)
local new_val=$(sum_download_speed)
if ! is_update_needed $DOWNLOAD_TIME_FILE; then
local vel=$(read_file $DOWNLOAD_CACHE_FILE)
else
local file=$DOWNLOAD_FILE
local old_val=$(read_file $file)
local new_val=$(sum_download_speed)

write_file $file $new_val
local vel=$(get_velocity $new_val $old_val)
write_file $file $new_val
local vel=$(get_velocity $new_val $old_val)

write_file $DOWNLOAD_TIME_FILE $(date +%s)
write_file $DOWNLOAD_CACHE_FILE "$vel"
fi

## Format output
local format=$(get_tmux_option @download_speed_format "%s")
Expand Down
17 changes: 17 additions & 0 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
##
DOWNLOAD_FILE="/tmp/tmux_net_speed.download"
UPLOAD_FILE="/tmp/tmux_net_speed.upload"
DOWNLOAD_CACHE_FILE="/tmp/tmux_net_speed.download.cache"
UPLOAD_CACHE_FILE="/tmp/tmux_net_speed.upload.cache"
DOWNLOAD_TIME_FILE="/tmp/tmux_net_speed.download.time"
UPLOAD_TIME_FILE="/tmp/tmux_net_speed.upload.time"

get_tmux_option() {
local option=$1
Expand All @@ -24,6 +28,19 @@ set_tmux_option() {
tmux set-option -gq "$option" "$value"
}

is_update_needed()
{
local update_file=$1

local interval=$(get_tmux_option 'status-interval' 5)
local update_time=$(read_file $update_file)
local cur_time=$(date +%s)
if [ $((update_time + interval)) -gt $cur_time ]; then
return 1;
fi;
return 0;
}

get_velocity()
{
local new_value=$1
Expand Down
17 changes: 12 additions & 5 deletions scripts/upload_speed.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ main()
{
# TODO make configurable
#local upload_file=$(get_tmux_option $UPLOAD_FILE)
local file=$UPLOAD_FILE
local old_val=$(read_file $file)
local new_val=$(sum_upload_speed)
if ! is_update_needed $UPLOAD_TIME_FILE; then
local vel=$(read_file $UPLOAD_CACHE_FILE)
else
local file=$UPLOAD_FILE
local old_val=$(read_file $file)
local new_val=$(sum_upload_speed)

write_file $file $new_val
local vel=$(get_velocity $new_val $old_val)
write_file $file $new_val
local vel=$(get_velocity $new_val $old_val)

write_file $UPLOAD_TIME_FILE $(date +%s)
write_file $UPLOAD_CACHE_FILE "$vel"
fi

## Format output
local format=$(get_tmux_option @upload_speed_format "%s")
Expand Down