# Path: performanceplatform/collector/ga/realtime.py
# class Collector(object):
#     def __init__(self, credentials):
#         self._realtime = Realtime(credentials)
# 
#     def send_records_for(self, query, to):
#         data_set = DataSet.from_config(to)
# 
#         visitor_count = self._realtime.query(query)
# 
#         record = self._create_record(visitor_count,
#                                      query.get('filters', ''))
# 
#         data_set.post(record)
# 
#     def _create_record(self, visitor_count, for_url):
#         timestamp = _timestamp()
#         return {
#             "_timestamp": timestamp,
#             "_id": timestamp,
#             "unique_visitors": visitor_count,
#             "for_url": for_url
#         }
# 
# class Realtime(object):
#     def __init__(self, credentials):
#         self._client = create_client(credentials)
# 
#     def execute_ga_query(self, query):
#         return self._client._service.data().realtime().get(
#             **query
#         ).execute()
# 
#     def query(self, query):
#         response = self.execute_ga_query(query)
# 
#         if "rows" in response:
#             visitor_count = int(response["rows"][0][0])
#         else:
#             visitor_count = 0
# 
#         return visitor_count
# 
# def _timestamp():
#     timezone = pytz.UTC
#     timestamp = datetime.now(timezone).replace(microsecond=0)
#     return timestamp.isoformat()


from freezegun import freeze_time
from mock import Mock, patch, ANY
from nose.tools import *
from performanceplatform.collector.ga.realtime import Collector, Realtime, _timestamp
from hamcrest.library.text.stringmatches import matches_regexp
from hamcrest.library.integration import match_equality
import performanceplatform
import re
import json

        collector = Collector('credentials')
        data_set_config = {"url": 'url', "token": 'token'}

        collector.send_records_for({},
                                   to=data_set_config)

        Realtime.assert_called_with('credentials')
        DataSet.from_config.assert_called_with(data_set_config)

        realtime.query.assert_called_with({})
        data_set.post.assert_called_with({
            '_timestamp': is_timestamp(),
            '_id': is_timestamp(),
            'unique_visitors': 12,
            'for_url': '',
        })

    @patch("performanceplatform.collector.ga.realtime.Realtime")
    def test_sending_records_fails_with_invalid_target(self, _):
        collector = Collector(None)
        assert_raises(KeyError, collector.send_records_for,
                      None, {'foo': 'bar'})

    @patch("performanceplatform.collector.ga.realtime.DataSet")
    @patch("performanceplatform.collector.ga.realtime.Realtime")
    def test_sending_records_sends_for_url(self, _, DataSet):
        data_set = mock_instance(DataSet)
        DataSet.from_config.return_value = data_set


# Path: performanceplatform/collector/ga/realtime.py
# class Collector(object):
#     def __init__(self, credentials):
#         self._realtime = Realtime(credentials)
# 
#     def send_records_for(self, query, to):
#         data_set = DataSet.from_config(to)
# 
#         visitor_count = self._realtime.query(query)
# 
#         record = self._create_record(visitor_count,
#                                      query.get('filters', ''))
# 
#         data_set.post(record)
# 
#     def _create_record(self, visitor_count, for_url):
#         timestamp = _timestamp()
#         return {
#             "_timestamp": timestamp,
#             "_id": timestamp,
#             "unique_visitors": visitor_count,
#             "for_url": for_url
#         }
# 
# class Realtime(object):
#     def __init__(self, credentials):
#         self._client = create_client(credentials)
# 
#     def execute_ga_query(self, query):
#         return self._client._service.data().realtime().get(
#             **query
#         ).execute()
# 
#     def query(self, query):
#         response = self.execute_ga_query(query)
# 
#         if "rows" in response:
#             visitor_count = int(response["rows"][0][0])
#         else:
#             visitor_count = 0
# 
#         return visitor_count
# 
# def _timestamp():
#     timezone = pytz.UTC
#     timestamp = datetime.now(timezone).replace(microsecond=0)
#     return timestamp.isoformat()


# Path: tests/performanceplatform/collector/ga/test_realtime.py
from freezegun import freeze_time
from mock import Mock, patch, ANY
from nose.tools import *
from performanceplatform.collector.ga.realtime import Collector, Realtime, _timestamp
from hamcrest.library.text.stringmatches import matches_regexp
from hamcrest.library.integration import match_equality
import performanceplatform
import re
import json


        collector = Collector('credentials')
        data_set_config = {"url": 'url', "token": 'token'}

        collector.send_records_for({},
                                   to=data_set_config)

        Realtime.assert_called_with('credentials')
        DataSet.from_config.assert_called_with(data_set_config)

        realtime.query.assert_called_with({})
        data_set.post.assert_called_with({
            '_timestamp': is_timestamp(),
            '_id': is_timestamp(),
            'unique_visitors': 12,
            'for_url': '',
        })

    @patch("performanceplatform.collector.ga.realtime.Realtime")
    def test_sending_records_fails_with_invalid_target(self, _):
        collector = Collector(None)
        assert_raises(KeyError, collector.send_records_for,
                      None, {'foo': 'bar'})

    @patch("performanceplatform.collector.ga.realtime.DataSet")
    @patch("performanceplatform.collector.ga.realtime.Realtime")
    def test_sending_records_sends_for_url(self, _, DataSet):
        data_set = mock_instance(DataSet)
        DataSet.from_config.return_value = data_set


        collector = Collector(None)