Skip to content

Commit 6459b5b

Browse files
sparsh00sparsh0204
andauthored
Instrument more cli stats (#165)
* instrument cli start time and mac address * use hash and move to util Co-authored-by: Sparsh Agarwal <[email protected]>
1 parent 6703003 commit 6459b5b

File tree

6 files changed

+100
-6
lines changed

6 files changed

+100
-6
lines changed

bin/commands/runs.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ const archiver = require("../helpers/archiver"),
1111
syncRunner = require("../helpers/syncRunner"),
1212
checkUploaded = require("../helpers/checkUploaded"),
1313
reportGenerator = require('../helpers/reporterHTML').reportGenerator,
14-
{initTimeComponents, markBlockStart, markBlockEnd, getTimeComponents} = require('../helpers/timeComponents'),
14+
{initTimeComponents, instrumentEventTime, markBlockStart, markBlockEnd, getTimeComponents} = require('../helpers/timeComponents'),
1515
downloadBuildArtifacts = require('../helpers/buildArtifacts').downloadBuildArtifacts;
1616

1717
module.exports = function run(args) {
1818
let bsConfigPath = utils.getConfigPath(args.cf);
1919
//Delete build_results.txt from log folder if already present.
2020
initTimeComponents();
21+
instrumentEventTime("cliStart")
2122
markBlockStart('deleteOldResults');
2223
utils.deleteResults();
2324
markBlockEnd('deleteOldResults');
@@ -168,6 +169,7 @@ module.exports = function run(args) {
168169
if(!args.sync) logger.info(Constants.userMessages.EXIT_SYNC_CLI_MESSAGE.replace("<build-id>", data.build_id));
169170
let dataToSend = {
170171
time_components: getTimeComponents(),
172+
unique_id: utils.generateUniqueHash(),
171173
build_id: data.build_id,
172174
};
173175
if (bsConfig && bsConfig.connection_settings) {

bin/helpers/timeComponents.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
const { isUndefined } = require('./utils');
44

5+
56
let sessionTimes = {
67
referenceTimes: {
78
absoluteStartTime: Date.now()
89
}, // Absolute times which needs to be used later to calculate logTimes
910
logTimes: {}, // Time Difference in ms which we need to push to EDS
11+
eventTime: {}, // Time for particular events
1012
};
1113

1214
const initTimeComponents = () => {
13-
sessionTimes = {referenceTimes: {absoluteStartTime: Date.now()}, logTimes: {}};
15+
sessionTimes = {referenceTimes: {absoluteStartTime: Date.now()}, logTimes: {}, eventTime: {}};
1416
};
1517

1618
const markBlockStart = (blockName) => {
@@ -26,10 +28,13 @@ const markBlockDiff = (blockName, startTime, stopTime) => {
2628
sessionTimes.logTimes[blockName] = stopTime - startTime;
2729
}
2830

31+
const instrumentEventTime = (eventName) => {
32+
sessionTimes.eventTime[eventName] = new Date(new Date().toUTCString());
33+
}
34+
2935
const getTimeComponents = () => {
3036
const data = convertDotToNestedObject(sessionTimes.logTimes);
31-
32-
return data;
37+
return Object.assign(data, sessionTimes.eventTime);
3338
};
3439

3540
const convertDotToNestedObject = (dotNotationObject) => {
@@ -58,6 +63,7 @@ const convertDotToNestedObject = (dotNotationObject) => {
5863

5964
module.exports = {
6065
initTimeComponents,
66+
instrumentEventTime,
6167
markBlockStart,
6268
markBlockEnd,
6369
markBlockDiff,

bin/helpers/utils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const glob = require('glob');
66
const getmac = require('getmac').default;
77
const { v4: uuidv4 } = require('uuid');
88
const browserstack = require('browserstack-local');
9+
const crypto = require('crypto');
910

1011
const usageReporting = require("./usageReporting"),
1112
logger = require("./logger").winstonLogger,
@@ -686,6 +687,18 @@ exports.sanitizeSpecsPattern = (pattern) => {
686687
return pattern && pattern.split(",").length > 1 ? "{" + pattern + "}" : pattern;
687688
}
688689

690+
exports.generateUniqueHash = () => {
691+
const loopback = /(?:[0]{2}[:-]){5}[0]{2}/
692+
const interFaceList = os.networkInterfaces();
693+
for (let inter in interFaceList){
694+
for (const address of interFaceList[inter]) {
695+
if (loopback.test(address.mac) === false) {
696+
return crypto.createHash('md5').update(address.mac).digest('hex');
697+
};
698+
};
699+
};
700+
};
701+
689702
exports.getBrowserCombinations = (bsConfig) => {
690703
let osBrowserArray = [];
691704
let osBrowser = "";

test/unit/bin/commands/runs.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ describe("runs", () => {
647647
setUsernameStub = sandbox.stub();
648648
setAccessKeyStub = sandbox.stub();
649649
setBuildNameStub = sandbox.stub();
650+
generateUniqueHashStub = sandbox.stub().returns('random_hash');
650651
setCypressConfigFilenameStub = sandbox.stub();
651652
setUserSpecsStub = sandbox.stub();
652653
setTestEnvsStub = sandbox.stub();
@@ -678,6 +679,7 @@ describe("runs", () => {
678679
setLocalConfigFileStub = sandbox.stub();
679680
getTimeComponentsStub = sandbox.stub().returns({});
680681
initTimeComponentsStub = sandbox.stub();
682+
instrumentEventTimeStub = sandbox.stub();
681683
markBlockStartStub = sandbox.stub();
682684
markBlockEndStub = sandbox.stub();
683685
setConfigStub = sandbox.stub();
@@ -696,7 +698,7 @@ describe("runs", () => {
696698
let errorCode = null;
697699
let message = `Success! ${Constants.userMessages.BUILD_CREATED} with build id: random_build_id`;
698700
let dashboardLink = `${Constants.userMessages.VISIT_DASHBOARD} ${dashboardUrl}`;
699-
let data = {time_components: {}, build_id: 'random_build_id'}
701+
let data = {time_components: {}, unique_id: 'random_hash', build_id: 'random_build_id'}
700702

701703
const runs = proxyquire('../../../../bin/commands/runs', {
702704
'../helpers/utils': {
@@ -720,6 +722,7 @@ describe("runs", () => {
720722
setHeaded: setHeadedStub,
721723
setNoWrap: setNoWrapStub,
722724
setOtherConfigs: setOtherConfigsStub,
725+
generateUniqueHash: generateUniqueHashStub,
723726
exportResults: exportResultsStub,
724727
deleteResults: deleteResultsStub,
725728
setDefaults: setDefaultsStub,
@@ -754,6 +757,7 @@ describe("runs", () => {
754757
},
755758
'../helpers/timeComponents': {
756759
initTimeComponents: initTimeComponentsStub,
760+
instrumentEventTime: instrumentEventTimeStub,
757761
getTimeComponents: getTimeComponentsStub,
758762
markBlockStart: markBlockStartStub,
759763
markBlockEnd: markBlockEndStub,
@@ -792,6 +796,7 @@ describe("runs", () => {
792796
sinon.assert.calledOnce(setHeadedStub);
793797
sinon.assert.calledOnce(setNoWrapStub);
794798
sinon.assert.calledOnce(setOtherConfigsStub);
799+
sinon.assert.calledOnce(generateUniqueHashStub);
795800
sinon.assert.calledOnce(archiverStub);
796801
sinon.assert.calledOnce(setUsageReportingFlagStub);
797802
sinon.assert.calledOnce(zipUploadStub);

test/unit/bin/helpers/timeComponents.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const chai = require("chai"),
88
let timeComponents = rewire('../../../../bin/helpers/timeComponents');
99

1010
let initTimeComponents = timeComponents.__get__('initTimeComponents');
11+
let instrumentEventTime = timeComponents.__get__('instrumentEventTime');
1112
let markBlockStart = timeComponents.__get__('markBlockStart');
1213
let markBlockEnd = timeComponents.__get__('markBlockEnd');
1314
let markBlockDiff = timeComponents.__get__('markBlockDiff');
@@ -24,6 +25,15 @@ describe('timeComponents', () => {
2425
});
2526
});
2627

28+
describe('instrumentEventTime', () => {
29+
it('should set reset sessionTimes object', () => {
30+
initTimeComponents();
31+
instrumentEventTime('cliTest');
32+
let sessionTimes = timeComponents.__get__('sessionTimes');
33+
expect(Object.keys(sessionTimes.eventTime)).to.include('cliTest');
34+
});
35+
});
36+
2737
describe('markBlockStart', () => {
2838
it('should add key to reference times', () => {
2939
initTimeComponents();
@@ -86,7 +96,6 @@ describe('timeComponents', () => {
8696
it('should call convertDotToNestedObject and return data', () => {
8797
let convertDotToNestedObjectStub = sinon.stub().returns({sampleBlock: 100}),
8898
convertDotToNestedObjectUnset = timeComponents.__set__('convertDotToNestedObject', convertDotToNestedObjectStub);
89-
9099
expect(getTimeComponents()).to.deep.equal({sampleBlock:100});
91100

92101
convertDotToNestedObjectUnset();

test/unit/bin/helpers/utils.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const chai = require('chai'),
99
chaiAsPromised = require('chai-as-promised'),
1010
glob = require('glob'),
1111
chalk = require('chalk'),
12+
os = require("os"),
13+
crypto = require('crypto'),
1214
fs = require('fs');
1315
const getmac = require('getmac').default;
1416
const usageReporting = require('../../../../bin/helpers/usageReporting');
@@ -2248,6 +2250,63 @@ describe('utils', () => {
22482250
});
22492251
});
22502252

2253+
describe('generateUniqueHash', () => {
2254+
beforeEach(() => {
2255+
let interfaceList = {
2256+
lo0: [
2257+
{
2258+
address: 'fe80::1',
2259+
netmask: 'ffff:ffff:ffff:ffff::',
2260+
family: 'IPv6',
2261+
mac: '00:00:00:00:00:00',
2262+
internal: true,
2263+
cidr: 'fe80::1/64',
2264+
scopeid: 1
2265+
}
2266+
],
2267+
en5: [
2268+
{
2269+
address: 'fe80::1',
2270+
netmask: 'ffff:ffff:ffff:ffff::',
2271+
family: 'IPv6',
2272+
mac: '00:00:00:00:00:00',
2273+
internal: true,
2274+
cidr: 'fe80::1/64',
2275+
scopeid: 1
2276+
},
2277+
{
2278+
address: 'fe80::aede:48ff:fe00:1122',
2279+
netmask: 'ffff:ffff:ffff:ffff::',
2280+
family: 'IPv6',
2281+
mac: 'ra:nd:om:01:23:45',
2282+
internal: false,
2283+
cidr: 'fe80::aede:48ff:fe00:1122/64',
2284+
scopeid: 7
2285+
}
2286+
],
2287+
en0: [
2288+
{
2289+
address: '192.168.29.250',
2290+
netmask: '255.255.255.0',
2291+
family: 'IPv4',
2292+
mac: '00:00:00:00:00:00',
2293+
internal: false,
2294+
cidr: '192.168.29.250/24'
2295+
}
2296+
]
2297+
};
2298+
sinon.stub(os, 'networkInterfaces').returns(interfaceList);
2299+
sinon.stub(crypto, 'createHash').returns({
2300+
update: sinon.stub().returns({
2301+
digest: sinon.stub().returns("random_hash")
2302+
})
2303+
});
2304+
});
2305+
it('should return non zero mac address', () => {
2306+
expect(utils.generateUniqueHash()).to.equal('random_hash');
2307+
});
2308+
});
2309+
22512310
describe('setBrowsers', () => {
22522311
it('the args browser should override the bsconfig browsers', async () => {
22532312
let bsConfig = {

0 commit comments

Comments
 (0)