From 509b203591d3776c433d94c1b5c42ea382336379 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Fri, 12 Nov 2021 15:40:03 +0100 Subject: [PATCH] chg: [instance:home] Added support of both `modified` and `created` in stat panels --- src/Controller/InstanceController.php | 1 - src/Model/Table/InstanceTable.php | 83 +++++++++++-------- templates/Instance/home.php | 8 +- templates/element/charts/bar.php | 29 ++++--- templates/element/widgets/highlight-panel.php | 25 +++++- 5 files changed, 94 insertions(+), 52 deletions(-) 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'])
- $statistics) : ?> + $statisticForModel) : ?>
user_settings_by_name['ui.bookmarks']['value']) ); echo $this->element('widgets/highlight-panel', [ 'titleHtml' => $panelTitle, - 'number' => $statistics['amount'], - 'variation' => $statistics['variation'] ?? '', - 'chartData' => $statistics['timeline'] ?? [] + 'number' => $statisticForModel['created']['amount'], + 'variation' => $statisticForModel['created']['variation'] ?? '', + 'timeline' => $statisticForModel ?? [] ]); ?>
diff --git a/templates/element/charts/bar.php b/templates/element/charts/bar.php index 573a61b..a3766f0 100644 --- a/templates/element/charts/bar.php +++ b/templates/element/charts/bar.php @@ -1,14 +1,22 @@ $entry) { - $data[] = $entry['count']; +$chartData = $chartData ?? []; +$chartSeries = []; +if (!empty($series)) { + $chartSeries = $series; +} else { + // Transform the chart data into the expected format + $data = []; + foreach ($chartData as $i => $entry) { + $data[] = $entry['count']; + } + $chartSeries = [ + ['data' => $data] + ]; } ?> @@ -20,7 +28,7 @@ foreach ($chartData as $i => $entry) { const defaultOptions = { chart: { id: '', - type: 'bar', + type: 'line', sparkline: { enabled: true }, @@ -35,10 +43,7 @@ foreach ($chartData as $i => $entry) { enabled: false }, }, - series: [{ - data: , - }], - colors: ['var(--bs-light)'], + series: , tooltip: { x: { show: false @@ -46,11 +51,11 @@ foreach ($chartData as $i => $entry) { y: { title: { formatter: function formatter(val) { - return ''; + return ''; } } }, - theme: '' + theme: 'dark' }, } const chartOptions = Object.assign({}, defaultOptions, passedOptions) diff --git a/templates/element/widgets/highlight-panel.php b/templates/element/widgets/highlight-panel.php index 68b31ea..4cbe290 100644 --- a/templates/element/widgets/highlight-panel.php +++ b/templates/element/widgets/highlight-panel.php @@ -1,4 +1,6 @@ %s
', $variationClass, @@ -25,9 +43,12 @@ $leftContent = sprintf('
%s

%s

%s', $variationHtml ); $rightContent = sprintf('
%s
', $this->element('charts/bar', [ - 'chartData' => $chartData, + 'series' => $series, 'chartOptions' => [ - + // 'colors' => ['var(--bs-light)', 'var(--bs-primary)'], + 'stroke' => [ + 'width' => [0, 2] + ], ] ]));