Skip to content

Commit

Permalink
get_bundles_in_drs
Browse files Browse the repository at this point in the history
  • Loading branch information
BinamB authored and BinamB committed May 5, 2020
1 parent ffdefdb commit c752578
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
27 changes: 25 additions & 2 deletions indexd/drs/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from indexd.errors import UnexpectedError

# from indexd.index.blueprint import get_index
from indexd.index.blueprint import get_index
from indexd.index.blueprint import get_index, get_bundle_record_list

blueprint = flask.Blueprint("drs", __name__)

Expand Down Expand Up @@ -33,8 +33,31 @@ def get_drs_object(object_id):

@blueprint.route("/ga4gh/drs/v1/objects", methods=["GET"])
def list_drs_records():
limit = flask.request.args.get("limit")
start = flask.request.args.get("start")
page = flask.request.args.get("page")

try:
limit = 100 if limit is None else int(limit)
except ValueError as err:
raise UserError("limit must be an integer")

if limit < 0 or limit > 1024:
raise UserError("limit must be between 0 and 1024")

if page is not None:
try:
page = int(page)
except ValueError as err:
raise UserError("page must be an integer")

records = get_index()[0].json["records"]
ret = {"drs_objects": [indexd_to_drs(record, True) for record in records]}
bundles = get_bundle_record_list()[0].json

ret = {
"drs_objects": [indexd_to_drs(record, True) for record in records],
"bundles": [bundle_to_drs(bundle, is_content=True) for bundle in bundles],
}

return flask.jsonify(ret), 200

Expand Down
13 changes: 9 additions & 4 deletions indexd/index/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,12 +576,9 @@ def post_bundle():
for bundle in bundles:
data = get_index_record(bundle)[0]
data = data.json
print("------------------------data---------------------------------")
print(data)
size += data["size"]
checksums.append(get_checksum(data))
data = bundle_to_drs(data, expand=True, is_content=True)
# del data["checksums"]
bundle_data.append(data)
checksum = (
flask.request.json.get("checksum")
Expand All @@ -607,6 +604,8 @@ def get_bundle_record_list():
"""

limit = flask.request.args.get("limit")
start = flask.request.args.get("start")
page = flask.request.args.get("page")

try:
limit = 100 if limit is None else int(limit)
Expand All @@ -616,7 +615,13 @@ def get_bundle_record_list():
if limit < 0 or limit > 1024:
raise UserError("limit must be between 0 and 1024")

ret = blueprint.index_driver.get_bundle_list(limit=limit)
if page is not None:
try:
page = int(page)
except ValueError as err:
raise UserError("page must be an integer")

ret = blueprint.index_driver.get_bundle_list(start=start, limit=limit, page=page)

return flask.jsonify(ret), 200

Expand Down
13 changes: 10 additions & 3 deletions indexd/index/drivers/alchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class DrsBundleRecord(Base):
size = Column(BigInteger)
bundle_data = Column(Text)

def to_document_dict(self, expand):
def to_document_dict(self, expand=False):
"""
Get the full bundle document
expand: True to include bundle_data
Expand Down Expand Up @@ -1371,22 +1371,29 @@ def add_bundle(

return record.bundle_id, record.name, record.bundle_data

def get_bundle_list(self, limit=100):
def get_bundle_list(self, start=None, limit=100, page=None):
"""
Returns list of all bundles
"""
with self.session as session:
query = session.query(DrsBundleRecord)
query = query.limit(limit)

return [i.to_document_dict(expand=False) for i in query]
if start is not None:
query = query.filter(DrsBundleRecord.bundle_id > start)

if page is not None:
query = query.offset(limit * page)

return [i.to_document_dict() for i in query]

def get_bundle(self, bundle_id, expand=False):
"""
Gets a bundle record given the bundle_id.
"""
with self.session as session:
query = session.query(DrsBundleRecord)

query = query.filter(or_(DrsBundleRecord.bundle_id == bundle_id)).order_by(
DrsBundleRecord.created_time.desc()
)
Expand Down
8 changes: 7 additions & 1 deletion tests/test_drs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import requests
import responses
from tests.default_test_settings import settings
from tests.test_bundles import get_bundle_doc


def generate_presigned_url_response(did, protocol="", status=200):
Expand Down Expand Up @@ -102,12 +103,17 @@ def test_drs_list(client, user):
submitted_guids = []
for _ in range(record_length):
res_1 = client.post("/index/", json=data, headers=user)
submitted_guids.append(res_1.json["did"])
did = res_1.json["did"]
submitted_guids.append(did)
bundle_data = get_bundle_doc(bundles=[did])
res2 = client.post("/bundle/", json=bundle_data, headers=user)
assert res_1.status_code == 200

res_2 = client.get("/ga4gh/drs/v1/objects")
assert res_2.status_code == 200
rec_2 = res_2.json
assert len(rec_2["drs_objects"]) == record_length
assert len(rec_2["bundles"]) == record_length
assert submitted_guids.sort() == [r["id"] for r in rec_2["drs_objects"]].sort()


Expand Down

0 comments on commit c752578

Please sign in to comment.