Skip to content

Instantly share code, notes, and snippets.

@jonasjancarik
Created March 1, 2023 14:31
Show Gist options
  • Save jonasjancarik/a16390d630c9fc33bb008f259f35d6b5 to your computer and use it in GitHub Desktop.
Save jonasjancarik/a16390d630c9fc33bb008f259f35d6b5 to your computer and use it in GitHub Desktop.
Elasticsearch tasks API response parser
#!/bin/bash
# Read the input from the curl command
input=$(cat -)
# Extract the running_time_in_nanos field from the input
running_time_in_nanos=$(echo $input | jq '.task.running_time_in_nanos')
# Extract the values of "total" and the sum of "updated", "created", "deleted", and "noops" from the input
total=$(echo $input | jq '.task.status.total')
processed=$(echo $input | jq '.task.status.updated + .task.status.created + .task.status.deleted + .task.status.noops')
# Convert running_time_in_nanos to human-readable time format
# 1 second = 1,000,000,000 nanoseconds
seconds=$(expr $running_time_in_nanos / 1000000000)
minutes=$(expr $seconds / 60)
hours=$(expr $minutes / 60)
minutes=$(expr $minutes % 60)
seconds=$(expr $seconds % 60)
# Calculate the average processing time per item
if [ $processed -ne 0 ]; then
avg_time_per_item=$(expr $running_time_in_nanos / $processed)
else
avg_time_per_item=0
fi
# Calculate the remaining items
remaining=$(expr $total - $processed)
# Calculate the estimated remaining time in nanoseconds
eta_in_nanos=$(expr $avg_time_per_item \* $remaining)
# Convert the estimated remaining time to human-readable format
eta_seconds=$(expr $eta_in_nanos / 1000000000)
eta_minutes=$(expr $eta_seconds / 60)
eta_hours=$(expr $eta_minutes / 60)
eta_minutes=$(expr $eta_minutes % 60)
eta_seconds=$(expr $eta_seconds % 60)
# Format the human-readable time format
running_time_in_human_readable_format=$(printf "%02d:%02d:%02d" $hours $minutes $seconds)
eta_in_human_readable_format=$(printf "%02d:%02d:%02d" $eta_hours $eta_minutes $eta_seconds)
# Add the human-readable time format to the JSON output
output=$(echo $input | jq --arg running_time "$running_time_in_human_readable_format" --arg eta "$eta_in_human_readable_format" '.task += {"running_time_in_human_readable_format": $running_time, "eta_in_human_readable_format": $eta}')
# Output the result
echo "$output"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment