record_search_controller.py 2.29 KB
Newer Older
David Mendez's avatar
David Mendez committed
1
2
3
"""
Module that describes and handles the requests concerned with recording the search events
"""
4
from flask import abort, request
David Mendez's avatar
David Mendez committed
5
from flask_restx import Namespace, Resource, fields
David Mendez's avatar
David Mendez committed
6

7
from app.authorisation.decorators import token_required_for_job_id
8
9
from app.models import delayed_job_models
from app.namespaces.job_statistics import record_statistics_service
10

David Mendez's avatar
David Mendez committed
11
# pylint: disable=redefined-builtin,invalid-name,no-self-use
12
13
14
15
16
17
18
API = Namespace('record/search', description='Requests to record statistics of a search')

SEARCH_RECORD = API.model('SearchRecord', {
    'total_items': fields.Integer(required=True, description='The type of search performed', min=0),
    'file_size': fields.Integer(required=True, description='The final size of the results file in bytes', min=0),
})

19
FULL_STATISTICS = API.inherit('FullSearchRecord', SEARCH_RECORD, {
20
    'time_taken': fields.Integer(required=True, description='The time the job took to finish', min=0),
21
    'search_type': fields.String(required=True, description='The type of the job ',
David Mendez's avatar
David Mendez committed
22
                                 enum=[str(possible_type) for possible_type in delayed_job_models.JobTypes]),
23
24
25
    'request_date': fields.Float(required=True, description='The time (POSIX timestamp) at what the job started', min=0)
})

26
27
28
29

@API.route('/<id>')
@API.param('id', 'The job identifier')
@API.response(404, 'Job not found')
30
@API.response(412, 'Job must be finished before saving statistics')
31
32
33
34
35
class SearchRecord(Resource):
    """
        Resource that handles requests regarding recording search statistics
    """

36
    @API.doc(security='jobKey', body=SEARCH_RECORD)
37
    @API.marshal_with(FULL_STATISTICS)
38
    @token_required_for_job_id
39
40
41
42
43
44
    def post(self, id):
        """
        Records statistics of a search with the data provided
        :return: a json response with the result of the submission
        """

45
46
47
48
49
50
        statistics = {}  # this is to avoid using custom data structures i.e CombinedMultiDict
        for key in request.values.keys():
            new_value = request.values.get(key)
            statistics[key] = new_value

        try:
51
52
            return record_statistics_service.save_statistics_for_job(id, statistics)
        except record_statistics_service.JobNotFoundError:
53
            abort(404)
54
        except record_statistics_service.JobNotFinishedError:
55
            abort(412)