make-mp-data 1.1.14 → 1.1.16

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 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
- // Define business hours
477
- const peakStartHour = 4; // 4 AM
478
- const peakEndHour = 23; // 11 PM
479
- const likelihoodOfPeakDay = chance.integer({ min: integer(5, 42), max: integer(43, 69) }); // Randomize likelihood with CHAOS!~~
480
-
481
- // Select a day, with a preference for peak days
482
- let selectedDay;
483
- if (chance.bool({ likelihood: likelihoodOfPeakDay })) { // Randomized likelihood to pick a peak day
484
- selectedDay = peakDays.length > 0 ? chance.pickone(peakDays) : integer(earliestTime, latestTime);
485
- } else {
486
- // Introduce minor peaks by allowing some events to still occur during business hours
487
- selectedDay = chance.bool({ likelihood: integer(1, 42) })
488
- ? chance.pickone(peakDays)
489
- : integer(earliestTime, latestTime);
490
- }
491
-
492
- // Normalize selectedDay to the start of the day
493
- selectedDay = dayjs.unix(selectedDay).startOf('day').unix();
494
-
495
- // Generate a random time within business hours with a higher concentration in the middle of the period
496
- const businessStart = dayjs.unix(selectedDay).hour(peakStartHour).minute(0).second(0).unix();
497
- const businessEnd = dayjs.unix(selectedDay).hour(peakEndHour).minute(0).second(0).unix();
476
+ let chosenTime;
498
477
  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
- }
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
+ }
506
502
 
507
- // usually, ensure the event time is within business hours
508
- if (chance.bool({ likelihood: 42 })) eventTime = Math.min(Math.max(eventTime, businessStart), businessEnd);
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
+ }
509
517
 
510
- return dayjs.unix(eventTime).toISOString();
518
+ // usually, ensure the event time is within business hours
519
+ if (chance.bool({ likelihood: 42 })) eventTime = Math.min(Math.max(eventTime, businessStart), businessEnd);
520
+
521
+ const compareTime = dayjs.unix(eventTime).toISOString();
522
+ if (dayjs(chosenTime).isAfter(dayjs.unix(0))) validTime = true;
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "make-mp-data",
3
- "version": "1.1.14",
3
+ "version": "1.1.16",
4
4
  "description": "builds all mixpanel primitives for a given project",
5
5
  "main": "index.js",
6
6
  "types": "types.d.ts",
@@ -38,10 +38,11 @@
38
38
  },
39
39
  "homepage": "https://github.com/ak--47/make-mp-data#readme",
40
40
  "dependencies": {
41
- "ak-tools": "^1.0.53",
42
- "chance": "^1.1.7",
43
- "dayjs": "^1.11.10",
44
- "mixpanel-import": "^2.5.5",
41
+ "ak-tools": "^1.0.55",
42
+ "chance": "^1.1.11",
43
+ "dayjs": "^1.11.11",
44
+ "mixpanel-import": "^2.5.51",
45
+ "papaparse": "^5.4.1",
45
46
  "yargs": "^17.7.2"
46
47
  }
47
48
  }
@@ -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('./index.js');
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 < 10000; 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
+ });