diff --git a/lib/modules/miners/bfgminer.js b/lib/modules/miners/bfgminer.js index eca2a1a..9e94616 100644 --- a/lib/modules/miners/bfgminer.js +++ b/lib/modules/miners/bfgminer.js @@ -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); } }); @@ -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, { diff --git a/test/specs/lib/modules/miners/bfgminerSpec.js b/test/specs/lib/modules/miners/bfgminerSpec.js index abee472..dd61c32 100644 --- a/test/specs/lib/modules/miners/bfgminerSpec.js +++ b/test/specs/lib/modules/miners/bfgminerSpec.js @@ -75,16 +75,8 @@ 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) { @@ -92,6 +84,16 @@ describe('modules/miners/bfgminer', function () { } 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(), @@ -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); @@ -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 ] }),