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