Exemplo n.º 1
0
 def test_set_day_offset_none(self):
     tr = TimeRange()
     self.assertIsNone(tr.start)
     self.assertIsNone(tr.start_org)
     tr.set_start_day_offset(5)
     self.assertIsNone(tr.start)
     self.assertIsNone(tr.start_org)
Exemplo n.º 2
0
    def test_set_day_offset_neg(self):
        start_dt = datetime(2020, 2, 14, 1, 1, 1, tzinfo=pytz.UTC)

        tr = TimeRange(start=start_dt)
        self.assertEqual(start_dt, tr.start)
        self.assertEqual(start_dt, tr.start_org)
        tr.set_start_day_offset(-5)
        self.assertEqual(start_dt, tr.start_org)
        self.assertEqual(start_dt - timedelta(days=5), tr.start)
Exemplo n.º 3
0
    def mean_message_count(
            self, channel_oids: Union[ObjectId, List[ObjectId]], *, hours_within: Optional[int] = None,
            start: Optional[datetime] = None, end: Optional[datetime] = None, tzinfo_: PytzInfo = UTC.to_tzinfo(),
            max_mean_days: int = 5) -> \
            MeanMessageResultGenerator:
        match_d = self._channel_oids_filter_(channel_oids)

        trange = TimeRange(range_hr=hours_within,
                           start=start,
                           end=end,
                           tzinfo_=tzinfo_)
        trange.set_start_day_offset(-max_mean_days)

        self._attach_time_range_(match_d, trange=trange)

        pipeline = [{
            "$match": match_d
        }, {
            "$group": {
                "_id": {
                    MeanMessageResultGenerator.KEY_DATE: {
                        "$dateToString": {
                            "date": "$_id",
                            "format": MeanMessageResultGenerator.FMT_DATE,
                            "timezone": tzinfo_.tzidentifier
                        }
                    }
                },
                MeanMessageResultGenerator.KEY_COUNT: {
                    "$sum": 1
                }
            }
        }, {
            "$sort": {
                "_id": pymongo.ASCENDING
            }
        }]

        return MeanMessageResultGenerator(list(self.aggregate(pipeline)),
                                          HourlyResult.data_days_collected(
                                              self,
                                              match_d,
                                              hr_range=hours_within,
                                              start=trange.start_org,
                                              end=end),
                                          tzinfo_,
                                          trange=trange,
                                          max_mean_days=max_mean_days)