aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/background-tasks/utils.ts
blob: 239cf09f131ab3e6e1734d04ceb66558ad1508ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * SonarQube
 * Copyright (C) 2009-2022 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import { toShortNotSoISOString } from '../../helpers/dates';
import { Task, TaskStatuses } from '../../types/tasks';
import { ALL_TYPES, CURRENTS, STATUSES } from './constants';

export interface Query {
  currents: string;
  maxExecutedAt?: Date;
  minSubmittedAt?: Date;
  query: string;
  status: string;
  taskType: string;
}

export function updateTask(tasks: Task[], newTask: Task) {
  return tasks.map(task => (task.id === newTask.id ? newTask : task));
}

export function mapFiltersToParameters(filters: Partial<Query> = {}) {
  const parameters: any = {};

  if (filters.status === STATUSES.ALL) {
    parameters.status = [
      TaskStatuses.Pending,
      TaskStatuses.InProgress,
      TaskStatuses.Success,
      TaskStatuses.Failed,
      TaskStatuses.Canceled
    ].join();
  } else if (filters.status === STATUSES.ALL_EXCEPT_PENDING) {
    parameters.status = [
      TaskStatuses.InProgress,
      TaskStatuses.Success,
      TaskStatuses.Failed,
      TaskStatuses.Canceled
    ].join();
  } else {
    parameters.status = filters.status;
  }

  if (filters.taskType !== ALL_TYPES) {
    parameters.type = filters.taskType;
  }

  if (filters.currents !== CURRENTS.ALL) {
    parameters.onlyCurrents = true;
  }

  if (filters.minSubmittedAt) {
    parameters.minSubmittedAt = toShortNotSoISOString(filters.minSubmittedAt);
  }

  if (filters.maxExecutedAt) {
    parameters.maxExecutedAt = toShortNotSoISOString(filters.maxExecutedAt);
  }

  if (filters.query) {
    parameters.q = filters.query;
  }

  return parameters;
}

const ONE_SECOND = 1000;
const ONE_MINUTE = 60 * ONE_SECOND;
const ONE_HOUR = 60 * ONE_MINUTE;

function format(int: number, suffix: string) {
  return `${int}${suffix}`;
}

export function formatDuration(value: number | undefined) {
  if (!value) {
    return '';
  }
  if (value < ONE_SECOND) {
    return format(value, 'ms');
  } else if (value < ONE_SECOND * 10) {
    const seconds = Math.floor(value / ONE_SECOND);
    const ms = value - seconds * ONE_SECOND;
    return seconds + '.' + format(ms, 's');
  } else if (value < ONE_MINUTE) {
    const seconds = Math.floor(value / ONE_SECOND);
    return format(seconds, 's');
  } else if (value < ONE_MINUTE * 10) {
    const minutes = Math.floor(value / ONE_MINUTE);
    const seconds = Math.floor((value - minutes * ONE_MINUTE) / ONE_SECOND);
    return format(minutes, 'min') + ' ' + format(seconds, 's');
  }
  const hours = Math.floor(value / ONE_HOUR);
  const minutes = Math.floor((value - hours * ONE_HOUR) / ONE_MINUTE);
  return format(hours, 'h') + ' ' + format(minutes, 'min');
}