Skip to content

Commit

Permalink
use stacked chart for aggregated hashrate
Browse files Browse the repository at this point in the history
  • Loading branch information
selaux committed Jun 13, 2014
1 parent 76b0f97 commit 4a9ca81
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 42 deletions.
33 changes: 21 additions & 12 deletions lib/modules/miners/aggregated.js
Expand Up @@ -16,7 +16,12 @@ module.exports = Module.extend({
},

initialize: function () {
var self = this;
var self = this,
attributesToSave = _.map(self.config.miners, function (minerId) {
return 'currentHashrate.' + minerId;
});

self.set = setWithHistoricalData(attributesToSave, Module.prototype.set);

self.title = this.config.title || 'Total Hashrate';

Expand All @@ -27,18 +32,22 @@ module.exports = Module.extend({

aggregateHashrates: function () {
var self = this,
currentHashrate = 0;

_.each(this.app.modules, function (module) {
if (self.config.miners.indexOf(module.id) !== -1) {
currentHashrate += module.get('currentHashrate') || 0;
}
});
currentHashrate = 0,
individualHashrates = {},
minerTitles = {};

self.set({
currentHashrate: currentHashrate
self.config.miners.forEach(function (id) {
var module = _.find(self.app.modules, function (module) {
return module.id === id;
});
minerTitles[id] = module ? module.title : id;
individualHashrates['currentHashrate.' + module.id] = module ? module.get('currentHashrate') : 0;
currentHashrate += module.get('currentHashrate') || 0;
});
},

set: setWithHistoricalData([ 'currentHashrate' ], Module.prototype.set)
self.set(_.extend({
currentHashrate: currentHashrate,
minerTitles: minerTitles
}, individualHashrates));
}
});
53 changes: 42 additions & 11 deletions lib/views/aggregatedMiners.js
@@ -1,18 +1,49 @@
'use strict';

var hashrateHelper = require('../handlebars/helpers/hashrate'),
var _ = require('lodash'),
hashrateHelper = require('../handlebars/helpers/hashrate'),
renderHistoricalDataGraph = require('./mixins/renderHistoricalDataGraph'),

View = require('../View'),
graphMixin = renderHistoricalDataGraph([ {
attr: 'currentHashrate',
name: 'Total Hashrate'
} ], '.graph', {}, {
yFormatter: function (value) {
return hashrateHelper(value);
}
});
View = require('../View');

module.exports = View.extend({
postRender: function () {
var self = this,
colors = [ '#cae2f7', '#a7d2f7' ],
series,
graphMixin;

if (self.graph === undefined) {
series = _(self.module.get('historicalData'))
.map(function (val) {
return _(val)
.keys()
.filter(function (key) { return key.split('.')[0] === 'currentHashrate'; })
.map(function (key) { return key.split('.')[1]; })
.value();
})
.flatten()
.unique()
.map(function (minerId, it) {
return {
attr: 'currentHashrate.' + minerId,
name: self.module.get('minerTitles')[minerId],
color: colors[it % colors.length]
};
})
.value();
graphMixin = renderHistoricalDataGraph(series, '.graph', {
stack: true
}, {
yFormatter: function (value) {
return hashrateHelper(value);
}
});

_.extend(this, graphMixin);
this.postRender();
}
},

template: 'aggregatedMiners'
}).extend(graphMixin);
});
25 changes: 19 additions & 6 deletions lib/views/mixins/renderHistoricalDataGraph.js
Expand Up @@ -9,6 +9,22 @@ if (typeof window !== 'undefined') {
Rickshaw = require('rickshaw');
}

function getValueRange(series, stacked) {
var values = _(series).map(function (ser) {
return _.pluck(ser.data, 'y');
});

if (stacked) {
return _(values).zip().map(function (ser) {
return _.reduce(ser, function (a, b) { return a + b; });
});
} else {
return _(values).map(function (ser) {
return _.max(ser);
});
}
}

module.exports = function (attributes, element, graphOptions, hoverOptions) {
return {
graph: null,
Expand Down Expand Up @@ -46,6 +62,7 @@ module.exports = function (attributes, element, graphOptions, hoverOptions) {
renderer: 'area',
interpolation: 'linear',
stroke: true,
stack: false,
series: this.getSeries()
}, graphOptions));
this.detail = new Rickshaw.Graph.HoverDetail(_.extend({
Expand All @@ -66,12 +83,8 @@ module.exports = function (attributes, element, graphOptions, hoverOptions) {
updateGraph: function () {
var series = this.getSeries();

this.graph.min = _(series).map(function (ser) {
return _(ser.data).pluck('y').min().value();
}).min().value() * 0.99;
this.graph.max = _(series).map(function (ser) {
return _(ser.data).pluck('y').max().value();
}).max().value() * 1.01;
this.graph.min = _.min(getValueRange(series, this.graph.config.stack)) * 0.99;
this.graph.min = _.max(getValueRange(series, this.graph.config.stack)) * 1.01;
_.each(this.graph.series, function (graphSeries, index) {
graphSeries.data = series[index].data;
});
Expand Down
44 changes: 31 additions & 13 deletions test/specs/lib/modules/miners/aggregatedSpec.js
Expand Up @@ -14,6 +14,11 @@ describe('modules/miners/aggregated', function () {

beforeEach(function () {
app = new EventEmitter();
app.modules = [
{ get: sinon.stub().returns(123), id: 'miner1', title: 'Miner 1' },
{ get: sinon.stub().returns(456), id: 'miner2', title: 'Miner 2' },
];

});

it('should have the default title set to "Total Hashrate"', function () {
Expand All @@ -31,13 +36,14 @@ describe('modules/miners/aggregated', function () {
it('should aggregate the hashrate whenever a miner hashrate changes', function (done) {
var aggregated = new Aggregated(app, defaultConfig);

app.modules = [
{ get: sinon.stub().returns(123), id: 'miner1' },
{ get: sinon.stub().returns(456), id: 'miner2' },
];

aggregated.on('change', function () {
expect(aggregated.get('currentHashrate')).to.equal(579);
expect(aggregated.get('minerTitles')).to.deep.equal({
miner1: 'Miner 1',
miner2: 'Miner 2'
});
expect(aggregated.get('currentHashrate.miner1')).to.equal(123);
expect(aggregated.get('currentHashrate.miner2')).to.equal(456);
done();
});
app.emit('update:data:miner2');
Expand All @@ -47,28 +53,40 @@ describe('modules/miners/aggregated', function () {
it('should aggregate the hashrates of the provided modules', function () {
var aggregated = new Aggregated(app, defaultConfig);

app.modules = [
{ get: sinon.stub().returns(123), id: 'miner1' },
{ get: sinon.stub().returns(456), id: 'miner2' },
];
aggregated.set = sinon.stub();
aggregated.aggregateHashrates();

expect(aggregated.set).to.have.been.calledOnce;
expect(aggregated.set).to.have.been.calledWith({
currentHashrate: 579
currentHashrate: 579,
minerTitles: {
miner1: 'Miner 1',
miner2: 'Miner 2'
},
'currentHashrate.miner1': 123,
'currentHashrate.miner2': 456
});
});
});

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

solo.set({ currentHashrate: 123 });
solo.set({
currentHashrate: 579,
'currentHashrate.miner1': 123,
'currentHashrate.miner2': 456,
minerTitles: {
miner1: 'Miner 1',
miner2: 'Miner 2'
}
});
console.log(solo.get('historicalData')[0].source);
expect(solo.get('historicalData')).to.have.length(1);
expect(solo.get('historicalData')[0].currentHashrate).to.equal(123);
expect(solo.get('historicalData')[0]['currentHashrate.miner1']).to.equal(123);
expect(solo.get('historicalData')[0]['currentHashrate.miner2']).to.equal(456);
expect(solo.get('historicalData')[0].timestamp).to.be.within(now-1, now+1);
});
});
Expand Down

0 comments on commit 4a9ca81

Please sign in to comment.