Uncategorized

InitialState: writing data from the past and bash arrays

Turns out the documentation on Initialstate is, to be blunt, lacking. I complained that it took the current time for events, therefore making impossible for me to send the data I have accumulated before. I was wrong.

After one message to their tech support, I got the answer I needed. It’s not just possible, but easy, to send data “from the past”. This is how I did it.

Oh, and I learned to use BASH arrays, which I never bothered with before. I have been using the “cut” utility since the 80s, before bash even existed. So, just have a look at the code here 🙂

I have my uptime data in a SQL table. So, I want to grab this data and send it to initialstate.com. The source is something like this:

#!/bin/bash
BUCKET_KEY="blablabla"
ACCESS_KEY="myaccesskey"
I=0
for LINE in $(sqlite3 uptime.db "SELECT * FROM solaruptime"); do
    #    line looks like 2016-07-13|20:05|75627
    # Bash has an "internal field separator" and I have 
    # to redefine to every turn, or else it gets lost. Go figure.
    IFS="|"
    # Converts the string into an array with the IFS separator
    DATE=( $LINE )
    REQUEST="[ { \"iso8601\": \"${DATE[0]}T${DATE[1]}\", \
    \"key\": \"Uptime\", \"value\": \"${DATE[2]}\"} ]"

     curl --include --request POST \
     --header "Content-Type: application/json" \
     --header "X-IS-AccessKey: $ACCESS_KEY" \
     --header "X-IS-BucketKey: $BUCKET_KEY" \
     --header "Accept-Version: 0.0.1" \
     --data-binary $REQUEST \
     'https://groker.initialstate.com/api/events'
     I=$[I+1]
     if [ "$I" -eq "3000" ]; then
        exit
     fi
done

So, this stupid script basically sends the first 3000 lines of my database to initialstate, with the proper time.

The thing is self-explanatory . If you need help, let me know. I am glad to explain everything.

Advertisements
Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s