diff --git a/src/Controller/InstanceController.php b/src/Controller/InstanceController.php index 8479e5c..e9fc392 100644 --- a/src/Controller/InstanceController.php +++ b/src/Controller/InstanceController.php @@ -21,7 +21,6 @@ class InstanceController extends AppController public function home() { - // $this->set('md', file_get_contents(ROOT . '/README.md')); $statistics = $this->Instance->getStatistics(); $this->set('statistics', $statistics); } diff --git a/src/Model/Table/InstanceTable.php b/src/Model/Table/InstanceTable.php index 0ec2a53..9b527f2 100644 --- a/src/Model/Table/InstanceTable.php +++ b/src/Model/Table/InstanceTable.php @@ -25,47 +25,64 @@ class InstanceTable extends AppTable return $validator; } - public function getStatistics($days=30): array + public function getStatistics(int $days=30): array { $models = ['Individuals', 'Organisations', 'Alignments', 'EncryptionKeys', 'SharingGroups', 'Users', 'Broods', 'Tags.Tags']; foreach ($models as $model) { $table = TableRegistry::getTableLocator()->get($model); - $statistics[$model]['amount'] = $table->find()->all()->count(); - if ($table->behaviors()->has('Timestamp')) { - $query = $table->find(); - $query->select([ - 'count' => $query->func()->count('id'), - 'date' => 'DATE(modified)', - ]) - ->where(['modified >' => new \DateTime("-{$days} days")]) - ->group(['date']) - ->order(['date']); - $data = $query->toArray(); - $interval = new \DateInterval('P1D'); - $period = new \DatePeriod(new \DateTime("-{$days} days"), $interval, new \DateTime()); - $timeline = []; - foreach ($period as $date) { - $timeline[$date->format("Y-m-d")] = [ - 'time' => $date->format("Y-m-d"), - 'count' => 0 - ]; - } - foreach ($data as $entry) { - $timeline[$entry->date]['count'] = $entry->count; - } - $statistics[$model]['timeline'] = array_values($timeline); - - $startCount = $table->find()->where(['modified <' => new \DateTime("-{$days} days")])->all()->count(); - $endCount = $statistics[$model]['amount']; - $statistics[$model]['variation'] = $endCount - $startCount; - } else { - $statistics[$model]['timeline'] = []; - $statistics[$model]['variation'] = 0; - } + $statistics[$model]['created'] = $this->getActivityStatistic($table, $days, 'created'); + $statistics[$model]['modified'] = $this->getActivityStatistic($table, $days, 'modified'); } return $statistics; } + public function getActivityStatistic(Object $table, int $days=30, string $field='modified', bool $includeTimeline=true): array + { + $statistics = []; + $statistics['amount'] = $table->find()->all()->count(); + if ($table->behaviors()->has('Timestamp') && $includeTimeline) { + $statistics['timeline'] = $this->buildTimeline($table, $days, $field); + $statistics['variation'] = $table->find()->where(["{$field} >" => new \DateTime("-{$days} days")])->all()->count(); - $statistics['amount']; + } else { + $statistics['timeline'] = []; + $statistics['variation'] = 0; + } + return $statistics; + } + + public function buildTimeline(Object $table, int $days=30, string $field='modified'): array + { + $timeline = []; + $authorizedFields = ['modified', 'created']; + if ($table->behaviors()->has('Timestamp')) { + if (!in_array($field, $authorizedFields)) { + throw new MethodNotAllowedException(__('Cannot construct timeline for field `{0}`', $field)); + } + $query = $table->find(); + $query->select([ + 'count' => $query->func()->count('id'), + 'date' => "DATE({$field})", + ]) + ->where(["{$field} >" => new \DateTime("-{$days} days")]) + ->group(['date']) + ->order(['date']); + $data = $query->toArray(); + $interval = new \DateInterval('P1D'); + $period = new \DatePeriod(new \DateTime("-{$days} days"), $interval, new \DateTime()); + foreach ($period as $date) { + $timeline[$date->format("Y-m-d")] = [ + 'time' => $date->format("Y-m-d"), + 'count' => 0 + ]; + } + foreach ($data as $entry) { + $timeline[$entry->date]['count'] = $entry->count; + } + $timeline = array_values($timeline); + } + return $timeline; + } + public function searchAll($value, $limit=5, $model=null) { $results = []; diff --git a/templates/Instance/home.php b/templates/Instance/home.php index 6d91266..099f2cf 100644 --- a/templates/Instance/home.php +++ b/templates/Instance/home.php @@ -32,7 +32,7 @@ $bookmarks = !empty($loggedUser->user_settings_by_name['ui.bookmarks']['value']) = __('Activity') ?>