Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
29e1ae34fa | ||
|
|
8653076c97 | ||
|
|
c9e86a1bd9 |
Executable
+101
@@ -0,0 +1,101 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Usage: ./scripts/fetch-year.sh YEAR [--reverse]
|
||||||
|
# Fetches sequentially; prints JSON responses to stdout and errors to stderr.
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
reverse=false
|
||||||
|
year=
|
||||||
|
for arg in "$@"; do
|
||||||
|
case "$arg" in
|
||||||
|
--reverse) reverse=true ;;
|
||||||
|
*)
|
||||||
|
if [[ -n $year ]]; then
|
||||||
|
echo "Usage: $0 YEAR [--reverse]" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
year=$arg
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
if [[ -z $year ]]; then
|
||||||
|
echo "Usage: $0 YEAR [--reverse]" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if [[ ! $year =~ ^[0-9]{4}$ || $year == 0000 ]]; then
|
||||||
|
echo "YEAR must be a four-digit year from 0001 to 9999." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
year_number=$((10#$year))
|
||||||
|
days_in_month=(31 28 31 30 31 30 31 31 30 31 30 31)
|
||||||
|
if (( year_number % 400 == 0 || (year_number % 4 == 0 && year_number % 100 != 0) )); then
|
||||||
|
days_in_month[1]=29
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Reuse a reachable service, or keep a port-forward alive while fetching.
|
||||||
|
service_ready() {
|
||||||
|
curl --fail --silent --output /dev/null --connect-timeout 1 --max-time 2 \
|
||||||
|
'http://localhost:4101/api/health'
|
||||||
|
}
|
||||||
|
|
||||||
|
forward_pid=
|
||||||
|
cleanup() {
|
||||||
|
if [[ -n $forward_pid ]]; then
|
||||||
|
kill "$forward_pid" 2>/dev/null || true
|
||||||
|
wait "$forward_pid" 2>/dev/null || true
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
trap cleanup EXIT
|
||||||
|
trap 'exit 130' INT
|
||||||
|
trap 'exit 143' TERM
|
||||||
|
|
||||||
|
if ! service_ready; then
|
||||||
|
if ! command -v kubectl >/dev/null 2>&1; then
|
||||||
|
echo "Service unavailable on localhost:4101 and kubectl is not installed." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "Starting kubectl port-forward for sejs/btcdata..." >&2
|
||||||
|
kubectl -n sejs port-forward svc/btcdata 4101:4101 >&2 &
|
||||||
|
forward_pid=$!
|
||||||
|
|
||||||
|
ready=false
|
||||||
|
for (( attempt = 0; attempt < 30; attempt++ )); do
|
||||||
|
if ! kill -0 "$forward_pid" 2>/dev/null; then
|
||||||
|
echo "Port-forward exited before the service became ready." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if service_ready; then
|
||||||
|
ready=true
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
if [[ $ready != true ]]; then
|
||||||
|
echo "Timed out waiting for the service on localhost:4101." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Generate dates without relying on platform-specific date command options.
|
||||||
|
status=0
|
||||||
|
for (( month_index = 0; month_index < 12; month_index++ )); do
|
||||||
|
month=$((month_index + 1))
|
||||||
|
if [[ $reverse == true ]]; then
|
||||||
|
month=$((12 - month_index))
|
||||||
|
fi
|
||||||
|
days=${days_in_month[month - 1]}
|
||||||
|
for (( day_index = 0; day_index < days; day_index++ )); do
|
||||||
|
day=$((day_index + 1))
|
||||||
|
if [[ $reverse == true ]]; then
|
||||||
|
day=$((days - day_index))
|
||||||
|
fi
|
||||||
|
printf -v date '%s-%02d-%02d' "$year" "$month" "$day"
|
||||||
|
if ! curl --fail --silent --show-error -X GET \
|
||||||
|
"http://localhost:4101/api/price/day/$date" \
|
||||||
|
-H "accept: application/json" --write-out '\n'; then
|
||||||
|
echo "Failed to fetch $date" >&2
|
||||||
|
status=1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
done
|
||||||
|
exit "$status"
|
||||||
Reference in New Issue
Block a user