From 9fe8af1b05366e7724708385dc3520afb7acce1b Mon Sep 17 00:00:00 2001 From: sgiehl Date: Mon, 3 Jul 2017 16:02:13 +0200 Subject: [PATCH] Show bandwidth statistics in visitor profile --- VisitorDetails.php | 53 ++++++++++++++++++++++++++++++++-- templates/_profileSummary.twig | 13 +++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 templates/_profileSummary.twig diff --git a/VisitorDetails.php b/VisitorDetails.php index b2469ad..3b2a395 100644 --- a/VisitorDetails.php +++ b/VisitorDetails.php @@ -17,7 +17,7 @@ class VisitorDetails extends VisitorDetailsAbstract public function extendActionDetails(&$action, $nextAction, $visitorDetails) { if (array_key_exists('bandwidth', $action)) { - $formatter = new Formatter(); + $formatter = new Formatter(); $action['bandwidth_pretty'] = $formatter->getPrettySizeFromBytes($action['bandwidth']); } } @@ -25,11 +25,60 @@ public function extendActionDetails(&$action, $nextAction, $visitorDetails) public function renderActionTooltip($action, $visitInfo) { if (!empty($action['bandwidth'])) { - $view = new View('@Bandwidth/_actionTooltip'); + $view = new View('@Bandwidth/_actionTooltip'); $view->action = $action; return $view->render(); } return ''; } + + protected $sumBandwidth = 0; + protected $actionsWithBandwidth = 0; + protected $maxBandwidth = null; + protected $minBandwidth = null; + + public function initProfile($visits, &$profile) + { + $this->sumBandwidth = 0; + $this->actionsWithBandwidth = 0; + } + + public function handleProfileAction($action, &$profile) + { + if (!empty($action['bandwidth'])) { + $this->sumBandwidth += $action['bandwidth']; + if (is_null($this->minBandwidth)) { + $this->minBandwidth = $action['bandwidth']; + } + $this->minBandwidth = min($this->minBandwidth, $action['bandwidth']); + if (is_null($this->minBandwidth)) { + $this->minBandwidth = $action['bandwidth']; + } + $this->maxBandwidth = max($this->maxBandwidth, $action['bandwidth']); + $this->actionsWithBandwidth++; + } + } + + public function finalizeProfile($visits, &$profile) + { + if ($this->actionsWithBandwidth) { + $formatter = new Formatter(); + $profile['sumBandwidth'] = $formatter->getPrettySizeFromBytes($this->sumBandwidth); + $profile['minBandwidth'] = $formatter->getPrettySizeFromBytes($this->minBandwidth); + $profile['maxBandwidth'] = $formatter->getPrettySizeFromBytes($this->maxBandwidth); + $profile['avgBandwidth'] = $formatter->getPrettySizeFromBytes(round($this->sumBandwidth / $this->actionsWithBandwidth)); + } + } + + public function renderProfileSummary($profile) + { + if (!isset($profile['avgBandwidth'])) { + return array(); + } + + $view = new View('@Bandwidth/_profileSummary.twig'); + $view->visitorData = $profile; + return array(array(5, $view->render())); + } } \ No newline at end of file diff --git a/templates/_profileSummary.twig b/templates/_profileSummary.twig new file mode 100644 index 0000000..c97c389 --- /dev/null +++ b/templates/_profileSummary.twig @@ -0,0 +1,13 @@ +
+

{{ 'Bandwidth_Bandwidth'|translate }}

+
+

+ {{ 'Bandwidth_SparklineTotalBandwidthDetails'|translate(visitorData.sumBandwidth) }} + ( + {{ 'Bandwidth_ColumnAvgBandwidth'|translate }}: {{ visitorData.avgBandwidth }}, + {{ 'Bandwidth_ColumnMinBandwidth'|translate }}: {{ visitorData.minBandwidth }}, + {{ 'Bandwidth_ColumnMaxBandwidth'|translate }}: {{ visitorData.maxBandwidth }} + ) +

+
+
\ No newline at end of file