Added pagination features
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
9c3f8c65fb
commit
459b3fa6df
@ -151,8 +151,62 @@ class SampleResource(Resource):
|
||||
Get all stored items
|
||||
:return:
|
||||
"""
|
||||
with opentracing.tracer.start_active_span('sqlalchemy.select'):
|
||||
samples = SampleMetadata.query.all()
|
||||
with opentracing.tracer.start_active_span('compileQuery'):
|
||||
query = SampleMetadata.query
|
||||
|
||||
## Compile filters ##
|
||||
|
||||
filters = []
|
||||
try:
|
||||
first = int(request.args.get('first'))
|
||||
filters.append(
|
||||
SampleMetadata.id >= first
|
||||
)
|
||||
except ValueError:
|
||||
first = None
|
||||
|
||||
try:
|
||||
after = datetime.fromisoformat(request.args.get('after'))
|
||||
filters.append(
|
||||
SampleMetadata.timestamp > after
|
||||
)
|
||||
except ValueError:
|
||||
after = None
|
||||
|
||||
try:
|
||||
before = datetime.fromisoformat(request.args.get('after'))
|
||||
filters.append(
|
||||
SampleMetadata.timestamp < before
|
||||
)
|
||||
except ValueError:
|
||||
before = None
|
||||
|
||||
if filters:
|
||||
query = query.filter(db.and_(*filters))
|
||||
|
||||
try:
|
||||
limit = int(request.args.get('limit'))
|
||||
query = query.limit(limit)
|
||||
except ValueError:
|
||||
limit = None
|
||||
|
||||
## Run query ##
|
||||
count = "count" in request.args.get
|
||||
tags = {
|
||||
"first": first,
|
||||
"limit": limit,
|
||||
"after": after,
|
||||
"before": before
|
||||
}
|
||||
|
||||
if count:
|
||||
with opentracing.tracer.start_active_span('sqlalchemy.count', tags=tags):
|
||||
rows = query.count()
|
||||
|
||||
return {"count": rows}, 200
|
||||
else:
|
||||
with opentracing.tracer.start_active_span('sqlalchemy.select', tags=tags):
|
||||
samples = query.all()
|
||||
|
||||
return self.samplemetadataschema.dump(list(samples)), 200
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user