Created
February 18, 2017 00:57
-
-
Save wtsnz/9394638ebc7fde8ea3a63678efaa58ae to your computer and use it in GitHub Desktop.
Fabric to Pushover: New Users
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
var Pushover = require('node-pushover'); | |
var moment = require('moment'); | |
var request = require('request'); | |
// Fill out with your secrets | |
var secrets = { | |
pushover: { | |
token: '', | |
user: '' | |
}, | |
fabric: { | |
organizationId: '', | |
applicationId: '', | |
cookie: '', | |
crashlyticsDeveloperToken: '' | |
} | |
} | |
if (!Array.prototype.last){ | |
Array.prototype.last = function(){ | |
return this[this.length - 1]; | |
}; | |
}; | |
var push = new Pushover({ | |
token: secrets.pushover.token, | |
user: secrets.pushover.user | |
}); | |
function requestOptions(fromDate, toDate) { | |
return { | |
url: 'https://fabric.io/api/v2/organizations/' + secrets.fabric.organizationId + '/apps/' + secrets.fabric.applicationId + '/growth_analytics/daily_new.json?start=' + fromDate + '&end=' + toDate + '&transformation=seasonal', | |
method: 'GET', | |
headers: { | |
'Cookie': secrets.fabric.cookie, | |
'X-CRASHLYTICS-DEVELOPER-TOKEN': secrets.fabric.crashlyticsDeveloperToken | |
} | |
} | |
} | |
function sendNotification(newUserCount) { | |
push.send("New Road Code User!", "There are now " + newUserCount + " new users today.", function (err, res){ | |
if (err) { | |
console.log("We have an error:"); | |
console.log(err); | |
console.log(err.stack); | |
} else { | |
console.log("Message send successfully"); | |
console.log(res); | |
} | |
}); | |
} | |
// Save a variable to the last known user count | |
var lastNewUserCount = 1 | |
function updateLastUserCount() { | |
// Fetch the UTC date of today + 1 | |
let date = moment().utc().startOf('date'); | |
let toDate = Math.floor(date / 1000) | |
let fromDate = toDate - 60 * 60 * 24 * 5 // Last 5 days.. | |
let options = requestOptions(fromDate, toDate) | |
console.info("Checking user count at " + moment().format('MMMM Do YYYY, h:mm:ss a')) | |
request(options, function(error, response, body) { | |
if (!error && response.statusCode == 200) { | |
try { | |
var json = JSON.parse(body) | |
var lastEntry = json.series.last().last() | |
console.info("New user count: " + lastEntry); | |
// Update lastUserCount & send notification if the count is different | |
// It can be different when it ticks over to a new day. | |
if (lastNewUserCount != lastEntry) { | |
sendNotification(lastEntry) | |
lastNewUserCount = lastEntry | |
} else { | |
console.info("User count not changed") | |
} | |
} catch (e) { | |
console.error("Failed to parse json response") | |
console.error(e) | |
} | |
} else { | |
console.error("Invalid response status code") | |
} | |
}) | |
} | |
setInterval(function () { | |
updateLastUserCount() | |
}, 1000 * 60 * 5); | |
// Perform the initial update | |
updateLastUserCount() |
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": "pushover-monitor", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"moment": "^2.17.1", | |
"node-pushover": "^0.2.2", | |
"request": "^2.79.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment