Skip to content
This repository has been archived by the owner on Mar 27, 2021. It is now read-only.

Fix dailySummary static method on Snapshot model #1

Open
JasonBernert opened this issue Aug 11, 2017 · 1 comment
Open

Fix dailySummary static method on Snapshot model #1

JasonBernert opened this issue Aug 11, 2017 · 1 comment
Assignees
Labels

Comments

@JasonBernert
Copy link
Owner

JasonBernert commented Aug 11, 2017

I've tried a couple ways to aggregate all the snapshots on one day and order them by time. There are both unexpected outcomes (out of order days/snapshots and skipped snapshots) and the struggle of defining a daily summary. It be easy to aggregate by sectionIdentifier or date, but if I go to bed after midnight, which happens 6% of the time, it'd be on the next day.

This method returns all the days in the correct order, but the snapshots are still out of order and post after midnight are on the next day.

snapshotSchema.statics.dailySummary = function () {
  return this.aggregate([
    { $group: {
      _id: { year: { $year: '$date' }, month: { $month: '$date' }, day: { $dayOfMonth: '$date' } },
      count: { $sum: 1 },
      snapshots: { $push: { _id: '$_id', date: '$date', responses: '$responses' } }
    } },
    { $project: {
      snapshots: '$snapshots',
      count: 1,
      _id: 1
    } },
    { $sort: { '_id.year': -1, '_id.month': -1, '_id.day': -1 } }
  ]);
 };

This method returns days mostly in order, but skips some.

snapshotSchema.statics.dailySummary = function () {
   return this.aggregate([
     { $sort: { date: 1 } },
     { $group: {
       _id: '$sectionIdentifier',
       count: { $sum: 1 },
       snapshots: { $push: { _id: '$_id', date: '$date', responses: '$responses' } }
     } },
   ]);
 };

Maybe this would work for aggregating snapshots from when I wake up to when I go to bed?

  1. Sort snapshots by oldest first
  2. Find where reportImpetus === 4, aka waking up.
  3. Then find the next report where reportImpetus === 3, aka sleeping.
  4. Then group those reports and reports in between with a date id of the "waking day"
  5. Then sort grouped by id/date
@JasonBernert
Copy link
Owner Author

This commit at least gets the days and snapshots in the right order, but doesn't solve the going to bed after midnight problem. Also, getting a little verbose...

snapshotSchema.statics.dailySummary = function () {
  return this.aggregate([
    { $group: {
      _id: '$sectionIdentifier',
      count: { $sum: 1 },
      time: { $push: { year: { $year: '$date' }, month: { $month: '$date' }, day: { $dayOfMonth: '$date' } } },
      snapshots: { $push: { _id: '$_id', date: '$date', responses: '$responses' } }
    } },
    { $project: {
      snapshots: '$snapshots',
      count: 1,
      _id: 1
    } },
    { $sort: { 'time.year': -1, 'time.month': -1, 'time.day': -1, 'snapshots.date': -1 } }
  ]);
};

@JasonBernert JasonBernert self-assigned this Aug 11, 2017
JasonBernert added a commit that referenced this issue Sep 7, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant