Skip to content

Instantly share code, notes, and snippets.

@johan--
Forked from soldair/package.json
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johan--/b03279488661a443fbfd to your computer and use it in GitHub Desktop.
Save johan--/b03279488661a443fbfd to your computer and use it in GitHub Desktop.
{
"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 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