repo.py 14.8 KB
Newer Older
Markus Scheidgen's avatar
Markus Scheidgen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Copyright 2018 Markus Scheidgen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an"AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
The repository API of the nomad@FAIRDI APIs. Currently allows to resolve repository
meta-data.
"""

20
from typing import List
Markus Scheidgen's avatar
Markus Scheidgen committed
21
from flask_restplus import Resource, abort, fields
22
23
from flask import request, g
from elasticsearch_dsl import Q
24
from elasticsearch.exceptions import NotFoundError
Markus Scheidgen's avatar
Markus Scheidgen committed
25
import datetime
Markus Scheidgen's avatar
Markus Scheidgen committed
26

Markus Scheidgen's avatar
Markus Scheidgen committed
27
from nomad import search
Markus Scheidgen's avatar
Markus Scheidgen committed
28

Markus Scheidgen's avatar
Markus Scheidgen committed
29
from .app import api, rfc3339DateTime
30
from .auth import login_if_available
Markus Scheidgen's avatar
Markus Scheidgen committed
31
32
from .common import pagination_model, pagination_request_parser, calc_route

33
ns = api.namespace('repo', description='Access repository metadata.')
Markus Scheidgen's avatar
Markus Scheidgen committed
34
35
36
37
38


@calc_route(ns)
class RepoCalcResource(Resource):
    @api.response(404, 'The upload or calculation does not exist')
39
    @api.response(401, 'Not authorized to access the calculation')
40
    @api.response(200, 'Metadata send', fields.Raw)
41
    @api.doc('get_repo_calc')
42
    @login_if_available
43
    def get(self, upload_id, calc_id):
Markus Scheidgen's avatar
Markus Scheidgen committed
44
45
46
        """
        Get calculation metadata in repository form.

47
        Repository metadata only entails the quantities shown in the repository.
48
        Calcs are references via *upload_id*, *calc_id* pairs.
Markus Scheidgen's avatar
Markus Scheidgen committed
49
50
        """
        try:
51
52
53
54
55
56
57
58
59
60
61
            calc = search.Entry.get(calc_id)
        except NotFoundError:
            abort(404, message='There is no calculation %s/%s' % (upload_id, calc_id))

        if calc.with_embargo or not calc.published:
            if g.user is None:
                abort(401, message='Not logged in to access %s/%s.' % (upload_id, calc_id))

            is_owner = g.user.user_id == 0
            if not is_owner:
                for owner in calc.owners:
62
63
64
                    # At somepoint ids will be emails (strings) anyways.
                    # Right now it is hard to make sure that both are either str or int.
                    if str(owner.user_id) == str(g.user.user_id):
65
66
67
68
69
70
                        is_owner = True
                        break
            if not is_owner:
                abort(401, message='Not authorized to access %s/%s.' % (upload_id, calc_id))

        return calc.to_dict(), 200
Markus Scheidgen's avatar
Markus Scheidgen committed
71
72
73


repo_calcs_model = api.model('RepoCalculations', {
74
75
76
77
78
    'pagination': fields.Nested(pagination_model, allow_null=True),
    'scroll': fields.Nested(allow_null=True, skip_none=True, model=api.model('Scroll', {
        'total': fields.Integer(description='The total amount of hits for the search.'),
        'scroll_id': fields.String(allow_null=True, description='The scroll_id that can be used to retrieve the next page.'),
        'size': fields.Integer(help='The size of the returned scroll page.')})),
79
80
81
    'results': fields.List(fields.Raw, description=(
        'A list of search results. Each result is a dict with quantitie names as key and '
        'values as values')),
82
    'quantities': fields.Raw(description=(
Markus Scheidgen's avatar
Markus Scheidgen committed
83
        'A dict with all aggregations. Each aggregation is dictionary with a metrics dict as '
84
85
        'value and quantity value as key. The metrics are code runs(calcs), %s. '
        'There is a pseudo quantity "total" with a single value "all" that contains the metrics over all results. ' %
86
        ', '.join(search.metrics_names)))
Markus Scheidgen's avatar
Markus Scheidgen committed
87
88
})

89
90
91
92
93
94
95
96
97
98
99
100

def add_common_parameters(request_parser):
    request_parser.add_argument(
        'owner', type=str,
        help='Specify which calcs to return: ``all``, ``public``, ``user``, ``staging``, default is ``all``')
    request_parser.add_argument(
        'from_time', type=lambda x: rfc3339DateTime.parse(x),
        help='A yyyy-MM-ddTHH:mm:ss (RFC3339) minimum entry time (e.g. upload time)')
    request_parser.add_argument(
        'until_time', type=lambda x: rfc3339DateTime.parse(x),
        help='A yyyy-MM-ddTHH:mm:ss (RFC3339) maximum entry time (e.g. upload time)')

101
102
103
104
    for quantity in search.search_quantities.values():
        request_parser.add_argument(
            quantity.name, type=str, help=quantity.description,
            action='append' if quantity.multi else None)
105
106


Markus Scheidgen's avatar
Markus Scheidgen committed
107
repo_request_parser = pagination_request_parser.copy()
108
add_common_parameters(repo_request_parser)
109
110
111
112
repo_request_parser.add_argument(
    'scroll', type=bool, help='Enable scrolling')
repo_request_parser.add_argument(
    'scroll_id', type=str, help='The id of the current scrolling window to use.')
Markus Scheidgen's avatar
Markus Scheidgen committed
113
repo_request_parser.add_argument(
114
    'metrics', type=str, action='append', help=(
115
        'Metrics to aggregate over all quantities and their values as comma separated list. '
116
        'Possible values are %s.' % ', '.join(search.metrics_names)))
Markus Scheidgen's avatar
Markus Scheidgen committed
117

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

def create_owner_query():
    owner = request.args.get('owner', 'all')

    # TODO this should be removed after migration
    # if owner == 'migrated':
    #     q = Q('term', published=True) & Q('term', with_embargo=False)
    #     if g.user is not None:
    #         q = q | Q('term', owners__user_id=g.user.user_id)
    #     q = q & ~Q('term', **{'uploader.user_id': 1})  # pylint: disable=invalid-unary-operand-type
    if owner == 'all':
        q = Q('term', published=True) & Q('term', with_embargo=False)
        if g.user is not None:
            q = q | Q('term', owners__user_id=g.user.user_id)
    elif owner == 'public':
        q = Q('term', published=True) & Q('term', with_embargo=False)
    elif owner == 'user':
        if g.user is None:
            abort(401, message='Authentication required for owner value user.')

        q = Q('term', owners__user_id=g.user.user_id)
    elif owner == 'staging':
        if g.user is None:
            abort(401, message='Authentication required for owner value user.')
        q = Q('term', published=False) & Q('term', owners__user_id=g.user.user_id)
    elif owner == 'admin':
        if g.user is None or not g.user.is_admin:
            abort(401, message='This can only be used by the admin user.')
        q = None
    else:
        abort(400, message='Invalid owner value. Valid values are all|user|staging, default is all')

    return q


def create_search_parameters():
    """ Helper that creates a request.args dict with isolated search parameters """
    search_parameters = dict(**request.args)
    search_parameters.pop('owner', None)
    search_parameters.pop('scroll', None)
    search_parameters.pop('scroll_id', None)
    search_parameters.pop('per_page', None)
    search_parameters.pop('page', None)
    search_parameters.pop('order', None)
    search_parameters.pop('order_by', None)
    search_parameters.pop('metrics', None)
    search_parameters.pop('from_time', None)
    search_parameters.pop('until_time', None)
    search_parameters.pop('size', None)
    search_parameters.pop('after', None)

    return search_parameters
170

Markus Scheidgen's avatar
Markus Scheidgen committed
171
172
173

@ns.route('/')
class RepoCalcsResource(Resource):
174
    @api.doc('search')
175
    @api.response(400, 'Invalid requests, e.g. wrong owner type or bad search parameters')
Markus Scheidgen's avatar
Markus Scheidgen committed
176
    @api.expect(repo_request_parser, validate=True)
177
    @api.marshal_with(repo_calcs_model, skip_none=True, code=200, description='Search results send')
Markus Scheidgen's avatar
Markus Scheidgen committed
178
179
180
    @login_if_available
    def get(self):
        """
181
        Search for calculations in the repository form, paginated.
182
183

        The ``owner`` parameter determines the overall entries to search through.
184
185
186
187
        Possible values are: ``all`` (show all entries visible to the current user), ``public``
        (show all publically visible entries), ``user`` (show all user entries, requires login),
        ``staging`` (show all user entries in staging area, requires login).

188
189
190
191
192
        You can use the various quantities to search/filter for. For some of the
        indexed quantities this endpoint returns aggregation information. This means
        you will be given a list of all possible values and the number of entries
        that have the certain value. You can also use these aggregations on an empty
        search to determine the possible values.
193
194
195

        The pagination parameters allows determine which page to return via the
        ``page`` and ``per_page`` parameters. Pagination however, is limited to the first
Markus Scheidgen's avatar
Markus Scheidgen committed
196
197
198
199
200
201
202
203
        100k (depending on ES configuration) hits.

        An alternative to pagination is to use ``scroll`` and ``scroll_id``. With ``scroll``
        you will get a ``scroll_id`` on the first request. Each call with ``scroll`` and
        the respective ``scroll_id`` will return the next ``per_page`` (here the default is 1000)
        results. Scroll however, ignores ordering and does not return aggregations.
        The scroll view used in the background will stay alive for 1 minute between requests.
        If the given ``scroll_id`` is not available anymore, a HTTP 400 is raised.
204
205
206
207
208

        The search will return aggregations on a predefined set of quantities. Aggregations
        will tell you what quantity values exist and how many entries match those values.

        Ordering is determined by ``order_by`` and ``order`` parameters.
209
        """
210
211

        try:
212
213
            scroll = bool(request.args.get('scroll', False))
            scroll_id = request.args.get('scroll_id', None)
214
            page = int(request.args.get('page', 1))
215
            per_page = int(request.args.get('per_page', 10 if not scroll else 1000))
216
            order = int(request.args.get('order', -1))
217
            metrics: List[str] = request.args.getlist('metrics')
Markus Scheidgen's avatar
Markus Scheidgen committed
218
219
            from_time = rfc3339DateTime.parse(request.args.get('from_time', '2000-01-01'))
            until_time_str = request.args.get('until_time', None)
220
            until_time = rfc3339DateTime.parse(until_time_str) if until_time_str is not None else datetime.datetime.utcnow()
Markus Scheidgen's avatar
Markus Scheidgen committed
221
            time_range = (from_time, until_time)
222
223
224
225
        except Exception:
            abort(400, message='bad parameter types')

        order_by = request.args.get('order_by', 'formula')
226
227

        try:
228
            assert page >= 1
229
230
231
232
            assert per_page > 0
        except AssertionError:
            abort(400, message='invalid pagination')

233
234
235
        if order not in [-1, 1]:
            abort(400, message='invalid pagination')

236
237
238
239
        for metric in metrics:
            if metric not in search.metrics_names:
                abort(400, message='there is not metric %s' % metric)

240
        q = create_owner_query()
241

242
243
244
        # TODO this should be removed after migration
        without_currupted_mainfile = ~Q('term', code_name='currupted mainfile')  # pylint: disable=invalid-unary-operand-type
        q = q & without_currupted_mainfile if q is not None else without_currupted_mainfile
Markus Scheidgen's avatar
Markus Scheidgen committed
245

246
        search_parameters = create_search_parameters()
247
248

        try:
249
            if scroll:
250
251
252
                results = search.scroll_search(
                    q=q, scroll_id=scroll_id, size=per_page, search_parameters=search_parameters)

253
            else:
254
255
256
257
258
259
260
261
262
263
                results = search.metrics_search(
                    q=q, per_page=per_page, page=page, order=order, order_by=order_by,
                    time_range=time_range, metrics_to_use=metrics, search_parameters=search_parameters)

                # TODO just a work around to make things prettier
                quantities = results['quantities']
                if 'code_name' in quantities and 'currupted mainfile' in quantities['code_name']:
                    del(quantities['code_name']['currupted mainfile'])

            return results, 200
Markus Scheidgen's avatar
Markus Scheidgen committed
264
265
        except search.ScrollIdNotFound:
            abort(400, 'The given scroll_id does not exist.')
266
267
        # except KeyError as e:
        #     abort(400, str(e))
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317


repo_quantity_values_model = api.model('RepoQuantityValues', {
    'quantities': fields.Raw(description='''
        A dict with the requested quantity as single key.
        The value is a dictionary with 'after' and 'values' keys.
        The 'values' key holds a dict with actual values as keys and their entry count
        as values (i.e. number of entries with that value). ''')
})

repo_quantity_search_request_parser = api.parser()
add_common_parameters(repo_quantity_search_request_parser)
repo_quantity_search_request_parser.add_argument(
    'after', type=str, help='The after value to use for "scrolling".')
repo_request_parser.add_argument(
    'size', type=int, help='The max size of the returned values.')


@ns.route('/<string:quantity>')
class RepoQuantityResource(Resource):
    @api.doc('quantity_search')
    @api.response(400, 'Invalid requests, e.g. wrong owner type, bad quantity, bad search parameters')
    @api.expect(repo_quantity_search_request_parser, validate=True)
    @api.marshal_with(repo_quantity_values_model, skip_none=True, code=200, description='Search results send')
    @login_if_available
    def get(self, quantity: str):
        """
        Retrieve quantity values from entries matching the search.

        You can use the various quantities to search/filter for. For some of the
        indexed quantities this endpoint returns aggregation information. This means
        you will be given a list of all possible values and the number of entries
        that have the certain value. You can also use these aggregations on an empty
        search to determine the possible values.

        There is no ordering and no pagination. Instead there is an 'after' key based
        scrolling. The result will contain an 'after' value, that can be specified
        for the next request. You can use the 'size' and 'after' parameters accordingly.

        The result will contain a 'quantities' key with the given quantity and the
        respective values (upto 'size' many). For the rest of the values use the
        'after' parameter accordingly.
        """

        try:
            after = request.args.get('after', None)
            size = int(request.args.get('size', 100))

            from_time = rfc3339DateTime.parse(request.args.get('from_time', '2000-01-01'))
            until_time_str = request.args.get('until_time', None)
318
            until_time = rfc3339DateTime.parse(until_time_str) if until_time_str is not None else datetime.datetime.utcnow()
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
            time_range = (from_time, until_time)
        except Exception:
            abort(400, message='bad parameter types')

        try:
            assert size >= 0
        except AssertionError:
            abort(400, message='invalid size')

        q = create_owner_query()
        search_parameters = create_search_parameters()

        try:
            results = search.quantity_search(
                q=q, time_range=time_range, search_parameters=search_parameters,
                quantities={quantity: after}, size=size, with_entries=False)

            return results, 200
        except KeyError as e:
            import traceback
            traceback.print_exc()
            abort(400, 'Given quantity does not exist: %s' % str(e))