Ejemplo n.º 1
0
    def test_get_skip_and_get_take(self):
        paging = PagingParams(25, 50, False)
        assert 50 == paging.get_skip(50)
        assert 25 == paging.get_skip(10)
        assert 50 == paging.get_take(100)
        assert 25 == paging.get_take(25)

        paging = PagingParams()
        assert 10 == paging.get_skip(10)
        assert 10 == paging.get_skip(10)
Ejemplo n.º 2
0
    def get_page_by_filter(self, correlation_id: Optional[str],
                           filter: FilterParams,
                           paging: PagingParams) -> DataPage:
        filters = filter if filter is not None else FilterParams()
        key = filters.get_as_nullable_string("key")

        paging = paging if not (paging is None) else PagingParams()
        skip = paging.get_skip(0)
        take = paging.get_take(100)

        result = []
        self.__lock.acquire()
        try:
            for item in self.__items:
                if not (key is None) and key != item.key:
                    continue

                skip -= 1
                if skip >= 0: continue

                take -= 1
                if take < 0: break

                result.append(item)
        finally:
            self.__lock.release()

        return DataPage(result)
    def get_page_by_filter(self,
                           correlation_id: Optional[str],
                           filter: Any,
                           paging: PagingParams,
                           sort: Any = None,
                           select: Any = None) -> DataPage:
        """
        Gets a page of data items retrieved by a given filter and sorted according to sort parameters.

        This method shall be called by a public :func:`get_page_by_filter` method from child class that
        receives :class:`FilterParams <pip_services3_commons.data.FilterParams.FilterParams>` and converts them into a filter function.

        :param correlation_id: (optional) transaction id to trace execution through call chain.

        :param filter: (optional) a filter function to filter items

        :param paging: (optional) paging parameters

        :param sort: (optional) sorting parameters

        :param select: (optional) projection parameters (not used yet)

        :return: a data page of result by filter.
        """
        with self._lock:
            items = deepcopy(self._items)

        # Filter and sort
        if filter is not None:
            items = list(filtered(filter, items))
        if sort is not None:
            items = list(filtered(sort, items))
            # items = sorted(items, sort)

        # Prepare paging parameters
        paging = paging if paging is not None else PagingParams()
        skip = paging.get_skip(-1)
        take = paging.get_take(self._max_page_size)

        # Get a page
        data = items
        if skip > 0:
            data = data[skip:]
        if take > 0:
            data = data[:take]

        # Convert values
        if not (select is None):
            data = map(select, data)

        self._logger.trace(correlation_id,
                           "Retrieved " + str(len(data)) + " items")

        # Return a page
        return DataPage(data, len(items))
Ejemplo n.º 4
0
    def get_page_by_filter(self, correlation_id: Optional[str], filter: Any, paging: PagingParams,
                           sort: Any, select: Any) -> DataPage:
        """
        Gets a page of data items retrieved by a given filter and sorted according to sort parameters.
        This method shall be called by a public getPageByFilter method from child class that
        receives FilterParams and converts them into a filter function.

        :param correlation_id: (optional) transaction id to trace execution through call chain.
        :param filter: (optional) a filter JSON object
        :param paging: (optional) paging parameters
        :param sort: (optional) sorting JSON object
        :param select: (optional) projection JSON object
        :return: a data page or raise error
        """
        select = select if select and len(select) > 0 else '*'
        query = "SELECT " + select + " FROM " + self._quoted_table_name()

        # Adjust max item count based on configuration
        paging = paging or PagingParams()
        skip = paging.get_skip(-1)
        take = paging.get_take(self._max_page_size)
        paging_enabled = paging.total

        if filter and filter != '':
            query += " WHERE " + filter

        if sort:
            query += " ORDER BY " + sort

        query += " LIMIT " + str(take)
        
        if skip >= 0:
            query += " OFFSET " + str(skip)

        result = self._request(query)
        items = result['items']

        if items is not None:
            self._logger.trace(correlation_id, "Retrieved %d from %s", len(items), self._table_name)

        items = list(map(self._convert_to_public, items))

        if paging_enabled:
            query = 'SELECT COUNT(*) AS count FROM ' + self._quoted_table_name()
            if filter is not None and filter != '':
                query += " WHERE " + filter
            result = self._request(query)
            count = LongConverter.to_long(len(result['items'][0])) if result and len(result['items']) == 1 else 0

            return DataPage(items, count)
        else:
            return DataPage(items)
Ejemplo n.º 5
0
    def get_page_by_filter(self,
                           correlation_id: Optional[str],
                           filter: Any,
                           paging: PagingParams,
                           sort: Any = None,
                           select: Any = None) -> DataPage:
        """
        Gets a page of data items retrieved by a given filter and sorted according to sort parameters.

        This method shall be called by a public get_page_by_filter method from child class that
        receives FilterParams and converts them into a filter function.

        :param correlation_id: (optional) transaction id to trace execution through call chain.
        :param filter: (optional) a filter JSON object
        :param paging: (optional) paging parameters
        :param sort: (optional) sorting JSON object
        :param select: (optional) projection JSON object
        :return: a data page of result by filter
        """
        # Adjust max item count based on configuration
        paging = paging if paging is not None else PagingParams()
        skip = paging.get_skip(-1)
        take = paging.get_take(self._max_page_size)
        paging_enabled = paging.total

        # Configure statement
        statement = self._collection.find(filter, projection=select or {})

        if skip >= 0:
            statement = statement.skip(skip)
        statement = statement.limit(take)
        if sort is not None:
            statement = statement.sort(sort)

        # Retrive page items
        items = []
        for item in statement:
            item = self._convert_to_public(item)
            items.append(item)

        if items:
            self._logger.trace(correlation_id, "Retrieved %d from %s",
                               len(items), self._collection_name)

        # Calculate total if needed
        total = None
        if paging_enabled:
            total = self._collection.count_documents(filter)

        return DataPage(items, total)
Ejemplo n.º 6
0
    def get_accounts(self, correlation_id: Optional[str],
                     filter_params: FilterParams,
                     paging: PagingParams) -> DataPage:
        filter_curl = self.__compose_filter(filter_params)
        accounts = list(filter(filter_curl, self.__accounts))

        # Extract a page
        paging = PagingParams() if paging is None else paging
        skip = paging.get_skip(-1)
        take = paging.get_take(self.__max_page_size)

        total = None
        if paging.total:
            total = len(accounts)

        if skip > 0:
            accounts = accounts[skip:]

        accounts = accounts[:take]

        page = DataPage(accounts, total)

        return page