Skip to content

Commit

Permalink
EPMRPP-94905 || Microseconds support for timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
AmsterGet committed Sep 20, 2024
1 parent b00c5d9 commit 009fc50
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 125 deletions.
34 changes: 32 additions & 2 deletions __tests__/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const os = require('os');
const fs = require('fs');
const glob = require('glob');
const microtime = require('microtime');
const helpers = require('../lib/helpers');
const pjson = require('../package.json');

Expand All @@ -20,9 +21,38 @@ describe('Helpers', () => {
});
});

describe('formatMicrosecondsToISOString', () => {
test('converts microseconds to ISO string with microseconds precision', () => {
const input = 1726842755304456;
const expected = '2024-09-20T14:32:35.304456Z';
const result = helpers.formatMicrosecondsToISOString(input);
expect(result).toBe(expected);
});

test('handles microseconds at the start of the epoch', () => {
const input = 654321;
const expected = '1970-01-01T00:00:00.654321Z';
const result = helpers.formatMicrosecondsToISOString(input);
expect(result).toBe(expected);
});

test('handles rounding down of microseconds correctly', () => {
const input = 1000001;
const expected = '1970-01-01T00:00:01.000001Z';
const result = helpers.formatMicrosecondsToISOString(input);
expect(result).toBe(expected);
});
});

describe('now', () => {
it('returns milliseconds from unix time', () => {
expect(new Date() - helpers.now()).toBeLessThan(100); // less than 100 miliseconds difference
it('should return the current timestamp with microseconds precision in ISO string format', () => {
const spyMicrotime = jest.spyOn(microtime, 'now').mockReturnValue(1726842755304456);
const expectedISOString = '2024-09-20T14:32:35.304456Z';

const result = helpers.now();

expect(spyMicrotime).toHaveBeenCalled();
expect(result).toBe(expectedISOString);
});
});

Expand Down
187 changes: 103 additions & 84 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs');
const glob = require('glob');
const os = require('os');
const microtime = require('microtime');
const RestClient = require('./rest');
const pjson = require('../package.json');

Expand All @@ -11,92 +12,110 @@ const PJSON_NAME = pjson.name;

const getUUIDFromFileName = (filename) => filename.match(/rplaunch-(.*)\.tmp/)[1];

module.exports = {
formatName(name) {
const len = name.length;
// eslint-disable-next-line no-mixed-operators
return (len < MIN ? name + new Array(MIN - len + 1).join('.') : name).slice(-MAX);
},

now() {
return new Date().valueOf();
},

// TODO: deprecate and remove
getServerResult(url, request, options, method) {
return new RestClient(options).request(method, url, request, options);
},

readLaunchesFromFile() {
const files = glob.sync('rplaunch-*.tmp');
const ids = files.map(getUUIDFromFileName);

return ids;
},

saveLaunchIdToFile(launchId) {
const filename = `rplaunch-${launchId}.tmp`;
fs.open(filename, 'w', (err) => {
if (err) {
throw err;
}
});
},

getSystemAttribute() {
const osType = os.type();
const osArchitecture = os.arch();
const RAMSize = os.totalmem();
const nodeVersion = process.version;
const systemAttr = [
{
key: 'client',
value: `${PJSON_NAME}|${PJSON_VERSION}`,
system: true,
},
{
key: 'os',
value: `${osType}|${osArchitecture}`,
system: true,
},
{
key: 'RAMSize',
value: RAMSize,
system: true,
},
{
key: 'nodeJS',
value: nodeVersion,
system: true,
},
];

return systemAttr;
},

generateTestCaseId(codeRef, params) {
if (!codeRef) {
return;
const formatName = (name) => {
const len = name.length;
// eslint-disable-next-line no-mixed-operators
return (len < MIN ? name + new Array(MIN - len + 1).join('.') : name).slice(-MAX);
};

const formatMicrosecondsToISOString = (timestampInMicroseconds) => {
const milliseconds = Math.floor(timestampInMicroseconds / 1000);
const microseconds = String(timestampInMicroseconds).slice(-3);
const isoDate = new Date(milliseconds).toISOString();

return isoDate.replace('Z', `${microseconds}Z`);
};

const now = () => {
return formatMicrosecondsToISOString(microtime.now());
};

// TODO: deprecate and remove
const getServerResult = (url, request, options, method) => {
return new RestClient(options).request(method, url, request, options);
};

const readLaunchesFromFile = () => {
const files = glob.sync('rplaunch-*.tmp');
const ids = files.map(getUUIDFromFileName);

return ids;
};

const saveLaunchIdToFile = (launchId) => {
const filename = `rplaunch-${launchId}.tmp`;
fs.open(filename, 'w', (err) => {
if (err) {
throw err;
}
});
};

const getSystemAttribute = () => {
const osType = os.type();
const osArchitecture = os.arch();
const RAMSize = os.totalmem();
const nodeVersion = process.version;
const systemAttr = [
{
key: 'client',
value: `${PJSON_NAME}|${PJSON_VERSION}`,
system: true,
},
{
key: 'os',
value: `${osType}|${osArchitecture}`,
system: true,
},
{
key: 'RAMSize',
value: RAMSize,
system: true,
},
{
key: 'nodeJS',
value: nodeVersion,
system: true,
},
];

return systemAttr;
};

const generateTestCaseId = (codeRef, params) => {
if (!codeRef) {
return;
}

if (!params) {
return codeRef;
if (!params) {
return codeRef;
}

const parameters = params.reduce(
(result, item) => (item.value ? result.concat(item.value) : result),
[],
);

return `${codeRef}[${parameters}]`;
};

const saveLaunchUuidToFile = (launchUuid) => {
const filename = `rp-launch-uuid-${launchUuid}.tmp`;
fs.open(filename, 'w', (err) => {
if (err) {
throw err;
}
});
};

const parameters = params.reduce(
(result, item) => (item.value ? result.concat(item.value) : result),
[],
);

return `${codeRef}[${parameters}]`;
},

saveLaunchUuidToFile(launchUuid) {
const filename = `rp-launch-uuid-${launchUuid}.tmp`;
fs.open(filename, 'w', (err) => {
if (err) {
throw err;
}
});
},
module.exports = {
formatName,
formatMicrosecondsToISOString,
now,
getServerResult,
readLaunchesFromFile,
saveLaunchIdToFile,
getSystemAttribute,
generateTestCaseId,
saveLaunchUuidToFile,
};
66 changes: 28 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"glob": "^8.1.0",
"ini": "^2.0.0",
"uniqid": "^5.4.0",
"uuid": "^9.0.1"
"uuid": "^9.0.1",
"microtime": "^3.1.1"
},
"license": "Apache-2.0",
"devDependencies": {
Expand Down

0 comments on commit 009fc50

Please sign in to comment.