コード例 #1
0
 def setUp(self):
     self.period_begin = tzutils.local_to_utc(
         tzutils.get_month_start()).isoformat()
     self.period_end = tzutils.local_to_utc(
         tzutils.get_next_month()).isoformat()
     self.client = influx.InfluxClient()
     self._storage = influx.InfluxStorage()
コード例 #2
0
ファイル: test_tz.py プロジェクト: wanghuiict/cloudkitty
 def test_get_month_start_no_arg(self):
     naive_utc_now = timeutils.utcnow()
     naive_month_start = datetime.datetime(naive_utc_now.year,
                                           naive_utc_now.month, 1)
     month_start = tzutils.get_month_start()
     self.assertIsNotNone(month_start.tzinfo)
     self.assertEqual(naive_month_start, month_start.replace(tzinfo=None))
コード例 #3
0
 def total(self,
           groupby=None,
           begin=None,
           end=None,
           metric_types=None,
           filters=None,
           offset=0,
           limit=1000,
           paginate=True):
     begin, end = self._local_to_utc(begin or tzutils.get_month_start(), end
                                     or tzutils.get_next_month())
     total, docs = self._conn.total(begin,
                                    end,
                                    metric_types,
                                    filters,
                                    groupby,
                                    offset=offset,
                                    limit=limit,
                                    paginate=paginate)
     return {
         'total':
         total,
         'results':
         [self._doc_to_total_result(doc, begin, end) for doc in docs],
     }
コード例 #4
0
ファイル: fixtures.py プロジェクト: stackhpc/cloudkitty
 def initialize_data(self):
     dt = tzutils.get_month_start(naive=True).replace(tzinfo=tz.UTC)
     hour_delta = datetime.timedelta(seconds=3600)
     limit = dt + hour_delta * 12
     while dt < limit:
         project_id = '3d9a1b33-482f-42fd-aef9-b575a3da9369'
         data = self.create_fake_data(dt, dt + hour_delta, project_id)
         self.storage.push(data, project_id)
         dt += hour_delta
コード例 #5
0
 def retrieve(self, begin=None, end=None,
              filters=None,
              metric_types=None,
              offset=0, limit=1000, paginate=True):
     begin, end = self._local_to_utc(begin or tzutils.get_month_start(),
                                     end or tzutils.get_next_month())
     total, docs = self._conn.retrieve(
         begin, end, filters, metric_types,
         offset=offset, limit=limit, paginate=paginate)
     return {
         'total': total,
         'dataframes': self._build_dataframes(docs),
     }
コード例 #6
0
    def get(self,
            response_format=TABLE_RESPONSE_FORMAT,
            custom_fields=None,
            groupby=None,
            filters={},
            begin=None,
            end=None,
            offset=0,
            limit=100):

        if response_format not in ALL_RESPONSE_FORMATS:
            raise voluptuous.Invalid("Invalid response format [%s]. Valid "
                                     "format are [%s]." %
                                     (response_format, ALL_RESPONSE_FORMATS))

        policy.authorize(flask.request.context, 'summary:get_summary',
                         {'project_id': flask.request.context.project_id})
        begin = begin or tzutils.get_month_start()
        end = end or tzutils.get_next_month()

        if not flask.request.context.is_admin:
            if flask.request.context.project_id is None:
                # Unscoped non-admin user
                return {
                    'total': 0,
                    'columns': [],
                    'results': [],
                }
            filters['project_id'] = flask.request.context.project_id

        metric_types = filters.pop('type', [])
        if not isinstance(metric_types, list):
            metric_types = [metric_types]

        arguments = {
            'begin': begin,
            'end': end,
            'groupby': groupby,
            'filters': filters,
            'metric_types': metric_types,
            'offset': offset,
            'limit': limit,
            'paginate': True
        }
        if custom_fields:
            arguments['custom_fields'] = custom_fields

        total = self._storage.total(**arguments)

        return self.generate_response(response_format, total)
コード例 #7
0
ファイル: summary.py プロジェクト: simhaonline/cloudkitty
    def get(self,
            custom_fields=None,
            groupby=None,
            filters={},
            begin=None,
            end=None,
            offset=0,
            limit=100):
        policy.authorize(flask.request.context, 'summary:get_summary',
                         {'project_id': flask.request.context.project_id})
        begin = begin or tzutils.get_month_start()
        end = end or tzutils.get_next_month()

        if not flask.request.context.is_admin:
            if flask.request.context.project_id is None:
                # Unscoped non-admin user
                return {
                    'total': 0,
                    'columns': [],
                    'results': [],
                }
            filters['project_id'] = flask.request.context.project_id

        metric_types = [filters.pop('type')] if 'type' in filters else None
        arguments = {
            'begin': begin,
            'end': end,
            'groupby': groupby,
            'filters': filters,
            'metric_types': metric_types,
            'offset': offset,
            'limit': limit,
            'paginate': True
        }
        if custom_fields:
            arguments['custom_fields'] = custom_fields

        total = self._storage.total(**arguments)
        columns = []
        if len(total['results']) > 0:
            columns = list(total['results'][0].keys())

        return {
            'total': total['total'],
            'columns': columns,
            'results': [list(res.values()) for res in total['results']]
        }
コード例 #8
0
 def test_non_admin_request_is_filtered_on_project_id(self):
     policy_mock = mock.patch('cloudkitty.common.policy.authorize')
     with mock.patch.object(self.endpoint._storage, 'retrieve') as ret_mock:
         with policy_mock, mock.patch('flask.request') as fmock:
             ret_mock.return_value = {'total': 42, 'dataframes': []}
             fmock.args.lists.return_value = []
             fmock.context.is_admin = False
             fmock.context.project_id = 'test-project'
             self.endpoint.get()
             ret_mock.assert_called_once_with(
                 begin=tzutils.get_month_start(),
                 end=tzutils.get_next_month(),
                 metric_types=None,
                 filters={'project_id': 'test-project'},
                 offset=0,
                 limit=100,
             )
コード例 #9
0
ファイル: test_summary.py プロジェクト: stackhpc/cloudkitty
 def test_type_filter_is_passed_separately(self):
     policy_mock = mock.patch('cloudkitty.common.policy.authorize')
     with mock.patch.object(self.endpoint._storage, 'total') as total_mock:
         with policy_mock, mock.patch('flask.request') as fmock:
             total_mock.return_value = {'total': 0, 'results': []}
             fmock.args.lists.return_value = [
                 ('filters', 'a:b,type:awesome')]
             self.endpoint.get()
             total_mock.assert_called_once_with(
                 begin=tzutils.get_month_start(),
                 end=tzutils.get_next_month(),
                 groupby=None,
                 filters={'a': 'b'},
                 metric_types=['awesome'],
                 offset=0,
                 limit=100,
                 paginate=True,
             )
コード例 #10
0
    def get(self, offset=0, limit=100, begin=None, end=None, filters=None):

        policy.authorize(
            flask.request.context,
            'dataframes:get',
            {'tenant_id': flask.request.context.project_id},
        )

        begin = begin or tzutils.get_month_start()
        end = end or tzutils.get_next_month()

        if filters and 'type' in filters:
            metric_types = [filters.pop('type')]
        else:
            metric_types = None

        if not flask.request.context.is_admin:
            if flask.request.context.project_id is None:
                # Unscoped non-admin user
                return {'total': 0, 'dataframes': []}
            scope_key = CONF.collect.scope_key
            if filters:
                filters[scope_key] = flask.request.context.project_id
            else:
                filters = {scope_key: flask.request.context.project_id}

        results = self._storage.retrieve(
            begin=begin,
            end=end,
            filters=filters,
            metric_types=metric_types,
            offset=offset,
            limit=limit,
        )

        if results['total'] < 1:
            raise http_exceptions.NotFound(
                "No resource found for provided filters.")

        return {
            'total': results['total'],
            'dataframes': results['dataframes'],
        }
コード例 #11
0
ファイル: summary.py プロジェクト: noyonict/cloudkitty
    def get(self,
            groupby=None,
            filters={},
            begin=None,
            end=None,
            offset=0,
            limit=100):
        policy.authorize(flask.request.context, 'summary:get_summary',
                         {'project_id': flask.request.context.project_id})
        begin = begin or tzutils.get_month_start()
        end = end or tzutils.get_next_month()

        if not flask.request.context.is_admin:
            if flask.request.context.project_id is None:
                # Unscoped non-admin user
                return {
                    'total': 0,
                    'columns': [],
                    'results': [],
                }
            filters['project_id'] = flask.request.context.project_id

        metric_types = [filters.pop('type')] if 'type' in filters else None
        total = self._storage.total(
            begin=begin,
            end=end,
            groupby=groupby,
            filters=filters,
            metric_types=metric_types,
            offset=offset,
            limit=limit,
            paginate=True,
        )
        columns = []
        if len(total['results']) > 0:
            columns = list(total['results'][0].keys())

        return {
            'total': total['total'],
            'columns': columns,
            'results': [list(res.values()) for res in total['results']]
        }
コード例 #12
0
def check_time_state(timestamp=None, period=0, wait_periods=0):
    """Checks the state of a timestamp compared to the current time.

    Returns the next timestamp based on the current timestamp and the period if
    the next timestamp is inferior to the current time and the waiting period
    or None if not.

    :param timestamp: Current timestamp
    :type timestamp: datetime.datetime
    :param period: Period, in seconds
    :type period: int
    :param wait_periods: periods to wait before the current timestamp.
    :type wait_periods: int
    :rtype: datetime.datetime
    """
    if not timestamp:
        return tzutils.get_month_start()

    period_delta = datetime.timedelta(seconds=period)
    next_timestamp = tzutils.add_delta(timestamp, period_delta)
    wait_time = wait_periods * period_delta
    if tzutils.add_delta(next_timestamp, wait_time) < tzutils.localized_now():
        return next_timestamp
    return None
コード例 #13
0
 def _check_begin_end(begin, end):
     if not begin:
         begin = tzutils.get_month_start()
     if not end:
         end = tzutils.get_next_month()
     return tzutils.local_to_utc(begin), tzutils.local_to_utc(end)
コード例 #14
0
ファイル: fixtures.py プロジェクト: stackhpc/cloudkitty
 def initialize_data(self):
     data = test_utils.generate_v2_storage_data(
         start=tzutils.get_month_start(),
         end=tzutils.localized_now().replace(hour=0),
     )
     self.storage.push([data])
コード例 #15
0
ファイル: test_tz.py プロジェクト: wanghuiict/cloudkitty
 def test_get_month_start_with_arg(self):
     param = datetime.datetime(2019, 1, 3, 4, 5)
     month_start = tzutils.get_month_start(param)
     self.assertIsNotNone(month_start.tzinfo)
     self.assertEqual(month_start.replace(tzinfo=None),
                      datetime.datetime(2019, 1, 1))
コード例 #16
0
ファイル: test_tz.py プロジェクト: wanghuiict/cloudkitty
 def test_get_month_start_with_arg_naive(self):
     param = datetime.datetime(2019, 1, 3, 4, 5)
     month_start = tzutils.get_month_start(param, naive=True)
     self.assertIsNone(month_start.tzinfo)
     self.assertEqual(month_start, datetime.datetime(2019, 1, 1))