Skip to content

Commit

Permalink
add charts to revenue modules
Browse files Browse the repository at this point in the history
  • Loading branch information
selaux committed Apr 6, 2014
1 parent d2004b2 commit ea09651
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 28 deletions.
27 changes: 17 additions & 10 deletions frontend/stylesheets/style.styl
Expand Up @@ -17,6 +17,19 @@ a
dt
font-weight: normal

.graph-element
position: relative
.graph
height: 120px
margin-bottom: 20px
strong
position: absolute
left: 20px
top: 5px
opacity: 1
&:hover strong
opacity: 0

#modules
section
position: relative
Expand All @@ -39,16 +52,7 @@ dt
font-size: 10px
.miner
.hashrate
position: relative
.graph
height: 120px
strong
position: absolute
left: 20px
top: 5px
opacity: 1
&:hover strong
opacity: 0
@extend .graph-element
.devices
margin-bottom: 20px
table
Expand All @@ -60,3 +64,6 @@ dt
width: 10%
&:nth-child(3)
width: 30%
.revenue
.value
@extend .graph-element
10 changes: 9 additions & 1 deletion lib/modules/revenue/solo.js
Expand Up @@ -2,12 +2,18 @@

var _ = require('lodash'),

setWithHistoricalData = require('../../utils/setWithHistoricalData'),
Module = require('../../Module');

module.exports = Module.extend({

viewId: 'revenue',

defaults: {
chartTimespan: 14 * 24 * 60 * 60 * 1000,
chartPrecision: 60 * 60 * 1000
},

initialize: function () {
var self = this,
miner = self.config.miner;
Expand Down Expand Up @@ -62,6 +68,8 @@ module.exports = Module.extend({
currency: currency,
interval: 'Day'
});
}
},

set: setWithHistoricalData([ 'value' ], Module.prototype.set)

});
9 changes: 7 additions & 2 deletions lib/modules/revenue/triplemining.js
Expand Up @@ -4,14 +4,17 @@ var async = require('async'),
request = require('request'),
_ = require('lodash'),

setWithHistoricalData = require('../../utils/setWithHistoricalData'),
Module = require('../../Module');

module.exports = Module.extend({

viewId: 'revenue',

defaults: {
interval: 3600000
interval: 3600000,
chartTimespan: 14 * 24 * 60 * 60 * 1000,
chartPrecision: 60 * 60 * 1000
},

initialize: function () {
Expand Down Expand Up @@ -99,6 +102,8 @@ module.exports = Module.extend({
value: this.tripleminingData.hashrate * 1e6 * this.technicalData.probability * this.tripleminingData.estimatedPayout * bid * 24 * 3600,
interval: 'Day'
});
}
},

set: setWithHistoricalData([ 'value' ], Module.prototype.set)

});
79 changes: 77 additions & 2 deletions lib/views/revenue.js
@@ -1,7 +1,82 @@
'use strict';

var View = require('../View');
var Rickshaw = require('rickshaw'),
_ = require('lodash'),
timeHelper = require('../handlebars/helpers/time'),

View = require('../View');

module.exports = View.extend({
template: 'revenue'
template: 'revenue',

graph: null,

getChartData: function () {
return this.module.get('historicalData').map(function (measurement) {
return {
x: (measurement.timestamp / 1000),
y: measurement.value
};
});
},

initializeGraph: function () {
var module = this.module;

this.graph = new Rickshaw.Graph({
element: this.$('.graph')[0],
height: 120,
renderer: 'area',
interpolation: 'linear',
stroke: true,
series: [
{
color: '#cae2f7',
name: 'Revenue',
data: this.getChartData()
}
]
});
this.detail = new Rickshaw.Graph.HoverDetail({
graph: this.graph,
yFormatter: function (value) {
return value.toFixed(5) + ' ' + module.get('currency');
},
xFormatter: function (value) {
return timeHelper(value * 1000);
}
});
this.xAxis = new Rickshaw.Graph.Axis.Time({
graph: this.graph
});
this.yAxis = new Rickshaw.Graph.Axis.Y({
graph: this.graph
});
},

updateGraph: function () {
var chartData = this.getChartData();

this.graph.min = _(chartData).pluck('y').min().value() * 0.99;
this.graph.max = _(chartData).pluck('y').max().value() * 1.01;
this.graph.series[0].data = this.getChartData();

this.xAxis.render();
this.yAxis.render();
this.graph.update();
},

postRender: function () {
var graphElement = this.$('.graph'),
graphShouldBeRendered = graphElement.length > 0 && this.module.get('historicalData');

if (graphShouldBeRendered) {
if (this.graph) {
graphElement.replaceWith(this.graph.element);
} else {
this.initializeGraph();
}
this.updateGraph();
}
}
});
12 changes: 8 additions & 4 deletions templates/revenue.hbs
Expand Up @@ -3,8 +3,12 @@
<h2 class="panel-title col-xs-6">{{ title }}</h2>
</div>
</header>
<dl class="panel-body dl-horizontal">
<dt class="big-label">Revenue:</dt>
<dd class="big">{{number value}} {{currency}}/{{interval}}</dd>
</dl>
<div class="panel-body dl-horizontal">
<div class="row">
<div class="value col-xs-12 col-md-3">
<div class="graph"></div>
<strong>Current Revenue: {{number value}} {{currency}}/{{interval}}</strong>
</div>
</div>
</div>
<footer class="text-muted">Updated: {{time lastUpdated}}</footer>
21 changes: 16 additions & 5 deletions test/specs/lib/modules/revenue/soloSpec.js
Expand Up @@ -4,6 +4,7 @@ var EventEmitter = require('events').EventEmitter,

chai = require('chai'),
expect = chai.expect,
_ = require('lodash'),

Solo = require('../../../../../lib/modules/revenue/solo');

Expand Down Expand Up @@ -33,7 +34,7 @@ describe('modules/revenue/solo', function () {
solo.technicalData = { btcPerBlock: 10, probability: 0.0001 };

solo.on('change', function () {
expect(solo.toJSON()).to.deep.equal({
expect(_.omit(solo.toJSON(), 'historicalData')).to.deep.equal({
value: 8640,
currency: 'NMC',
interval: 'Day'
Expand All @@ -51,7 +52,7 @@ describe('modules/revenue/solo', function () {
solo.technicalData = { btcPerBlock: 10, probability: 0.0001 };

solo.on('change', function () {
expect(solo.toJSON()).to.deep.equal({
expect(_.omit(solo.toJSON(), 'historicalData')).to.deep.equal({
value: 0,
currency: 'NMC',
interval: 'Day'
Expand All @@ -69,7 +70,7 @@ describe('modules/revenue/solo', function () {
solo.technicalData = { btcPerBlock: 10, probability: 0.0001 };

solo.on('change', function () {
expect(solo.toJSON()).to.deep.equal({
expect(_.omit(solo.toJSON(), 'historicalData')).to.deep.equal({
value: 8640,
currency: 'NMC',
interval: 'Day'
Expand All @@ -89,7 +90,7 @@ describe('modules/revenue/solo', function () {
solo.technicalData = { btcPerBlock: 10, probability: 0.0001 };

solo.on('change', function () {
expect(solo.toJSON()).to.deep.equal({
expect(_.omit(solo.toJSON(), 'historicalData')).to.deep.equal({
value: 86.4,
currency: 'BTC',
interval: 'Day'
Expand All @@ -107,7 +108,7 @@ describe('modules/revenue/solo', function () {
solo.marketData = { ask: 100, currency: 'NMC' };

solo.on('change', function () {
expect(solo.toJSON()).to.deep.equal({
expect(_.omit(solo.toJSON(), 'historicalData')).to.deep.equal({
value: 8640,
currency: 'NMC',
interval: 'Day'
Expand All @@ -130,4 +131,14 @@ describe('modules/revenue/solo', function () {
expect(solo.title).to.equal('Some Title');
});

it('should add the values to historicalData', function () {
var solo = new Solo(this.app, {}),
now = new Date().getTime();

solo.set({ currency: 'BTC', value: 123, interval: 'Day' });
expect(solo.get('historicalData')).to.have.length(1);
expect(solo.get('historicalData')[0].value).to.equal(123);
expect(solo.get('historicalData')[0].timestamp).to.be.within(now-1, now+1);
});

});
19 changes: 15 additions & 4 deletions test/specs/lib/modules/revenue/tripleminingSpec.js
Expand Up @@ -54,7 +54,7 @@ describe('modules/revenue/triplemining', function () {
triplemining.marketData = marketData;

triplemining.on('change', function () {
expect(triplemining.toJSON()).to.deep.equal({
expect(_.omit(triplemining.toJSON(), 'historicalData')).to.deep.equal({
value: 2073600000 * 1e6,
currency: 'NMC',
interval: 'Day'
Expand All @@ -71,7 +71,7 @@ describe('modules/revenue/triplemining', function () {

setTimeout(function () {
triplemining.on('change', function () {
expect(triplemining.toJSON()).to.deep.equal({
expect(_.omit(triplemining.toJSON(), 'historicalData')).to.deep.equal({
value: 2073600000 * 1e6,
currency: 'NMC',
interval: 'Day'
Expand All @@ -91,7 +91,7 @@ describe('modules/revenue/triplemining', function () {

setTimeout(function () {
triplemining.on('change', function () {
expect(triplemining.toJSON()).to.deep.equal({
expect(_.omit(triplemining.toJSON(), 'historicalData')).to.deep.equal({
value: 2073600000 * 1e6,
currency: 'NMC',
interval: 'Day'
Expand All @@ -109,7 +109,7 @@ describe('modules/revenue/triplemining', function () {
triplemining.technicalData = technicalData;

triplemining.on('change', function () {
expect(triplemining.toJSON()).to.deep.equal({
expect(_.omit(triplemining.toJSON(), 'historicalData')).to.deep.equal({
value: 1036800000 * 1e6,
currency: 'BTC',
interval: 'Day'
Expand Down Expand Up @@ -173,4 +173,15 @@ describe('modules/revenue/triplemining', function () {
expect(triplemining.title).to.equal('Some Title');
});

it('should add the values to historicalData', function () {
var app = new EventEmitter(),
triplemining = new Triplemining(app),
now = new Date().getTime();

triplemining.set({ currency: 'BTC', value: 123, interval: 'Day' });
expect(triplemining.get('historicalData')).to.have.length(1);
expect(triplemining.get('historicalData')[0].value).to.equal(123);
expect(triplemining.get('historicalData')[0].timestamp).to.be.within(now-1, now+1);
});

});

0 comments on commit ea09651

Please sign in to comment.