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

Replace expensive node json calls by jq #1342

Closed
wants to merge 4 commits into from
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
7 changes: 6 additions & 1 deletion bin/monitor-xdrip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ date
cp -rf xdrip/glucose.json xdrip/last-glucose.json
curl --compressed -s http://localhost:5000/api/v1/entries?count=288 | jq '[ .[] | .glucose = .sgv ]' > xdrip/glucose.json
if ! cmp --silent xdrip/glucose.json xdrip/last-glucose.json; then
if cat xdrip/glucose.json | json -c "minAgo=(new Date()-new Date(this.dateString))/60/1000; return minAgo < 10 && minAgo > -5 && this.glucose > 38" | grep -q glucose; then
if cat xdrip/glucose.json \
| jq \
--arg AT_LATEST "`date -u +'%Y-%m-%dT%H:%M:%S.000Z' --date '5 minutes'`"\
--arg AT_EARLIEST "`date -u +'%Y-%m-%dT%H:%M:%S.000Z' --date '-10 minutes'`"\
'[.[] | (select (.dateString >= $AT_EARLIEST)) | (select (.dateString <= $AT_LATEST)) | (select (.glucose > 38))]' \
| grep -q glucose; then
cp -up xdrip/glucose.json monitor/glucose.json
else
echo No recent glucose data downloaded from xDrip.
Expand Down
10 changes: 6 additions & 4 deletions bin/oref0-ns-loop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ function glucose_fresh {
}

function find_valid_ns_glucose {
# TODO: use jq for this if possible
cat cgm/ns-glucose.json | json -c "minAgo=(new Date()-new Date(this.dateString))/60/1000; return minAgo < 10 && minAgo > -5 && this.glucose > 38"
jq \
--arg AT_LATEST "`date -u +'%Y-%m-%dT%H:%M:%S.000Z' --date '5 minutes'`"\
--arg AT_EARLIEST "`date -u +'%Y-%m-%dT%H:%M:%S.000Z' --date '-10 minutes'`"\
'[.[] | (select (.dateString >= $AT_EARLIEST)) | (select (.dateString <= $AT_LATEST)) | (select (.glucose > 38))]' cgm/ns-glucose.json
}

function ns_temptargets {
Expand Down Expand Up @@ -194,11 +196,11 @@ function format_ns_status {
fi
}

#openaps format-latest-nightscout-treatments && test $(json -f upload/latest-treatments.json -a created_at eventType | wc -l ) -gt 0 && (ns-upload $NIGHTSCOUT_HOST $API_SECRET treatments.json upload/latest-treatments.json ) || echo \\\"No recent treatments to upload\\\"
#openaps format-latest-nightscout-treatments && test $(jq -r '.[] |.created_at + " " + .eventType' upload/latest-treatments.json | wc -l ) -gt 0 && (ns-upload $NIGHTSCOUT_HOST $API_SECRET treatments.json upload/latest-treatments.json ) || echo \\\"No recent treatments to upload\\\"
function upload_recent_treatments {
#echo Uploading treatments
format_latest_nightscout_treatments || die "Couldn't format latest NS treatments"
if test $(json -f upload/latest-treatments.json -a created_at eventType | wc -l ) -gt 0; then
if test $(jq -r '.[] |.created_at + " " + .eventType' upload/latest-treatments.json | wc -l ) -gt 0; then # this could probably just be a grep
ns-upload $NIGHTSCOUT_HOST $API_SECRET treatments.json upload/latest-treatments.json | colorize_json || die "Couldn't upload latest treatments to NS"
else
echo "No new treatments to upload"
Expand Down