Skip to content

Commit

Permalink
Add a script to delete device status entries that are older than 2 mo…
Browse files Browse the repository at this point in the history
…nths. (#1232)

* Add a script to delete device status entries that are older than 2 months.

Signed-off-by: Tzachi Dar <[email protected]>

* Fixes to delete old device status:
1) Create a local backup of the data.
2) Add number of days to delete as a parameter.
3) Add a nightly mode (parameters are given as enviorment variables).

* Write the backedup records in one line.

* New implmentation:
Delete all entries in one command.

* Fix enviorment variable for nightly mode.
  • Loading branch information
tzachi-dar authored Dec 18, 2021
1 parent d15d8d4 commit d8af80b
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions bin/ns-delete-old-devicestatus.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

source $(dirname $0)/oref0-bash-common-functions.sh || (echo "ERROR: Failed to run oref0-bash-common-functions.sh. Is oref0 correctly installed?"; exit 1)

usage "$@" <<EOF
Usage: $self --find <NIGHTSCOUT_HOST> <API_SECREAT> <number_of_days>- No-op version, find out what delete would do.
$self delete <NIGHTSCOUT_HOST> <API_SECREAT> <number_of_days> - move entries from NIGHTSCOUT_HOST devicestatus collection to "$HOME/myopenaps/backup
$self nightly <number_of_days> - move entries from NIGHTSCOUT_HOST devicestatus collection to "$HOME/myopenaps/backup
EOF

function write_backup() {
json -a -o jsony-0 >> $BACKUP_DIR/devicestatus.txt
}

export API_SECRET
test -n "$3" && API_SECRET=$(nightscout hash-api-secret $3)
test -n "$4" && NUM_DAYS=$4
BACKUP_DIR="$HOME/myopenaps"/backup
mkdir -p $BACKUP_DIR

ENDPOINT=$2/api/v1/devicestatus

if [ $1 = "nightly" ]; then
test -n "$2" && NUM_DAYS=$2
ENDPOINT=$NIGHTSCOUT_HOST/api/v1/devicestatus
fi

if [[ -z "$API_SECRET" || -z "$NUM_DAYS" ]] ; then
test -z "$API_SECRET" && echo API_SECRET undefined.
test -z "$NUM_DAYS" && echo NUM_DAYS undefined.
print_usage
exit 1;
fi

date_string=$(date -d "-$NUM_DAYS days" +%Y-%m-%d)
fetch_cmd="curl --compressed -s -g $ENDPOINT.json?find\[created_at\]\[\\"\$"lte\]=$date_string\&count=100000"
delete_cmd="curl -X DELETE -H \"API-SECRET: $API_SECRET\" -s -g $ENDPOINT.json?find\[created_at\]\[\\"\$"lte\]=$date_string\&count=100000"

case "$1" in
--find)
echo $fetch_cmd
echo $delete_cmd
;;
delete)
#echo $fetch_cmd
#echo $delete_cmd
eval $fetch_cmd | write_backup
eval $delete_cmd
;;
nightly)
#echo $fetch_cmd
#echo $delete_cmd
eval $fetch_cmd | write_backup
eval $delete_cmd
;;
*|help|--help|-h)
print_usage
exit 1;
;;
esac

0 comments on commit d8af80b

Please sign in to comment.