def test_querying_for_stats(self, send_request):

        def mock_send_request(*args, **kwargs):
            if kwargs["path"] == "summary.performance/12345":
                return {"summary": {"hours": []}}
            if kwargs["path"] == "checks":
                return {"checks": [{"name": "Foo", "id": 12345}]}

        send_request.side_effect = mock_send_request

        pingdom = Pingdom(self.config)

        uptime = pingdom.stats_for_24_hours(
            name='Foo',
            limit_time=datetime(2013, 1, 1, 18, 0, 0)
        )

        send_request.assert_called_with(
            path="summary.performance/12345",
            user="******",
            password="******",
            app_key="12345",
            url_params={
                "includeuptime": "true",
                "from": unix_timestamp(datetime(2012, 12, 31, 18, 0, 0)),
                "to": unix_timestamp(datetime(2013, 1, 1, 18, 0, 0)),
                "resolution": "hour"
            }
        )

        assert_that(uptime, is_([]))
    def test_stats_returns_none_when_there_is_an_error(self, send_request):
        send_request.side_effect = requests.exceptions.HTTPError()

        pingdom = Pingdom(self.config)
        mock_check_id = Mock()
        mock_check_id.return_value = '12345'
        pingdom.check_id = mock_check_id
        uptime = pingdom.stats_for_24_hours(name="don't care",
                                            limit_time=date(2013, 1, 1))
        assert_that(uptime, is_(None))
    def test_response_unixtime_converted_to_isodate(self, mock_get_request):
        mock_response = Mock()
        mock_response.json.return_value = {
            'summary': {
                'hours': [{'starttime': 1356998400}, {'starttime': 1356998500}]
            }
        }
        mock_get_request.return_value = mock_response

        pingdom = Pingdom(self.config)

        mock_check_id = Mock()
        mock_check_id.return_value = '12345'
        pingdom.check_id = mock_check_id
        uptime = pingdom.stats_for_24_hours(name='Foo',
                                            limit_time=date(2013, 1, 1))

        assert_that(uptime[0]['starttime'],
                    is_(datetime(2013, 1, 1, 0, tzinfo=pytz.UTC)))
        assert_that(uptime[1]['starttime'],
                    is_(datetime(2013, 1, 1, 0, 1, 40, tzinfo=pytz.UTC)))
        'check': name_of_check
    }


def truncate_hour_fraction(a_datetime):
    return a_datetime.replace(minute=0, second=0, microsecond=0)


if __name__ == '__main__':
    app_path = os.path.dirname(os.path.realpath(__file__))
    logfile_path = os.path.join(app_path, 'log')
    set_up_logging('pingdom', logging.DEBUG, logfile_path)

    args = arguments.parse_args(name="Pingdom")

    collection_date = datetime.now()
    if args.end_at:
        collection_date = args.end_at

    pingdom = Pingdom(args.credentials)

    check_name = args.query['query']['name']
    timestamp = truncate_hour_fraction(collection_date)
    pingdom_stats = pingdom.stats_for_24_hours(check_name, timestamp)

    bucket_url = args.query['target']['bucket']
    bucket_token = args.query['target']['token']
    bucket = Bucket(url=bucket_url, token=bucket_token)
    bucket.post([convert_from_pingdom_to_backdrop(thing, check_name) for
                 thing in pingdom_stats])