Skip to content

Commit

Permalink
Add command to remove project activities after one year
Browse files Browse the repository at this point in the history
  • Loading branch information
fguillot committed Dec 1, 2017
1 parent 0573c92 commit 0153cb3
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 32 deletions.
1 change: 1 addition & 0 deletions app/Console/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @property \Kanboard\Export\TransitionExport $transitionExport
* @property \Kanboard\Model\NotificationModel $notificationModel
* @property \Kanboard\Model\ProjectModel $projectModel
* @property \Kanboard\Model\ProjectActivityModel $projectActivityModel
* @property \Kanboard\Model\ProjectPermissionModel $projectPermissionModel
* @property \Kanboard\Model\ProjectDailyColumnStatsModel $projectDailyColumnStatsModel
* @property \Kanboard\Model\ProjectDailyStatsModel $projectDailyStatsModel
Expand Down
21 changes: 21 additions & 0 deletions app/Console/ProjectActivityArchiveCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Kanboard\Console;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ProjectActivityArchiveCommand extends BaseCommand
{
protected function configure()
{
$this
->setName('projects:archive-activities')
->setDescription('Remove project activities after one year');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->projectActivityModel->cleanup(strtotime('-1 year'));
}
}
18 changes: 5 additions & 13 deletions app/Model/ProjectActivityModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,14 @@ class ProjectActivityModel extends Base
*/
public function createEvent($project_id, $task_id, $creator_id, $event_name, array $data)
{
$values = array(
return $this->db->table(self::TABLE)->insert(array(
'project_id' => $project_id,
'task_id' => $task_id,
'creator_id' => $creator_id,
'event_name' => $event_name,
'date_creation' => time(),
'data' => json_encode($data),
);

$this->cleanup(PROJECT_ACTIVITIES_MAX_EVENTS - 1);
return $this->db->table(self::TABLE)->insert($values);
));
}

/**
Expand Down Expand Up @@ -73,15 +70,10 @@ public function getQuery()
* Remove old event entries to avoid large table
*
* @access public
* @param integer $max Maximum number of items to keep in the table
* @param integer $ts Timestamp
*/
public function cleanup($max)
public function cleanup($ts)
{
$total = $this->db->table(self::TABLE)->count();

if ($total > $max) {
$ids = $this->db->table(self::TABLE)->asc('id')->limit($total - $max)->findAllByColumn('id');
$this->db->table(self::TABLE)->in('id', $ids)->remove();
}
$this->db->table(self::TABLE)->lt('date_creation', $ts)->remove();
}
}
2 changes: 2 additions & 0 deletions app/ServiceProvider/CommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Kanboard\Console\PluginInstallCommand;
use Kanboard\Console\PluginUninstallCommand;
use Kanboard\Console\PluginUpgradeCommand;
use Kanboard\Console\ProjectActivityArchiveCommand;
use Kanboard\Console\ProjectArchiveCommand;
use Kanboard\Console\ProjectDailyColumnStatsExportCommand;
use Kanboard\Console\ProjectDailyStatsCalculationCommand;
Expand Down Expand Up @@ -48,6 +49,7 @@ public function register(Container $container)
$application->add(new SubtaskExportCommand($container));
$application->add(new TaskExportCommand($container));
$application->add(new ProjectArchiveCommand($container));
$application->add(new ProjectActivityArchiveCommand($container));
$application->add(new ProjectDailyStatsCalculationCommand($container));
$application->add(new ProjectDailyColumnStatsExportCommand($container));
$application->add(new TransitionExportCommand($container));
Expand Down
2 changes: 0 additions & 2 deletions app/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,5 @@

defined('TOTP_ISSUER') or define('TOTP_ISSUER', 'Kanboard');

defined('PROJECT_ACTIVITIES_MAX_EVENTS') or define('PROJECT_ACTIVITIES_MAX_EVENTS', 10000);

// Comma separated list of fields to not synchronize when using external authentication providers
defined('EXTERNAL_AUTH_EXCLUDE_FIELDS') or define('EXTERNAL_AUTH_EXCLUDE_FIELDS', 'username');
3 changes: 0 additions & 3 deletions config.default.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,5 @@
// TOTP (2FA) issuer name
define('TOTP_ISSUER', 'Kanboard');

// Maximum number of events stored in the table "project_activities"
define('PROJECT_ACTIVITIES_MAX_EVENTS', 10000);

// Comma separated list of fields to not synchronize when using external authentication providers
define('EXTERNAL_AUTH_EXCLUDE_FIELDS', 'username');
1 change: 1 addition & 0 deletions doc/en_US/cli.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Available commands:
plugin:upgrade Update all installed plugins
projects
projects:archive Disable projects not touched during one year
projects:archive-activities Remove project activities after one year
projects:daily-stats Calculate daily statistics for all projects
trigger
trigger:tasks Trigger scheduler event for all tasks
Expand Down
3 changes: 0 additions & 3 deletions doc/en_US/config.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,6 @@ define('API_AUTHENTICATION_TOKEN', 'My unique API Token');
// TOTP (2FA) issuer name
define('TOTP_ISSUER', 'Kanboard');

// Maximum number of events stored in the table "project_activities"
define('PROJECT_ACTIVITIES_MAX_EVENTS', 10000);

// Comma separated list of fields to not synchronize when using external authentication providers
define('EXTERNAL_AUTH_EXCLUDE_FIELDS', 'username');
```
22 changes: 11 additions & 11 deletions tests/units/Model/ProjectActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@ public function testCleanup()
$this->assertEquals(1, $projectModel->create(array('name' => 'Project #1')));
$this->assertEquals(1, $taskCreation->create(array('title' => 'Task #1', 'project_id' => 1)));

$max = 15;
$nb_events = 100;
$nbEvents = 100;
$task = $taskFinder->getById(1);

for ($i = 0; $i < $nb_events; $i++) {
for ($i = 0; $i < $nbEvents; $i++) {
$this->assertTrue($projectActivity->createEvent(1, 1, 1, TaskModel::EVENT_CLOSE, array('task' => $task)));
}

$this->assertEquals($nb_events, $this->container['db']->table('project_activities')->count());
$projectActivity->cleanup($max);
$this->assertEquals($nbEvents, $this->container['db']->table('project_activities')->count());
$projectActivity->cleanup(strtotime('-12 months'));
$this->assertEquals($nbEvents, $this->container['db']->table('project_activities')->count());

$this->container['db']->table('project_activities')->in('id', array(1, 2, 3))->update(array(
'date_creation' => strtotime('-13 months')
));

$events = $projectActivity->getQuery()->desc('id')->findAll();

$this->assertNotEmpty($events);
$this->assertCount($max, $events);
$this->assertEquals(100, $events[0]['id']);
$this->assertEquals(99, $events[1]['id']);
$this->assertEquals(86, $events[14]['id']);
$projectActivity->cleanup(strtotime('-12 months'));
$this->assertEquals($nbEvents - 3, $this->container['db']->table('project_activities')->count());
}
}
2 changes: 2 additions & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@
'Kanboard\\Console\\PluginInstallCommand' => $baseDir . '/app/Console/PluginInstallCommand.php',
'Kanboard\\Console\\PluginUninstallCommand' => $baseDir . '/app/Console/PluginUninstallCommand.php',
'Kanboard\\Console\\PluginUpgradeCommand' => $baseDir . '/app/Console/PluginUpgradeCommand.php',
'Kanboard\\Console\\ProjectActivityArchiveCommand' => $baseDir . '/app/Console/ProjectActivityArchiveCommand.php',
'Kanboard\\Console\\ProjectArchiveCommand' => $baseDir . '/app/Console/ProjectArchiveCommand.php',
'Kanboard\\Console\\ProjectDailyColumnStatsExportCommand' => $baseDir . '/app/Console/ProjectDailyColumnStatsExportCommand.php',
'Kanboard\\Console\\ProjectDailyStatsCalculationCommand' => $baseDir . '/app/Console/ProjectDailyStatsCalculationCommand.php',
'Kanboard\\Console\\ResetPasswordCommand' => $baseDir . '/app/Console/ResetPasswordCommand.php',
Expand Down
2 changes: 2 additions & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ class ComposerStaticInit6edea6294a88689e3f5c56484bb70c9b
'Kanboard\\Console\\PluginInstallCommand' => __DIR__ . '/../..' . '/app/Console/PluginInstallCommand.php',
'Kanboard\\Console\\PluginUninstallCommand' => __DIR__ . '/../..' . '/app/Console/PluginUninstallCommand.php',
'Kanboard\\Console\\PluginUpgradeCommand' => __DIR__ . '/../..' . '/app/Console/PluginUpgradeCommand.php',
'Kanboard\\Console\\ProjectActivityArchiveCommand' => __DIR__ . '/../..' . '/app/Console/ProjectActivityArchiveCommand.php',
'Kanboard\\Console\\ProjectArchiveCommand' => __DIR__ . '/../..' . '/app/Console/ProjectArchiveCommand.php',
'Kanboard\\Console\\ProjectDailyColumnStatsExportCommand' => __DIR__ . '/../..' . '/app/Console/ProjectDailyColumnStatsExportCommand.php',
'Kanboard\\Console\\ProjectDailyStatsCalculationCommand' => __DIR__ . '/../..' . '/app/Console/ProjectDailyStatsCalculationCommand.php',
'Kanboard\\Console\\ResetPasswordCommand' => __DIR__ . '/../..' . '/app/Console/ResetPasswordCommand.php',
Expand Down

0 comments on commit 0153cb3

Please sign in to comment.