Skip to content

Commit

Permalink
[ENG-619] Add dashboard widget for campaign delays.
Browse files Browse the repository at this point in the history
  • Loading branch information
heathdutton committed Feb 19, 2019
1 parent 2ae7d83 commit 0388d13
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 39 deletions.
10 changes: 9 additions & 1 deletion Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'author' => 'Mautic',

'services' => [
'models' => [
'models' => [
'mautic.health.model.health' => [
'class' => 'MauticPlugin\MauticHealthBundle\Model\HealthModel',
'arguments' => [
Expand All @@ -27,5 +27,13 @@
],
],
],
'events' => [
'mautic.health.dashboard.subscriber' => [
'class' => 'MauticPlugin\MauticHealthBundle\EventListener\DashboardSubscriber',
'arguments' => [
'mautic.health.model.health',
],
],
],
],
];
82 changes: 82 additions & 0 deletions EventListener/DashboardSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* @copyright 2018 Mautic Contributors. All rights reserved
* @author Digital Media Solutions, LLC
*
* @link http://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace MauticPlugin\MauticHealthBundle\EventListener;

use Mautic\DashboardBundle\Event\WidgetDetailEvent;
use Mautic\DashboardBundle\EventListener\DashboardSubscriber as MainDashboardSubscriber;
use MauticPlugin\MauticHealthBundle\Model\HealthModel;

/**
* Class DashboardSubscriber.
*/
class DashboardSubscriber extends MainDashboardSubscriber
{
/**
* Define the name of the bundle/category of the widget(s).
*
* @var string
*/
protected $bundle = 'campaign';

/**
* Define the widget(s).
*
* @var string
*/
protected $types = [
'campaign.health' => [],
];

/**
* @var HealthModel
*/
protected $healthModel;

/**
* DashboardSubscriber constructor.
*
* @param HealthModel $healthModel
*/
public function __construct(HealthModel $healthModel)
{
$this->healthModel = $healthModel;
}

/**
* Set a widget detail when needed.
*
* @param WidgetDetailEvent $event
*/
public function onWidgetDetailGenerate(WidgetDetailEvent $event)
{
// This always pulls from cached data from the cron task.
// if (!$event->isCached()) {
// }
$cache = $this->healthModel->getCache();
$widget = $event->getWidget();
if ($widget->getHeight() < 330) {
$widget->setHeight(330);
}
$params = $widget->getParams();
$data['params'] = $params;
$data['height'] = $widget->getHeight();
$data['delays'] = isset($cache['delays']) ? $cache['delays'] : [];
$data['lastCached'] = isset($cache['lastCached']) ? $cache['lastCached'] : null;
$event->setTemplateData(['data' => $data]);

if ('campaign.health' == $event->getType()) {
$event->setTemplate('MauticHealthBundle:Widgets:health.html.php');
}

$event->stopPropagation();
}
}
2 changes: 1 addition & 1 deletion Integration/HealthIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function setComponentStatus($incidentStatus = null, $componentStatus = nu
}
if ($change) {
// Update/Close the incident.
if (!empty($lastUpdate)) {
if (!empty($lastUpdate) && is_array($lastUpdate['affected_components'])) {
foreach ($lastUpdate['affected_components'] as $affectedComponent) {
if (!empty($affectedComponent['id'])) {
$componentIds[] = $affectedComponent['id'];
Expand Down
81 changes: 45 additions & 36 deletions Model/HealthModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,28 +140,6 @@ public function campaignKickoffCheck(OutputInterface $output = null)
}
}

/**
* @param $delay
* @param $limit
* @param $output
*/
private function output($delay, $limit, $output)
{
if ($delay['avg_delay_s'] > $limit) {
$status = 'error';
$this->incidents[] = $delay;
} else {
$status = 'info';
}
$progress = ProgressBarHelper::init($output, $limit);
$progress->start();
$progress->advance($delay['avg_delay_s']);
$output->write(
' <'.$status.'>'.$delay['body'].'</'.$status.'>'
);
$output->writeln('');
}

/**
* @param null $campaignId
*
Expand All @@ -182,6 +160,23 @@ private function getPublishedCampaigns($campaignId = null)
}
}

/**
* Create a DBAL QueryBuilder preferring a slave connection if available.
*
* @return QueryBuilder
*/
private function slaveQueryBuilder()
{
/** @var Connection $connection */
$connection = $this->em->getConnection();
if ($connection instanceof MasterSlaveConnection) {
// Prefer a slave connection if available.
$connection->connect('slave');
}

return new QueryBuilder($connection);
}

// /**
// * Discern the number of leads waiting on mautic:campaign:rebuild.
// * This typically means a large segment has been given a campaign.
Expand Down Expand Up @@ -233,20 +228,25 @@ private function getPublishedCampaigns($campaignId = null)
// }

/**
* Create a DBAL QueryBuilder preferring a slave connection if available.
*
* @return QueryBuilder
* @param $delay
* @param $limit
* @param $output
*/
private function slaveQueryBuilder()
private function output($delay, $limit, $output)
{
/** @var Connection $connection */
$connection = $this->em->getConnection();
if ($connection instanceof MasterSlaveConnection) {
// Prefer a slave connection if available.
$connection->connect('slave');
if ($delay['avg_delay_s'] > $limit) {
$status = 'error';
$this->incidents[] = $delay;
} else {
$status = 'info';
}

return new QueryBuilder($connection);
$progress = ProgressBarHelper::init($output, $limit);
$progress->start();
$progress->advance($delay['avg_delay_s']);
$output->write(
' <'.$status.'>'.$delay['body'].'</'.$status.'>'
);
$output->writeln('');
}

/**
Expand All @@ -257,12 +257,15 @@ public function getCache()
if (!$this->cache) {
$this->cache = new CacheStorageHelper(
CacheStorageHelper::ADAPTOR_DATABASE,
'MauticHealthBundle',
'Health',
$this->em->getConnection()
);
}

return $this->cache->get('delays');
return [
'delays' => $this->cache->get('delays'),
'lastCached' => $this->cache->get('lastCached'),
];
}

/**
Expand All @@ -273,10 +276,16 @@ public function setCache()
if (!$this->cache) {
$this->cache = new CacheStorageHelper(
CacheStorageHelper::ADAPTOR_DATABASE,
'MauticHealthBundle',
'Health',
$this->em->getConnection()
);
}
usort(
$this->delays,
function ($a, $b) {
return $b['avg_delay_s'] - $a['avg_delay_s'];
}
);
$this->cache->set('delays', $this->delays, null);
$this->cache->set('lastCached', time(), null);
$this->delays = [];
Expand Down
3 changes: 2 additions & 1 deletion Translations/en_US/messages.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ mautic.health.campaign_inactive_threshold.tooltip = "The maximum number of secon
mautic.health.statuspage_component_incidents = "Statuspage incidents"
mautic.health.statuspage_component_incidents.tooltip = "In addition to updating component statuses, automatically create/update/close incidents for more detailed information."
mautic.health.kickoff = "Running kickoff checks"
mautic.health.scheduled = "Running scheduled checks"
mautic.health.scheduled = "Running scheduled checks"
mautic.widget.campaign.health = "Processing Delays"
52 changes: 52 additions & 0 deletions Views/Widgets/health.html.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* @copyright 2019 Mautic Contributors. All rights reserved
* @author Digital Media Solutions, LLC
*
* @link http://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

?>

<div class="chart-wrapper" style="height:<?php echo $data['height']; ?>px">
<div class="pt-sd pr-md pb-md pl-md">
<?php if (count($data['delays'])): ?>
<div id="health-status-table">
<div class="responsive-table">
<table id="health-status" class="table table-striped table-bordered" width="100%" data-height="<?php echo $data['height']; ?>">
<thead>
<th>Campaign</th>
<th>Event</th>
<th>Type</th>
<th>Contacts</th>
<th>Delay</th>
</thead>
<tbody>
<?php foreach ($data['delays'] as $delay): ?>
<tr>
<th>
<a href="/s/campaigns/view/<?php echo $delay['campaign_id']; ?>"><?php echo $delay['campaign_name']; ?></a>
</th>
<th><?php echo $delay['event_name']; ?> (<?php echo $delay['event_id']; ?>)</th>
<th><?php echo $delay['type']; ?></th>
<th><?php echo $delay['contact_count']; ?></th>
<th><?php echo $delay['avg_delay_s']; ?>s</th>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<?php if (!empty($data['lastCached'])) {
echo '<small>Calculated: '.(new \DateTime($data['lastCached']))->format('c').'</small>';
}
?>
<?php else: ?>
<h3>No delays detected</h3>
<?php endif; ?>
</div>
</div>

0 comments on commit 0388d13

Please sign in to comment.