Skip to content

Commit

Permalink
Use constant for task status
Browse files Browse the repository at this point in the history
While doing so, also make status and ProgressBar titles translatable.

Having the getTranslatableStatus() method for tasks PLUS the exported
getTranslatableTaskStatus function was "necessary", because having it as
a Task-method is nice, but it is also needed in the StatusBar, where the
status needs translation but the task is not available (just pure status
string).
  • Loading branch information
swaterkamp committed Jun 14, 2018
1 parent 16a9bc5 commit 1417edf
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 44 deletions.
48 changes: 37 additions & 11 deletions ng/src/gmp/models/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,23 @@ export const AUTO_DELETE_KEEP = 'keep';
export const AUTO_DELETE_NO = 'no';
export const AUTO_DELETE_DEFAULT_VALUE = 5;

/* eslint-disable quote-props */
export const TASK_STATUS = {
running: 'Running',
stoprequested: 'Stop Requested',
deleterequested: 'Delete Requested',
ultimatedeleterequested: 'Ultimate Delete Requested',
resumerequested: 'Resume Requested',
requested: 'Requested',
stopped: 'Stopped',
new: 'New',
interrupted: 'Interrupted',
container: 'Container',
uploading: 'Uploading',
done: 'Done',
};

/* eslint-disable quote-props */
const TASK_STATUS_TRANSLATIONS = {
'Running': _('Running'),
'Stop Requested': _('Stop Requested'),
'Delete Requested': _('Delete Requested'),
Expand All @@ -57,40 +72,47 @@ export const TASK_STATUS = {
'Stopped': _('Stopped'),
'New': _('New'),
'Interrupted': _('Interrupted'),
'Container': _('Container'),
'Uploading': _('Uploading'),
'Done': _('Done'),
};
/* eslint-disable quote-props */

function parse_yes(value) {
return value === 'yes' ? YES_VALUE : NO_VALUE;
}

export const getTranslatableTaskStatus = status => {
return TASK_STATUS_TRANSLATIONS[status];
};

class Task extends Model {

static entity_type = 'task';

isActive() {
return this.status === 'Running' ||
this.status === 'Stop Requested' ||
this.status === 'Delete Requested' ||
this.status === 'Ultimate Delete Requested' ||
this.status === 'Resume Requested' ||
this.status === 'Requested';
return this.status === TASK_STATUS.running ||
this.status === TASK_STATUS.stoprequested ||
this.status === TASK_STATUS.deleterequested ||
this.status === TASK_STATUS.ultimatedeleterequested ||
this.status === TASK_STATUS.resumerequested ||
this.status === TASK_STATUS.requested;
}

isRunning() {
return this.status === 'Running';
return this.status === TASK_STATUS.running;
}

isStopped() {
return this.status === 'Stopped';
return this.status === TASK_STATUS.stopped;
}

isInterrupted() {
return this.status === 'Interrupted';
return this.status === TASK_STATUS.interrupted;
}

isNew() {
return this.status === 'New';
return this.status === TASK_STATUS.new;
}

isChangeable() {
Expand All @@ -105,6 +127,10 @@ class Task extends Model {
return !is_defined(this.target);
}

getTranslatableStatus() {
return getTranslatableTaskStatus(this.status);
};

parseProperties(elem) {
elem = super.parseProperties(elem);

Expand Down
51 changes: 35 additions & 16 deletions ng/src/web/components/bar/statusbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*
* Authors:
* Björn Ricks <bjoern.ricks@greenbone.net>
* Steffen Waterkamp <steffen.waterkamp@greenbone.net>
*
* Copyright:
* Copyright (C) 2016 - 2018 Greenbone Networks GmbH
Expand All @@ -24,46 +25,64 @@ import 'core-js/fn/string/includes';

import React from 'react';

import _ from 'gmp/locale';

import {
getTranslatableTaskStatus,
TASK_STATUS,
} from 'gmp/models/task';

import PropTypes from '../../utils/proptypes.js';

import ProgressBar from './progressbar.js';

const StatusBar = ({status = 'Unknown', progress = '0'}) => {
const st = status.toLowerCase();
let text = status;

if (st === 'unknown' || st === 'new' || st === 'done' || st === 'container' ||
st.includes('requested')) {
let text = getTranslatableTaskStatus(status);
if (status === 'Unknown' ||
status === TASK_STATUS.new ||
status === TASK_STATUS.done ||
status === TASK_STATUS.container ||
status === TASK_STATUS.stoprequested ||
status === TASK_STATUS.deleterequested ||
status === TASK_STATUS.ultimatedeleterequested ||
status === TASK_STATUS.resumerequested ||
status === TASK_STATUS.requested) {
progress = '100';
}

if (st === 'stopped') {
text = status + ' at ' + progress + ' %';
if (status === TASK_STATUS.stopped || status === TASK_STATUS.interrupted) {
text = _('{{status}} at {{progress}} %', {status, progress});
}
else if (st === 'running') {
text = progress + ' %';
else if (status === TASK_STATUS.running) {
text = _('{{progress}} %', {progress});
}

let background;
if (st === 'stopped' || st.includes('requested') || st === 'interrupted') {
if (status === TASK_STATUS.stopped ||
status === TASK_STATUS.stoprequested ||
status === TASK_STATUS.deleterequested ||
status === TASK_STATUS.ultimatedeleterequested ||
status === TASK_STATUS.resumerequested ||
status === TASK_STATUS.requested) {
background = 'warn';
}
else if (st.includes('error')) {
else if (status === TASK_STATUS.interrupted) {
background = 'error';
}
else if (st === 'uploading' || st === 'container' ||
st === 'done') {
else if (status === TASK_STATUS.uploading ||
status === TASK_STATUS.container ||
status === TASK_STATUS.done) {
background = 'low';
}
else if (st === 'new') {
else if (status === TASK_STATUS.new) {
background = 'new';
}
else if (st === 'running') {
else if (status === TASK_STATUS.running) {
background = 'run';
}
return (
<ProgressBar
title={status}
title={getTranslatableTaskStatus(status)}
progress={progress}
background={background}
>
Expand Down
17 changes: 11 additions & 6 deletions ng/src/web/pages/reports/row.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
*
* Authors:
* Björn Ricks <bjoern.ricks@greenbone.net>
* Steffen Waterkamp <steffen.waterkamp@greenbone.net>
*
* Copyright:
* Copyright (C) 2017 Greenbone Networks GmbH
* Copyright (C) 2017 - 2018 Greenbone Networks GmbH
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand All @@ -28,6 +29,8 @@ import {longDate} from 'gmp/locale/date';

import {is_defined} from 'gmp/utils/identity';

import {TASK_STATUS} from 'gmp/models/task';

import PropTypes from '../../utils/proptypes.js';
import {render_component} from '../../utils/render.js';

Expand All @@ -54,10 +57,10 @@ const IconActions = ({
onReportDeltaSelect,
}) => {
const {report} = entity;
const active = report.scan_run_status !== 'Running' &&
report.scan_run_status !== 'Requested' &&
report.scan_run_status !== 'Stop Requested' &&
report.scan_run_status !== 'Resume Requested';
const active = report.scan_run_status !== TASK_STATUS.running &&
report.scan_run_status !== TASK_STATUS.requested &&
report.scan_run_status !== TASK_STATUS.stoprequested &&
report.scan_run_status !== TASK_STATUS.resumerequested;

const title = active ? _('Delete Report') : _('Scan is active');

Expand Down Expand Up @@ -111,7 +114,9 @@ const Row = ({entity, links = true, actions, ...other}) => {

if (is_defined(task)) {
if (task.isContainer()) {
status = status === 'Running' ? 'Uploading' : 'Container';
status = status === TASK_STATUS.running ?
TASK_STATUS.uploading :
TASK_STATUS.container;
}
progress = task.progress;
}
Expand Down
19 changes: 10 additions & 9 deletions ng/src/web/pages/tasks/dashboard/statusdisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {interpolateHcl} from 'd3-interpolate';
import _ from 'gmp/locale';

import Filter, {TASKS_FILTER_FILTER} from 'gmp/models/filter';
import {TASK_STATUS} from 'gmp/models/task';

import {is_defined} from 'gmp/utils/identity';

Expand Down Expand Up @@ -58,15 +59,15 @@ const orange = interpolateHcl('#ff7f0e', '#ffbb78');

const taskStatusColorScale = scaleOrdinal()
.domain([
'Delete Requested',
'Ultimate Delete Requested',
'Interrupted',
'New',
'Requested',
'Running',
'Stop Requested',
'Stopped',
'Done',
TASK_STATUS.deleterequested,
TASK_STATUS.ultimatedeleterequested,
TASK_STATUS.interrupted,
TASK_STATUS.new,
TASK_STATUS.requested,
TASK_STATUS.running,
TASK_STATUS.stoprequested,
TASK_STATUS.stopped,
TASK_STATUS.done,
'N/A',
])
.range([
Expand Down
7 changes: 5 additions & 2 deletions ng/src/web/pages/tasks/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
*
* Authors:
* Björn Ricks <bjoern.ricks@greenbone.net>
* Steffen Waterkamp <steffen.waterkamp@greenbone.net>
*
* Copyright:
* Copyright (C) 2017 Greenbone Networks GmbH
* Copyright (C) 2017 - 2018 Greenbone Networks GmbH
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand All @@ -23,6 +24,8 @@

import React from 'react';

import {TASK_STATUS} from 'gmp/models/task';

import {is_defined} from 'gmp/utils';

import PropTypes from '../../utils/proptypes.js';
Expand Down Expand Up @@ -51,7 +54,7 @@ const TaskStatus = ({task, links = true}) => {
textOnly={!links}
>
<StatusBar
status={task.isContainer() ? 'Container' : task.status}
status={task.isContainer() ? TASK_STATUS.container : task.status}
progress={task.progress}/>
</DetailsLink>
);
Expand Down

0 comments on commit 1417edf

Please sign in to comment.