Skip to content

Commit

Permalink
accumulate current hashrate from devices if not returned by summary c…
Browse files Browse the repository at this point in the history
…ommand, #39
  • Loading branch information
selaux committed Mar 23, 2014
1 parent 0299012 commit cf8cf33
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
11 changes: 10 additions & 1 deletion lib/modules/miners/bfgminer.js
Expand Up @@ -79,6 +79,11 @@ module.exports = Module.extend({
} else {
data.devices = devices;
data.pools = pools;
if (data.avgHashrate === undefined) {
data.avgHashrate = _(data.devices).pluck('avgHashrate').reduce(function(sum, num) {
return sum + num;
});
}
self.set(data);
}
});
Expand Down Expand Up @@ -138,7 +143,11 @@ module.exports = Module.extend({
avgHashrateKey = _.find(_.keys(response.SUMMARY[0]), function (key) {
return key.match(/^MHS\s[0-9]+s$/g) || key.match(/^GHS\s[0-9]+s$/g);
});
avgHashrate = avgHashrateKey.indexOf('GHS') === 0 ? response.SUMMARY[0][avgHashrateKey] * 1000 : response.SUMMARY[0][avgHashrateKey];
if (avgHashrateKey) {
avgHashrate = avgHashrateKey.indexOf('GHS') === 0 ? response.SUMMARY[0][avgHashrateKey] * 1000 : response.SUMMARY[0][avgHashrateKey];
} else {
avgHashrate = undefined;
}
totalDifficulty = response.SUMMARY[0]['Difficulty Accepted'] + response.SUMMARY[0]['Difficulty Rejected'] + response.SUMMARY[0]['Difficulty Stale'];

data = _.extend({}, defaults, {
Expand Down
62 changes: 51 additions & 11 deletions test/specs/lib/modules/miners/bfgminerSpec.js
Expand Up @@ -75,23 +75,25 @@ describe('modules/miners/bfgminer', function () {
'devs',
'pools'
],
responseData = {
summary: 'summary data',
devs: 'device data',
pools: 'pool data'
},
parsedData = {
summary: { summary: 'data' },
devs: [ { device: 'data' } ],
pools: [ { pool: 'data' } ]
},
responseData,
parsedData,
bfgAdapterStub;

function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}

beforeEach(function () {
responseData = {
summary: { avgHashrate: 123 },
devs: [ { avgHashrate: 124 } ],
pools: [ { pool: 'data' } ]
};
parsedData = {
summary: { avgHashrate: 123 },
devs: [ { avgHashrate: 124 } ],
pools: [ { pool: 'data' } ]
};
bfgAdapterStub = {
sendCommand: sinon.stub(),
handleSummaryResponse: sinon.stub(),
Expand All @@ -117,7 +119,32 @@ describe('modules/miners/bfgminer', function () {

commands.forEach(function (command) {
bfgAdapterStub.sendCommand.withArgs(command).yieldsAsync(null, responseData[command]);
bfgAdapterStub['handle' + capitalizeFirstLetter(command) + 'Response'].returns(parsedData[command]);
bfgAdapterStub['handle' + capitalizeFirstLetter(command) + 'Response'].returns(responseData[command]);
});

BfgAdapter.prototype.update.call(bfgAdapterStub);
});

it('should sum up the device hashrates if the current hashrate is not returned in the summary', function (done) {
responseData.summary = {};

bfgAdapterStub.set = function (data) {
commands.forEach(function (command) {
expect(bfgAdapterStub['handle' + capitalizeFirstLetter(command) + 'Response']).to.have.been.calledOnce;
});

expect(bfgAdapterStub.sendCommand).to.have.been.calledThrice;

expect(data).to.deep.equal(_.extend({}, { avgHashrate: 124 }, {
devices: parsedData.devs,
pools: parsedData.pools
}));
done();
};

commands.forEach(function (command) {
bfgAdapterStub.sendCommand.withArgs(command).yieldsAsync(null, responseData[command]);
bfgAdapterStub['handle' + capitalizeFirstLetter(command) + 'Response'].returns(responseData[command]);
});

BfgAdapter.prototype.update.call(bfgAdapterStub);
Expand Down Expand Up @@ -340,6 +367,19 @@ describe('modules/miners/bfgminer', function () {
}));
});

it('should handle a response containing "MHS av" only', function () {
var summary,
response,
bfgAdapter = new BfgAdapter({}, config);

summary = _.extend({}, _.omit(summaryResponse.SUMMARY[0], 'MHS 10s'), { 'MHS av': 58 });
response = _.extend({}, summaryResponse, { SUMMARY: [ summary ] });

expect(bfgAdapter.handleSummaryResponse(response)).to.deep.equal(_.extend({}, parsedResponse, {
avgHashrate: undefined
}));
});

it('should correctly calculate percentages if no work has been done yet', function () {
var summary = _.extend({}, summaryResponse.SUMMARY[0], { 'Difficulty Accepted': 0, 'Difficulty Rejected': 0, 'Difficulty Stale': 0 }),
response = _.extend({}, summaryResponse, { SUMMARY: [ summary ] }),
Expand Down

0 comments on commit cf8cf33

Please sign in to comment.