diff --git a/libraries/Advisor.class.php b/libraries/Advisor.class.php index 04c93e38ab..088e84012d 100644 --- a/libraries/Advisor.class.php +++ b/libraries/Advisor.class.php @@ -456,4 +456,64 @@ class Advisor return array('rules' => $rules, 'lines' => $lines, 'errors' => $errors); } } + + +/** + * Formats interval like 10 per hour + * + * @param integer $num number to format + * @param integer $precision required precision + * + * @return string formatted string + */ +function ADVISOR_bytime($num, $precision) +{ + if ($num >= 1) { // per second + $per = __('per second'); + } elseif ($num * 60 >= 1) { // per minute + $num = $num * 60; + $per = __('per minute'); + } elseif ($num * 60 * 60 >= 1 ) { // per hour + $num = $num * 60 * 60; + $per = __('per hour'); + } else { + $num = $num * 60 * 60 * 24; + $per = __('per day'); + } + + $num = round($num, $precision); + + if ($num == 0) { + $num = '<' . PMA_Util::pow(10, -$precisio); + } + + return "$num $per"; +} + +/** + * Wrapper for PMA_Util::timespanFormat + * + * @param int $seconds the timespan + * + * @return string the formatted value + */ +function ADVISOR_timespanFormat($seconds) +{ + return PMA_Util::timespanFormat($seconds); +} + +/** + * Wrapper around PMA_Util::formatByteDown + * + * @param double $value the value to format + * @param int $limes the sensitiveness + * @param int $comma the number of decimals to retain + * + * @return string the formatted value with unit + */ +function ADVISOR_formatByteDown($value, $limes = 6, $comma = 0) +{ + return implode(' ', PMA_Util::formatByteDown($value, $limes, $comma)); +} + ?> diff --git a/test/classes/PMA_Advisor_test.php b/test/classes/PMA_Advisor_test.php index 5c1db512e3..dec751a2d5 100644 --- a/test/classes/PMA_Advisor_test.php +++ b/test/classes/PMA_Advisor_test.php @@ -79,6 +79,37 @@ class Advisor_Test extends PHPUnit_Framework_TestCase $this->assertEquals($parseResult['errors'], array()); } + /** + * test for ADVISOR_bytime + * + * @return void + */ + public function testAdvisorBytime() + { + $result = ADVISOR_bytime(10, 2); + $this->assertEquals("10 per second", $result); + + $result = ADVISOR_bytime(0.02, 2); + $this->assertEquals("1.2 per minute", $result); + + $result = ADVISOR_bytime(0.003, 2); + $this->assertEquals("10.8 per hour", $result); + } + + /** + * test for ADVISOR_timespanFormat + * + * @return void + */ + public function testAdvisorTimespanFormat() + { + $result = ADVISOR_timespanFormat(1200); + $this->assertEquals("0 days, 0 hours, 20 minutes and 0 seconds", $result); + + $result = ADVISOR_timespanFormat(100); + $this->assertEquals("0 days, 0 hours, 1 minutes and 40 seconds", $result); + } + /** * Test for adding rule *