xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 1 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Module for interacting with google APIs.""" |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 6 | # pylint: disable=g-bad-import-order |
Xixuan Wu | d55ac6e | 2019-03-14 10:56:39 -0700 | [diff] [blame] | 7 | # pylint: disable=g-bad-exception-name |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 8 | |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 9 | import ast |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 10 | import httplib2 |
Xixuan Wu | 55d38c5 | 2019-05-21 14:26:23 -0700 | [diff] [blame] | 11 | import logging |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 12 | import re |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 13 | |
| 14 | import apiclient |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 15 | import build_lib |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 16 | import constants |
| 17 | import file_getter |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 18 | import global_config |
| 19 | import time_converter |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 20 | |
| 21 | from oauth2client import service_account |
| 22 | from oauth2client.contrib import appengine |
| 23 | |
| 24 | |
| 25 | class RestClientError(Exception): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 26 | """Raised when there is a general error.""" |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 27 | |
| 28 | |
| 29 | class NoServiceRestClientError(RestClientError): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 30 | """Raised when there is no ready service for a google API.""" |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 31 | |
| 32 | |
| 33 | class BaseRestClient(object): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 34 | """Base class of REST client for google APIs.""" |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 35 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 36 | def __init__(self, scopes, service_name, service_version): |
| 37 | """Initialize a REST client to connect to a google API. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 38 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 39 | Args: |
| 40 | scopes: the scopes of the to-be-connected API. |
| 41 | service_name: the service name of the to-be-connected API. |
| 42 | service_version: the service version of the to-be-connected API. |
| 43 | """ |
| 44 | self.running_env = constants.environment() |
| 45 | self.scopes = scopes |
| 46 | self.service_name = service_name |
| 47 | self.service_version = service_version |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 48 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 49 | @property |
| 50 | def service(self): |
| 51 | if not self._service: |
| 52 | raise NoServiceRestClientError('No service created for calling API') |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 53 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 54 | return self._service |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 55 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 56 | def create_service(self, discovery_url=None): |
| 57 | """Create the service for a google API.""" |
| 58 | self._init_credentials() |
| 59 | # Explicitly specify timeout for http to avoid DeadlineExceededError. |
| 60 | # It's used for services like AndroidBuild API, which raise such error |
| 61 | # when being triggered too many calls in a short time frame. |
| 62 | # http://stackoverflow.com/questions/14698119/httpexception-deadline-exceeded-while-waiting-for-http-response-from-url-dead |
| 63 | http_auth = self._credentials.authorize(httplib2.Http(timeout=30)) |
| 64 | if discovery_url is None: |
| 65 | self._service = apiclient.discovery.build( |
| 66 | self.service_name, self.service_version, |
| 67 | http=http_auth) |
| 68 | else: |
| 69 | self._service = apiclient.discovery.build( |
| 70 | self.service_name, self.service_version, http=http_auth, |
| 71 | discoveryServiceUrl=discovery_url) |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 72 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 73 | def _init_credentials(self): |
| 74 | """Initialize the credentials for a google API.""" |
| 75 | if (self.running_env == constants.RunningEnv.ENV_STANDALONE or |
| 76 | self.running_env == constants.RunningEnv.ENV_DEVELOPMENT_SERVER): |
| 77 | # Running locally |
| 78 | service_credentials = service_account.ServiceAccountCredentials |
| 79 | self._credentials = service_credentials.from_json_keyfile_name( |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 80 | file_getter.STAGING_CLIENT_SECRETS_FILE, self.scopes) |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 81 | else: |
| 82 | # Running in app-engine production |
| 83 | self._credentials = appengine.AppAssertionCredentials(self.scopes) |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 84 | |
| 85 | |
| 86 | class AndroidBuildRestClient(object): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 87 | """REST client for android build API.""" |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 88 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 89 | def __init__(self, rest_client): |
| 90 | """Initialize a REST client for connecting to Android Build API.""" |
| 91 | self._rest_client = rest_client |
| 92 | self._rest_client.create_service() |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 93 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 94 | def get_latest_build_id(self, branch, target): |
| 95 | """Get the latest build id for a given branch and target. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 96 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 97 | Args: |
| 98 | branch: an android build's branch |
| 99 | target: an android build's target |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 100 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 101 | Returns: |
| 102 | A string representing latest build id. |
| 103 | """ |
| 104 | request = self._rest_client.service.build().list( |
| 105 | buildType='submitted', |
| 106 | branch=branch, |
| 107 | target=target, |
| 108 | successful=True, |
| 109 | maxResults=1) |
| 110 | builds = request.execute(num_retries=10) |
| 111 | if not builds or not builds['builds']: |
| 112 | return None |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 113 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 114 | return builds['builds'][0]['buildId'] |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 115 | |
| 116 | |
| 117 | class StorageRestClient(object): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 118 | """REST client for google storage API.""" |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 119 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 120 | def __init__(self, rest_client): |
| 121 | """Initialize a REST client for connecting to Google storage API.""" |
| 122 | self._rest_client = rest_client |
| 123 | self._rest_client.create_service() |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 124 | |
Xixuan Wu | d55ac6e | 2019-03-14 10:56:39 -0700 | [diff] [blame] | 125 | def read_object(self, bucket, object_path): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 126 | """Read the contents of input_object in input_bucket. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 127 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 128 | Args: |
Xixuan Wu | d55ac6e | 2019-03-14 10:56:39 -0700 | [diff] [blame] | 129 | bucket: A string to indicate the bucket for fetching the object. |
| 130 | e.g. constants.StorageBucket.PROD_SUITE_SCHEDULER |
| 131 | object_path: A string to indicate the path of the object to read the |
| 132 | contents. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 133 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 134 | Returns: |
| 135 | the stripped string contents of the input object. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 136 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 137 | Raises: |
| 138 | apiclient.errors.HttpError |
| 139 | """ |
| 140 | req = self._rest_client.service.objects().get_media( |
Xixuan Wu | d55ac6e | 2019-03-14 10:56:39 -0700 | [diff] [blame] | 141 | bucket=bucket, |
| 142 | object=object_path) |
| 143 | return req.execute() |
| 144 | |
| 145 | def upload_object(self, bucket, src_object_path, dest_object_path): |
| 146 | """Upload object_path to input_bucket. |
| 147 | |
| 148 | Args: |
| 149 | bucket: A string to indicate the bucket for the object to be uploaded to. |
| 150 | src_object_path: A string the full path of the object to upload. |
| 151 | dest_object_path: A string path inside bucket to upload to. |
| 152 | |
| 153 | Returns: |
| 154 | A dict of uploaded object info. |
| 155 | |
| 156 | Raises: |
| 157 | apiclient.errors.HttpError |
| 158 | """ |
| 159 | req = self._rest_client.service.objects().insert( |
| 160 | bucket=bucket, |
| 161 | name=dest_object_path, |
| 162 | media_body=src_object_path, |
| 163 | media_mime_type='text/plain', |
| 164 | ) |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 165 | return req.execute() |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 166 | |
| 167 | |
| 168 | class CalendarRestClient(object): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 169 | """Class of REST client for google calendar API.""" |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 170 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 171 | def __init__(self, rest_client): |
| 172 | """Initialize a REST client for connecting to Google calendar API.""" |
| 173 | self._rest_client = rest_client |
| 174 | self._rest_client.create_service() |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 175 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 176 | def add_event(self, calendar_id, input_event): |
| 177 | """Add events of a given calendar. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 178 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 179 | Args: |
| 180 | calendar_id: the ID of the given calendar. |
| 181 | input_event: the event to be added. |
| 182 | """ |
| 183 | self._rest_client.service.events().insert( |
| 184 | calendarId=calendar_id, |
| 185 | body=input_event).execute() |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 186 | |
| 187 | |
Xixuan Wu | 6f117e9 | 2017-10-27 10:51:58 -0700 | [diff] [blame] | 188 | class StackdriverRestClient(object): |
| 189 | """REST client for google storage API.""" |
| 190 | |
| 191 | def __init__(self, rest_client): |
| 192 | """Initialize a REST client for connecting to Google storage API.""" |
| 193 | self._rest_client = rest_client |
| 194 | self._rest_client.create_service() |
| 195 | |
| 196 | def read_logs(self, request): |
Xinan Lin | 318cf75 | 2019-07-19 14:50:23 -0700 | [diff] [blame] | 197 | # project_id, page_size, order_by, query_filter=''): |
Xixuan Wu | 6f117e9 | 2017-10-27 10:51:58 -0700 | [diff] [blame] | 198 | """Read the logs of the project_id based on all filters. |
| 199 | |
| 200 | Args: |
| 201 | request: a request dict generated by |
| 202 | stackdriver_lib.form_logging_client_request. |
| 203 | |
| 204 | Returns: |
| 205 | A json object, can be parsed by |
| 206 | stackdriver_lib.parse_logging_client_response. |
| 207 | |
| 208 | Raises: |
| 209 | apiclient.errors.HttpError |
| 210 | """ |
| 211 | req = self._rest_client.service.entries().list( |
| 212 | fields='entries/protoPayload', body=request) |
| 213 | return req.execute() |
| 214 | |
| 215 | |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 216 | class SwarmingRestClient(object): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 217 | """REST client for swarming proxy API.""" |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 218 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 219 | DISCOVERY_URL_PATTERN = '%s/discovery/v1/apis/%s/%s/rest' |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 220 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 221 | def __init__(self, rest_client, service_url): |
| 222 | self._rest_client = rest_client |
| 223 | discovery_url = self.DISCOVERY_URL_PATTERN % ( |
| 224 | service_url, rest_client.service_name, rest_client.service_version) |
| 225 | self._rest_client.create_service(discovery_url=discovery_url) |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 226 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 227 | def create_task(self, request): |
| 228 | """Create new task. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 229 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 230 | Args: |
| 231 | request: a json-compatible dict expected by swarming server. |
| 232 | See _to_raw_request's output in swarming_lib.py for details. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 233 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 234 | Returns: |
| 235 | A json dict returned by API task.new. |
| 236 | """ |
| 237 | return self._rest_client.service.tasks().new( |
| 238 | fields='request,task_id', body=request).execute() |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 239 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 240 | def get_task_result(self, task_id): |
| 241 | """Get task results by a given task_id. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 242 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 243 | Args: |
| 244 | task_id: A string, represents task id. |
| 245 | |
| 246 | Returns: |
| 247 | A json dict returned by API task.result. |
| 248 | """ |
| 249 | return self._rest_client.service.task().result( |
| 250 | task_id=task_id).execute() |
Xixuan Wu | 7d142a9 | 2019-04-26 12:03:02 -0700 | [diff] [blame] | 251 | |
| 252 | |
| 253 | class BigqueryRestClient(object): |
| 254 | """Class of REST client for Bigquery API.""" |
| 255 | |
Xixuan Wu | 55d38c5 | 2019-05-21 14:26:23 -0700 | [diff] [blame] | 256 | PROJECT_TO_RUN_BIGQUERY_JOB = 'google.com:suite-scheduler' |
| 257 | |
Xinan Lin | c9f0115 | 2020-02-05 22:05:13 -0800 | [diff] [blame] | 258 | def __init__(self, rest_client, project=None, dataset=None, table=None): |
Xixuan Wu | 7d142a9 | 2019-04-26 12:03:02 -0700 | [diff] [blame] | 259 | """Initialize a REST client for connecting to Bigquery API.""" |
| 260 | self._rest_client = rest_client |
| 261 | self._rest_client.create_service() |
Xinan Lin | c9f0115 | 2020-02-05 22:05:13 -0800 | [diff] [blame] | 262 | self.project = project |
| 263 | self.dataset = dataset |
| 264 | self.table = table |
Xixuan Wu | 7d142a9 | 2019-04-26 12:03:02 -0700 | [diff] [blame] | 265 | |
Xixuan Wu | 55d38c5 | 2019-05-21 14:26:23 -0700 | [diff] [blame] | 266 | def query(self, query_str): |
| 267 | """Query bigquery. |
Xixuan Wu | 7d142a9 | 2019-04-26 12:03:02 -0700 | [diff] [blame] | 268 | |
| 269 | Args: |
Xixuan Wu | 55d38c5 | 2019-05-21 14:26:23 -0700 | [diff] [blame] | 270 | query_str: A string used to query Bigquery. |
Xixuan Wu | 7d142a9 | 2019-04-26 12:03:02 -0700 | [diff] [blame] | 271 | |
| 272 | Returns: |
| 273 | A json dict returned by API bigquery.jobs.query, e.g. |
| 274 | # {..., |
| 275 | # "rows": [ |
| 276 | # { |
| 277 | # "f": [ # field |
| 278 | # { |
| 279 | # "v": # value |
Xixuan Wu | 55d38c5 | 2019-05-21 14:26:23 -0700 | [diff] [blame] | 280 | # }, |
| 281 | # { |
| 282 | # "v": # value |
| 283 | # }, |
| 284 | # ... |
Xixuan Wu | 7d142a9 | 2019-04-26 12:03:02 -0700 | [diff] [blame] | 285 | # ] |
| 286 | # } |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 287 | # ... |
Xixuan Wu | 7d142a9 | 2019-04-26 12:03:02 -0700 | [diff] [blame] | 288 | # ] |
| 289 | # } |
| 290 | """ |
| 291 | query_data = { |
Xixuan Wu | 55d38c5 | 2019-05-21 14:26:23 -0700 | [diff] [blame] | 292 | 'query': query_str, |
| 293 | 'useLegacySql': False, |
Xixuan Wu | 7d142a9 | 2019-04-26 12:03:02 -0700 | [diff] [blame] | 294 | } |
| 295 | return self._rest_client.service.jobs().query( |
Xixuan Wu | 55d38c5 | 2019-05-21 14:26:23 -0700 | [diff] [blame] | 296 | projectId=self.PROJECT_TO_RUN_BIGQUERY_JOB, |
| 297 | fields='rows', |
Xixuan Wu | 7d142a9 | 2019-04-26 12:03:02 -0700 | [diff] [blame] | 298 | body=query_data).execute() |
Xixuan Wu | 55d38c5 | 2019-05-21 14:26:23 -0700 | [diff] [blame] | 299 | |
Xinan Lin | c9f0115 | 2020-02-05 22:05:13 -0800 | [diff] [blame] | 300 | def insert(self, rows): |
| 301 | """Insert rows to specified Bigquery table. |
| 302 | |
| 303 | Args: |
| 304 | rows: list of json objects. |
| 305 | |
| 306 | Raise: |
| 307 | RestClientError: if project/dataset/table is not defined. |
| 308 | """ |
| 309 | if not any([self.project, self.dataset, self.table]): |
| 310 | raise RestClientError('Project, dataset, table should be all set.' |
| 311 | 'Got project:%s, dataset:%s, table:%s' % |
| 312 | (self.project, self.dataset, self.table)) |
| 313 | body = { |
| 314 | 'kind': 'bigquery#tableDataInsertAllRequest', |
| 315 | 'rows': rows, |
| 316 | } |
| 317 | request = self._rest_client.service.tabledata().insertAll( |
| 318 | projectId=self.project, |
| 319 | datasetId=self.dataset, |
| 320 | tableId=self.table, |
| 321 | body=body) |
| 322 | response = request.execute(num_retries=3) |
| 323 | if response.get('insertErrors'): |
| 324 | logging.error('InsertRequest reported errors: %r', |
| 325 | response.get('insertErrors')) |
| 326 | return False |
| 327 | |
| 328 | return True |
Xixuan Wu | 55d38c5 | 2019-05-21 14:26:23 -0700 | [diff] [blame] | 329 | |
Xinan Lin | 80a9d93 | 2019-10-17 09:24:43 -0700 | [diff] [blame] | 330 | class CrOSTestPlatformBigqueryClient(BigqueryRestClient): |
| 331 | """REST client for cros_test_platform builder Bigquery API.""" |
Xixuan Wu | 55d38c5 | 2019-05-21 14:26:23 -0700 | [diff] [blame] | 332 | |
Xinan Lin | 80a9d93 | 2019-10-17 09:24:43 -0700 | [diff] [blame] | 333 | def get_past_job_nums(self, hours): |
| 334 | """Query the count of the jobs kicked off to cros_test_platform. |
Xixuan Wu | 55d38c5 | 2019-05-21 14:26:23 -0700 | [diff] [blame] | 335 | |
| 336 | Args: |
| 337 | hours: An integer. |
| 338 | |
| 339 | Returns: |
| 340 | An integer. |
| 341 | """ |
| 342 | query_str = """ |
| 343 | SELECT |
| 344 | COUNT(*) |
| 345 | FROM |
Xinan Lin | 80a9d93 | 2019-10-17 09:24:43 -0700 | [diff] [blame] | 346 | `cr-buildbucket.chromeos.builds` |
Xixuan Wu | 55d38c5 | 2019-05-21 14:26:23 -0700 | [diff] [blame] | 347 | WHERE |
Xinan Lin | 80a9d93 | 2019-10-17 09:24:43 -0700 | [diff] [blame] | 348 | created_by = 'user:suite-scheduler.google.com@appspot.gserviceaccount.com' |
| 349 | and create_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL %d HOUR); |
Xixuan Wu | 55d38c5 | 2019-05-21 14:26:23 -0700 | [diff] [blame] | 350 | """ |
| 351 | res = self.query(query_str % hours) |
| 352 | try: |
| 353 | return int(_parse_bq_job_query(res)[0][0]) |
| 354 | except (ValueError, KeyError) as e: |
| 355 | logging.debug('The returned json: \n%r', res) |
| 356 | logging.exception(str(e)) |
| 357 | raise |
| 358 | |
| 359 | |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 360 | class BuildBucketBigqueryClient(BigqueryRestClient): |
| 361 | """Rest client for buildbucket Bigquery API.""" |
| 362 | |
Xinan Lin | 028f958 | 2019-12-11 10:55:33 -0800 | [diff] [blame] | 363 | def get_latest_passed_firmware_builds(self): |
| 364 | """Get artifact link of the latest passed firmware builds for board. |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 365 | |
Xinan Lin | 028f958 | 2019-12-11 10:55:33 -0800 | [diff] [blame] | 366 | The query returns the latest firmware build for the combination of |
| 367 | board and build spec, which is cros or firmware. No restriction set |
| 368 | in the query, so it should return all available builds. |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 369 | |
| 370 | Returns: |
Xinan Lin | 028f958 | 2019-12-11 10:55:33 -0800 | [diff] [blame] | 371 | A list of (spec, board, firmware_artifact_link). |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 372 | """ |
| 373 | query_str = """ |
| 374 | SELECT |
Xinan Lin | 028f958 | 2019-12-11 10:55:33 -0800 | [diff] [blame] | 375 | spec, |
| 376 | board, |
| 377 | /* |
| 378 | * Firmware builds may contain artifacts for multiple boards in a |
| 379 | * single build - each in a separate directory. |
| 380 | */ |
| 381 | IF(spec = 'firmware', CONCAT(artifact, '/', board), artifact) as artifact |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 382 | FROM |
Xinan Lin | 028f958 | 2019-12-11 10:55:33 -0800 | [diff] [blame] | 383 | ( |
| 384 | SELECT |
| 385 | spec, |
| 386 | board, |
| 387 | artifact, |
| 388 | RANK() OVER (PARTITION BY spec, board ORDER BY end_time DESC) AS rank |
| 389 | FROM |
| 390 | ( |
| 391 | SELECT |
| 392 | /* |
| 393 | * build_config is a string contains the board and build type. |
| 394 | * For Cros build, it has the form of "BoardName-release", while |
| 395 | * the firmware config shows like "firmware-BoardName-[1]-firmwarebranch". |
| 396 | * [1] is the firmware ver. |
| 397 | */ |
| 398 | IF(prefix = 'firmware', 'firmware', 'cros') AS spec, |
| 399 | IF(prefix = 'firmware', firmware_post_prefix, cros_prefix) AS board, |
| 400 | artifact, |
| 401 | end_time |
| 402 | FROM |
| 403 | ( |
| 404 | SELECT |
| 405 | SPLIT(build_config, '-') [OFFSET(0)] AS prefix, |
| 406 | SPLIT(build_config, '-') [OFFSET(1)] AS firmware_post_prefix, |
| 407 | REGEXP_EXTRACT( |
| 408 | build_config, r"(^[a-zA-Z0-9_.+-]+)-release" |
| 409 | ) as cros_prefix, |
| 410 | end_time, |
| 411 | artifact |
| 412 | FROM |
| 413 | ( |
| 414 | SELECT |
| 415 | JSON_EXTRACT_SCALAR( |
| 416 | output.properties, '$.artifact_link' |
| 417 | ) as artifact, |
| 418 | JSON_EXTRACT_SCALAR( |
| 419 | output.properties, '$.cbb_config' |
| 420 | ) as build_config, |
| 421 | end_time |
| 422 | FROM `cr-buildbucket.chromeos.completed_builds_BETA` |
| 423 | WHERE |
| 424 | status = 'SUCCESS' |
| 425 | AND JSON_EXTRACT_SCALAR( |
| 426 | output.properties, '$.suite_scheduling' |
| 427 | ) = 'True' |
| 428 | ) |
| 429 | ) |
| 430 | ) |
| 431 | ) |
| 432 | WHERE rank = 1 |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 433 | """ |
Xinan Lin | 028f958 | 2019-12-11 10:55:33 -0800 | [diff] [blame] | 434 | res = self.query(query_str) |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 435 | res = _parse_bq_job_query(res) |
| 436 | if res is None: |
| 437 | return None |
Xinan Lin | 028f958 | 2019-12-11 10:55:33 -0800 | [diff] [blame] | 438 | logging.info('Fetched the latest artifact links: %s', |
| 439 | [row[2] for row in res]) |
| 440 | return res |
Xinan Lin | 318cf75 | 2019-07-19 14:50:23 -0700 | [diff] [blame] | 441 | |
Xinan Lin | ea1efcb | 2019-12-30 23:46:42 -0800 | [diff] [blame] | 442 | def get_passed_builds(self, earliest_end_time, latest_end_time): |
| 443 | """Get passed builds inside a given time span. |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 444 | |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 445 | BigQuery does not guarantee the inserted time of rows. A new build |
| 446 | may not get inserted when suite scheduler runs the query. To avoid |
| 447 | it, we run the query twice: |
| 448 | - the first run catches the new build from earliest_end_time to |
| 449 | latest_end_time, and inserts the result to a temp BQ table. |
| 450 | - the second run checks the build from (earliest_end_time - 1Day) |
| 451 | to (latest_end_time - 1Day) plus (earliest_end_time to |
| 452 | latest_end_time). The query returns the build which does not |
| 453 | appear in the temp table. Thus, if a build was not fetched by the |
| 454 | first run, we still could schedule test on it at most 1 day later. |
| 455 | |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 456 | Args: |
Xinan Lin | ea1efcb | 2019-12-30 23:46:42 -0800 | [diff] [blame] | 457 | earliest_end_time: a datetime.datetime object in UTC. |
| 458 | latest_end_time: a datetime.datetime object in UTC. |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 459 | |
| 460 | Returns: |
| 461 | A list of build_lib.BuildInfo objects. |
| 462 | """ |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 463 | base_query_str = """ |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 464 | SELECT |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 465 | JSON_EXTRACT_SCALAR(output.properties, '$.board') AS board, |
| 466 | JSON_EXTRACT_SCALAR(output.properties, '$.milestone_version') AS milestone, |
| 467 | JSON_EXTRACT_SCALAR(output.properties, '$.platform_version') AS platform, |
| 468 | JSON_EXTRACT_SCALAR(output.properties, '$.cbb_config') AS build_config, |
| 469 | -- Time info |
| 470 | end_time as build_end_time, |
| 471 | CURRENT_TIMESTAMP() as inserted_time |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 472 | FROM |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 473 | `cr-buildbucket.chromeos.builds` |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 474 | WHERE |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 475 | status = 'SUCCESS' |
| 476 | AND JSON_EXTRACT_SCALAR(output.properties, '$.suite_scheduling') = 'True' |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 477 | """ |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 478 | |
| 479 | insert_passed_builds = """ |
| 480 | INSERT |
| 481 | `google.com:{0}.builds.passed_builds`( |
| 482 | board, |
| 483 | milestone, |
| 484 | platform, |
| 485 | build_config, |
| 486 | build_end_time, |
| 487 | inserted_time |
| 488 | ) {1} |
| 489 | AND end_time > '{2}' |
| 490 | AND end_time < '{3}' |
| 491 | """ |
| 492 | earliest_end_time_str = earliest_end_time.strftime( |
| 493 | time_converter.TIME_FORMAT) |
| 494 | latest_end_time_str = latest_end_time.strftime( |
| 495 | time_converter.TIME_FORMAT) |
Xinan Lin | 66a60f4 | 2020-03-04 13:12:32 -0800 | [diff] [blame^] | 496 | project_id = constants.AppID.STAGING_APP |
| 497 | if constants.environment() == constants.RunningEnv.ENV_PROD: |
| 498 | project_id = constants.application_id() |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 499 | # Insert the currently visible builds to BQ. |
Xinan Lin | 66a60f4 | 2020-03-04 13:12:32 -0800 | [diff] [blame^] | 500 | logging.info('Insert the visible passed builds ' |
| 501 | 'between %s and %s to BQ.', |
| 502 | earliest_end_time_str, latest_end_time_str) |
| 503 | self.query( |
| 504 | insert_passed_builds.format( |
| 505 | project_id, |
| 506 | base_query_str, |
| 507 | earliest_end_time_str, |
| 508 | latest_end_time_str)) |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 509 | |
Xinan Lin | ea1efcb | 2019-12-30 23:46:42 -0800 | [diff] [blame] | 510 | logging.info('Getting passed builds finished between %s and %s', |
| 511 | earliest_end_time_str, latest_end_time_str) |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 512 | query_str = """ |
| 513 | WITH passed_builds AS |
| 514 | ({0}) |
| 515 | SELECT |
| 516 | b.board, |
| 517 | b.milestone, |
| 518 | b.platform, |
| 519 | b.build_config, |
| 520 | FROM |
| 521 | passed_builds AS b |
| 522 | LEFT JOIN |
| 523 | `google.com:{1}.builds.passed_builds` AS r |
| 524 | ON ( |
| 525 | r.board = b.board |
| 526 | AND r.milestone = b.milestone |
| 527 | AND r.build_config = b.build_config |
| 528 | AND r.platform = b.platform |
| 529 | AND r.build_end_time > TIMESTAMP_SUB( |
| 530 | PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', '{2}'), |
| 531 | INTERVAL 1 DAY) |
| 532 | AND r.build_end_time < TIMESTAMP_SUB( |
| 533 | PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', '{3}'), |
| 534 | INTERVAL 1 DAY) |
| 535 | ) |
| 536 | WHERE |
| 537 | -- Check if any build was inserted to release builder |
| 538 | -- in the past 24 hours. |
| 539 | (b.build_end_time > TIMESTAMP_SUB( |
| 540 | PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', '{2}'), |
| 541 | INTERVAL 1 DAY) |
| 542 | AND b.build_end_time < TIMESTAMP_SUB( |
| 543 | PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', '{3}'), |
| 544 | INTERVAL 1 DAY) |
| 545 | AND r.inserted_time is null) |
Xinan Lin | 66a60f4 | 2020-03-04 13:12:32 -0800 | [diff] [blame^] | 546 | OR (b.build_end_time > '{2}' |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 547 | AND b.build_end_time < '{3}') |
| 548 | """ |
| 549 | if global_config.GAE_TESTING: |
| 550 | query_str += 'limit 10' |
| 551 | res = self.query( |
| 552 | query_str.format( |
| 553 | base_query_str, |
Xinan Lin | 66a60f4 | 2020-03-04 13:12:32 -0800 | [diff] [blame^] | 554 | project_id, |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 555 | earliest_end_time_str, |
| 556 | latest_end_time_str)) |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 557 | res = _parse_bq_job_query(res) |
| 558 | if res is None: |
| 559 | return [] |
| 560 | |
| 561 | build_infos = [] |
| 562 | for board, milestone, platform, build_config in res: |
| 563 | board = _parse_board(build_config, board) |
| 564 | build_infos.append( |
| 565 | build_lib.BuildInfo(board, None, milestone, platform, build_config)) |
| 566 | |
| 567 | return build_infos |
| 568 | |
Xinan Lin | ea1efcb | 2019-12-30 23:46:42 -0800 | [diff] [blame] | 569 | def get_relaxed_passed_builds(self, earliest_end_time, latest_end_time): |
| 570 | """Get builds with successful "HWTest [sanity]" stages between a given span. |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 571 | |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 572 | Same as get_passed_builds, we run the query twice to ensure we fetched all |
| 573 | builds from BQ. |
| 574 | |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 575 | Args: |
Xinan Lin | ea1efcb | 2019-12-30 23:46:42 -0800 | [diff] [blame] | 576 | earliest_end_time: a datetime.datetime object in UTC. |
| 577 | latest_end_time: a datetime.datetime object in UTC. |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 578 | |
| 579 | Returns: |
| 580 | A list of build_lib.BuildInfo objects. |
| 581 | """ |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 582 | base_query_str = """ |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 583 | SELECT |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 584 | JSON_EXTRACT_SCALAR(output.properties, '$.unibuild') as unibuild, |
| 585 | s.name as stage_name, |
| 586 | JSON_EXTRACT_SCALAR(output.properties, '$.board') as board, |
| 587 | JSON_EXTRACT_SCALAR(output.properties, '$.milestone_version') as milestone, |
| 588 | JSON_EXTRACT_SCALAR(output.properties, '$.platform_version') as platform, |
| 589 | JSON_EXTRACT_SCALAR(output.properties, '$.cbb_config') as build_config, |
| 590 | -- Time info |
| 591 | c.end_time as build_end_time, |
| 592 | CURRENT_TIMESTAMP() as inserted_time |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 593 | FROM |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 594 | `cr-buildbucket.chromeos.builds` as c, |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 595 | UNNEST(c.steps) AS s |
| 596 | WHERE |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 597 | c.status != 'SUCCESS' |
| 598 | AND s.name like 'SkylabHWTest%%' |
| 599 | AND s.status = 'SUCCESS' |
| 600 | AND JSON_EXTRACT_SCALAR(output.properties, '$.suite_scheduling') = 'True' |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 601 | """ |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 602 | |
| 603 | insert_relaxed_builds = """ |
| 604 | INSERT |
| 605 | `google.com:{0}.builds.relaxed_builds`( |
| 606 | unibuild, |
| 607 | stage_name, |
| 608 | board, |
| 609 | milestone, |
| 610 | platform, |
| 611 | build_config, |
| 612 | build_end_time, |
| 613 | inserted_time |
| 614 | ) {1} |
| 615 | AND c.end_time > '{2}' |
| 616 | AND c.end_time < '{3}' |
| 617 | """ |
| 618 | earliest_end_time_str = earliest_end_time.strftime( |
| 619 | time_converter.TIME_FORMAT) |
| 620 | latest_end_time_str = latest_end_time.strftime( |
| 621 | time_converter.TIME_FORMAT) |
Xinan Lin | 66a60f4 | 2020-03-04 13:12:32 -0800 | [diff] [blame^] | 622 | project_id = constants.AppID.STAGING_APP |
| 623 | if constants.environment() == constants.RunningEnv.ENV_PROD: |
| 624 | project_id = constants.application_id() |
| 625 | logging.info('Insert the visible relaxed builds ' |
| 626 | 'between %s and %s to BQ.', |
| 627 | earliest_end_time_str, latest_end_time_str) |
| 628 | self.query( |
| 629 | insert_relaxed_builds.format( |
| 630 | project_id, |
| 631 | base_query_str, |
| 632 | earliest_end_time_str, |
| 633 | latest_end_time_str)) |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 634 | |
Xinan Lin | ea1efcb | 2019-12-30 23:46:42 -0800 | [diff] [blame] | 635 | logging.info('Getting relaxed passed builds finished between %s and %s', |
| 636 | earliest_end_time_str, latest_end_time_str) |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 637 | query_str = """ |
| 638 | WITH relaxed_builds AS |
| 639 | ({0}) |
| 640 | SELECT |
| 641 | b.unibuild, |
| 642 | b.stage_name, |
| 643 | b.board, |
| 644 | b.milestone, |
| 645 | b.platform, |
| 646 | b.build_config, |
| 647 | FROM |
| 648 | relaxed_builds AS b |
| 649 | LEFT JOIN |
| 650 | `google.com:{1}.builds.relaxed_builds` AS r |
| 651 | ON ( |
| 652 | r.board = b.board |
| 653 | AND r.milestone = b.milestone |
| 654 | AND r.build_config = b.build_config |
| 655 | AND r.platform = b.platform |
| 656 | AND r.build_end_time > TIMESTAMP_SUB( |
| 657 | PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', '{2}'), |
| 658 | INTERVAL 1 DAY) |
| 659 | AND r.build_end_time < TIMESTAMP_SUB( |
| 660 | PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', '{3}'), |
| 661 | INTERVAL 1 DAY) |
| 662 | ) |
| 663 | WHERE |
| 664 | -- Check if any build was inserted to release builder |
| 665 | -- in the past 24 hours. |
| 666 | (b.build_end_time > TIMESTAMP_SUB( |
| 667 | PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', '{2}'), |
| 668 | INTERVAL 1 DAY) |
| 669 | AND b.build_end_time < TIMESTAMP_SUB( |
| 670 | PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', '{3}'), |
| 671 | INTERVAL 1 DAY) |
| 672 | AND r.inserted_time is null) |
| 673 | OR (b.build_end_time > '{2}' |
| 674 | AND b.build_end_time < '{3}') |
| 675 | """ |
| 676 | if global_config.GAE_TESTING: |
| 677 | query_str += 'limit 10' |
| 678 | res = self.query( |
| 679 | query_str.format( |
| 680 | base_query_str, |
Xinan Lin | 66a60f4 | 2020-03-04 13:12:32 -0800 | [diff] [blame^] | 681 | project_id, |
Xinan Lin | 3330d67 | 2020-03-03 14:52:36 -0800 | [diff] [blame] | 682 | earliest_end_time_str, |
| 683 | latest_end_time_str)) |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 684 | res = _parse_bq_job_query(res) |
| 685 | if res is None: |
| 686 | return [] |
| 687 | |
| 688 | build_infos = [] |
| 689 | for unibuild, stage_name, board, milestone, platform, build_config in res: |
| 690 | board = _parse_board(build_config, board) |
| 691 | if ast.literal_eval(unibuild): |
| 692 | model = _parse_model(stage_name) |
| 693 | else: |
| 694 | model = None |
| 695 | |
| 696 | build_infos.append( |
| 697 | build_lib.BuildInfo(board, model, milestone, platform, build_config)) |
| 698 | |
| 699 | return build_infos |
| 700 | |
| 701 | |
Xixuan Wu | 55d38c5 | 2019-05-21 14:26:23 -0700 | [diff] [blame] | 702 | def _parse_bq_job_query(json_input): |
| 703 | """Parse response from API bigquery.jobs.query. |
| 704 | |
| 705 | Args: |
| 706 | json_input: a dict, representing jsons returned by query API. |
| 707 | |
| 708 | Returns: |
| 709 | A 2D string matrix: [rows[columns]], or None if no result. |
| 710 | E.g. Input: |
| 711 | "rows": [ |
| 712 | { |
| 713 | "f": [ # field |
| 714 | { |
| 715 | "v": 'foo1', |
| 716 | }, |
| 717 | { |
| 718 | "v": 'foo2', |
| 719 | } |
| 720 | ] |
| 721 | } |
| 722 | { |
| 723 | "f": [ # field |
| 724 | { |
| 725 | "v": 'bar1', |
| 726 | }, |
| 727 | { |
| 728 | "v": 'bar2', |
| 729 | } |
| 730 | ] |
| 731 | } |
| 732 | ] |
| 733 | => Output: [['foo1', 'foo2'], ['bar1', 'bar2']] |
| 734 | """ |
| 735 | if 'rows' not in json_input: |
| 736 | return None |
| 737 | |
| 738 | res = [] |
| 739 | for r in json_input['rows']: |
| 740 | rc = [] |
| 741 | for c in r['f']: |
| 742 | rc.append(c['v']) |
| 743 | |
| 744 | res.append(rc) |
| 745 | |
| 746 | return res |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 747 | |
| 748 | |
| 749 | def _parse_model(build_stage_name): |
| 750 | """Parse model name from the build stage name. |
| 751 | |
| 752 | It's only used for HWTest Sanity stage. An example build_stage_name will |
| 753 | be 'HWTest [sanity] [whitetip]'. |
| 754 | Args: |
| 755 | build_stage_name: The stage name of a HWTest sanity stage, e.g. |
| 756 | "HWTest [sanity] [whitetip]". |
| 757 | |
| 758 | Returns: |
| 759 | A model name, e.g. "whitetip" or None. |
| 760 | """ |
| 761 | if 'HWTest [sanity]' not in build_stage_name: |
| 762 | return None |
| 763 | |
| 764 | try: |
| 765 | model = build_stage_name.strip().split()[-1][1:-1] |
| 766 | if not model: |
| 767 | logging.warning('Cannot parse build stage name: %s', build_stage_name) |
| 768 | return None |
| 769 | |
| 770 | return model |
| 771 | except IndexError: |
| 772 | logging.error('Cannot parse build stage name: %s', build_stage_name) |
| 773 | return None |
| 774 | |
| 775 | |
| 776 | def _parse_board(build_config, board): |
| 777 | """Parse board from build_config if needed. |
| 778 | |
| 779 | Board could be None for old release. See crbug.com/944981#c16. |
| 780 | This function can be removed once all release are newer than R74. |
| 781 | |
| 782 | Args: |
| 783 | build_config: A string build config, e.g. reef-release. |
| 784 | board: A string board, e.g. reef. |
| 785 | |
| 786 | Returns: |
| 787 | A string board if exist, or None. |
| 788 | """ |
| 789 | if board is None: |
| 790 | match = re.match(r'(.+)-release', build_config) |
| 791 | if not match: |
| 792 | logging.debug('Cannot parse board from %s', build_config) |
| 793 | return None |
| 794 | |
| 795 | return match.groups()[0] |
| 796 | |
| 797 | return board |