1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-10 00:42:41 +01:00

Better format the Next and Updated columns in MetaMTA Mail Queue

Summary: They were only displaying seconds.  I found a function in viewutils.php that allowed for single-unit precision formatting, but I wanted more, so I wrote another function to allow more detail.

Test Plan: [site]/mail, and watch it work.  It's a new function, so it shouldn't break anything else.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Koolvin

Maniphest Tasks: T1296

Differential Revision: https://secure.phabricator.com/D2616
This commit is contained in:
Jonathan Lomas 2012-06-01 10:07:50 -07:00 committed by epriestley
parent fd20b18c2d
commit 103ff94a40
3 changed files with 134 additions and 2 deletions

View file

@ -69,12 +69,20 @@ final class PhabricatorMetaMTAListController
// Render the details table.
$rows = array();
foreach ($mails as $mail) {
$next_retry = $mail->getNextRetry() - time();
if ($next_retry <= 0) {
$next_retry = "None";
} else {
$next_retry = phabricator_format_relative_time_detailed($next_retry);
}
$rows[] = array(
PhabricatorMetaMTAMail::getReadableStatus($mail->getStatus()),
$mail->getRetryCount(),
($mail->getNextRetry() - time()).' s',
$next_retry,
phabricator_datetime($mail->getDateCreated(), $user),
(time() - $mail->getDateModified()).' s',
phabricator_format_relative_time_detailed(
time() - $mail->getDateModified()),
phutil_escape_html($mail->getSubject()),
phutil_render_tag(
'a',

View file

@ -63,4 +63,79 @@ final class PhabricatorUnitsTestCase extends PhabricatorTestCase {
}
}
public function testDetailedDurationFormatting() {
$expected_zero = 'now';
$tests = array (
12095939 => '19 w, 6 d',
-12095939 => '19 w, 6 d ago',
3380521 => '5 w, 4 d',
-3380521 => '5 w, 4 d ago',
0 => $expected_zero,
);
foreach ($tests as $duration => $expect) {
$this->assertEqual(
$expect,
phabricator_format_relative_time_detailed($duration),
'phabricator_format_relative_time_detailed('.$duration.')');
}
$tests = array(
3380521 => array(
-1 => '5 w',
0 => '5 w',
1 => '5 w',
2 => '5 w, 4 d',
3 => '5 w, 4 d, 3 h',
4 => '5 w, 4 d, 3 h, 2 m',
5 => '5 w, 4 d, 3 h, 2 m, 1 s',
6 => '5 w, 4 d, 3 h, 2 m, 1 s',
),
-3380521 => array(
-1 => '5 w ago',
0 => '5 w ago',
1 => '5 w ago',
2 => '5 w, 4 d ago',
3 => '5 w, 4 d, 3 h ago',
4 => '5 w, 4 d, 3 h, 2 m ago',
5 => '5 w, 4 d, 3 h, 2 m, 1 s ago',
6 => '5 w, 4 d, 3 h, 2 m, 1 s ago',
),
0 => array(
-1 => $expected_zero,
0 => $expected_zero,
1 => $expected_zero,
2 => $expected_zero,
3 => $expected_zero,
4 => $expected_zero,
5 => $expected_zero,
6 => $expected_zero,
),
);
foreach ($tests as $duration => $sub_tests) {
if (is_array($sub_tests)) {
foreach ($sub_tests as $levels => $expect) {
$this->assertEqual(
$expect,
phabricator_format_relative_time_detailed($duration, $levels),
'phabricator_format_relative_time_detailed('.$duration.',
'.$levels.')');
}
} else {
$expect = $sub_tests;
$this->assertEqual(
$expect,
phabricator_format_relative_time_detailed($duration),
'phabricator_format_relative_time_detailed('.$duration.')');
}
}
}
}

View file

@ -126,6 +126,55 @@ function phabricator_format_relative_time($duration) {
$precision = 0);
}
/**
* Format a relative time (duration) into weeks, days, hours, minutes,
* seconds, but unlike phabricator_format_relative_time, does so for more than
* just the largest unit.
*
* @param int Duration in seconds.
* @param int Levels to render - will render the three highest levels, ie:
* 5 h, 37 m, 1 s
* @return string Human-readable description.
*/
function phabricator_format_relative_time_detailed($duration, $levels = 2) {
if ($duration == 0) {
return 'now';
}
$levels = max(1, min($levels, 5));
$remainder = 0;
$is_negative = false;
if ($duration < 0) {
$is_negative = true;
$duration = abs($duration);
}
$this_level = 1;
$detailed_relative_time = phabricator_format_units_generic(
$duration,
array(60, 60, 24, 7),
array('s', 'm', 'h', 'd', 'w'),
$precision = 0,
$remainder);
$duration = $remainder;
while ($remainder > 0 && $this_level < $levels) {
$detailed_relative_time .= ', '.phabricator_format_units_generic(
$duration,
array(60, 60, 24, 7),
array('s', 'm', 'h', 'd', 'w'),
$precision = 0,
$remainder);
$duration = $remainder;
$this_level++;
};
if ($is_negative) {
$detailed_relative_time .= ' ago';
}
return $detailed_relative_time;
}
/**
* Format a byte count for human consumption, e.g. "10MB" instead of