Skip to content

Commit

Permalink
Show bandwidth statistics in visitor profile
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiehl committed Jul 3, 2017
1 parent b35f054 commit 9fe8af1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
53 changes: 51 additions & 2 deletions VisitorDetails.php
Expand Up @@ -17,19 +17,68 @@ 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']);
}
}

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()));
}
}
13 changes: 13 additions & 0 deletions templates/_profileSummary.twig
@@ -0,0 +1,13 @@
<div class="visitor-profile-summary visitor-profile-bandwidth">
<h1>{{ 'Bandwidth_Bandwidth'|translate }}</h1>
<div>
<p>
{{ 'Bandwidth_SparklineTotalBandwidthDetails'|translate(visitorData.sumBandwidth) }}
(
{{ 'Bandwidth_ColumnAvgBandwidth'|translate }}: {{ visitorData.avgBandwidth }},
{{ 'Bandwidth_ColumnMinBandwidth'|translate }}: {{ visitorData.minBandwidth }},
{{ 'Bandwidth_ColumnMaxBandwidth'|translate }}: {{ visitorData.maxBandwidth }}
)
</p>
</div>
</div>

0 comments on commit 9fe8af1

Please sign in to comment.