Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
refactor(calculateDurationMs): move this method to common utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed May 23, 2018
1 parent 916f396 commit a7f653b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
7 changes: 1 addition & 6 deletions lib/connection/apm.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const KillCursor = require('../connection/commands').KillCursor;
const GetMore = require('../connection/commands').GetMore;
const process = require('process');
const calculateDurationInMs = require('../utils').calculateDurationInMs;

/** Commands that we want to redact because of the sensitive nature of their contents */
const SENSITIVE_COMMANDS = new Set([
Expand All @@ -18,11 +18,6 @@ const SENSITIVE_COMMANDS = new Set([

// helper methods
const extractCommandName = command => Object.keys(command)[0];
const calculateDurationInMs = started => {
const hrtime = process.hrtime(started);
return (hrtime[0] * 1e9 + hrtime[1]) / 1e6;
};

const namespace = command => command.ns;
const databaseName = command => command.ns.split('.')[0];
const collectionName = command => command.ns.split('.')[1];
Expand Down
17 changes: 16 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@

const crypto = require('crypto');

/**
* Generate a UUIDv4
*/
const uuidV4 = () => {
const result = crypto.randomBytes(16);
result[6] = (result[6] & 0x0f) | 0x40;
result[8] = (result[8] & 0x3f) | 0x80;
return result;
};

/**
* Returns the duration calculated from two high resolution timers in milliseconds
*
* @param {Object} started A high resolution timestamp created from `process.hrtime()`
* @returns {Number} The duration in milliseconds
*/
const calculateDurationInMs = started => {
const hrtime = process.hrtime(started);
return (hrtime[0] * 1e9 + hrtime[1]) / 1e6;
};

module.exports = {
uuidV4: uuidV4
uuidV4,
calculateDurationInMs
};

0 comments on commit a7f653b

Please sign in to comment.