def test_get_gprs_downloaded_data(self):
     """We can get timeseries GPRS downloaded byte data."""
     level = 'global'
     kind = 'downloaded_data'
     gprs_stats_client = stats_client.GPRSStatsClient(level)
     data = gprs_stats_client.timeseries(kind=kind)
     data_sum = sum(zip(*data)[1])
     expected_data_total = (
         DOWNLOADED_BYTES * self.number_of_gprs_events_bts_one +
         DOWNLOADED_BYTES * self.number_of_gprs_events_bts_two
     )
     # Convert the data to MB.
     expected_data_total = expected_data_total / 2.**20
     self.assertEqual(expected_data_total, data_sum)
 def test_get_network_gprs_timeseries(self):
     """We can get timeseries GPRS stats for a specific network."""
     level = 'network'
     level_id = self.network_a.id
     gprs_stats_client = stats_client.GPRSStatsClient(level,
                                                      level_id=level_id)
     data = gprs_stats_client.timeseries()
     data_sum = sum(zip(*data)[1])
     # Only BTS one is on Network A so we should only see that BTS's events.
     expected_data_total = (
         UPLOADED_BYTES * self.number_of_gprs_events_bts_one +
         DOWNLOADED_BYTES * self.number_of_gprs_events_bts_one
     )
     # Convert the data to MB.
     expected_data_total = expected_data_total / 2.**20
     self.assertEqual(expected_data_total, data_sum)
 def test_get_global_gprs_timeseries(self):
     """We can get a timeseries of GPRS data with GPRS client."""
     level = 'global'
     gprs_stats_client = stats_client.GPRSStatsClient(level)
     data = gprs_stats_client.timeseries()
     # The timeseries method returns a list and the last value should be
     # (millisecond timestamp, <total amount of data used>).  Since we
     # haven't specified a timespan or a kind, the client will return all of
     # the data.
     timestamp, _ = data[-1]
     self.assertTrue(isinstance(timestamp, int))
     data_sum = sum(zip(*data)[1])
     expected_data_total = (
         UPLOADED_BYTES * self.number_of_gprs_events_bts_one +
         DOWNLOADED_BYTES * self.number_of_gprs_events_bts_one +
         UPLOADED_BYTES * self.number_of_gprs_events_bts_two +
         DOWNLOADED_BYTES * self.number_of_gprs_events_bts_two
     )
     # Convert the data to MB.
     expected_data_total = expected_data_total / 2.**20
     self.assertEqual(expected_data_total, data_sum)
 def test_get_gprs_with_date_range_and_interval(self):
     """We can specify a date range and interval."""
     level = 'global'
     gprs_stats_client = stats_client.GPRSStatsClient(level)
     # Setup client params, converting datetimes to timestamps.
     start_datetime = TIME_OF_LAST_EVENT - timedelta(hours=96)
     start_timestamp = calendar.timegm(start_datetime.utctimetuple())
     end_timestamp = calendar.timegm(TIME_OF_LAST_EVENT.utctimetuple())
     data = gprs_stats_client.timeseries(start_time_epoch=start_timestamp,
                                         end_time_epoch=end_timestamp,
                                         interval='days')
     days, values = zip(*data)
     # The interval comes back from qsstats with an extra day at the
     # beginning.
     expected_datetimes = [TIME_OF_LAST_EVENT - timedelta(hours=96),
                           TIME_OF_LAST_EVENT - timedelta(hours=72),
                           TIME_OF_LAST_EVENT - timedelta(hours=48),
                           TIME_OF_LAST_EVENT - timedelta(hours=24),
                           TIME_OF_LAST_EVENT]
     # TIME_OF_LAST_EVENT has hour/minute info -- we need to clear that out
     # because the qsstats datetimes are, in this example, days only.
     expected_days = [dt.replace(hour=0, minute=0) for dt in
                      expected_datetimes]
     # And finally we have to convert these to millisecond timestamps.
     expected_timestamps = [int(1e3 * calendar.timegm(dt.utctimetuple()))
                            for dt in expected_days]
     self.assertSequenceEqual(expected_timestamps, days)
     # We expect 110 total GPRS events to be created and we can work out how
     # they will accumulate day-to-day by knowing that we added one usage
     # event of each type per hour, counting back from 8:15a (see
     # _add_usage_events for the timings).
     expected_event_counts = [0+0, 0+3, 17+24, 24+24, 9+9]
     bytes_per_event = UPLOADED_BYTES + DOWNLOADED_BYTES
     expected_values = [bytes_per_event * i for i in expected_event_counts]
     # Convert the data to MB.
     expected_values = [e / 2.**20 for e in expected_values]
     self.assertSequenceEqual(expected_values, values)