diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e2b87ab01..472a7471ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +* Fixed bug rendering hosts when clock speed cannot be parsed. + ## Kolide Fleet 1.0.5 (Oct 17, 2017) * Renamed the binary from kolide to fleet diff --git a/frontend/redux/nodes/entities/hosts/helpers.js b/frontend/redux/nodes/entities/hosts/helpers.js index c8f80f05ec..a54db46834 100644 --- a/frontend/redux/nodes/entities/hosts/helpers.js +++ b/frontend/redux/nodes/entities/hosts/helpers.js @@ -4,14 +4,21 @@ export const parseEntityFunc = (host) => { const { network_interfaces: networkInterfaces } = host; const networkInterface = networkInterfaces && find(networkInterfaces, { id: host.primary_ip_id }); - let clockSpeed = null; - let clockSpeedFlt = null; let hostCpuOutput = null; - - if (host && host.cpu_brand) { - clockSpeed = host.cpu_brand.split('@ ')[1] || host.cpu_brand.split('@')[1]; - clockSpeedFlt = parseFloat(clockSpeed.split('GHz')[0].trim()); - hostCpuOutput = `${host.cpu_physical_cores} x ${Math.floor(clockSpeedFlt * 10) / 10} GHz`; + if (host) { + let clockSpeedOutput = null; + try { + const clockSpeed = host.cpu_brand.split('@ ')[1] || host.cpu_brand.split('@')[1]; + const clockSpeedFlt = parseFloat(clockSpeed.split('GHz')[0].trim()); + clockSpeedOutput = Math.floor(clockSpeedFlt * 10) / 10; + } catch (e) { + // Some CPU brand strings do not fit this format and we can't parse the + // clock speed. Leave it set to 'Unknown'. + console.log(`Unable to parse clock speed from cpu_brand: ${host.cpu_brand}`); + } + if (host.cpu_physical_cores || clockSpeedOutput) { + hostCpuOutput = `${host.cpu_physical_cores || 'Unknown'} x ${clockSpeedOutput || 'Unknown'} GHz`; + } } const additionalAttrs = { diff --git a/frontend/redux/nodes/entities/hosts/helpers.tests.js b/frontend/redux/nodes/entities/hosts/helpers.tests.js new file mode 100644 index 0000000000..2611bb9c9a --- /dev/null +++ b/frontend/redux/nodes/entities/hosts/helpers.tests.js @@ -0,0 +1,42 @@ +import expect from 'expect'; + +import { parseEntityFunc } from './helpers'; + +describe('reduxConfig - hosts helpers', () => { + describe('parseEntityFunc', () => { + it('parses an expected CPU string', () => { + const host = { + cpu_brand: 'Intel(R) Xeon(R) CPU E5-2420 0 @ 1.90GHz', + cpu_physical_cores: 2, + }; + expect(parseEntityFunc(host).host_cpu).toEqual('2 x 1.9 GHz'); + }); + + it('parses a host missing clock speed', () => { + const host = { + cpu_brand: 'Intel(R) Xeon(R) CPU E5-242', + cpu_physical_cores: 2, + }; + expect(parseEntityFunc(host).host_cpu).toEqual('2 x Unknown GHz'); + }); + + it('parses a host missing CPU brand', () => { + const host = { + cpu_physical_cores: 2, + }; + expect(parseEntityFunc(host).host_cpu).toEqual('2 x Unknown GHz'); + }); + + it('parses a host missing CPU cores', () => { + const host = { + cpu_brand: 'Intel(R) Xeon(R) CPU E5-2420 0 @ 1.90GHz', + }; + expect(parseEntityFunc(host).host_cpu).toEqual('Unknown x 1.9 GHz'); + }); + + it('parses a host missing CPU info entirely', () => { + const host = {}; + expect(parseEntityFunc(host).host_cpu).toEqual(undefined); + }); + }); +});