Skip to content

Instantly share code, notes, and snippets.

@jensb
Last active January 29, 2023 11:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jensb/9efa234b80024a0e335de760d9a4f3aa to your computer and use it in GitHub Desktop.
Save jensb/9efa234b80024a0e335de760d9a4f3aa to your computer and use it in GitHub Desktop.
InfluxDB measurement export-edit-reimport hack
#!/bin/bash
#
# Perform Influx v2 query with correct headers as CSV, open editor for changes, and write back to InfluxDB.
#
# Usage: influx-edit.sh <Org/Bucket> <flux_measurement> <from> <to>
# Prerequisites: Need to put login data (host, Org, token) into $HOME/.influxdbv2/configs file.
#
# Author: Jens Benecke <jens-github@spamfreemail.de>, 2022.
# Published as public domain, as far as legally possible.
if test "$1" == ""; then
echo "Usage: $0 Org/Bucket Measurement(energymeter_absolute) StartTime(-24h) EndTime(now)"
echo "Example: $0 Home watermeter_value -48h -24h"
echo "Set DEBUG=1 to see debug output."
exit 1
fi
ORG="${1-Home}"
MEAS=${2-mqtt.0.energymeter.value}
START="${3--24h}"
STOP="${4-now()}"
QUERY="from(bucket: \"$ORG\") |> range(start: ${START}, stop: ${STOP}) |> filter(fn: (r) => r._measurement == \"$MEAS\")"
TMPFILE=/tmp/influxedit-$$.tmp
test -z "$DEBUG" || echo "Executing: influx query --org $ORG --raw $QUERY"
# --http-debug
influx query --org $ORG --raw "$QUERY" > $TMPFILE
if test $? == 0 ; then
MD51="$(md5sum $TMPFILE)"
vim $TMPFILE
MD52="$(md5sum $TMPFILE)"
if [ "$MD51" != "$MD52" ] ; then
echo -n "Changes detected. Update now? " ; read YN
if test "$YN" = "y" ; then
#influx write dryrun -f $TMPFILE
influx write --org $ORG --bucket $ORG --format csv -f $TMPFILE
fi
else
echo "No changes, not updating."
fi
echo -n "Remove $TMPFILE? " ; read YN
test "$YN" = "y" && rm -v $TMPFILE
echo "Done."
else
echo "An error occured during query. Please fix and retry."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment