Ben Rohlfs | c9f9098 | 2020-12-13 22:00:11 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright (C) 2020 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Ben Rohlfs | 85a866b | 2021-04-15 11:20:02 +0200 | [diff] [blame] | 17 | import { |
| 18 | Action, |
| 19 | Category, |
Ben Rohlfs | 85a866b | 2021-04-15 11:20:02 +0200 | [diff] [blame] | 20 | CheckResult as CheckResultApi, |
Ben Rohlfs | f457f89 | 2021-06-18 10:57:44 +0200 | [diff] [blame] | 21 | CheckRun as CheckRunApi, |
| 22 | Link, |
Ben Rohlfs | 85a866b | 2021-04-15 11:20:02 +0200 | [diff] [blame] | 23 | LinkIcon, |
| 24 | RunStatus, |
| 25 | } from '../../api/checks'; |
Ben Rohlfs | 913ab76 | 2021-02-05 12:52:00 +0100 | [diff] [blame] | 26 | import {assertNever} from '../../utils/common-util'; |
Ben Rohlfs | a684e01 | 2021-03-22 13:53:57 +0100 | [diff] [blame] | 27 | import {CheckResult, CheckRun} from './checks-model'; |
Ben Rohlfs | c9f9098 | 2020-12-13 22:00:11 +0100 | [diff] [blame] | 28 | |
Ben Rohlfs | 345e946 | 2021-04-19 15:18:27 +0200 | [diff] [blame] | 29 | export function iconForLink(linkIcon?: LinkIcon) { |
| 30 | if (linkIcon === undefined) return 'launch'; |
Ben Rohlfs | a8bb0da | 2021-04-12 08:56:20 +0200 | [diff] [blame] | 31 | switch (linkIcon) { |
| 32 | case LinkIcon.EXTERNAL: |
| 33 | return 'launch'; |
| 34 | case LinkIcon.IMAGE: |
| 35 | return 'insert-photo'; |
| 36 | case LinkIcon.HISTORY: |
| 37 | return 'restore'; |
| 38 | case LinkIcon.DOWNLOAD: |
| 39 | return 'download'; |
| 40 | case LinkIcon.DOWNLOAD_MOBILE: |
| 41 | return 'system-update'; |
| 42 | case LinkIcon.HELP_PAGE: |
| 43 | return 'help-outline'; |
| 44 | case LinkIcon.REPORT_BUG: |
| 45 | return 'bug'; |
Ben Rohlfs | 7d262be | 2021-06-02 14:59:03 +0200 | [diff] [blame] | 46 | case LinkIcon.CODE: |
| 47 | return 'code'; |
Ben Rohlfs | 17b19b3 | 2021-06-22 11:14:36 +0200 | [diff] [blame] | 48 | case LinkIcon.FILE_PRESENT: |
| 49 | return 'file-present'; |
Ben Rohlfs | a8bb0da | 2021-04-12 08:56:20 +0200 | [diff] [blame] | 50 | default: |
Ben Rohlfs | 5d92c39 | 2021-04-20 10:14:57 +0200 | [diff] [blame] | 51 | // We don't throw an assertion error here, because plugins don't have to |
| 52 | // be written in TypeScript, so we may encounter arbitrary strings for |
| 53 | // linkIcon. |
| 54 | return 'launch'; |
Ben Rohlfs | a8bb0da | 2021-04-12 08:56:20 +0200 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Ben Rohlfs | 5d92c39 | 2021-04-20 10:14:57 +0200 | [diff] [blame] | 58 | export function tooltipForLink(linkIcon?: LinkIcon) { |
| 59 | if (linkIcon === undefined) return 'Link to details'; |
Ben Rohlfs | a8bb0da | 2021-04-12 08:56:20 +0200 | [diff] [blame] | 60 | switch (linkIcon) { |
| 61 | case LinkIcon.EXTERNAL: |
| 62 | return 'Link to details'; |
| 63 | case LinkIcon.IMAGE: |
| 64 | return 'Link to image'; |
| 65 | case LinkIcon.HISTORY: |
| 66 | return 'Link to result history'; |
| 67 | case LinkIcon.DOWNLOAD: |
| 68 | return 'Download'; |
| 69 | case LinkIcon.DOWNLOAD_MOBILE: |
| 70 | return 'Download'; |
| 71 | case LinkIcon.HELP_PAGE: |
| 72 | return 'Link to help page'; |
| 73 | case LinkIcon.REPORT_BUG: |
| 74 | return 'Link for reporting a problem'; |
Ben Rohlfs | 17b19b3 | 2021-06-22 11:14:36 +0200 | [diff] [blame] | 75 | case LinkIcon.CODE: |
| 76 | return 'Link to code'; |
| 77 | case LinkIcon.FILE_PRESENT: |
| 78 | return 'Link to file'; |
Ben Rohlfs | a8bb0da | 2021-04-12 08:56:20 +0200 | [diff] [blame] | 79 | default: |
Ben Rohlfs | 5d92c39 | 2021-04-20 10:14:57 +0200 | [diff] [blame] | 80 | // We don't throw an assertion error here, because plugins don't have to |
| 81 | // be written in TypeScript, so we may encounter arbitrary strings for |
| 82 | // linkIcon. |
| 83 | return 'Link to details'; |
Ben Rohlfs | a8bb0da | 2021-04-12 08:56:20 +0200 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Ben Rohlfs | c9f9098 | 2020-12-13 22:00:11 +0100 | [diff] [blame] | 87 | export function worstCategory(run: CheckRun) { |
Ben Rohlfs | 913ab76 | 2021-02-05 12:52:00 +0100 | [diff] [blame] | 88 | if (hasResultsOf(run, Category.ERROR)) return Category.ERROR; |
| 89 | if (hasResultsOf(run, Category.WARNING)) return Category.WARNING; |
| 90 | if (hasResultsOf(run, Category.INFO)) return Category.INFO; |
Ben Rohlfs | 6cc7e7b | 2021-05-10 13:54:38 +0200 | [diff] [blame] | 91 | if (hasResultsOf(run, Category.SUCCESS)) return Category.SUCCESS; |
Ben Rohlfs | c9f9098 | 2020-12-13 22:00:11 +0100 | [diff] [blame] | 92 | return undefined; |
| 93 | } |
| 94 | |
Ben Rohlfs | d58e07e | 2021-06-24 13:22:24 +0200 | [diff] [blame] | 95 | export function isCategory( |
| 96 | catStat?: Category | RunStatus |
| 97 | ): catStat is Category { |
| 98 | return ( |
| 99 | catStat === Category.ERROR || |
| 100 | catStat === Category.WARNING || |
| 101 | catStat === Category.INFO || |
| 102 | catStat === Category.SUCCESS |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | export function isStatus(catStat?: Category | RunStatus): catStat is RunStatus { |
Ben Rohlfs | f457f89 | 2021-06-18 10:57:44 +0200 | [diff] [blame] | 107 | return ( |
| 108 | catStat === RunStatus.COMPLETED || |
| 109 | catStat === RunStatus.RUNNABLE || |
| 110 | catStat === RunStatus.RUNNING |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | export function labelFor(catStat: Category | RunStatus) { |
| 115 | switch (catStat) { |
| 116 | case Category.ERROR: |
| 117 | return 'error'; |
| 118 | case Category.INFO: |
| 119 | return 'info'; |
| 120 | case Category.WARNING: |
| 121 | return 'warning'; |
| 122 | case Category.SUCCESS: |
| 123 | return 'success'; |
| 124 | case RunStatus.COMPLETED: |
| 125 | return 'completed'; |
| 126 | case RunStatus.RUNNABLE: |
| 127 | return 'runnable'; |
| 128 | case RunStatus.RUNNING: |
| 129 | return 'running'; |
| 130 | default: |
| 131 | assertNever(catStat, `Unsupported category/status: ${catStat}`); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | export function iconFor(catStat: Category | RunStatus) { |
| 136 | switch (catStat) { |
Ben Rohlfs | 913ab76 | 2021-02-05 12:52:00 +0100 | [diff] [blame] | 137 | case Category.ERROR: |
| 138 | return 'error'; |
| 139 | case Category.INFO: |
| 140 | return 'info-outline'; |
| 141 | case Category.WARNING: |
| 142 | return 'warning'; |
Ben Rohlfs | 6cc7e7b | 2021-05-10 13:54:38 +0200 | [diff] [blame] | 143 | case Category.SUCCESS: |
Ben Rohlfs | fee542a | 2021-02-24 13:24:54 +0100 | [diff] [blame] | 144 | return 'check-circle-outline'; |
Ben Rohlfs | f457f89 | 2021-06-18 10:57:44 +0200 | [diff] [blame] | 145 | // Note that this is only for COMPLETED without results! |
| 146 | case RunStatus.COMPLETED: |
| 147 | return 'check-circle-outline'; |
| 148 | case RunStatus.RUNNABLE: |
| 149 | return 'placeholder'; |
| 150 | case RunStatus.RUNNING: |
| 151 | return 'timelapse'; |
Ben Rohlfs | 913ab76 | 2021-02-05 12:52:00 +0100 | [diff] [blame] | 152 | default: |
Ben Rohlfs | f457f89 | 2021-06-18 10:57:44 +0200 | [diff] [blame] | 153 | assertNever(catStat, `Unsupported category/status: ${catStat}`); |
Ben Rohlfs | 913ab76 | 2021-02-05 12:52:00 +0100 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Ben Rohlfs | 5ee0cd4 | 2021-05-27 12:13:54 +0200 | [diff] [blame] | 157 | export enum PRIMARY_STATUS_ACTIONS { |
Ben Rohlfs | a77fd1d | 2021-02-15 10:18:45 +0100 | [diff] [blame] | 158 | RERUN = 'rerun', |
| 159 | RUN = 'run', |
Ben Rohlfs | a77fd1d | 2021-02-15 10:18:45 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | export function toCanonicalAction(action: Action, status: RunStatus) { |
| 163 | let name = action.name.toLowerCase(); |
| 164 | if (status === RunStatus.COMPLETED && (name === 'run' || name === 're-run')) { |
| 165 | name = PRIMARY_STATUS_ACTIONS.RERUN; |
| 166 | } |
Ben Rohlfs | a77fd1d | 2021-02-15 10:18:45 +0100 | [diff] [blame] | 167 | return {...action, name}; |
| 168 | } |
| 169 | |
Ben Rohlfs | 24743b1d | 2021-09-23 10:02:46 +0200 | [diff] [blame] | 170 | export function headerForStatus(status: RunStatus) { |
| 171 | switch (status) { |
| 172 | case RunStatus.COMPLETED: |
| 173 | return 'Completed'; |
| 174 | case RunStatus.RUNNABLE: |
| 175 | return 'Not run'; |
| 176 | case RunStatus.RUNNING: |
| 177 | return 'Running'; |
| 178 | default: |
| 179 | assertNever(status, `Unsupported status: ${status}`); |
| 180 | } |
| 181 | } |
| 182 | |
Ben Rohlfs | c50ac6e | 2021-09-07 11:00:08 +0200 | [diff] [blame] | 183 | function primaryActionName(status: RunStatus) { |
Ben Rohlfs | a77fd1d | 2021-02-15 10:18:45 +0100 | [diff] [blame] | 184 | switch (status) { |
| 185 | case RunStatus.COMPLETED: |
| 186 | return PRIMARY_STATUS_ACTIONS.RERUN; |
| 187 | case RunStatus.RUNNABLE: |
| 188 | return PRIMARY_STATUS_ACTIONS.RUN; |
| 189 | case RunStatus.RUNNING: |
Ben Rohlfs | c50ac6e | 2021-09-07 11:00:08 +0200 | [diff] [blame] | 190 | return undefined; |
Ben Rohlfs | a77fd1d | 2021-02-15 10:18:45 +0100 | [diff] [blame] | 191 | default: |
| 192 | assertNever(status, `Unsupported status: ${status}`); |
| 193 | } |
| 194 | } |
| 195 | |
Ben Rohlfs | 5ee0cd4 | 2021-05-27 12:13:54 +0200 | [diff] [blame] | 196 | export function primaryRunAction(run?: CheckRun): Action | undefined { |
| 197 | if (!run) return undefined; |
Ben Rohlfs | 586c4ef | 2021-04-22 18:05:29 +0200 | [diff] [blame] | 198 | return runActions(run).filter( |
Ben Rohlfs | d70a337 | 2021-05-18 12:52:12 +0200 | [diff] [blame] | 199 | action => !action.disabled && action.name === primaryActionName(run.status) |
Ben Rohlfs | 586c4ef | 2021-04-22 18:05:29 +0200 | [diff] [blame] | 200 | )[0]; |
| 201 | } |
| 202 | |
| 203 | export function runActions(run?: CheckRun): Action[] { |
| 204 | if (!run?.actions) return []; |
| 205 | return run.actions.map(action => toCanonicalAction(action, run.status)); |
Ben Rohlfs | a77fd1d | 2021-02-15 10:18:45 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Ben Rohlfs | 913ab76 | 2021-02-05 12:52:00 +0100 | [diff] [blame] | 208 | export function iconForRun(run: CheckRun) { |
Ben Rohlfs | e510067 | 2021-02-25 13:05:15 +0100 | [diff] [blame] | 209 | if (run.status !== RunStatus.COMPLETED) { |
Ben Rohlfs | f457f89 | 2021-06-18 10:57:44 +0200 | [diff] [blame] | 210 | return iconFor(run.status); |
Ben Rohlfs | e510067 | 2021-02-25 13:05:15 +0100 | [diff] [blame] | 211 | } else { |
| 212 | const category = worstCategory(run); |
Ben Rohlfs | f457f89 | 2021-06-18 10:57:44 +0200 | [diff] [blame] | 213 | return category ? iconFor(category) : iconFor(run.status); |
Ben Rohlfs | 913ab76 | 2021-02-05 12:52:00 +0100 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Ben Rohlfs | c0c2ea5 | 2021-01-21 09:45:16 +0100 | [diff] [blame] | 217 | export function hasCompleted(run: CheckRun) { |
| 218 | return run.status === RunStatus.COMPLETED; |
| 219 | } |
| 220 | |
| 221 | export function isRunning(run: CheckRun) { |
| 222 | return run.status === RunStatus.RUNNING; |
| 223 | } |
| 224 | |
| 225 | export function isRunningOrHasCompleted(run: CheckRun) { |
| 226 | return run.status === RunStatus.COMPLETED || run.status === RunStatus.RUNNING; |
| 227 | } |
| 228 | |
| 229 | export function hasCompletedWithoutResults(run: CheckRun) { |
| 230 | return run.status === RunStatus.COMPLETED && (run.results ?? []).length === 0; |
| 231 | } |
| 232 | |
Ben Rohlfs | 913ab76 | 2021-02-05 12:52:00 +0100 | [diff] [blame] | 233 | export function hasCompletedWith(run: CheckRun, category: Category) { |
| 234 | return hasCompleted(run) && hasResultsOf(run, category); |
| 235 | } |
| 236 | |
Ben Rohlfs | 8845349 | 2021-03-03 11:32:40 +0100 | [diff] [blame] | 237 | export function hasResults(run: CheckRun): boolean { |
| 238 | return (run.results ?? []).length > 0; |
| 239 | } |
| 240 | |
| 241 | export function allResults(runs: CheckRun[]): CheckResult[] { |
Frank Borden | 6988bdf | 2021-04-07 14:42:00 +0200 | [diff] [blame] | 242 | return runs.reduce( |
| 243 | (results: CheckResult[], run: CheckRun) => [ |
| 244 | ...results, |
| 245 | ...(run.results ?? []), |
| 246 | ], |
| 247 | [] |
| 248 | ); |
Ben Rohlfs | 8845349 | 2021-03-03 11:32:40 +0100 | [diff] [blame] | 249 | } |
| 250 | |
Ben Rohlfs | 913ab76 | 2021-02-05 12:52:00 +0100 | [diff] [blame] | 251 | export function hasResultsOf(run: CheckRun, category: Category) { |
| 252 | return getResultsOf(run, category).length > 0; |
| 253 | } |
| 254 | |
| 255 | export function getResultsOf(run: CheckRun, category: Category) { |
| 256 | return (run.results ?? []).filter(r => r.category === category); |
| 257 | } |
| 258 | |
Ben Rohlfs | c9f9098 | 2020-12-13 22:00:11 +0100 | [diff] [blame] | 259 | export function compareByWorstCategory(a: CheckRun, b: CheckRun) { |
| 260 | return level(worstCategory(b)) - level(worstCategory(a)); |
| 261 | } |
| 262 | |
| 263 | export function level(cat?: Category) { |
| 264 | if (!cat) return -1; |
| 265 | switch (cat) { |
Ben Rohlfs | 6cc7e7b | 2021-05-10 13:54:38 +0200 | [diff] [blame] | 266 | case Category.SUCCESS: |
Ben Rohlfs | c9f9098 | 2020-12-13 22:00:11 +0100 | [diff] [blame] | 267 | return 0; |
Ben Rohlfs | 6cc7e7b | 2021-05-10 13:54:38 +0200 | [diff] [blame] | 268 | case Category.INFO: |
Ben Rohlfs | c9f9098 | 2020-12-13 22:00:11 +0100 | [diff] [blame] | 269 | return 1; |
Ben Rohlfs | 6cc7e7b | 2021-05-10 13:54:38 +0200 | [diff] [blame] | 270 | case Category.WARNING: |
Ben Rohlfs | c9f9098 | 2020-12-13 22:00:11 +0100 | [diff] [blame] | 271 | return 2; |
Ben Rohlfs | 6cc7e7b | 2021-05-10 13:54:38 +0200 | [diff] [blame] | 272 | case Category.ERROR: |
| 273 | return 3; |
Ben Rohlfs | c9f9098 | 2020-12-13 22:00:11 +0100 | [diff] [blame] | 274 | } |
| 275 | } |
Ben Rohlfs | 05da424 | 2021-02-19 15:30:57 +0100 | [diff] [blame] | 276 | |
| 277 | export interface ActionTriggeredEventDetail { |
| 278 | action: Action; |
| 279 | run?: CheckRun; |
| 280 | } |
| 281 | |
| 282 | export type ActionTriggeredEvent = CustomEvent<ActionTriggeredEventDetail>; |
| 283 | |
| 284 | declare global { |
| 285 | interface HTMLElementEventMap { |
| 286 | 'action-triggered': ActionTriggeredEvent; |
| 287 | } |
| 288 | } |
| 289 | |
Ben Rohlfs | 85a866b | 2021-04-15 11:20:02 +0200 | [diff] [blame] | 290 | export interface AttemptDetail { |
| 291 | attempt: number | undefined; |
| 292 | icon: string; |
| 293 | } |
| 294 | |
| 295 | export interface AttemptInfo { |
| 296 | latestAttempt: number | undefined; |
| 297 | isSingleAttempt: boolean; |
| 298 | attempts: AttemptDetail[]; |
| 299 | } |
| 300 | |
| 301 | export function createAttemptMap(runs: CheckRunApi[]) { |
| 302 | const map = new Map<string, AttemptInfo>(); |
| 303 | for (const run of runs) { |
| 304 | const value = map.get(run.checkName); |
| 305 | const detail = { |
| 306 | attempt: run.attempt, |
| 307 | icon: iconForRun(fromApiToInternalRun(run)), |
| 308 | }; |
| 309 | if (value === undefined) { |
| 310 | map.set(run.checkName, { |
| 311 | latestAttempt: run.attempt, |
| 312 | isSingleAttempt: true, |
| 313 | attempts: [detail], |
| 314 | }); |
| 315 | continue; |
| 316 | } |
| 317 | if (!run.attempt || !value.latestAttempt) { |
| 318 | throw new Error( |
| 319 | 'If multiple run attempts are provided, ' + |
| 320 | 'then each run must have the "attempt" property set.' |
| 321 | ); |
| 322 | } |
| 323 | value.isSingleAttempt = false; |
| 324 | if (run.attempt > value.latestAttempt) { |
| 325 | value.latestAttempt = run.attempt; |
| 326 | } |
| 327 | value.attempts.push(detail); |
| 328 | } |
| 329 | return map; |
| 330 | } |
| 331 | |
| 332 | export function fromApiToInternalRun(run: CheckRunApi): CheckRun { |
| 333 | return { |
| 334 | ...run, |
Ben Rohlfs | c767e66 | 2021-08-05 15:23:32 +0200 | [diff] [blame] | 335 | pluginName: 'fake', |
Ben Rohlfs | 85a866b | 2021-04-15 11:20:02 +0200 | [diff] [blame] | 336 | internalRunId: 'fake', |
| 337 | isSingleAttempt: false, |
| 338 | isLatestAttempt: false, |
| 339 | attemptDetails: [], |
| 340 | results: (run.results ?? []).map(fromApiToInternalResult), |
| 341 | }; |
| 342 | } |
| 343 | |
| 344 | export function fromApiToInternalResult(result: CheckResultApi): CheckResult { |
| 345 | return { |
| 346 | ...result, |
| 347 | internalResultId: 'fake', |
| 348 | }; |
| 349 | } |
Ben Rohlfs | b04d856 | 2021-05-26 13:57:15 +0200 | [diff] [blame] | 350 | |
Ben Rohlfs | fb8e848 | 2021-06-02 14:39:11 +0200 | [diff] [blame] | 351 | function allPrimaryLinks(result?: CheckResultApi): Link[] { |
Ben Rohlfs | 3bc8128 | 2021-06-02 13:48:17 +0200 | [diff] [blame] | 352 | return (result?.links ?? []).filter(link => link.primary); |
Ben Rohlfs | b04d856 | 2021-05-26 13:57:15 +0200 | [diff] [blame] | 353 | } |
| 354 | |
Ben Rohlfs | 3bc8128 | 2021-06-02 13:48:17 +0200 | [diff] [blame] | 355 | export function firstPrimaryLink(result?: CheckResultApi): Link | undefined { |
| 356 | return allPrimaryLinks(result).find(link => link.icon === LinkIcon.EXTERNAL); |
| 357 | } |
| 358 | |
| 359 | export function otherPrimaryLinks(result?: CheckResultApi): Link[] { |
| 360 | const first = firstPrimaryLink(result); |
Ben Rohlfs | fb8e848 | 2021-06-02 14:39:11 +0200 | [diff] [blame] | 361 | return allPrimaryLinks(result).filter(link => link !== first); |
Ben Rohlfs | 3bc8128 | 2021-06-02 13:48:17 +0200 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | export function secondaryLinks(result?: CheckResultApi): Link[] { |
| 365 | return (result?.links ?? []).filter(link => !link.primary); |
Ben Rohlfs | b04d856 | 2021-05-26 13:57:15 +0200 | [diff] [blame] | 366 | } |