From 14e0fa90b3544de58ce77dbf36d67dc5f61633b0 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Mon, 6 Sep 2021 11:17:25 +0200 Subject: [PATCH 1/4] new: [instance:home] Added statistics and highlight panel - WiP --- src/Controller/InstanceController.php | 4 +- src/Model/Table/InstanceTable.php | 42 ++++++++++++ src/View/Helper/BootstrapHelper.php | 2 + templates/Instance/home.php | 23 ++++++- templates/element/charts/bar.php | 64 +++++++++++++++++++ templates/element/widgets/highlight-panel.php | 37 +++++++++++ 6 files changed, 169 insertions(+), 3 deletions(-) create mode 100644 templates/element/charts/bar.php create mode 100644 templates/element/widgets/highlight-panel.php diff --git a/src/Controller/InstanceController.php b/src/Controller/InstanceController.php index c5a9556..2df7bb5 100644 --- a/src/Controller/InstanceController.php +++ b/src/Controller/InstanceController.php @@ -19,7 +19,9 @@ class InstanceController extends AppController public function home() { - $this->set('md', file_get_contents(ROOT . '/README.md')); + // $this->set('md', file_get_contents(ROOT . '/README.md')); + $statistics = $this->Instance->getStatistics(); + $this->set('statistics', $statistics); } public function status() diff --git a/src/Model/Table/InstanceTable.php b/src/Model/Table/InstanceTable.php index dd935b8..50bc925 100644 --- a/src/Model/Table/InstanceTable.php +++ b/src/Model/Table/InstanceTable.php @@ -4,6 +4,7 @@ namespace App\Model\Table; use App\Model\Table\AppTable; use Cake\ORM\Table; +use Cake\ORM\TableRegistry; use Cake\Validation\Validator; use Migrations\Migrations; @@ -21,6 +22,47 @@ class InstanceTable extends AppTable return $validator; } + public function getStatistics($days=7): array + { + $models = ['Individuals', 'Organisations', 'Alignments', 'EncryptionKeys', 'SharingGroups', 'Users', '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; + } + } + return $statistics; + } + public function getMigrationStatus() { $migrations = new Migrations(); diff --git a/src/View/Helper/BootstrapHelper.php b/src/View/Helper/BootstrapHelper.php index 83cd7f1..47d8a3a 100644 --- a/src/View/Helper/BootstrapHelper.php +++ b/src/View/Helper/BootstrapHelper.php @@ -979,6 +979,7 @@ class BoostrapCard extends BootstrapGeneric 'headerHTML' => '', 'footerHTML' => '', 'bodyHTML' => '', + 'class' => '', 'headerClass' => '', 'bodyClass' => '', 'footerClass' => '', @@ -1010,6 +1011,7 @@ class BoostrapCard extends BootstrapGeneric 'card', !empty($this->options['variant']) ? "bg-{$this->options['variant']}" : '', !empty($this->options['variant']) ? $this->getTextClassForVariant($this->options['variant']) : '', + h($this->options['class']), ], ], implode('', [$this->genHeader(), $this->genBody(), $this->genFooter()])); return $card; diff --git a/templates/Instance/home.php b/templates/Instance/home.php index 74eb85a..20e3460 100644 --- a/templates/Instance/home.php +++ b/templates/Instance/home.php @@ -1,3 +1,22 @@ text($md); +// $Parsedown = new Parsedown(); +// echo $Parsedown->text($md); + +?> + +
+ $statistics): ?> +
+ element('widgets/highlight-panel', [ + 'title' => $modelName, + 'number' => $statistics['amount'], + 'variation' => $statistics['variation'] ?? '', + 'chartData' => $statistics['timeline'] ?? [] + ]); + ?> +
+ +
\ No newline at end of file diff --git a/templates/element/charts/bar.php b/templates/element/charts/bar.php new file mode 100644 index 0000000..1abb0b1 --- /dev/null +++ b/templates/element/charts/bar.php @@ -0,0 +1,64 @@ + $entry) { + $data[] = $entry['count']; +} +?> + +
+ + + + \ No newline at end of file diff --git a/templates/element/widgets/highlight-panel.php b/templates/element/widgets/highlight-panel.php new file mode 100644 index 0000000..6747620 --- /dev/null +++ b/templates/element/widgets/highlight-panel.php @@ -0,0 +1,37 @@ + 0) { + $variationIcon = 'arrow-up'; + $variationClass = 'text-success'; +} else { + $variationIcon = 'arrow-down'; + $variationClass = 'text-danger'; +} + +$variationHtml = sprintf('
%s
', + $variationClass, + $this->FontAwesome->getClass($variationIcon), + !empty($variation) ? h($variation) : '' +); + +$leftContent = sprintf('
%s

%s

%s', + h($title ?? ''), + h($number ?? ''), + $variationHtml +); +$rightContent = sprintf('
%s
', $this->element('charts/bar', [ + 'chartData' => $chartData +])); + +$cardContent = sprintf('
%s
%s
', $leftContent, $rightContent); + +echo $this->Bootstrap->card([ + 'variant' => 'secondary', + 'bodyHTML' => $cardContent, + 'bodyClass' => 'p-3' +]); + +?> From 85483327626961916e301fe806a5393c24bda2dc Mon Sep 17 00:00:00 2001 From: mokaddem Date: Mon, 6 Sep 2021 11:35:09 +0200 Subject: [PATCH 2/4] chg: [charts:bar] Tooltip text color for light theme --- templates/element/charts/bar.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/templates/element/charts/bar.php b/templates/element/charts/bar.php index 1abb0b1..b53a44d 100644 --- a/templates/element/charts/bar.php +++ b/templates/element/charts/bar.php @@ -53,12 +53,16 @@ foreach ($chartData as $i => $entry) { } }, theme: '' - } + }, } const chartOptions = Object.assign({}, defaultOptions, passedOptions) new ApexCharts(document.querySelector('#'), chartOptions).render(); - // const chart = new ApexCharts(document.querySelector("#"), options); - // chart.render(); })() - \ No newline at end of file + + + \ No newline at end of file From 3f4e2ae4b894c1fe3d31bab1c4533e95897e75d2 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Tue, 7 Sep 2021 09:59:36 +0200 Subject: [PATCH 3/4] chg: [home] Added link to index for each panels --- plugins/Tags/config/routes.php | 2 +- templates/Instance/home.php | 16 +++- templates/element/charts/bar.php | 79 +++++++++---------- templates/element/widgets/highlight-panel.php | 3 +- webroot/css/main.css | 4 + 5 files changed, 57 insertions(+), 47 deletions(-) diff --git a/plugins/Tags/config/routes.php b/plugins/Tags/config/routes.php index 3fab36a..93223e1 100644 --- a/plugins/Tags/config/routes.php +++ b/plugins/Tags/config/routes.php @@ -13,7 +13,7 @@ $routes->plugin( ['controller' => 'Tags'] ); - // $routes->get('/', ['controller' => 'Tags']); + $routes->get('/', ['controller' => 'Tags', 'action' => 'index']); // $routes->get('/{id}', ['controller' => 'Tags', 'action' => 'view']); // $routes->put('/{id}', ['controller' => 'Tags', 'action' => 'edit']); } diff --git a/templates/Instance/home.php b/templates/Instance/home.php index 20e3460..b9c41a4 100644 --- a/templates/Instance/home.php +++ b/templates/Instance/home.php @@ -1,17 +1,25 @@ text($md); - ?> +

$statistics): ?>
Html->link( + h($modelForDisplay), + $this->Url->build([ + 'controller' => $modelForDisplay, + 'action' => 'index', + ]), + ['class' => 'text-white'] + ); echo $this->element('widgets/highlight-panel', [ - 'title' => $modelName, + 'titleHtml' => $panelTitle, 'number' => $statistics['amount'], 'variation' => $statistics['variation'] ?? '', 'chartData' => $statistics['timeline'] ?? [] diff --git a/templates/element/charts/bar.php b/templates/element/charts/bar.php index b53a44d..ea3f56a 100644 --- a/templates/element/charts/bar.php +++ b/templates/element/charts/bar.php @@ -14,51 +14,48 @@ foreach ($chartData as $i => $entry) {
- -