Пример #1
0
 def test_shift_prev_week(self):
     with patch('zato.common.util._utcnow', self._utcnow):
         now = utcnow()
         info = shift(now, from_utc_to_user(now, self.user_profile), self.user_profile, 'today_prev_day_week', 'hour', 'date_time')
         eq_(info.utc_start, '2012-02-23T00:47:24.054903+00:00')
         eq_(info.utc_stop, '2012-02-23T01:47:24.054903+00:00')
         eq_(info.user_start, '23-02-2012 01:47:24')
         eq_(info.user_stop, '23-02-2012 02:47:24')
         eq_(info.step, None)
Пример #2
0
 def test_shift_prev_hour(self):
     with patch('zato.common.util._utcnow', self._utcnow):
         now = utcnow()
         info = shift(now, from_utc_to_user(now, self.user_profile), self.user_profile, 'last_hour_prev', 'hour', 'date_time')
         eq_(info.utc_start, '2012-02-29T23:47:24.054903+00:00')
         eq_(info.utc_stop, '2012-03-01T00:47:24.054903+00:00')
         eq_(info.user_start, '01-03-2012 00:47:24')
         eq_(info.user_stop, '01-03-2012 01:47:24')
         eq_(info.step, None)
Пример #3
0
 def test_shift_prev_day(self):
     with patch('zato.common.util._utcnow', self._utcnow):
         now = utcnow()
         info = shift(now, from_utc_to_user(now, self.user_profile), self.user_profile, 'today_prev_day', 'hour', 'date_time')
         eq_(info.utc_start, '2012-02-29T00:47:24.054903+00:00')
         eq_(info.utc_stop, '2012-02-29T01:47:24.054903+00:00')
         eq_(info.user_start, '29-02-2012 01:47:24')
         eq_(info.user_stop, '29-02-2012 02:47:24')
         eq_(info.step, None)
Пример #4
0
    def test_access_log(self):

        def _utcnow(self):
            return datetime(year=2014, month=1, day=12, hour=16, minute=22, second=12, tzinfo=UTC)

        local_tz = get_localzone()
        _now = _utcnow(None)

        local_dt = _now.replace(tzinfo=UTC).astimezone(local_tz)
        local_dt = local_tz.normalize(local_dt)

        request_timestamp = local_dt.strftime(ACCESS_LOG_DT_FORMAT)

        with patch('arrow.factory.ArrowFactory.utcnow', _utcnow):
            response = rand_string() * rand_int()
            cid = new_cid()
            cluster_id = 1

            channel_name = rand_string()
            url_path = '/{}'.format(rand_string())
            user_agent = rand_string()
            http_version = rand_string()
            request_method = rand_string()
            remote_ip = '10.{}.{}.{}'.format(rand_int(), rand_int(), rand_int())
            req_timestamp_utc = utcnow()

            channel_item = {
                'name': channel_name,
                'audit_enabled': False,
                'is_active': True,
                'transport': 'plain_http',
                'data_format': None,
                'match_target': url_path
            }

            wsgi_environ = {
                'gunicorn.socket': FakeGunicornSocket(None, None),
                'wsgi.url_scheme': 'http',
                'wsgi.input': StringIO(response),

                'zato.http.response.status': httplib.OK,
                'zato.http.channel_item': channel_item,
                'zato.request_timestamp_utc': req_timestamp_utc,

                'HTTP_X_FORWARDED_FOR': remote_ip,
                'PATH_INFO': url_path,
                'REQUEST_METHOD': request_method,
                'SERVER_PROTOCOL': http_version,
                'HTTP_USER_AGENT': user_agent,
            }

            class FakeBrokerClient(object):
                def __init__(self):
                    self.msg = None

                def publish(self, msg):
                    self.msg = msg

            class FakeODB(ODBManager):
                def __init__(self):
                    self.msg = None
                    self.cluster = Bunch(id=cluster_id)

                def session(self):
                    return fake_session

            class FakeURLData(URLData):
                def __init__(self):
                    self.url_sec = {url_path: Bunch(sec_def=ZATO_NONE)}

                def match(self, *ignored_args, **ignored_kwargs):
                    return True, channel_item

            class FakeRequestHandler(object):
                def handle(self, *ignored_args, **ignored_kwargs):
                    return Bunch(payload=response, content_type='text/plain', headers={}, status_code=httplib.OK)

            class FakeAccessLogger(object):
                def __init__(self):
                    self.extra = {}

                def info(self, msg, extra):
                    self.extra = extra

                def isEnabledFor(self, ignored):
                    return True

            bc = FakeBrokerClient()
            ws = FakeWorkerStore()
            ws.request_dispatcher = RequestDispatcher()
            ws.request_dispatcher.request_handler = FakeRequestHandler()
            ws.request_dispatcher.url_data = FakeURLData()
            ws.request_dispatcher.url_data.broker_client = bc
            ws.request_dispatcher.url_data.odb = FakeODB()

            ps = ParallelServer()
            ps.worker_store = ws
            ps.access_logger = FakeAccessLogger()
            ps.on_wsgi_request(wsgi_environ, StartResponse(), cid=cid)

            extra = Bunch(ps.access_logger.extra)

            eq_(extra.channel_name, channel_name)
            eq_(extra.user_agent, user_agent)
            eq_(extra.status_code, '200')
            eq_(extra.http_version, http_version)
            eq_(extra.response_size, len(response))
            eq_(extra.cid, cid)
            eq_(extra.path, url_path)
            eq_(extra.method, request_method)
            eq_(extra.remote_ip, remote_ip)
            eq_(extra.req_timestamp_utc, '12/Jan/2014:16:22:12 +0000')
            eq_(extra.req_timestamp, request_timestamp)
Пример #5
0
    def test_access_log(self):
        def _utcnow(self):
            return datetime(year=2014,
                            month=1,
                            day=12,
                            hour=16,
                            minute=22,
                            second=12,
                            tzinfo=UTC)

        local_tz = get_localzone()
        _now = _utcnow(None)

        local_dt = _now.replace(tzinfo=UTC).astimezone(local_tz)
        local_dt = local_tz.normalize(local_dt)

        request_timestamp = local_dt.strftime(ACCESS_LOG_DT_FORMAT)

        with patch('arrow.factory.ArrowFactory.utcnow', _utcnow):
            response = rand_string() * rand_int()
            cid = new_cid()
            cluster_id = 1

            channel_name = rand_string()
            url_path = '/{}'.format(rand_string())
            user_agent = rand_string()
            http_version = rand_string()
            request_method = rand_string()
            remote_ip = '10.{}.{}.{}'.format(rand_int(), rand_int(),
                                             rand_int())
            req_timestamp_utc = utcnow()

            channel_item = {
                'name': channel_name,
                'audit_enabled': False,
                'is_active': True,
                'transport': 'plain_http',
                'data_format': None,
                'match_target': url_path,
                'method': '',
            }

            wsgi_environ = {
                'gunicorn.socket': FakeGunicornSocket(None, None),
                'wsgi.url_scheme': 'http',
                'wsgi.input': StringIO(response),
                'zato.http.response.status': httplib.OK,
                'zato.http.channel_item': channel_item,
                'zato.request_timestamp_utc': req_timestamp_utc,
                'HTTP_X_FORWARDED_FOR': remote_ip,
                'PATH_INFO': url_path,
                'REQUEST_METHOD': request_method,
                'SERVER_PROTOCOL': http_version,
                'HTTP_USER_AGENT': user_agent,
            }

            class FakeBrokerClient(object):
                def __init__(self):
                    self.msg = None

                def publish(self, msg):
                    self.msg = msg

            class FakeODB(ODBManager):
                def __init__(self):
                    self.msg = None
                    self.cluster = Bunch(id=cluster_id)

            class FakeURLData(URLData):
                def __init__(self):
                    self.url_sec = {url_path: Bunch(sec_def=ZATO_NONE)}

                def match(self, *ignored_args, **ignored_kwargs):
                    return True, channel_item

            class FakeRequestHandler(object):
                def handle(self, *ignored_args, **ignored_kwargs):
                    return Bunch(payload=response,
                                 content_type='text/plain',
                                 headers={},
                                 status_code=httplib.OK)

            class FakeAccessLogger(object):
                def __init__(self):
                    self.extra = {}

                def info(self, msg, extra):
                    self.extra = extra

                def isEnabledFor(self, ignored):
                    return True

            bc = FakeBrokerClient()
            ws = FakeWorkerStore()
            ws.request_dispatcher = RequestDispatcher()
            ws.request_dispatcher.request_handler = FakeRequestHandler()
            ws.request_dispatcher.url_data = FakeURLData()
            ws.request_dispatcher.url_data.broker_client = bc
            ws.request_dispatcher.url_data.odb = FakeODB()

            ps = ParallelServer()
            ps.worker_store = ws
            ps.access_logger = FakeAccessLogger()
            ps.on_wsgi_request(wsgi_environ, StartResponse(), cid=cid)

            extra = Bunch(ps.access_logger.extra)

            eq_(extra.channel_name, channel_name)
            eq_(extra.user_agent, user_agent)
            eq_(extra.status_code, '200')
            eq_(extra.http_version, http_version)
            eq_(extra.response_size, len(response))
            eq_(extra.path, url_path)
            eq_(extra.cid_resp_time, '{}/0.0'.format(
                cid))  # It's 0.0 because we mock utcnow to be a constant value
            eq_(extra.method, request_method)
            eq_(extra.remote_ip, remote_ip)
            eq_(extra.req_timestamp_utc, '12/Jan/2014:16:22:12 +0000')
            eq_(extra.req_timestamp, request_timestamp)
Пример #6
0
def get_default_date(date_type, user_profile, format):
    """ Returns default start and stop date in UTC and user's timezone depending
    on the stats type and duration requested.
    """
    def get_today(_user_profile, _format):
        """ user_start is today's midnight but it needs to be in user's TZ. user_stop is current time simply,
        in user's timezone again.
        """
        user_now = now(timezone(_user_profile.timezone)).replace(tzinfo=None)
        user_today_midnight = datetime(user_now.year, user_now.month, user_now.day)

        utc_start = from_local_to_utc(user_today_midnight, _user_profile.timezone)
        utc_stop = from_local_to_utc(user_now, _user_profile.timezone)

        user_start = from_utc_to_user(utc_start, _user_profile, _format)
        user_stop = None

        return utc_start, utc_stop, user_start, user_stop

    if date_type == 'last_hour':
        # stop is what current time is now so return it in UTC and user's TZ
        # along with start which will be equal to stop - 1 hour.
        utc_stop = utc.fromutc(utcnow())
        utc_start = utc.fromutc(utc_stop + relativedelta(hours=-1))

        user_start = from_utc_to_user(utc_start, user_profile)
        user_stop = from_utc_to_user(utc_stop, user_profile)

        label = 'Last hour'
        step = 'hour'

    elif date_type == 'today':
        utc_start, utc_stop, user_start, user_stop = get_today(user_profile, format)
        label = 'Today'
        step = 'day'

    elif date_type == 'yesterday':
        # Yesterday's start is today's start - 1 day
        today_utc_start, today_utc_stop, today_user_start, user_stop = get_today(user_profile, format)

        utc_start = today_utc_start + relativedelta(days=-1)
        utc_stop = utc_start + relativedelta(days=1)

        user_start = from_utc_to_user(utc_start, user_profile, format)

        label = 'Yesterday'
        step = 'day'

    elif date_type == 'this_week':
        # This week extends from Monday midnight to right now
        user_now = now(timezone(user_profile.timezone)).replace(tzinfo=None)
        user_prev_monday = user_now + relativedelta(weekday=MO(-1))
        user_prev_monday = datetime(year=user_prev_monday.year, month=user_prev_monday.month, day=user_prev_monday.day)

        utc_start = from_local_to_utc(user_prev_monday, user_profile.timezone)
        utc_stop = from_local_to_utc(user_now, user_profile.timezone)

        user_start = from_utc_to_user(utc_start, user_profile, format)
        user_stop = from_utc_to_user(utc_stop, user_profile, format)

        label = 'This week'
        step = 'week'

    elif date_type == 'this_month':
        # From midnight the first day of month up until now
        user_now = now(timezone(user_profile.timezone)).replace(tzinfo=None)
        user_1st_of_month = datetime(year=user_now.year, month=user_now.month, day=1)

        utc_start = from_local_to_utc(user_1st_of_month, user_profile.timezone)
        utc_stop = from_local_to_utc(user_now, user_profile.timezone)

        user_start = from_utc_to_user(utc_start, user_profile, format)
        user_stop = None

        label = 'This month'
        step = 'month'

    elif date_type == 'this_year':
        # From midnight the first day of year up until now
        user_now = now(timezone(user_profile.timezone)).replace(tzinfo=None)
        user_new_year = datetime(year=user_now.year, month=1, day=1)

        utc_start = from_local_to_utc(user_new_year, user_profile.timezone)
        utc_stop = from_local_to_utc(user_now, user_profile.timezone)

        user_start = from_utc_to_user(utc_start, user_profile, format)
        user_stop = None

        label = 'This year'
        step = 'year'

    else:
        raise ValueError('Unrecognized date_type:[{}]'.format(date_type))

    return DateInfo(utc_start.isoformat(), utc_stop.isoformat(), user_start, user_stop, label, step)
Пример #7
0
def get_default_date(date_type, user_profile, format):
    """ Returns default start and stop date in UTC and user's timezone depending
    on the stats type and duration requested.
    """
    def get_today(_user_profile, _format):
        """ user_start is today's midnight but it needs to be in user's TZ. user_stop is current time simply,
        in user's timezone again.
        """
        user_now = now(timezone(_user_profile.timezone)).replace(tzinfo=None)
        user_today_midnight = datetime(user_now.year, user_now.month,
                                       user_now.day)

        utc_start = from_local_to_utc(user_today_midnight,
                                      _user_profile.timezone)
        utc_stop = from_local_to_utc(user_now, _user_profile.timezone)

        user_start = from_utc_to_user(utc_start, _user_profile, _format)
        user_stop = None

        return utc_start, utc_stop, user_start, user_stop

    if date_type == 'last_hour':
        # stop is what current time is now so return it in UTC and user's TZ
        # along with start which will be equal to stop - 1 hour.
        utc_stop = utc.fromutc(utcnow())
        utc_start = utc.fromutc(utc_stop + relativedelta(hours=-1))

        user_start = from_utc_to_user(utc_start, user_profile)
        user_stop = from_utc_to_user(utc_stop, user_profile)

        label = 'Last hour'
        step = 'hour'

    elif date_type == 'today':
        utc_start, utc_stop, user_start, user_stop = get_today(
            user_profile, format)
        label = 'Today'
        step = 'day'

    elif date_type == 'yesterday':
        # Yesterday's start is today's start - 1 day
        today_utc_start, today_utc_stop, today_user_start, user_stop = get_today(
            user_profile, format)

        utc_start = today_utc_start + relativedelta(days=-1)
        utc_stop = utc_start + relativedelta(days=1)

        user_start = from_utc_to_user(utc_start, user_profile, format)

        label = 'Yesterday'
        step = 'day'

    elif date_type == 'this_week':
        # This week extends from Monday midnight to right now
        user_now = now(timezone(user_profile.timezone)).replace(tzinfo=None)
        user_prev_monday = user_now + relativedelta(weekday=MO(-1))
        user_prev_monday = datetime(year=user_prev_monday.year,
                                    month=user_prev_monday.month,
                                    day=user_prev_monday.day)

        utc_start = from_local_to_utc(user_prev_monday, user_profile.timezone)
        utc_stop = from_local_to_utc(user_now, user_profile.timezone)

        user_start = from_utc_to_user(utc_start, user_profile, format)
        user_stop = from_utc_to_user(utc_stop, user_profile, format)

        label = 'This week'
        step = 'week'

    elif date_type == 'this_month':
        # From midnight the first day of month up until now
        user_now = now(timezone(user_profile.timezone)).replace(tzinfo=None)
        user_1st_of_month = datetime(year=user_now.year,
                                     month=user_now.month,
                                     day=1)

        utc_start = from_local_to_utc(user_1st_of_month, user_profile.timezone)
        utc_stop = from_local_to_utc(user_now, user_profile.timezone)

        user_start = from_utc_to_user(utc_start, user_profile, format)
        user_stop = None

        label = 'This month'
        step = 'month'

    elif date_type == 'this_year':
        # From midnight the first day of year up until now
        user_now = now(timezone(user_profile.timezone)).replace(tzinfo=None)
        user_new_year = datetime(year=user_now.year, month=1, day=1)

        utc_start = from_local_to_utc(user_new_year, user_profile.timezone)
        utc_stop = from_local_to_utc(user_now, user_profile.timezone)

        user_start = from_utc_to_user(utc_start, user_profile, format)
        user_stop = None

        label = 'This year'
        step = 'year'

    else:
        raise ValueError('Unrecognized date_type:[{}]'.format(date_type))

    return DateInfo(utc_start.isoformat(), utc_stop.isoformat(), user_start,
                    user_stop, label, step)