Skip to content

Instantly share code, notes, and snippets.

@juanmf
Created March 4, 2017 07:40
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 juanmf/441bbff38cb50b01349b5218eac06036 to your computer and use it in GitHub Desktop.
Save juanmf/441bbff38cb50b01349b5218eac06036 to your computer and use it in GitHub Desktop.
Webtask.io little hack
/**
* This script can be automatically triggered with IFTTT Stocks trigger and should
* be called with the following query string params:
*
* inputCurrency={{InputCurrency}}
* outputCurrency={{OutputCurrency}}
* exchangeRate={{ExchangeRate}}
* infoUrl={{InfoUrl}}
* checkTime={{CheckTime}}
*
* And it will create a document in a mongoDB as per ctx.data.MONGO_URL
* connection string.
*
* To create a webtask with it make sure you provide a secret as follows:
* </pre>
* $ wt create --secret MONGO_URL=mongodb://... daily-exchange.js
* https://....run.webtask.io/exchange-analytic
* </pre>
* Keep in mind that you will need this URL to configure the IFTTT hook.
*
* @link https://auth0.com/blog/if-this-then-node-dot-js-extending-ifttt-with-webtask-dot-io/
* @link https://ifttt.com/applets/50581531d For a working IFTTT trigger if Stock then Maker
*/
var MongoClient = require('mongodb').MongoClient;
/**
* creates a doc in a mongoDB "exchange" collection for given Currencies and ratio.
*
* @param {db object} db Mongo db associated to ctx.data.MONGO_URL
* @param {object} data contains all necessary info for the doc creation.
*/
var persist = function (db, data) {
console.log(data);
db.collection('exchange').insertOne(
{
"inputCurrency" : data.inputCurrency,
"outputCurrency" : data.outputCurrency,
"exchangeRate" : parseFloat(data.exchangeRate),
"infoUrl" : data.infoUrl,
"checkTime" : data.checkTime,
"timestamp" : Date.parse(data.checkTime.split(' at ')[0] + ' (en-US)')
},
{"forceServerObjectId": true}
).then(r => console.log(r));
};
var cbRunner = function (err, cb, main) {
if (err) {
console.log("cbRunner error received");
return cb(err);
}
main();
return cb(null, "Success!");
};
module.exports = function (ctx, done) {
return MongoClient.connect(
ctx.data.MONGO_URL,
(err, db) => cbRunner(err, done, () => persist(db, ctx.data))
);
};
/**
* This is an endpoint that can take useful insigths in gathered data.
* Currently it shows last week and last month revenue for a given Currency pair.
* Note that you must create one IFTTT hook for each Currency pair you want to keep updated.
* Currently it shows the calculated values as a JSON:
*
* {"weekRevenue":-0.058565..,"monthRevenue":-0.07215..}
* The revnue is relative to one, with the current formula, a value of 1 means 100% revenue.
*
* This endpoint can be triggereg manually with the browser or curl, or by a REST client.
*/
var MongoClient = require('mongodb').MongoClient;
var revenue = function (db, inputCurrency, outputCurrency) {
var now = new Date();
var today = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0);
var lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7, 0, 0, 0);
var lastMonth = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate(), 0, 0, 0);
var docsArr = fetchDocs(db, today, lastWeek, lastMonth, inputCurrency, outputCurrency);
return docsArr.then(function(docs) {
var weekRevenue, monthRevenue;
if (docs[0] && docs[1]) {
weekRevenue = (docs[0].exchangeRate / docs[1].exchangeRate) - 1;
}
if (docs[0] && docs[2]) {
monthRevenue = (docs[0].exchangeRate / docs[2].exchangeRate) - 1;
}
var out = {
"weekRevenue": weekRevenue,
"monthRevenue": monthRevenue
};
console.log(docsArr);
console.log(out);
return out;
});
};
function fetchDocs (db, today, lastWeek, lastMonth, inputCurrency, outputCurrency){
var query = {
"$or" : [
{"timestamp" : {$eq: Date.parse(today)}},
{"timestamp" : {$eq: Date.parse(lastWeek)}},
{"timestamp" : {$eq: Date.parse(lastMonth)}}
],
"inputCurrency" : {"$eq": inputCurrency},
"outputCurrency" : {"$eq": outputCurrency}
};
console.log(query, today, Date.parse(today), Date.parse(lastWeek), Date.parse(lastMonth));
return db.collection('exchange').find(query)
.sort({"timestamp": -1})
.toArray();
}
var cbRunner = function (err, cb, main) {
if (err) {
console.log("cbRunner error received");
return cb(err);
}
main().then(out => cb(null, out));
};
module.exports = function (ctx, done) {
return MongoClient.connect(
ctx.data.MONGO_URL,
(err, db) => cbRunner(err, done, () => revenue(db, ctx.data.inputCurrency, ctx.data.outputCurrency))
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment