make-mp-data 1.1.15 → 1.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +47 -31
- package/package.json +1 -1
- package/{e2e.test.js → tests/e2e.test.js} +1 -1
- package/tests/unit.test.js +23 -0
package/index.js
CHANGED
|
@@ -473,41 +473,56 @@ function buildFileNames(config) {
|
|
|
473
473
|
* @returns {number} - The generated event timestamp in Unix format.
|
|
474
474
|
*/
|
|
475
475
|
function AKsTimeSoup(earliestTime, latestTime = NOW, peakDays = PEAK_DAYS) {
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
476
|
+
let chosenTime;
|
|
477
|
+
let eventTime;
|
|
478
|
+
let validTime = false;
|
|
479
|
+
|
|
480
|
+
if (typeof earliestTime !== "number") {
|
|
481
|
+
if (parseInt(earliestTime) > 0) earliestTime = parseInt(earliestTime);
|
|
482
|
+
if (dayjs(earliestTime).isValid()) earliestTime = dayjs(earliestTime).unix();
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
while (!validTime) {
|
|
486
|
+
|
|
487
|
+
// Define business hours
|
|
488
|
+
const peakStartHour = 4; // 4 AM
|
|
489
|
+
const peakEndHour = 23; // 11 PM
|
|
490
|
+
const likelihoodOfPeakDay = chance.integer({ min: integer(5, 42), max: integer(43, 69) }); // Randomize likelihood with CHAOS!~~
|
|
491
|
+
|
|
492
|
+
// Select a day, with a preference for peak days
|
|
493
|
+
let selectedDay;
|
|
494
|
+
if (chance.bool({ likelihood: likelihoodOfPeakDay })) { // Randomized likelihood to pick a peak day
|
|
495
|
+
selectedDay = peakDays.length > 0 ? chance.pickone(peakDays) : integer(earliestTime, latestTime);
|
|
496
|
+
} else {
|
|
497
|
+
// Introduce minor peaks by allowing some events to still occur during business hours
|
|
498
|
+
selectedDay = chance.bool({ likelihood: integer(1, 42) })
|
|
499
|
+
? chance.pickone(peakDays)
|
|
500
|
+
: integer(earliestTime, latestTime);
|
|
501
|
+
}
|
|
491
502
|
|
|
492
|
-
|
|
493
|
-
|
|
503
|
+
// Normalize selectedDay to the start of the day
|
|
504
|
+
selectedDay = dayjs.unix(selectedDay).startOf('day').unix();
|
|
505
|
+
|
|
506
|
+
// Generate a random time within business hours with a higher concentration in the middle of the period
|
|
507
|
+
const businessStart = dayjs.unix(selectedDay).hour(peakStartHour).minute(0).second(0).unix();
|
|
508
|
+
const businessEnd = dayjs.unix(selectedDay).hour(peakEndHour).minute(0).second(0).unix();
|
|
509
|
+
|
|
510
|
+
if (selectedDay === peakDays[0]) {
|
|
511
|
+
// Use a skewed distribution for peak days
|
|
512
|
+
eventTime = chance.normal({ mean: (businessEnd + businessStart) / integer(1, 4), dev: (businessEnd - businessStart) / integer(2, 8) });
|
|
513
|
+
} else {
|
|
514
|
+
// For non-peak days, use a uniform distribution to add noise
|
|
515
|
+
eventTime = integer(integer(businessStart, businessEnd), integer(businessStart, businessEnd));
|
|
516
|
+
}
|
|
494
517
|
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
const businessEnd = dayjs.unix(selectedDay).hour(peakEndHour).minute(0).second(0).unix();
|
|
498
|
-
let eventTime;
|
|
499
|
-
if (selectedDay === peakDays[0]) {
|
|
500
|
-
// Use a skewed distribution for peak days
|
|
501
|
-
eventTime = chance.normal({ mean: (businessEnd + businessStart) / integer(1, 4), dev: (businessEnd - businessStart) / integer(2, 8) });
|
|
502
|
-
} else {
|
|
503
|
-
// For non-peak days, use a uniform distribution to add noise
|
|
504
|
-
eventTime = integer(integer(businessStart, businessEnd), integer(businessStart, businessEnd));
|
|
505
|
-
}
|
|
518
|
+
// usually, ensure the event time is within business hours
|
|
519
|
+
if (chance.bool({ likelihood: 42 })) eventTime = Math.min(Math.max(eventTime, businessStart), businessEnd);
|
|
506
520
|
|
|
507
|
-
|
|
508
|
-
if (chance.bool({ likelihood: 42 })) eventTime = Math.min(Math.max(eventTime, businessStart), businessEnd);
|
|
521
|
+
if (eventTime > 0) validTime = true;
|
|
509
522
|
|
|
510
|
-
|
|
523
|
+
}
|
|
524
|
+
chosenTime = dayjs.unix(eventTime).toISOString();
|
|
525
|
+
return chosenTime;
|
|
511
526
|
}
|
|
512
527
|
|
|
513
528
|
|
|
@@ -575,6 +590,7 @@ if (require.main === module) {
|
|
|
575
590
|
openFinder(path.resolve("./data"));
|
|
576
591
|
});
|
|
577
592
|
} else {
|
|
593
|
+
main.timeSoup = AKsTimeSoup;
|
|
578
594
|
module.exports = main;
|
|
579
595
|
}
|
|
580
596
|
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
/* eslint-disable no-undef */
|
|
4
4
|
/* eslint-disable no-debugger */
|
|
5
5
|
/* eslint-disable no-unused-vars */
|
|
6
|
-
const generate = require('
|
|
6
|
+
const generate = require('../index.js');
|
|
7
7
|
require('dotenv').config();
|
|
8
8
|
const { execSync } = require("child_process");
|
|
9
9
|
const u = require('ak-tools');
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const generate = require('../index.js');
|
|
2
|
+
const dayjs = require("dayjs");
|
|
3
|
+
const utc = require("dayjs/plugin/utc");
|
|
4
|
+
const u = require('ak-tools');
|
|
5
|
+
dayjs.extend(utc);
|
|
6
|
+
const { timeSoup } = generate;
|
|
7
|
+
require('dotenv').config();
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
describe('timeSoup', () => {
|
|
11
|
+
test('always positive dates', () => {
|
|
12
|
+
const dates = [];
|
|
13
|
+
for (let i = 0; i < 20000; i++) {
|
|
14
|
+
const earliest = dayjs().subtract(u.rand(2, 360), 'D');
|
|
15
|
+
dates.push(timeSoup());
|
|
16
|
+
}
|
|
17
|
+
const tooOld = dates.filter(d => dayjs(d).isBefore(dayjs.unix(0)));
|
|
18
|
+
const badYear = dates.filter(d => !d.startsWith('202'));
|
|
19
|
+
expect(dates.every(d => dayjs(d).isAfter(dayjs.unix(0)))).toBe(true);
|
|
20
|
+
expect(dates.every(d => d.startsWith('202'))).toBe(true);
|
|
21
|
+
|
|
22
|
+
});
|
|
23
|
+
});
|