alignak_backend_client/client.py
def get_all(self, endpoint, params=None):
        # pylint: disable=too-many-locals
        """
        Get all items in the specified endpoint of alignak backend

        If an error occurs, a BackendException is raised.

        If the max_results parameter is not specified in parameters, it is set to
        BACKEND_PAGINATION_LIMIT (backend maximum value) to limit requests number.

        This method builds a response that always contains: _items and _status::

            {
                u'_items': [
                    ...
                ],
                u'_status': u'OK'
            }

        :param endpoint: endpoint (API URL) relative from root endpoint
        :type endpoint: str
        :param params: list of parameters for the backend API
        :type params: dict
        :return: dict of properties
        :rtype: dict
        """
        # Set max results at maximum value supported by the backend to limit requests number
        if not params:
            params = {'max_results': BACKEND_PAGINATION_LIMIT}
        elif params and 'max_results' not in params:
            params['max_results'] = BACKEND_PAGINATION_LIMIT

        # Get first page
        last_page = False
        items = []
        if self.processes == 1:
            while not last_page:
                # Get elements ...
                resp = self.get(endpoint=endpoint, params=params)
                # Response contains:
                # _items:
                # ...
                # _links:
                #  self, parent, prev, last, next
                # _meta:
                # - max_results, total, page

                if 'next' in resp['_links']:
                    # Go to next page ...
                    params['page'] = int(resp['_meta']['page']) + 1
                    params['max_results'] = int(resp['_meta']['max_results'])
                else:
                    last_page = True
                items.extend(resp['_items'])
        else:
            def get_pages(endpoint, params, pages, out_q):
                """
                Function to get pages loaded by multiprocesses

                :param endpoint: endpoint to get data
                :type endpoint: string
                :param params: parameters for get request
                :type params: dict
                :param pages: range of pages to get
                :type pages: list
                :param out_q: Queue object
                :type out_q: multiprocessing.Queue
                :return: None
                """
                multi_items = []
                for page in pages:
                    params['page'] = page
                    resp = self.get(endpoint, params)
                    multi_items.extend(resp['_items'])
                out_q.put(multi_items)

            # Get first page
            resp = self.get(endpoint, params)
            number_pages = int(math.ceil(
                float(resp['_meta']['total']) / float(resp['_meta']['max_results'])))

            out_q = multiprocessing.Queue()
            chunksize = int(math.ceil(number_pages / float(self.processes)))
            procs = []
            for i in range(self.processes):
                begin = i * chunksize
                end = begin + chunksize
                if end > number_pages:
                    end = number_pages
                begin += 1
                end += 1
                p = multiprocessing.Process(target=get_pages,
                                            args=(endpoint, params, range(begin, end), out_q))
                procs.append(p)
                p.start()

            # Collect all results into a single result dict. We know how many dicts
            # with results to expect.
            for i in range(self.processes):
                items.extend(out_q.get())

            # Wait for all worker processes to finish
            for p in procs:
                p.join()

        return {
            '_items': items,
            '_status': 'OK'
        }
def get_all(self, endpoint, params=None):
        # pylint: disable=too-many-locals
        """
        Get all items in the specified endpoint of alignak backend

        If an error occurs, a BackendException is raised.

        If the max_results parameter is not specified in parameters, it is set to
        BACKEND_PAGINATION_LIMIT (backend maximum value) to limit requests number.

        This method builds a response that always contains: _items and _status::

            {
                u'_items': [
                    ...
                ],
                u'_status': u'OK'
            }

        :param endpoint: endpoint (API URL) relative from root endpoint
        :type endpoint: str
        :param params: list of parameters for the backend API
        :type params: dict
        :return: dict of properties
        :rtype: dict
        """
        # Set max results at maximum value supported by the backend to limit requests number
        if not params:
            params = {'max_results': BACKEND_PAGINATION_LIMIT}
        elif params and 'max_results' not in params:
            params['max_results'] = BACKEND_PAGINATION_LIMIT

        # Get first page
        last_page = False
        items = []
        if self.processes == 1:
            while not last_page:
                # Get elements ...
                resp = self.get(endpoint=endpoint, params=params)
                # Response contains:
                # _items:
                # ...
                # _links:
                #  self, parent, prev, last, next
                # _meta:
                # - max_results, total, page

                if 'next' in resp['_links']:
                    # Go to next page ...
                    params['page'] = int(resp['_meta']['page']) + 1
                    params['max_results'] = int(resp['_meta']['max_results'])
                else:
                    last_page = True
                items.extend(resp['_items'])
        else:
            def get_pages(endpoint, params, pages, out_q):
                """
                Function to get pages loaded by multiprocesses

                :param endpoint: endpoint to get data
                :type endpoint: string
                :param params: parameters for get request
                :type params: dict
                :param pages: range of pages to get
                :type pages: list
                :param out_q: Queue object
                :type out_q: multiprocessing.Queue
                :return: None
                """
                multi_items = []
                for page in pages:
                    params['page'] = page
                    resp = self.get(endpoint, params)
                    multi_items.extend(resp['_items'])
                out_q.put(multi_items)

            # Get first page
            resp = self.get(endpoint, params)
            number_pages = int(math.ceil(
                float(resp['_meta']['total']) / float(resp['_meta']['max_results'])))

            out_q = multiprocessing.Queue()
            chunksize = int(math.ceil(number_pages / float(self.processes)))
            procs = []
            for i in range(self.processes):
                begin = i * chunksize
                end = begin + chunksize
                if end > number_pages:
                    end = number_pages
                begin += 1
                end += 1
                p = multiprocessing.Process(target=get_pages,
                                            args=(endpoint, params, range(begin, end), out_q))
                procs.append(p)
                p.start()

            # Collect all results into a single result dict. We know how many dicts
            # with results to expect.
            for i in range(self.processes):
                items.extend(out_q.get())

            # Wait for all worker processes to finish
            for p in procs:
                p.join()

        return {
            '_items': items,
            '_status': 'OK'
        }
Get all items in the specified endpoint of alignak backend

        If an error occurs, a BackendException is raised.

        If the max_results parameter is not specified in parameters, it is set to
        BACKEND_PAGINATION_LIMIT (backend maximum value) to limit requests number.

        This method builds a response that always contains: _items and _status::

            {
                u'_items': [
                    ...
                ],
                u'_status': u'OK'
            }

        :param endpoint: endpoint (API URL) relative from root endpoint
        :type endpoint: str
        :param params: list of parameters for the backend API
        :type params: dict
        :return: dict of properties
        :rtype: dict