You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.ts 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { STATUSES, CURRENTS, ALL_TYPES } from './constants';
  21. import { toShortNotSoISOString } from '../../helpers/dates';
  22. import { Task } from '../../app/types';
  23. export interface Query {
  24. currents: string;
  25. maxExecutedAt?: Date;
  26. minSubmittedAt?: Date;
  27. query: string;
  28. status: string;
  29. taskType: string;
  30. }
  31. export function updateTask(tasks: Task[], newTask: Task) {
  32. return tasks.map(task => (task.id === newTask.id ? newTask : task));
  33. }
  34. export function mapFiltersToParameters(filters: Partial<Query> = {}) {
  35. const parameters: any = {};
  36. if (filters.status === STATUSES.ALL) {
  37. parameters.status = [
  38. STATUSES.PENDING,
  39. STATUSES.IN_PROGRESS,
  40. STATUSES.SUCCESS,
  41. STATUSES.FAILED,
  42. STATUSES.CANCELED
  43. ].join();
  44. } else if (filters.status === STATUSES.ALL_EXCEPT_PENDING) {
  45. parameters.status = [
  46. STATUSES.IN_PROGRESS,
  47. STATUSES.SUCCESS,
  48. STATUSES.FAILED,
  49. STATUSES.CANCELED
  50. ].join();
  51. } else {
  52. parameters.status = filters.status;
  53. }
  54. if (filters.taskType !== ALL_TYPES) {
  55. parameters.type = filters.taskType;
  56. }
  57. if (filters.currents !== CURRENTS.ALL) {
  58. parameters.onlyCurrents = true;
  59. }
  60. if (filters.minSubmittedAt) {
  61. parameters.minSubmittedAt = toShortNotSoISOString(filters.minSubmittedAt);
  62. }
  63. if (filters.maxExecutedAt) {
  64. parameters.maxExecutedAt = toShortNotSoISOString(filters.maxExecutedAt);
  65. }
  66. if (filters.query) {
  67. parameters.componentQuery = filters.query;
  68. }
  69. return parameters;
  70. }
  71. const ONE_SECOND = 1000;
  72. const ONE_MINUTE = 60 * ONE_SECOND;
  73. const ONE_HOUR = 60 * ONE_MINUTE;
  74. function format(int: number, suffix: string) {
  75. return `${int}${suffix}`;
  76. }
  77. export function formatDuration(value: number | undefined) {
  78. if (!value) {
  79. return '';
  80. }
  81. if (value < ONE_SECOND) {
  82. return format(value, 'ms');
  83. } else if (value < ONE_SECOND * 10) {
  84. const seconds = Math.floor(value / ONE_SECOND);
  85. const ms = value - seconds * ONE_SECOND;
  86. return seconds + '.' + format(ms, 's');
  87. } else if (value < ONE_MINUTE) {
  88. const seconds = Math.floor(value / ONE_SECOND);
  89. return format(seconds, 's');
  90. } else if (value < ONE_MINUTE * 10) {
  91. const minutes = Math.floor(value / ONE_MINUTE);
  92. const seconds = Math.floor((value - minutes * ONE_MINUTE) / ONE_SECOND);
  93. return format(minutes, 'min') + ' ' + format(seconds, 's');
  94. }
  95. const hours = Math.floor(value / ONE_HOUR);
  96. const minutes = Math.floor((value - hours * ONE_HOUR) / ONE_MINUTE);
  97. return format(hours, 'h') + ' ' + format(minutes, 'min');
  98. }