Some new stuff

This commit is contained in:
Martin Donnelly 2017-08-20 23:20:43 +01:00
parent f20f9bc8fc
commit fa6dede096
10 changed files with 258 additions and 169 deletions

View File

@ -1932,14 +1932,17 @@ li {
border: solid 1px #80007e;
background-color: #ffffff; }
#btc, #fx {
font-size: 85%; }
#btc, #fx, #trend {
font-size: 85%;
}
.up, .ontime {
color: #4CAF50 !important; }
.up, .ontime, .trendUp {
color: green !important;
}
.down, .delayed {
color: #F44336 !important; }
.down, .delayed, .trendDown {
color: red !important;
}
.nochange {
color: #000000; }
@ -1949,3 +1952,10 @@ li {
background-color: #EEEEEE;
font-family: monospace;
white-space: pre; }
.trendUp:before {
content: "▲";
}
.trendDown:before{content:'▼'}

View File

@ -44,15 +44,15 @@ li {
background-color: #ffffff;
}
#btc, #fx {
#btc, #fx, #trend {
font-size: 85%;
}
.up, .ontime {
.up, .ontime, .trendUp {
color: mui-color('green') !important;
}
.down, .delayed {
.down, .delayed, .trendDown {
color: mui-color('red') !important;
}
@ -65,3 +65,9 @@ li {
font-family: monospace;
white-space: pre;
}
.trendUp:before {
content: "";
}
.trendDown:before{content:''}

View File

@ -3,170 +3,189 @@
*/
let BitcoinModel = Backbone.Model.extend({
initialize: function () {
this.set('url', '/btc');
this.set('balanceUrl', '/balance');
let data = {
lastGBP: 0.0,
lastUSD: 0.0,
lows: {gbp: 0, usd: 0},
highs: {gbp: 0, usd: 0},
eclass: '',
balance: 0.0
};
this.set('btcdata', data);
this.set('balance', 0);
this.update();
this.updateHourly();
},
update: function () {
this.getBTC();
initialize: function () {
this.set('url', '/btc');
this.set('balanceUrl', '/balance');
let data = {
lastGBP: 0.0,
lastUSD: 0.0,
lows: {gbp: 0, usd: 0},
highs: {gbp: 0, usd: 0},
eclass: '',
balance: 0.0,
trend: 0
};
this.set('btcdata', data);
this.set('balance', 0);
this.update();
this.updateHourly();
},
update: function () {
this.getBTC();
//this.getBalance();
const now = new Date();
const mod = 300000 - (now.getTime() % 300000);
const now = new Date();
const mod = 300000 - (now.getTime() % 300000);
let btcupdateFn = function() {
this.update();
};
setTimeout(btcupdateFn.bind(this), mod + 10);
},
updateHourly: function () {
let btcupdateFn = function() {
this.update();
};
setTimeout(btcupdateFn.bind(this), mod + 10);
},
updateHourly: function () {
this.getBalance();
const now = new Date();
const mod = 3600000 - (now.getTime() % 3600000);
this.getBalance();
const now = new Date();
const mod = 3600000 - (now.getTime() % 3600000);
let btcupdateFn = function() {
this.update();
};
setTimeout(btcupdateFn.bind(this), mod + 10);
},
recalc: function () {
let data = this.get('btcdata');
let lastGBP = data.lastGBP;
let lastUSD;
let g = data.gbp;
let u = data.usd;
let lows = data.lows;
let highs = data.highs;
let eclass = data.eclass;
let balance = data.balance;
if (g !== undefined) {
if (data.lastGBP !== 0) {
if (g > lastGBP) {
eclass = 'up';
} else {
eclass = 'down';
}
} else {
lows.gbp = g;
lows.usd = u;
let btcupdateFn = function() {
this.update();
};
setTimeout(btcupdateFn.bind(this), mod + 10);
},
recalc: function () {
let data = this.get('btcdata');
let lastGBP = data.lastGBP;
let lastUSD;
let g = data.gbp;
let u = data.usd;
let lows = data.lows;
let highs = data.highs;
let eclass = data.eclass;
let balance = data.balance;
let trend = data.trend;
highs.gbp = g;
highs.usd = u;
}
lastGBP = g;
lastUSD = u;
if ((trend === undefined) || ( trend === null)) trend = 1;
if (g < lows.gbp) lows.gbp = g;
if (u < lows.usd) lows.usd = u;
if (highs.gbp < g) highs.gbp = g;
if (highs.usd < u) highs.usd = u;
data = {
lastGBP,
lastUSD,
lows,
highs,
eclass,
balance
};
if (g !== undefined) {
if (data.lastGBP !== 0)
if (g > lastGBP) {
eclass = 'up';
} else {
eclass = 'down';
}
data.stub = Math.random(Number.MAX_SAFE_INTEGER).toString(32);
else {
lows.gbp = g;
lows.usd = u;
this.set('btcdata', data);
// total = myBTC * g;
},
getBTC: function () {
let self = this;
let url = this.get('url');
console.log('>> getBTC');
$.ajax({
type: 'GET',
url: url,
data: '',
dataType: 'json',
timeout: 10000,
//contentType: ('application/json'),
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
},
success: function (data) {
let gbp = data.bpi.GBP.rate_float,
usd = data.bpi.USD.rate_float;
let btcdata = self.get('btcdata');
btcdata.gbp = gbp;
btcdata.usd = usd;
self.set('btcdata', btcdata);
self.recalc();
},
error: function (xhr, type) {
console.log('ajax error');
console.log(xhr);
console.log(type);
}
});
},
getBalance: function() {
let self = this;
let url = this.get('balanceUrl');
$.ajax({
type: 'GET',
url: url,
data: '',
dataType: 'json',
timeout: 10000,
//contentType: ('application/json'),
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
},
success: function (data) {
let balance = data.balance;
highs.gbp = g;
highs.usd = u;
}
lastGBP = g;
lastUSD = u;
self.set('balance', balance);
self.recalc();
},
error: function (xhr, type) {
console.log('ajax error');
console.log(xhr);
console.log(type);
}
});
if (g < lows.gbp) lows.gbp = g;
if (u < lows.usd) lows.usd = u;
if (highs.gbp < g) highs.gbp = g;
if (highs.usd < u) highs.usd = u;
data = {
lastGBP,
lastUSD,
lows,
highs,
eclass,
balance,
trend
};
}
data.stub = Math.random(Number.MAX_SAFE_INTEGER).toString(32);
this.set('btcdata', data);
// total = myBTC * g;
},
getBTC: function () {
let self = this;
let url = this.get('url');
$.ajax({
type: 'GET',
url: url,
data: '',
dataType: 'json',
timeout: 10000,
//contentType: ('application/json'),
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
},
success: function (data) {
let gbp = data.bpi.GBP.rate_float,
usd = data.bpi.USD.rate_float;
let trend = data.trend;
let btcdata = self.get('btcdata');
btcdata.gbp = gbp;
btcdata.usd = usd;
btcdata.trend = trend;
self.set('btcdata', btcdata);
self.recalc();
},
error: function (xhr, type) {
console.log('ajax error');
console.log(xhr);
console.log(type);
}
});
},
getBalance: function() {
let self = this;
let url = this.get('balanceUrl');
$.ajax({
type: 'GET',
url: url,
data: '',
dataType: 'json',
timeout: 10000,
//contentType: ('application/json'),
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
},
success: function (data) {
let balance = data.balance;
self.set('balance', balance);
self.recalc();
},
error: function (xhr, type) {
console.log('ajax error');
console.log(xhr);
console.log(type);
}
});
}
});
let Bitcoin = Backbone.View.extend({
tagName: 'div',
initialize: function () {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
this.$btc = $('#btc');
},
render: function () {
let btcdata = this.model.get('btcdata');
let balance = this.model.get('balance');
tagName: 'div',
initialize: function () {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
this.$btc = $('#btc');
this.$trend = $('#trend');
},
render: function () {
let btcdata = this.model.get('btcdata');
let balance = this.model.get('balance');
//console.log(`Balance: ${btcdata.balance.toFixed(4)}`);
//console.log(btcdata.lastGBP);
let owned = parseFloat(btcdata.lastGBP) * parseFloat(balance);
let owned = parseFloat(btcdata.lastGBP) * parseFloat(balance);
//console.log(`owned: ${owned}`);
let title = 'High: $' + parseFloat(btcdata.highs.usd.toFixed(2)) + ' / Low $' + parseFloat(btcdata.lows.usd.toFixed(2));
this.$btc.removeClass();
this.$btc.addClass(btcdata.eclass);
this.$btc.html(`&#36;${parseFloat(btcdata.lastUSD.toFixed(2)) } / &pound;${parseFloat(btcdata.lastGBP.toFixed(2))} <div>&#8383;${balance} &pound;${parseFloat(owned.toFixed(2))}</div>` );
this.$btc.prop('title', title);
}
let title = 'High: $' + parseFloat(btcdata.highs.usd.toFixed(2)) + ' / Low $' + parseFloat(btcdata.lows.usd.toFixed(2));
let trendClass = '';
if (btcdata.trend > 1.00)
trendClass = 'trendUp';
else if (btcdata.trend < 1.00)
trendClass = 'trendDown';
else
trendClass = '';
this.$trend.removeClass();
this.$trend.addClass(trendClass);
this.$btc.removeClass();
this.$btc.addClass(btcdata.eclass);
this.$btc.html(`&#36;${parseFloat(btcdata.lastUSD.toFixed(2)) } / &pound;${parseFloat(btcdata.lastGBP.toFixed(2))} <div>&#8383;${balance} &pound;${parseFloat(owned.toFixed(2))}</div>` );
this.$btc.prop('title', title);
}
});

52
app/libs/float.js Normal file
View File

@ -0,0 +1,52 @@
/**
* Credit: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
*/
var decimalAdjust = function(type, value, exp) {
value = value || 0;
exp = exp === undefined ? -2 : -Math.abs(exp);
// If the exp is undefined or zero...
if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value);
}
value = +value;
exp = +exp;
// If the value is not a number or the exp is not an integer...
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
// Shift
value = value.toString().split('e');
value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
// Shift back
value = value.toString().split('e');
return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
};
var round = function(v, d) {
return decimalAdjust('round', v, d);
};
module.exports = {
round: round,
floor: function(v, d) {
return decimalAdjust('floor', v, d);
},
ceil: function(v, d) {
return decimalAdjust('ceil', v, d);
},
equals: function(a, b, d) {
return round(a, d) === round(b, d);
},
lessThan: function(a, b, d) {
return round(a, d) < round(b, d);
},
lessThanOrEquals: function(a, b, d) {
return round(a, d) <= round(b, d);
},
greaterThan: function(a, b, d) {
return round(a, d) > round(b, d);
},
greaterThanOrEquals: function(a, b, d) {
return round(a, d) >= round(b, d);
}
};

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -16,6 +16,8 @@
"tests"
],
"dependencies": {
"moment": "^2.18.1"
"moment": "^2.18.1",
"mui": "^0.9.21",
"float": "^1.0.2"
}
}

View File

@ -2,8 +2,9 @@ const http = require('http');
const LimitedArray = require('limitedarray');
const trend = require('trend');
const logger = require('log4js').getLogger('fx');
const delay = (60000 * 30);
let fxCache = {};
let history = new LimitedArray(96); // one days worth in 5 minute chunks
let history = new LimitedArray(48); // one days worth in 5 minute chunks
function getFx() {
logger.info('FX request');
@ -37,17 +38,16 @@ function getFx() {
history.push(a.rates.GBP);
fxCache.history = history.get();
fxCache.trend = trend(fxCache.history, {avgPoints: 24});
fxCache.trend = trend(fxCache.history, {avgPoints: 12});
});
}
function updateFX() {
logger.warn('updateBitcoin');
getFx();
const now = new Date();
const mod = (60000 * 15) - (now.getTime() % (60000 * 15));
const mod = delay - (now.getTime() % delay);
let fxUpdateFn = () => {
updateFX();

View File

@ -58,7 +58,7 @@
</div>
</div>
<div class="mui-col-md-4">
<div class="mui--text-title mui-text-black">Bitcoin <span id="btc"></span></div>
<div class="mui--text-title mui-text-black">Bitcoin <span id="trend"> </span><span id="btc"></span></div>
<ul>
<li><a href="https://www.bitstamp.net">Bitstamp</a></li>
<li><a href="https://www.kraken.net">Kraken</a></li>

View File

@ -133,7 +133,7 @@ app.route('/poly').get(polys);
app.get('/slack', function(req, res) {
res.render('pages/slackV2-min');
//res.render('pages/slackV2');
// res.render('pages/slackV2');
});
app.get('/temp', function(req, res) {