added limit to asset queries

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2022-12-21 10:07:21 +01:00
parent 648ce0579a
commit 9e5b7ac62d
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
4 changed files with 28 additions and 6 deletions

View File

@ -55,8 +55,8 @@ def get_transaction(connection, tx_id: str) -> DbTransaction:
return NotImplemented return NotImplemented
@register_query(TarantoolDBConnection) @register_query(TarantoolDBConnection)
def get_transactions_by_asset(connection, asset: str) -> list[DbTransaction]: def get_transactions_by_asset(connection, asset: str, limit: int = 1000) -> list[DbTransaction]:
txs = connection.run(connection.space(TARANT_TABLE_TRANSACTION).select(asset, index="transactions_by_asset_cid")) txs = connection.run(connection.space(TARANT_TABLE_TRANSACTION).select(asset, limit=limit, index="transactions_by_asset_cid"))
tx_ids = [tx[0] for tx in txs] tx_ids = [tx[0] for tx in txs]
return get_complete_transactions_by_ids(connection, tx_ids) return get_complete_transactions_by_ids(connection, tx_ids)

View File

@ -452,8 +452,8 @@ class Planetmint(object):
""" """
return backend.query.get_assets(self.connection, asset_ids) return backend.query.get_assets(self.connection, asset_ids)
def get_assets_by_cid(self, asset_cid) -> list[dict]: def get_assets_by_cid(self, asset_cid, **kwargs) -> list[dict]:
asset_txs = backend.query.get_transactions_by_asset(self.connection, asset_cid) asset_txs = backend.query.get_transactions_by_asset(self.connection, asset_cid, **kwargs)
# flatten and return all found assets # flatten and return all found assets
return list(chain.from_iterable([Asset.list_to_dict(tx.assets) for tx in asset_txs])) return list(chain.from_iterable([Asset.list_to_dict(tx.assets) for tx in asset_txs]))

View File

@ -9,7 +9,7 @@ For more information please refer to the documentation: http://planetmint.io/htt
""" """
import logging import logging
from flask_restful import Resource from flask_restful import Resource, reqparse
from flask import current_app from flask import current_app
from planetmint.backend.exceptions import OperationError from planetmint.backend.exceptions import OperationError
from planetmint.web.views.base import make_error from planetmint.web.views.base import make_error
@ -19,10 +19,17 @@ logger = logging.getLogger(__name__)
class AssetListApi(Resource): class AssetListApi(Resource):
def get(self, cid: str): def get(self, cid: str):
parser = reqparse.RequestParser()
parser.add_argument("limit", type=int)
args = parser.parse_args()
if not args["limit"]:
del args["limit"]
pool = current_app.config["bigchain_pool"] pool = current_app.config["bigchain_pool"]
with pool() as planet: with pool() as planet:
assets = planet.get_assets_by_cid(cid) assets = planet.get_assets_by_cid(cid, **args)
try: try:
# This only works with MongoDB as the backend # This only works with MongoDB as the backend

View File

@ -23,3 +23,18 @@ def test_get_assets_tendermint(client, b, alice):
assert res.status_code == 200 assert res.status_code == 200
assert len(res.json) == 1 assert len(res.json) == 1
assert res.json[0] == {"data": assets[0]["data"]} assert res.json[0] == {"data": assets[0]["data"]}
@pytest.mark.bdb
def test_get_assets_tendermint_limit(client, b, alice, bob):
# create assets
assets = [{"data": multihash(marshal({"msg": "abc"}))}]
tx_1 = Create.generate([alice.public_key], [([alice.public_key], 1)], assets=assets).sign([alice.private_key])
tx_2 = Create.generate([bob.public_key], [([bob.public_key], 1)], assets=assets).sign([bob.private_key])
b.store_bulk_transactions([tx_1, tx_2])
res = client.get(ASSETS_ENDPOINT + assets[0]["data"] + "?limit=1")
assert res.status_code == 200
assert len(res.json) == 1
assert res.json[0] == {"data": assets[0]["data"]}