| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """URI endpoint for nudging Anomaly entities and updating alert bug IDs.""" | 5 """URI endpoint for nudging Anomaly entities and updating alert bug IDs.""" |
| 6 | 6 |
| 7 import json | 7 import json |
| 8 | 8 |
| 9 from google.appengine.api import users | 9 from google.appengine.api import users |
| 10 from google.appengine.ext import ndb | 10 from google.appengine.ext import ndb |
| 11 | 11 |
| 12 from dashboard.common import request_handler | 12 from dashboard.common import request_handler |
| 13 from dashboard.common import utils | 13 from dashboard.common import utils |
| 14 from dashboard.common import xsrf | 14 from dashboard.common import xsrf |
| 15 from dashboard.models import alert_group | |
| 16 | 15 |
| 17 | 16 |
| 18 class EditAnomaliesHandler(request_handler.RequestHandler): | 17 class EditAnomaliesHandler(request_handler.RequestHandler): |
| 19 """Handles editing the bug IDs and revision range of Alerts.""" | 18 """Handles editing the bug IDs and revision range of Alerts.""" |
| 20 | 19 |
| 21 @xsrf.TokenRequired | 20 @xsrf.TokenRequired |
| 22 def post(self): | 21 def post(self): |
| 23 """Allows adding or resetting bug IDs and invalid statuses to Alerts. | 22 """Allows adding or resetting bug IDs and invalid statuses to Alerts. |
| 24 | 23 |
| 25 Additionally, this endpoint is also responsible for changing the start | 24 Additionally, this endpoint is also responsible for changing the start |
| (...skipping 24 matching lines...) Expand all Loading... |
| 50 self.response.out.write(json.dumps({ | 49 self.response.out.write(json.dumps({ |
| 51 'error': 'No alerts specified to add bugs to.'})) | 50 'error': 'No alerts specified to add bugs to.'})) |
| 52 return | 51 return |
| 53 keys = [ndb.Key(urlsafe=k) for k in urlsafe_keys.split(',')] | 52 keys = [ndb.Key(urlsafe=k) for k in urlsafe_keys.split(',')] |
| 54 alert_entities = ndb.get_multi(keys) | 53 alert_entities = ndb.get_multi(keys) |
| 55 | 54 |
| 56 # Get the parameters which specify the changes to make. | 55 # Get the parameters which specify the changes to make. |
| 57 bug_id = self.request.get('bug_id') | 56 bug_id = self.request.get('bug_id') |
| 58 new_start_revision = self.request.get('new_start_revision') | 57 new_start_revision = self.request.get('new_start_revision') |
| 59 new_end_revision = self.request.get('new_end_revision') | 58 new_end_revision = self.request.get('new_end_revision') |
| 60 result = None | |
| 61 if bug_id: | 59 if bug_id: |
| 62 result = self.ChangeBugId(alert_entities, bug_id) | 60 self.ChangeBugId(alert_entities, bug_id) |
| 63 elif new_start_revision and new_end_revision: | 61 elif new_start_revision and new_end_revision: |
| 64 result = self.NudgeAnomalies( | 62 self.NudgeAnomalies(alert_entities, new_start_revision, new_end_revision) |
| 65 alert_entities, new_start_revision, new_end_revision) | |
| 66 else: | 63 else: |
| 67 result = {'error': 'No bug ID or new revision specified.'} | 64 self.response.out.write( |
| 68 self.response.out.write(json.dumps(result)) | 65 json.dumps({'error': 'No bug ID or new revision specified.'})) |
| 69 | 66 |
| 70 def ChangeBugId(self, alert_entities, bug_id): | 67 def ChangeBugId(self, alert_entities, bug_id): |
| 71 """Changes or resets the bug ID of all given alerts.""" | 68 """Changes or resets the bug ID of all given alerts.""" |
| 72 # Change the bug ID if a new bug ID is specified and valid. | 69 # Change the bug ID if a new bug ID is specified and valid. |
| 73 if bug_id == 'REMOVE': | 70 if bug_id == 'REMOVE': |
| 74 bug_id = None | 71 bug_id = None |
| 75 else: | 72 else: |
| 76 try: | 73 try: |
| 77 bug_id = int(bug_id) | 74 bug_id = int(bug_id) |
| 78 except ValueError: | 75 except ValueError: |
| 79 return {'error': 'Invalid bug ID %s' % str(bug_id)} | 76 self.response.out.write(json.dumps({ |
| 77 'error': 'Invalid bug ID %s' % str(bug_id)})) |
| 78 return |
| 79 for a in alert_entities: |
| 80 a.bug_id = bug_id |
| 80 | 81 |
| 81 alert_group.ModifyAlertsAndAssociatedGroups(alert_entities, bug_id=bug_id) | 82 ndb.put_multi(alert_entities) |
| 82 | 83 self.response.out.write(json.dumps({'bug_id': bug_id})) |
| 83 return {'bug_id': bug_id} | |
| 84 | 84 |
| 85 def NudgeAnomalies(self, anomaly_entities, start, end): | 85 def NudgeAnomalies(self, anomaly_entities, start, end): |
| 86 # Change the revision range if a new revision range is specified and valid. | 86 # Change the revision range if a new revision range is specified and valid. |
| 87 try: | 87 try: |
| 88 start = int(start) | 88 start = int(start) |
| 89 end = int(end) | 89 end = int(end) |
| 90 except ValueError: | 90 except ValueError: |
| 91 return {'error': 'Invalid revisions %s, %s' % (start, end)} | 91 self.response.out.write( |
| 92 json.dumps({'error': 'Invalid revisions %s, %s' % (start, end)})) |
| 93 return |
| 94 for a in anomaly_entities: |
| 95 a.start_revision = start |
| 96 a.end_revision = end |
| 92 | 97 |
| 93 alert_group.ModifyAlertsAndAssociatedGroups( | 98 ndb.put_multi(anomaly_entities) |
| 94 anomaly_entities, start_revision=start, end_revision=end) | 99 self.response.out.write(json.dumps({'success': 'Alerts nudged.'})) |
| 95 | |
| 96 return {'success': 'Alerts nudged.'} | |
| OLD | NEW |