Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ui] Simplify times in task events #18595

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion ui/app/models/task-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ export default class TaskEvent extends Fragment {

@attr('date') time;
@attr('number') timeNanos;
@attr('string') displayMessage;

@attr('string') message;
get message() {
let message = simplifyTimeMessage(this.displayMessage);
return message;
}
}

function simplifyTimeMessage(message) {
return message.replace(/(\d+h)?(\d+m)?(\d+\.\d+)s/g, (_, h, m, s) => {
h = h ? parseInt(h) : 0;
m = m ? parseInt(m) : 0;
s = Math.round(parseFloat(s));

m += Math.floor(s / 60);
s %= 60;
h += Math.floor(m / 60);
m %= 60;

return `${h ? h + 'h' : ''}${h || m ? m + 'm' : ''}${s}s`;
});
}
65 changes: 65 additions & 0 deletions ui/tests/unit/models/task-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,69 @@ module('Unit | Model | task', function (hooks) {
);
});
});

// Test that message comes back with proper time formatting
test('displayMessage shows simplified time', function (assert) {
assert.expect(5);

const longTaskEvent = run(() =>
this.owner.lookup('service:store').createRecord('task-event', {
displayMessage: 'Task restarting in 1h2m3.456s',
})
);

assert.equal(
longTaskEvent.get('message'),
'Task restarting in 1h2m3s',
'hour-specific displayMessage is simplified'
);

const mediumTaskEvent = run(() =>
this.owner.lookup('service:store').createRecord('task-event', {
displayMessage: 'Task restarting in 1m2.345s',
})
);

assert.equal(
mediumTaskEvent.get('message'),
'Task restarting in 1m2s',
'minute-specific displayMessage is simplified'
);

const shortTaskEvent = run(() =>
this.owner.lookup('service:store').createRecord('task-event', {
displayMessage: 'Task restarting in 1.234s',
})
);

assert.equal(
shortTaskEvent.get('message'),
'Task restarting in 1s',
'second-specific displayMessage is simplified'
);

const roundedTaskEvent = run(() =>
this.owner.lookup('service:store').createRecord('task-event', {
displayMessage: 'I bet I can knock this out in about 1.999s',
})
);

assert.equal(
roundedTaskEvent.get('message'),
'I bet I can knock this out in about 2s',
'displayMessage is rounded'
);

const timelessTaskEvent = run(() =>
this.owner.lookup('service:store').createRecord('task-event', {
displayMessage: 'All 3000 tasks look great, no notes.',
})
);

assert.equal(
timelessTaskEvent.get('message'),
'All 3000 tasks look great, no notes.',
'displayMessage is unchanged'
);
});
});