Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: appengine/chromium_cq_status/handlers/query.py

Issue 515093004: chromium-cq-status: StatsTest utility class for testing CQ stats (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Rebase Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | appengine/chromium_cq_status/model/record.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 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 import json 5 import json
6 6
7 from google.appengine.datastore.datastore_query import Cursor 7 from google.appengine.datastore.datastore_query import Cursor
8 import webapp2 8 import webapp2
9 9
10 from shared.config import MAXIMUM_QUERY_SIZE 10 from shared.config import MAXIMUM_QUERY_SIZE
11 from shared.parsing import ( 11 from shared.parsing import (
12 parse_cursor, 12 parse_cursor,
13 parse_url_tags, 13 parse_url_tags,
14 parse_fields, 14 parse_fields,
15 parse_key, 15 parse_key,
16 parse_non_negative_integer, 16 parse_non_negative_integer,
17 parse_request, 17 parse_request,
18 parse_tags, 18 parse_tags,
19 parse_timestamp, 19 parse_timestamp,
20 use_default, 20 use_default,
21 ) 21 )
22 from shared.utils import to_unix_timestamp
23 from model.record import Record # pylint: disable-msg=E0611 22 from model.record import Record # pylint: disable-msg=E0611
24 23
25 def execute_query( 24 def execute_query(
26 key, begin, end, tags, fields, count, cursor): # pragma: no cover 25 key, begin, end, tags, fields, count, cursor): # pragma: no cover
27 count = min(count, MAXIMUM_QUERY_SIZE) 26 count = min(count, MAXIMUM_QUERY_SIZE)
28 27
29 records = [] 28 records = []
30 next_cursor = '' 29 next_cursor = ''
31 if key and count > 0: 30 if key and count > 0:
32 record = Record.get_by_id(key) 31 record = Record.get_by_id(key)
(...skipping 15 matching lines...) Expand all
48 for tag in tags: 47 for tag in tags:
49 filters.append(Record.tags == tag) 48 filters.append(Record.tags == tag)
50 query = Record.query().filter(*filters).order(-Record.timestamp) 49 query = Record.query().filter(*filters).order(-Record.timestamp)
51 page_records, next_cursor, more = query.fetch_page(count - len(records), 50 page_records, next_cursor, more = query.fetch_page(count - len(records),
52 start_cursor=Cursor(urlsafe=next_cursor or cursor)) 51 start_cursor=Cursor(urlsafe=next_cursor or cursor))
53 next_cursor = next_cursor.urlsafe() if next_cursor else '' 52 next_cursor = next_cursor.urlsafe() if next_cursor else ''
54 for record in page_records: 53 for record in page_records:
55 if matches_fields(fields, record): 54 if matches_fields(fields, record):
56 records.append(record) 55 records.append(record)
57 56
58 results = []
59 for record in records:
60 result = record.to_dict(exclude=['timestamp'])
61 result['timestamp'] = to_unix_timestamp(record.timestamp)
62 record_key = record.key.id()
63 result['key'] = record_key if type(record_key) != long else None
64 results.append(result)
65
66 return { 57 return {
67 'results': results, 58 'results': [record.to_dict() for record in records],
68 'cursor': next_cursor, 59 'cursor': next_cursor,
69 'more': more, 60 'more': more,
70 } 61 }
71 62
72 def matches_fields(fields, record): # pragma: no cover 63 def matches_fields(fields, record): # pragma: no cover
73 for field, value in fields.items(): 64 for field, value in fields.items():
74 if not field in record.fields or record.fields[field] != value: 65 if not field in record.fields or record.fields[field] != value:
75 return False 66 return False
76 return True 67 return True
77 68
(...skipping 11 matching lines...) Expand all
89 }) 80 })
90 data['tags'].extend(parse_url_tags(url_tags)) 81 data['tags'].extend(parse_url_tags(url_tags))
91 except ValueError, e: 82 except ValueError, e:
92 self.response.write(e) 83 self.response.write(e)
93 return 84 return
94 85
95 results = execute_query(**data) 86 results = execute_query(**data)
96 self.response.headers.add_header("Access-Control-Allow-Origin", "*") 87 self.response.headers.add_header("Access-Control-Allow-Origin", "*")
97 self.response.headers.add_header('Content-Type', 'application/json') 88 self.response.headers.add_header('Content-Type', 'application/json')
98 json.dump(results, self.response) 89 json.dump(results, self.response)
OLDNEW
« no previous file with comments | « no previous file | appengine/chromium_cq_status/model/record.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698