Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style |
| 3 | # license that can be found in the LICENSE file or at |
| 4 | # https://developers.google.com/open-source/licenses/bsd |
| 5 | |
| 6 | """A servlet that implements the backend of issues search. |
| 7 | |
| 8 | The GET request to a backend search has the same query string |
| 9 | parameters as the issue list servlet. But, instead of rendering a |
| 10 | HTML page, the backend search handler returns a JSON response with a |
| 11 | list of matching, sorted issue IID numbers from this shard that are |
| 12 | viewable by the requesting user. |
| 13 | |
| 14 | Each backend search request works within a single shard. Each |
| 15 | besearch backend job can access any single shard while processing a request. |
| 16 | |
| 17 | The current user ID must be passed in from the frontend for permission |
| 18 | checking. The user ID for the special "me" term can also be passed in |
| 19 | (so that you can view another user's dashboard and "me" will refer to |
| 20 | them). |
| 21 | """ |
| 22 | from __future__ import print_function |
| 23 | from __future__ import division |
| 24 | from __future__ import absolute_import |
| 25 | |
| 26 | import logging |
| 27 | import time |
| 28 | |
| 29 | from framework import jsonfeed |
| 30 | from search import backendsearchpipeline |
| 31 | from tracker import tracker_constants |
| 32 | |
| 33 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame^] | 34 | # Change to FlaskInternalTask |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 35 | class BackendSearch(jsonfeed.InternalTask): |
| 36 | """JSON servlet for issue search in a GAE backend.""" |
| 37 | |
| 38 | CHECK_SAME_APP = True |
| 39 | _DEFAULT_RESULTS_PER_PAGE = tracker_constants.DEFAULT_RESULTS_PER_PAGE |
| 40 | |
| 41 | def HandleRequest(self, mr): |
| 42 | """Search for issues and respond with the IIDs of matching issues. |
| 43 | |
| 44 | Args: |
| 45 | mr: common information parsed from the HTTP request. |
| 46 | |
| 47 | Returns: |
| 48 | Results dictionary in JSON format. |
| 49 | """ |
| 50 | # Users are never logged into backends, so the frontends tell us. |
| 51 | logging.info('query_project_names is %r', mr.query_project_names) |
| 52 | pipeline = backendsearchpipeline.BackendSearchPipeline( |
| 53 | mr, self.services, self._DEFAULT_RESULTS_PER_PAGE, |
| 54 | mr.query_project_names, mr.specified_logged_in_user_id, |
| 55 | mr.specified_me_user_ids) |
| 56 | pipeline.SearchForIIDs() |
| 57 | |
| 58 | start = time.time() |
| 59 | # Backends work in parallel to precache issues that the |
| 60 | # frontend is very likely to need. |
| 61 | _prefetched_issues = self.services.issue.GetIssues( |
| 62 | mr.cnxn, pipeline.result_iids[:mr.start + mr.num], |
| 63 | shard_id=mr.shard_id) |
| 64 | logging.info('prefetched and memcached %d issues in %d ms', |
| 65 | len(pipeline.result_iids[:mr.start + mr.num]), |
| 66 | int(1000 * (time.time() - start))) |
| 67 | |
| 68 | if pipeline.error: |
| 69 | error_message = pipeline.error.message |
| 70 | else: |
| 71 | error_message = None |
| 72 | |
| 73 | return { |
| 74 | 'unfiltered_iids': pipeline.result_iids, |
| 75 | 'search_limit_reached': pipeline.search_limit_reached, |
| 76 | 'error': error_message, |
| 77 | } |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame^] | 78 | |
| 79 | # def GetBackendSearch(self, **kwargs): |
| 80 | # return self.handler(**kwargs) |
| 81 | |
| 82 | # def PostBackendSearch(self, **kwargs): |
| 83 | # return self.handler(**kwargs) |