s3db.js 11.0.3 → 11.0.5

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/dist/s3db.es.js CHANGED
@@ -6181,7 +6181,7 @@ async function rollupPeriod(period, cohort, sourcePrefix, analyticsResource, con
6181
6181
  } else if (period === "week") {
6182
6182
  sourcePeriod = "day";
6183
6183
  } else if (period === "month") {
6184
- sourcePeriod = "week";
6184
+ sourcePeriod = "day";
6185
6185
  } else {
6186
6186
  sourcePeriod = "day";
6187
6187
  }
@@ -6514,6 +6514,139 @@ async function getTopRecords(resourceName, field, options, fieldHandlers) {
6514
6514
  });
6515
6515
  return records.slice(0, limit);
6516
6516
  }
6517
+ async function getYearByDay(resourceName, field, year, options, fieldHandlers) {
6518
+ const startDate = `${year}-01-01`;
6519
+ const endDate = `${year}-12-31`;
6520
+ const data = await getAnalytics(resourceName, field, {
6521
+ period: "day",
6522
+ startDate,
6523
+ endDate
6524
+ }, fieldHandlers);
6525
+ if (options.fillGaps) {
6526
+ return fillGaps(data, "day", startDate, endDate);
6527
+ }
6528
+ return data;
6529
+ }
6530
+ async function getWeekByDay(resourceName, field, week, options, fieldHandlers) {
6531
+ const year = parseInt(week.substring(0, 4));
6532
+ const weekNum = parseInt(week.substring(6, 8));
6533
+ const jan4 = new Date(Date.UTC(year, 0, 4));
6534
+ const jan4Day = jan4.getUTCDay() || 7;
6535
+ const firstMonday = new Date(Date.UTC(year, 0, 4 - jan4Day + 1));
6536
+ const weekStart = new Date(firstMonday);
6537
+ weekStart.setUTCDate(weekStart.getUTCDate() + (weekNum - 1) * 7);
6538
+ const days = [];
6539
+ for (let i = 0; i < 7; i++) {
6540
+ const day = new Date(weekStart);
6541
+ day.setUTCDate(weekStart.getUTCDate() + i);
6542
+ days.push(day.toISOString().substring(0, 10));
6543
+ }
6544
+ const startDate = days[0];
6545
+ const endDate = days[6];
6546
+ const data = await getAnalytics(resourceName, field, {
6547
+ period: "day",
6548
+ startDate,
6549
+ endDate
6550
+ }, fieldHandlers);
6551
+ if (options.fillGaps) {
6552
+ return fillGaps(data, "day", startDate, endDate);
6553
+ }
6554
+ return data;
6555
+ }
6556
+ async function getWeekByHour(resourceName, field, week, options, fieldHandlers) {
6557
+ const year = parseInt(week.substring(0, 4));
6558
+ const weekNum = parseInt(week.substring(6, 8));
6559
+ const jan4 = new Date(Date.UTC(year, 0, 4));
6560
+ const jan4Day = jan4.getUTCDay() || 7;
6561
+ const firstMonday = new Date(Date.UTC(year, 0, 4 - jan4Day + 1));
6562
+ const weekStart = new Date(firstMonday);
6563
+ weekStart.setUTCDate(weekStart.getUTCDate() + (weekNum - 1) * 7);
6564
+ const weekEnd = new Date(weekStart);
6565
+ weekEnd.setUTCDate(weekEnd.getUTCDate() + 6);
6566
+ const startDate = weekStart.toISOString().substring(0, 10);
6567
+ const endDate = weekEnd.toISOString().substring(0, 10);
6568
+ const data = await getAnalytics(resourceName, field, {
6569
+ period: "hour",
6570
+ startDate,
6571
+ endDate
6572
+ }, fieldHandlers);
6573
+ if (options.fillGaps) {
6574
+ return fillGaps(data, "hour", startDate, endDate);
6575
+ }
6576
+ return data;
6577
+ }
6578
+ async function getLastNHours(resourceName, field, hours = 24, options, fieldHandlers) {
6579
+ const now = /* @__PURE__ */ new Date();
6580
+ const hoursAgo = new Date(now);
6581
+ hoursAgo.setHours(hoursAgo.getHours() - hours + 1);
6582
+ const startHour = hoursAgo.toISOString().substring(0, 13);
6583
+ const endHour = now.toISOString().substring(0, 13);
6584
+ const data = await getAnalytics(resourceName, field, {
6585
+ period: "hour",
6586
+ startDate: startHour,
6587
+ endDate: endHour
6588
+ }, fieldHandlers);
6589
+ if (options.fillGaps) {
6590
+ const result = [];
6591
+ const emptyRecord = { count: 0, sum: 0, avg: 0, min: 0, max: 0, recordCount: 0 };
6592
+ const dataMap = new Map(data.map((d) => [d.cohort, d]));
6593
+ const current = new Date(hoursAgo);
6594
+ for (let i = 0; i < hours; i++) {
6595
+ const cohort = current.toISOString().substring(0, 13);
6596
+ result.push(dataMap.get(cohort) || { cohort, ...emptyRecord });
6597
+ current.setHours(current.getHours() + 1);
6598
+ }
6599
+ return result;
6600
+ }
6601
+ return data;
6602
+ }
6603
+ async function getLastNWeeks(resourceName, field, weeks = 4, options, fieldHandlers) {
6604
+ const now = /* @__PURE__ */ new Date();
6605
+ const weeksAgo = new Date(now);
6606
+ weeksAgo.setDate(weeksAgo.getDate() - weeks * 7);
6607
+ const weekCohorts = [];
6608
+ const currentDate = new Date(weeksAgo);
6609
+ while (currentDate <= now) {
6610
+ const weekCohort = getCohortWeekFromDate(currentDate);
6611
+ if (!weekCohorts.includes(weekCohort)) {
6612
+ weekCohorts.push(weekCohort);
6613
+ }
6614
+ currentDate.setDate(currentDate.getDate() + 7);
6615
+ }
6616
+ const startWeek = weekCohorts[0];
6617
+ const endWeek = weekCohorts[weekCohorts.length - 1];
6618
+ const data = await getAnalytics(resourceName, field, {
6619
+ period: "week",
6620
+ startDate: startWeek,
6621
+ endDate: endWeek
6622
+ }, fieldHandlers);
6623
+ return data;
6624
+ }
6625
+ async function getLastNMonths(resourceName, field, months = 12, options, fieldHandlers) {
6626
+ const now = /* @__PURE__ */ new Date();
6627
+ const monthsAgo = new Date(now);
6628
+ monthsAgo.setMonth(monthsAgo.getMonth() - months + 1);
6629
+ const startDate = monthsAgo.toISOString().substring(0, 7);
6630
+ const endDate = now.toISOString().substring(0, 7);
6631
+ const data = await getAnalytics(resourceName, field, {
6632
+ period: "month",
6633
+ startDate,
6634
+ endDate
6635
+ }, fieldHandlers);
6636
+ if (options.fillGaps) {
6637
+ const result = [];
6638
+ const emptyRecord = { count: 0, sum: 0, avg: 0, min: 0, max: 0, recordCount: 0 };
6639
+ const dataMap = new Map(data.map((d) => [d.cohort, d]));
6640
+ const current = new Date(monthsAgo);
6641
+ for (let i = 0; i < months; i++) {
6642
+ const cohort = current.toISOString().substring(0, 7);
6643
+ result.push(dataMap.get(cohort) || { cohort, ...emptyRecord });
6644
+ current.setMonth(current.getMonth() + 1);
6645
+ }
6646
+ return result;
6647
+ }
6648
+ return data;
6649
+ }
6517
6650
 
6518
6651
  function addHelperMethods(resource, plugin, config) {
6519
6652
  resource.set = async (id, field, value) => {
@@ -7170,6 +7303,72 @@ class EventualConsistencyPlugin extends Plugin {
7170
7303
  async getTopRecords(resourceName, field, options = {}) {
7171
7304
  return await getTopRecords(resourceName, field, options, this.fieldHandlers);
7172
7305
  }
7306
+ /**
7307
+ * Get analytics for entire year, broken down by days
7308
+ * @param {string} resourceName - Resource name
7309
+ * @param {string} field - Field name
7310
+ * @param {number} year - Year (e.g., 2025)
7311
+ * @param {Object} options - Options
7312
+ * @returns {Promise<Array>} Daily analytics for the year (up to 365/366 records)
7313
+ */
7314
+ async getYearByDay(resourceName, field, year, options = {}) {
7315
+ return await getYearByDay(resourceName, field, year, options, this.fieldHandlers);
7316
+ }
7317
+ /**
7318
+ * Get analytics for entire week, broken down by days
7319
+ * @param {string} resourceName - Resource name
7320
+ * @param {string} field - Field name
7321
+ * @param {string} week - Week in YYYY-Www format (e.g., '2025-W42')
7322
+ * @param {Object} options - Options
7323
+ * @returns {Promise<Array>} Daily analytics for the week (7 records)
7324
+ */
7325
+ async getWeekByDay(resourceName, field, week, options = {}) {
7326
+ return await getWeekByDay(resourceName, field, week, options, this.fieldHandlers);
7327
+ }
7328
+ /**
7329
+ * Get analytics for entire week, broken down by hours
7330
+ * @param {string} resourceName - Resource name
7331
+ * @param {string} field - Field name
7332
+ * @param {string} week - Week in YYYY-Www format (e.g., '2025-W42')
7333
+ * @param {Object} options - Options
7334
+ * @returns {Promise<Array>} Hourly analytics for the week (168 records)
7335
+ */
7336
+ async getWeekByHour(resourceName, field, week, options = {}) {
7337
+ return await getWeekByHour(resourceName, field, week, options, this.fieldHandlers);
7338
+ }
7339
+ /**
7340
+ * Get analytics for last N hours
7341
+ * @param {string} resourceName - Resource name
7342
+ * @param {string} field - Field name
7343
+ * @param {number} hours - Number of hours to look back (default: 24)
7344
+ * @param {Object} options - Options
7345
+ * @returns {Promise<Array>} Hourly analytics
7346
+ */
7347
+ async getLastNHours(resourceName, field, hours = 24, options = {}) {
7348
+ return await getLastNHours(resourceName, field, hours, options, this.fieldHandlers);
7349
+ }
7350
+ /**
7351
+ * Get analytics for last N weeks
7352
+ * @param {string} resourceName - Resource name
7353
+ * @param {string} field - Field name
7354
+ * @param {number} weeks - Number of weeks to look back (default: 4)
7355
+ * @param {Object} options - Options
7356
+ * @returns {Promise<Array>} Weekly analytics
7357
+ */
7358
+ async getLastNWeeks(resourceName, field, weeks = 4, options = {}) {
7359
+ return await getLastNWeeks(resourceName, field, weeks, options, this.fieldHandlers);
7360
+ }
7361
+ /**
7362
+ * Get analytics for last N months
7363
+ * @param {string} resourceName - Resource name
7364
+ * @param {string} field - Field name
7365
+ * @param {number} months - Number of months to look back (default: 12)
7366
+ * @param {Object} options - Options
7367
+ * @returns {Promise<Array>} Monthly analytics
7368
+ */
7369
+ async getLastNMonths(resourceName, field, months = 12, options = {}) {
7370
+ return await getLastNMonths(resourceName, field, months, options, this.fieldHandlers);
7371
+ }
7173
7372
  }
7174
7373
 
7175
7374
  class FullTextPlugin extends Plugin {
@@ -13188,7 +13387,7 @@ class Database extends EventEmitter {
13188
13387
  this.id = idGenerator(7);
13189
13388
  this.version = "1";
13190
13389
  this.s3dbVersion = (() => {
13191
- const [ok, err, version] = tryFn(() => true ? "11.0.3" : "latest");
13390
+ const [ok, err, version] = tryFn(() => true ? "11.0.5" : "latest");
13192
13391
  return ok ? version : "latest";
13193
13392
  })();
13194
13393
  this.resources = {};