Commit 82b03b19 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Switched pagination to start with 1.

parent 6f62b6d0
......@@ -61,7 +61,7 @@ class Repo extends React.Component {
state = {
data: [],
page: 1,
rowsPerPage: 5,
rowsPerPage: 10,
total: 0,
loading: true,
owner: 'all'
......
......@@ -199,10 +199,10 @@ class Api {
async repoAll(page, perPage, owner) {
const client = await this.swaggerPromise
return client.apis.repo.get_calcs({
return client.apis.repo.search({
page: page,
per_page: perPage,
ower: owner || 'all'
owner: owner || 'all'
})
.catch(this.handleApiError)
.then(response => response.body)
......
......@@ -81,7 +81,7 @@ for search_quantity in search.search_quantities.keys():
@ns.route('/')
class RepoCalcsResource(Resource):
@api.doc('get_calcs')
@api.doc('search')
@api.response(400, 'Invalid requests, e.g. wrong owner type or bad quantities')
@api.expect(repo_request_parser, validate=True)
@api.marshal_with(repo_calcs_model, skip_none=True, code=200, description='Metadata send')
......@@ -97,12 +97,12 @@ class RepoCalcsResource(Resource):
that have the certain value. You can also use these aggregations on an empty
search to determine the possible values.
"""
page = int(request.args.get('page', 0))
page = int(request.args.get('page', 1))
per_page = int(request.args.get('per_page', 10))
owner = request.args.get('owner', 'all')
try:
assert page >= 0
assert page >= 1
assert per_page > 0
except AssertionError:
abort(400, message='invalid pagination')
......
......@@ -59,7 +59,7 @@ def run_worker():
processing.app.worker_main(['worker', '--loglevel=INFO'])
@run.command(help='Run both api and worker with watchdog.')
@run.command(help='Run both api and worker.')
def apiworker():
executor = ProcessPoolExecutor(2)
loop = asyncio.get_event_loop()
......
......@@ -220,10 +220,10 @@ def reset():
try:
if not elastic_client:
setup_elastic()
elastic_client.indices.delete(index=config.elastic.index_name)
from nomad.search import Entry
Entry.init(index=config.elastic.index_name)
logger.info('elastic index resetted')
elastic_client.indices.delete(index=config.elastic.index_name)
from nomad.search import Entry
Entry.init(index=config.elastic.index_name)
logger.info('elastic index resetted')
except Exception as e:
logger.error('exception resetting elastic', exc_info=e)
......
......@@ -221,13 +221,13 @@ elastic field and description.
def aggregate_search(
page: int = 0, per_page: int = 10, q: Q = None, **kwargs) -> Tuple[int, List[dict], Dict[str, Dict[str, int]]]:
page: int = 1, per_page: int = 10, q: Q = None, **kwargs) -> Tuple[int, List[dict], Dict[str, Dict[str, int]]]:
"""
Performs a search and returns paginated search results and aggregation bucket sizes
based on key quantities.
Arguments:
page: The page to return starting with 0
page: The page to return starting with page 1
per_page: Results per page
q: An *elasticsearch_dsl* query used to further filter the results (via `and`)
aggregations: A customized list of aggregations to perform. Keys are index fields,
......@@ -238,7 +238,7 @@ def aggregate_search(
the aggregation data.
"""
search = Search()
search = Search(index=config.elastic.index_name)
if q is not None:
search = search.query(q)
......@@ -259,7 +259,7 @@ def aggregate_search(
else:
search.aggs.bucket(aggregation, A('terms', field=aggregation, size=size))
response = search[page * per_page: (page + 1) * per_page].execute() # pylint: disable=no-member
response = search[(page - 1) * per_page: page * per_page].execute() # pylint: disable=no-member
total_results = response.hits.total
search_results = [hit.to_dict() for hit in response.hits]
......
......@@ -42,7 +42,6 @@ bravado
PyJWT
jsonschema[format]
python-magic
watchgod
# dev/ops related
setuptools
......
......@@ -620,14 +620,16 @@ class TestRepo(UploadFilesBasedTests):
assert len(aggregations['system']) == 1
assert value in aggregations['system']
def test_search_pagination(self, client, example_elastic_calcs, no_warn):
rv = client.get('/repo/?page=1&per_page=1')
@pytest.mark.parametrize('n_results, page, per_page', [(2, 1, 5), (1, 1, 1), (0, 2, 3)])
def test_search_pagination(self, client, example_elastic_calcs, no_warn, n_results, page, per_page):
rv = client.get('/repo/?page=%d&per_page=%d' % (page, per_page))
assert rv.status_code == 200
data = json.loads(rv.data)
results = data.get('results', None)
assert data['pagination']['total'] == 2
assert results is not None
assert isinstance(results, list)
assert len(results) == 1
assert len(results) == n_results
def test_search_user_authrequired(self, client, example_elastic_calcs, no_warn):
rv = client.get('/repo/?owner=user')
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment