From 0a7370b9e5652918ad9386baa81cc12627069755 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 21 Nov 2015 18:41:15 -0500 Subject: Remove workaround for 'INSERT ON DUPLICATE KEY UPDATE...' --- app/Model/ProjectDailyStats.php | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'app/Model/ProjectDailyStats.php') diff --git a/app/Model/ProjectDailyStats.php b/app/Model/ProjectDailyStats.php index 46ca0a4b..7ec1ee2c 100644 --- a/app/Model/ProjectDailyStats.php +++ b/app/Model/ProjectDailyStats.php @@ -29,27 +29,35 @@ class ProjectDailyStats extends Base */ public function updateTotals($project_id, $date) { - $lead_cycle_time = $this->projectAnalytic->getAverageLeadAndCycleTime($project_id); + $this->db->startTransaction(); - return $this->db->transaction(function (Database $db) use ($project_id, $date, $lead_cycle_time) { + $lead_cycle_time = $this->projectAnalytic->getAverageLeadAndCycleTime($project_id); - // This call will fail if the record already exists - // (cross database driver hack for INSERT..ON DUPLICATE KEY UPDATE) - $db->table(ProjectDailyStats::TABLE)->insert(array( - 'day' => $date, - 'project_id' => $project_id, - 'avg_lead_time' => 0, - 'avg_cycle_time' => 0, - )); + $exists = $this->db->table(ProjectDailyStats::TABLE) + ->eq('day', $date) + ->eq('project_id', $project_id) + ->exists(); - $db->table(ProjectDailyStats::TABLE) + if ($exists) { + $this->db->table(ProjectDailyStats::TABLE) ->eq('project_id', $project_id) ->eq('day', $date) ->update(array( 'avg_lead_time' => $lead_cycle_time['avg_lead_time'], 'avg_cycle_time' => $lead_cycle_time['avg_cycle_time'], )); - }); + } else { + $this->db->table(ProjectDailyStats::TABLE)->insert(array( + 'day' => $date, + 'project_id' => $project_id, + 'avg_lead_time' => $lead_cycle_time['avg_lead_time'], + 'avg_cycle_time' => $lead_cycle_time['avg_cycle_time'], + )); + } + + $this->db->closeTransaction(); + + return true; } /** -- cgit v1.2.3 From 73ce90ac568909f04d28f41492f99b66ba3fb037 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 5 Dec 2015 21:34:26 -0500 Subject: Remove unused use statements --- app/Api/User.php | 2 +- app/Controller/Base.php | 1 - app/Controller/Customfilter.php | 2 ++ app/Core/Security/AuthenticationManager.php | 1 - app/Core/Session/SessionManager.php | 1 - app/Helper/Url.php | 1 - app/Helper/User.php | 2 -- app/Model/ProjectDailyColumnStats.php | 2 -- app/Model/ProjectDailyStats.php | 2 -- app/User/DatabaseUserProvider.php | 1 - app/User/OAuthUserProvider.php | 1 - 11 files changed, 3 insertions(+), 13 deletions(-) (limited to 'app/Model/ProjectDailyStats.php') diff --git a/app/Api/User.php b/app/Api/User.php index 078c82f1..06e305f2 100644 --- a/app/Api/User.php +++ b/app/Api/User.php @@ -2,7 +2,7 @@ namespace Kanboard\Api; -use Kanboard\Auth\Ldap; +use LogicException; use Kanboard\Core\Security\Role; use Kanboard\Core\Ldap\Client as LdapClient; use Kanboard\Core\Ldap\ClientException as LdapException; diff --git a/app/Controller/Base.php b/app/Controller/Base.php index 76948a0f..35ceee09 100644 --- a/app/Controller/Base.php +++ b/app/Controller/Base.php @@ -2,7 +2,6 @@ namespace Kanboard\Controller; -use Pimple\Container; use Kanboard\Core\Security\Role; /** diff --git a/app/Controller/Customfilter.php b/app/Controller/Customfilter.php index ef75a837..12cc8e78 100644 --- a/app/Controller/Customfilter.php +++ b/app/Controller/Customfilter.php @@ -2,6 +2,8 @@ namespace Kanboard\Controller; +use Kanboard\Core\Security\Role; + /** * Custom Filter management * diff --git a/app/Core/Security/AuthenticationManager.php b/app/Core/Security/AuthenticationManager.php index cced58c0..dbc41b09 100644 --- a/app/Core/Security/AuthenticationManager.php +++ b/app/Core/Security/AuthenticationManager.php @@ -4,7 +4,6 @@ namespace Kanboard\Core\Security; use LogicException; use Kanboard\Core\Base; -use Kanboard\Core\User\UserProviderInterface; use Kanboard\Event\AuthFailureEvent; use Kanboard\Event\AuthSuccessEvent; diff --git a/app/Core/Session/SessionManager.php b/app/Core/Session/SessionManager.php index 750711b0..776d02da 100644 --- a/app/Core/Session/SessionManager.php +++ b/app/Core/Session/SessionManager.php @@ -3,7 +3,6 @@ namespace Kanboard\Core\Session; use Kanboard\Core\Base; -use Kanboard\Core\Http\Request; /** * Session Manager diff --git a/app/Helper/Url.php b/app/Helper/Url.php index 3658ef5f..6ada8068 100644 --- a/app/Helper/Url.php +++ b/app/Helper/Url.php @@ -2,7 +2,6 @@ namespace Kanboard\Helper; -use Kanboard\Core\Http\Request; use Kanboard\Core\Base; /** diff --git a/app/Helper/User.php b/app/Helper/User.php index b242dbb4..101d8dbe 100644 --- a/app/Helper/User.php +++ b/app/Helper/User.php @@ -2,8 +2,6 @@ namespace Kanboard\Helper; -use Kanboard\Core\Security\Role; - /** * User helpers * diff --git a/app/Model/ProjectDailyColumnStats.php b/app/Model/ProjectDailyColumnStats.php index 4b75fff2..7c89283d 100644 --- a/app/Model/ProjectDailyColumnStats.php +++ b/app/Model/ProjectDailyColumnStats.php @@ -2,8 +2,6 @@ namespace Kanboard\Model; -use PicoDb\Database; - /** * Project Daily Column Stats * diff --git a/app/Model/ProjectDailyStats.php b/app/Model/ProjectDailyStats.php index 7ec1ee2c..e79af372 100644 --- a/app/Model/ProjectDailyStats.php +++ b/app/Model/ProjectDailyStats.php @@ -2,8 +2,6 @@ namespace Kanboard\Model; -use PicoDb\Database; - /** * Project Daily Stats * diff --git a/app/User/DatabaseUserProvider.php b/app/User/DatabaseUserProvider.php index b6d41186..9a0047b2 100644 --- a/app/User/DatabaseUserProvider.php +++ b/app/User/DatabaseUserProvider.php @@ -3,7 +3,6 @@ namespace Kanboard\User; use Kanboard\Core\User\UserProviderInterface; -use Kanboard\Core\Security\Role; /** * Database User Provider diff --git a/app/User/OAuthUserProvider.php b/app/User/OAuthUserProvider.php index 3879fa76..527fefde 100644 --- a/app/User/OAuthUserProvider.php +++ b/app/User/OAuthUserProvider.php @@ -3,7 +3,6 @@ namespace Kanboard\User; use Kanboard\Core\User\UserProviderInterface; -use Kanboard\Core\Security\Role; /** * OAuth User Provider -- cgit v1.2.3 From 73ff5ec89b711791b3993f9158dc9554a623602c Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 16 Jan 2016 17:01:56 -0500 Subject: Remove ProjectAnalytic class --- app/Analytic/AverageLeadCycleTimeAnalytic.php | 112 +++++++++++++++ app/Analytic/AverageTimeSpentColumnAnalytic.php | 151 +++++++++++++++++++++ app/Controller/Analytic.php | 6 +- app/Core/Base.php | 2 + app/Locale/fr_FR/translations.php | 4 +- app/Model/ProjectAnalytic.php | 106 --------------- app/Model/ProjectDailyStats.php | 2 +- app/ServiceProvider/ClassProvider.php | 3 +- .../Analytic/AverageLeadCycleTimeAnalyticTest.php | 73 ++++++++++ .../AverageTimeSpentColumnAnalyticTest.php | 116 ++++++++++++++++ 10 files changed, 461 insertions(+), 114 deletions(-) create mode 100644 app/Analytic/AverageLeadCycleTimeAnalytic.php create mode 100644 app/Analytic/AverageTimeSpentColumnAnalytic.php delete mode 100644 app/Model/ProjectAnalytic.php create mode 100644 tests/units/Analytic/AverageLeadCycleTimeAnalyticTest.php create mode 100644 tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php (limited to 'app/Model/ProjectDailyStats.php') diff --git a/app/Analytic/AverageLeadCycleTimeAnalytic.php b/app/Analytic/AverageLeadCycleTimeAnalytic.php new file mode 100644 index 00000000..96c803cc --- /dev/null +++ b/app/Analytic/AverageLeadCycleTimeAnalytic.php @@ -0,0 +1,112 @@ + 0, + 'total_lead_time' => 0, + 'total_cycle_time' => 0, + 'avg_lead_time' => 0, + 'avg_cycle_time' => 0, + ); + + foreach ($this->getTasks($project_id) as &$task) { + $stats['count']++; + $stats['total_lead_time'] += $this->calculateLeadTime($task); + $stats['total_cycle_time'] += $this->calculateCycleTime($task); + } + + $stats['avg_lead_time'] = $this->calculateAverage($stats, 'total_lead_time'); + $stats['avg_cycle_time'] = $this->calculateAverage($stats, 'total_cycle_time'); + + return $stats; + } + + /** + * Calculate average + * + * @access private + * @param array &$stats + * @param string $field + * @return float + */ + private function calculateAverage(array &$stats, $field) + { + if ($stats['count'] > 0) { + return (int) ($stats[$field] / $stats['count']); + } + + return 0; + } + + /** + * Calculate lead time + * + * @access private + * @param array &$task + * @return integer + */ + private function calculateLeadTime(array &$task) + { + $end = $task['date_completed'] ?: time(); + $start = $task['date_creation']; + + return $end - $start; + } + + /** + * Calculate cycle time + * + * @access private + * @param array &$task + * @return integer + */ + private function calculateCycleTime(array &$task) + { + if (empty($task['date_started'])) { + return 0; + } + + $end = $task['date_completed'] ?: time(); + $start = $task['date_started']; + + return $end - $start; + } + + /** + * Get the 1000 last created tasks + * + * @access private + * @return array + */ + private function getTasks($project_id) + { + return $this->db + ->table(Task::TABLE) + ->columns('date_completed', 'date_creation', 'date_started') + ->eq('project_id', $project_id) + ->desc('id') + ->limit(1000) + ->findAll(); + } +} diff --git a/app/Analytic/AverageTimeSpentColumnAnalytic.php b/app/Analytic/AverageTimeSpentColumnAnalytic.php new file mode 100644 index 00000000..820db801 --- /dev/null +++ b/app/Analytic/AverageTimeSpentColumnAnalytic.php @@ -0,0 +1,151 @@ +initialize($project_id); + + $this->processTasks($stats, $project_id); + $this->calculateAverage($stats); + + return $stats; + } + + /** + * Initialize default values for each column + * + * @access private + * @param integer $project_id + * @return array + */ + private function initialize($project_id) + { + $stats = array(); + $columns = $this->board->getColumnsList($project_id); + + foreach ($columns as $column_id => $column_title) { + $stats[$column_id] = array( + 'count' => 0, + 'time_spent' => 0, + 'average' => 0, + 'title' => $column_title, + ); + } + + return $stats; + } + + /** + * Calculate time spent for each tasks for each columns + * + * @access private + * @param array $stats + * @param integer $project_id + */ + private function processTasks(array &$stats, $project_id) + { + foreach ($this->getTasks($project_id) as &$task) { + foreach ($this->getTaskTimeByColumns($task) as $column_id => $time_spent) { + if (isset($stats[$column_id])) { + $stats[$column_id]['count']++; + $stats[$column_id]['time_spent'] += $time_spent; + } + } + } + } + + /** + * Calculate averages + * + * @access private + * @param array $stats + */ + private function calculateAverage(array &$stats) + { + foreach ($stats as &$column) { + $this->calculateColumnAverage($column); + } + } + + /** + * Calculate column average + * + * @access private + * @param array $column + */ + private function calculateColumnAverage(array &$column) + { + if ($column['count'] > 0) { + $column['average'] = (int) ($column['time_spent'] / $column['count']); + } + } + + /** + * Get time spent for each column for a given task + * + * @access private + * @param array $task + * @return array + */ + private function getTaskTimeByColumns(array &$task) + { + $columns = $this->transition->getTimeSpentByTask($task['id']); + + if (! isset($columns[$task['column_id']])) { + $columns[$task['column_id']] = 0; + } + + $columns[$task['column_id']] += $this->getTaskTimeSpentInCurrentColumn($task); + + return $columns; + } + + /** + * Calculate time spent of a task in the current column + * + * @access private + * @param array $task + */ + private function getTaskTimeSpentInCurrentColumn(array &$task) + { + $end = $task['date_completed'] ?: time(); + return $end - $task['date_moved']; + } + + /** + * Fetch the last 1000 tasks + * + * @access private + * @param integer $project_id + * @return array + */ + private function getTasks($project_id) + { + return $this->db + ->table(Task::TABLE) + ->columns('id', 'date_completed', 'date_moved', 'column_id') + ->eq('project_id', $project_id) + ->desc('id') + ->limit(1000) + ->findAll(); + } +} diff --git a/app/Controller/Analytic.php b/app/Controller/Analytic.php index f9ea511b..26386029 100644 --- a/app/Controller/Analytic.php +++ b/app/Controller/Analytic.php @@ -37,8 +37,6 @@ class Analytic extends Base $project = $this->getProject(); $values = $this->request->getValues(); - $this->projectDailyStats->updateTotals($project['id'], date('Y-m-d')); - $from = $this->request->getStringParam('from', date('Y-m-d', strtotime('-1week'))); $to = $this->request->getStringParam('to', date('Y-m-d')); @@ -53,7 +51,7 @@ class Analytic extends Base 'to' => $to, ), 'project' => $project, - 'average' => $this->projectAnalytic->getAverageLeadAndCycleTime($project['id']), + 'average' => $this->averageLeadCycleTimeAnalytic->build($project['id']), 'metrics' => $this->projectDailyStats->getRawMetrics($project['id'], $from, $to), 'date_format' => $this->config->get('application_date_format'), 'date_formats' => $this->dateParser->getAvailableFormats(), @@ -72,7 +70,7 @@ class Analytic extends Base $this->response->html($this->layout('analytic/avg_time_columns', array( 'project' => $project, - 'metrics' => $this->projectAnalytic->getAverageTimeSpentByColumn($project['id']), + 'metrics' => $this->averageTimeSpentColumnAnalytic->build($project['id']), 'title' => t('Average time spent into each column for "%s"', $project['name']), ))); } diff --git a/app/Core/Base.php b/app/Core/Base.php index ea8c0abf..2821e5ae 100644 --- a/app/Core/Base.php +++ b/app/Core/Base.php @@ -13,6 +13,8 @@ use Pimple\Container; * @property \Kanboard\Analytic\TaskDistributionAnalytic $taskDistributionAnalytic * @property \Kanboard\Analytic\UserDistributionAnalytic $userDistributionAnalytic * @property \Kanboard\Analytic\EstimatedTimeComparisonAnalytic $estimatedTimeComparisonAnalytic + * @property \Kanboard\Analytic\AverageLeadCycleTimeAnalytic $averageLeadCycleTimeAnalytic + * @property \Kanboard\Analytic\AverageTimeSpentColumnAnalytic $averageTimeSpentColumnAnalytic * @property \Kanboard\Core\Action\ActionManager $actionManager * @property \Kanboard\Core\Cache\MemoryCache $memoryCache * @property \Kanboard\Core\Event\EventManager $eventManager diff --git a/app/Locale/fr_FR/translations.php b/app/Locale/fr_FR/translations.php index 016c16d7..2724c300 100644 --- a/app/Locale/fr_FR/translations.php +++ b/app/Locale/fr_FR/translations.php @@ -1074,8 +1074,8 @@ return array( 'You were mentioned in a comment on the task #%d' => 'Vous avez été mentionné dans un commentaire de la tâche n°%d', 'Mentioned' => 'Mentionné', 'Compare Estimated Time vs Actual Time' => 'Comparer le temps estimé et le temps actuel', - 'Estimated hours: ' => 'Heures estimées :', - 'Actual hours: ' => 'Heures actuelles :', + 'Estimated hours: ' => 'Heures estimées : ', + 'Actual hours: ' => 'Heures actuelles : ', 'Hours Spent' => 'Heures passées', 'Hours Estimated' => 'Heures estimées', 'Estimated Time' => 'Temps estimé', diff --git a/app/Model/ProjectAnalytic.php b/app/Model/ProjectAnalytic.php deleted file mode 100644 index 5753a5ae..00000000 --- a/app/Model/ProjectAnalytic.php +++ /dev/null @@ -1,106 +0,0 @@ - 0, - 'total_lead_time' => 0, - 'total_cycle_time' => 0, - 'avg_lead_time' => 0, - 'avg_cycle_time' => 0, - ); - - $tasks = $this->db - ->table(Task::TABLE) - ->columns('date_completed', 'date_creation', 'date_started') - ->eq('project_id', $project_id) - ->desc('id') - ->limit(1000) - ->findAll(); - - foreach ($tasks as &$task) { - $stats['count']++; - $stats['total_lead_time'] += ($task['date_completed'] ?: time()) - $task['date_creation']; - $stats['total_cycle_time'] += empty($task['date_started']) ? 0 : ($task['date_completed'] ?: time()) - $task['date_started']; - } - - $stats['avg_lead_time'] = $stats['count'] > 0 ? (int) ($stats['total_lead_time'] / $stats['count']) : 0; - $stats['avg_cycle_time'] = $stats['count'] > 0 ? (int) ($stats['total_cycle_time'] / $stats['count']) : 0; - - return $stats; - } - - /** - * Get the average time spent into each column - * - * @access public - * @param integer $project_id - * @return array - */ - public function getAverageTimeSpentByColumn($project_id) - { - $stats = array(); - $columns = $this->board->getColumnsList($project_id); - - // Get the time spent of the last move for each tasks - $tasks = $this->db - ->table(Task::TABLE) - ->columns('id', 'date_completed', 'date_moved', 'column_id') - ->eq('project_id', $project_id) - ->desc('id') - ->limit(1000) - ->findAll(); - - // Init values - foreach ($columns as $column_id => $column_title) { - $stats[$column_id] = array( - 'count' => 0, - 'time_spent' => 0, - 'average' => 0, - 'title' => $column_title, - ); - } - - // Get time spent foreach task/column and take into account the last move - foreach ($tasks as &$task) { - $sums = $this->transition->getTimeSpentByTask($task['id']); - - if (! isset($sums[$task['column_id']])) { - $sums[$task['column_id']] = 0; - } - - $sums[$task['column_id']] += ($task['date_completed'] ?: time()) - $task['date_moved']; - - foreach ($sums as $column_id => $time_spent) { - if (isset($stats[$column_id])) { - $stats[$column_id]['count']++; - $stats[$column_id]['time_spent'] += $time_spent; - } - } - } - - // Calculate average for each column - foreach ($columns as $column_id => $column_title) { - $stats[$column_id]['average'] = $stats[$column_id]['count'] > 0 ? (int) ($stats[$column_id]['time_spent'] / $stats[$column_id]['count']) : 0; - } - - return $stats; - } -} diff --git a/app/Model/ProjectDailyStats.php b/app/Model/ProjectDailyStats.php index e79af372..12fecbe6 100644 --- a/app/Model/ProjectDailyStats.php +++ b/app/Model/ProjectDailyStats.php @@ -29,7 +29,7 @@ class ProjectDailyStats extends Base { $this->db->startTransaction(); - $lead_cycle_time = $this->projectAnalytic->getAverageLeadAndCycleTime($project_id); + $lead_cycle_time = $this->averageLeadCycleTimeAnalytic->build($project_id); $exists = $this->db->table(ProjectDailyStats::TABLE) ->eq('day', $date) diff --git a/app/ServiceProvider/ClassProvider.php b/app/ServiceProvider/ClassProvider.php index ed7f4c08..31cbfa8d 100644 --- a/app/ServiceProvider/ClassProvider.php +++ b/app/ServiceProvider/ClassProvider.php @@ -18,6 +18,8 @@ class ClassProvider implements ServiceProviderInterface 'TaskDistributionAnalytic', 'UserDistributionAnalytic', 'EstimatedTimeComparisonAnalytic', + 'AverageLeadCycleTimeAnalytic', + 'AverageTimeSpentColumnAnalytic', ), 'Model' => array( 'Action', @@ -39,7 +41,6 @@ class ClassProvider implements ServiceProviderInterface 'PasswordReset', 'Project', 'ProjectActivity', - 'ProjectAnalytic', 'ProjectDuplication', 'ProjectDailyColumnStats', 'ProjectDailyStats', diff --git a/tests/units/Analytic/AverageLeadCycleTimeAnalyticTest.php b/tests/units/Analytic/AverageLeadCycleTimeAnalyticTest.php new file mode 100644 index 00000000..9c445dca --- /dev/null +++ b/tests/units/Analytic/AverageLeadCycleTimeAnalyticTest.php @@ -0,0 +1,73 @@ +container); + $projectModel = new Project($this->container); + $averageLeadCycleTimeAnalytic = new AverageLeadCycleTimeAnalytic($this->container); + $now = time(); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test1'))); + + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(3, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(4, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + // LT=3600 CT=1800 + $this->container['db']->table(Task::TABLE)->eq('id', 1)->update(array('date_completed' => $now + 3600, 'date_started' => $now + 1800)); + + // LT=1800 CT=900 + $this->container['db']->table(Task::TABLE)->eq('id', 2)->update(array('date_completed' => $now + 1800, 'date_started' => $now + 900)); + + // LT=3600 CT=0 + $this->container['db']->table(Task::TABLE)->eq('id', 3)->update(array('date_completed' => $now + 3600)); + + // LT=2*3600 CT=0 + $this->container['db']->table(Task::TABLE)->eq('id', 4)->update(array('date_completed' => $now + 2 * 3600)); + + $stats = $averageLeadCycleTimeAnalytic->build(1); + $expected = array( + 'count' => 4, + 'total_lead_time' => 3600 + 1800 + 3600 + 2*3600, + 'total_cycle_time' => 1800 + 900, + 'avg_lead_time' => (3600 + 1800 + 3600 + 2*3600) / 4, + 'avg_cycle_time' => (1800 + 900) / 4, + ); + + $this->assertEquals($expected, $stats); + } + + public function testBuildWithNoTasks() + { + $taskCreationModel = new TaskCreation($this->container); + $projectModel = new Project($this->container); + $averageLeadCycleTimeAnalytic = new AverageLeadCycleTimeAnalytic($this->container); + $now = time(); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test1'))); + + $stats = $averageLeadCycleTimeAnalytic->build(1); + + $expected = array( + 'count' => 0, + 'total_lead_time' => 0, + 'total_cycle_time' => 0, + 'avg_lead_time' => 0, + 'avg_cycle_time' => 0, + ); + + $this->assertEquals($expected, $stats); + } +} diff --git a/tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php b/tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php new file mode 100644 index 00000000..75cb181d --- /dev/null +++ b/tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php @@ -0,0 +1,116 @@ +container); + $projectModel = new Project($this->container); + $averageLeadCycleTimeAnalytic = new AverageTimeSpentColumnAnalytic($this->container); + $now = time(); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $this->container['db']->table(Task::TABLE)->eq('id', 1)->update(array('date_completed' => $now + 3600)); + $this->container['db']->table(Task::TABLE)->eq('id', 2)->update(array('date_completed' => $now + 1800)); + + $stats = $averageLeadCycleTimeAnalytic->build(1); + $expected = array( + 1 => array( + 'count' => 2, + 'time_spent' => 3600+1800, + 'average' => (int) ((3600+1800)/2), + 'title' => 'Backlog', + ), + 2 => array( + 'count' => 0, + 'time_spent' => 0, + 'average' => 0, + 'title' => 'Ready', + ), + 3 => array( + 'count' => 0, + 'time_spent' => 0, + 'average' => 0, + 'title' => 'Work in progress', + ), + 4 => array( + 'count' => 0, + 'time_spent' => 0, + 'average' => 0, + 'title' => 'Done', + ) + ); + + $this->assertEquals($expected, $stats); + } + + public function testAverageWithTransitions() + { + $transitionModel = new Transition($this->container); + $taskFinderModel = new TaskFinder($this->container); + $taskCreationModel = new TaskCreation($this->container); + $projectModel = new Project($this->container); + $averageLeadCycleTimeAnalytic = new AverageTimeSpentColumnAnalytic($this->container); + $now = time(); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $this->container['db']->table(Task::TABLE)->eq('id', 1)->update(array('date_completed' => $now + 3600)); + $this->container['db']->table(Task::TABLE)->eq('id', 2)->update(array('date_completed' => $now + 1800)); + + foreach (array(1, 2) as $task_id) { + $task = $taskFinderModel->getById($task_id); + $task['task_id'] = $task['id']; + $task['date_moved'] = $now - 900; + $task['src_column_id'] = 3; + $task['dst_column_id'] = 1; + $this->assertTrue($transitionModel->save(1, $task)); + } + + $stats = $averageLeadCycleTimeAnalytic->build(1); + $expected = array( + 1 => array( + 'count' => 2, + 'time_spent' => 3600+1800, + 'average' => (int) ((3600+1800)/2), + 'title' => 'Backlog', + ), + 2 => array( + 'count' => 0, + 'time_spent' => 0, + 'average' => 0, + 'title' => 'Ready', + ), + 3 => array( + 'count' => 2, + 'time_spent' => 1800, + 'average' => 900, + 'title' => 'Work in progress', + ), + 4 => array( + 'count' => 0, + 'time_spent' => 0, + 'average' => 0, + 'title' => 'Done', + ) + ); + + $this->assertEquals($expected, $stats); + } +} -- cgit v1.2.3 From c603cb5949eba24fc09012e8f2684f33078ed713 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 16 Jan 2016 19:46:23 -0500 Subject: Simplify class ProjectDailyStats --- app/Model/ProjectDailyStats.php | 39 ++++++++++------------------- tests/units/Model/ProjectDailyStatsTest.php | 5 +++- 2 files changed, 17 insertions(+), 27 deletions(-) (limited to 'app/Model/ProjectDailyStats.php') diff --git a/app/Model/ProjectDailyStats.php b/app/Model/ProjectDailyStats.php index 12fecbe6..957ad51d 100644 --- a/app/Model/ProjectDailyStats.php +++ b/app/Model/ProjectDailyStats.php @@ -31,27 +31,14 @@ class ProjectDailyStats extends Base $lead_cycle_time = $this->averageLeadCycleTimeAnalytic->build($project_id); - $exists = $this->db->table(ProjectDailyStats::TABLE) - ->eq('day', $date) - ->eq('project_id', $project_id) - ->exists(); + $this->db->table(self::TABLE)->eq('day', $date)->eq('project_id', $project_id)->remove(); - if ($exists) { - $this->db->table(ProjectDailyStats::TABLE) - ->eq('project_id', $project_id) - ->eq('day', $date) - ->update(array( - 'avg_lead_time' => $lead_cycle_time['avg_lead_time'], - 'avg_cycle_time' => $lead_cycle_time['avg_cycle_time'], - )); - } else { - $this->db->table(ProjectDailyStats::TABLE)->insert(array( - 'day' => $date, - 'project_id' => $project_id, - 'avg_lead_time' => $lead_cycle_time['avg_lead_time'], - 'avg_cycle_time' => $lead_cycle_time['avg_cycle_time'], - )); - } + $this->db->table(self::TABLE)->insert(array( + 'day' => $date, + 'project_id' => $project_id, + 'avg_lead_time' => $lead_cycle_time['avg_lead_time'], + 'avg_cycle_time' => $lead_cycle_time['avg_cycle_time'], + )); $this->db->closeTransaction(); @@ -70,11 +57,11 @@ class ProjectDailyStats extends Base public function getRawMetrics($project_id, $from, $to) { return $this->db->table(self::TABLE) - ->columns('day', 'avg_lead_time', 'avg_cycle_time') - ->eq(self::TABLE.'.project_id', $project_id) - ->gte('day', $from) - ->lte('day', $to) - ->asc(self::TABLE.'.day') - ->findAll(); + ->columns('day', 'avg_lead_time', 'avg_cycle_time') + ->eq('project_id', $project_id) + ->gte('day', $from) + ->lte('day', $to) + ->asc('day') + ->findAll(); } } diff --git a/tests/units/Model/ProjectDailyStatsTest.php b/tests/units/Model/ProjectDailyStatsTest.php index 677a97d0..9efdb199 100644 --- a/tests/units/Model/ProjectDailyStatsTest.php +++ b/tests/units/Model/ProjectDailyStatsTest.php @@ -18,9 +18,12 @@ class ProjectDailyStatsTest extends Base $ts = new TaskStatus($this->container); $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); + $this->assertEquals(2, $p->create(array('name' => 'UnitTest'))); $this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1, 'date_started' => strtotime('-1 day')))); - $this->assertEquals(2, $tc->create(array('title' => 'Task #1', 'project_id' => 1))); + $this->assertEquals(2, $tc->create(array('title' => 'Task #2', 'project_id' => 1))); + $this->assertEquals(3, $tc->create(array('title' => 'Task #3', 'project_id' => 2))); + $pds->updateTotals(1, date('Y-m-d', strtotime('-1 day'))); $this->assertTrue($ts->close(1)); -- cgit v1.2.3