Last active
August 29, 2015 14:01
-
-
Save soldair/c11d6ae6f4bead140838 to your computer and use it in GitHub Desktop.
posting data to couch db from the pinoccio api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "pinoccio-couch-example", | |
"description": "couch example for saving pinoccio stats", | |
"version": "0.0.0", | |
"repository": { | |
"url":"https://gist.github.com/c11d6ae6f4bead140838.git" | |
}, | |
"main": "index.js", | |
"scripts": { | |
"test": "tape test/*.js" | |
}, | |
"author": "Ryan Day", | |
"dependencies": { | |
"nano": "~5.8.0", | |
"pinoccio": "~0.1.3" | |
}, | |
"devDependencies": { | |
"tape": "~2.3.2" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this script will run forever insert all temp reports from scout 1 in troop 1 into couch | |
var path = require('path'); | |
var fs = require('fs'); | |
// the nodejs pinoccio api client | |
// replace this read only token for account id 1 with your own auth token. | |
var pinoccio = require('pinoccio')("f46d89506224ac3c40f4a4775fc00366"); | |
// if you run this script with this example token remember to delete the values in the database and delete the time file when you change it | |
// nano is a minimal couchdb driver. | |
// assumes you have already created the pinoccio database | |
// you can check it out in futon http://localhost:5984/_utils | |
var db = require('nano')('http://localhost:5984/pinoccio'); | |
var troop = 3; | |
var scout = 8; | |
var report = "temp"; // export temperature report. | |
// start time in ms. start from beginning of time 1970 | |
var start = 1; | |
var timeFilename = path.join(__dirname,"pinoccio-time-file-"+troop+':'+scout+':'+report); | |
// read time file at startup to see where i left off. | |
if(fs.existsSync(timeFilename)){ | |
start = +(fs.readFileSync(timeFilename)+'');// read the file cast it to a sting then a number. the timestamp. | |
start += 0.001;// i dont want the last data point i got i want the next after it. | |
} | |
// any change that is reported from any of your scouts is emitted as "data" from the sync stream | |
// https://docs.pinocc.io/api.html#realtime-stream-of-changes | |
pinoccio.stats({troop:troop,scout:scout,report:report,start:start}).on('data',function(data){ | |
data = data.data;// get at the data in the data. =/ | |
var key = troop+':'+scout+':'+report+':'+data.time; | |
db.insert(data,key,function(err){ | |
// this should not get update conflicts but depending on your key structure you may need to handle them | |
if(err) { | |
if(err.error == 'conflict'){ | |
console.log('stat has already been copied to the database.'); | |
} else { | |
throw err; | |
} | |
} | |
console.log('inserted ',key,'into couch.'); | |
updateTime(data.time);// update time so we dont try this again. | |
}); | |
}).on('error',function(e){ | |
console.log('stats stream error: ',e.message+''); | |
throw e; | |
}).on('end',function(){ | |
console.log('end!'); | |
}); | |
var updatePending = false; | |
// update the time file but only one concurrent write at a time. | |
function updateTime(time){ | |
if(!time) return; | |
if(updatePending) { | |
updatePending = time; | |
return; | |
} | |
updatePending = true; | |
fs.writeFile(timeFilename,time+'',function(err){ | |
if(err) throw err;// if i cant save the last position crash. | |
var change = updatePending; | |
updatePending = false; | |
if(change !== true){ | |
updateTime(change); | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment