monitoring-8.x-1.x-dev/modules/demo/monitoring_demo.module
modules/demo/monitoring_demo.module
<?php
/**
* @file
* Bootstrap file of the monitoring_demo module.
*/
use Drupal\comment\Entity\Comment;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Random;
use Drupal\Core\Language\LanguageInterface;
use Drupal\monitoring\Entity\SensorConfig;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
use Drupal\search_api\Entity\Index;
/**
* Implements hook_modules_installed().
*
* Executes setup of monitoring sensors to provide the demo functionality.
*/
function monitoring_demo_modules_installed($modules, $is_syncing) {
if (!in_array('monitoring_demo', $modules)) {
return;
}
$random = new Random();
// Set the front page to monitoring-demo.
\Drupal::configFactory()
->getEditable('system.site')
->set('page.front', '/monitoring-demo')
->save();
// Create a few nodes and comments as sample data for some sensors.
$nodes = [];
$nodes[] = _monitoring_setup_create_node(array('type' => 'article'));
_monitoring_setup_create_comment(array('entity_id' => $nodes[0]->id()));
_monitoring_setup_create_comment(array('entity_id' => $nodes[0]->id()));
_monitoring_setup_create_node();
$nodes[] = _monitoring_setup_create_node(array('type' => 'article'));
_monitoring_setup_create_comment(array('entity_id' => $nodes[1]->id()));
_monitoring_setup_create_comment(array('entity_id' => $nodes[1]->id()));
_monitoring_setup_create_comment(array('entity_id' => $nodes[1]->id()));
_monitoring_setup_create_node();
$sensor_manager = monitoring_sensor_manager();
// Setup search API.
$sensor_manager->resetCache();
$indices = Index::loadMultiple();
// Enable sensor for the index created above.
if (!empty($indices)) {
foreach ($indices as $index) {
$sensor_manager->enableSensor('search_api_' . $index->id());
}
}
// Create node type sensors.
foreach (['page', 'article'] as $node_type_id) {
$node_type = NodeType::load($node_type_id);
$sensor = SensorConfig::create(array(
'id' => 'node_new_' . $node_type->id(),
'label' => new FormattableMarkup('New @type nodes', array('@type' => $node_type->label())),
'description' => new FormattableMarkup('New nodes of type @type', array('@type' => $node_type->label())),
'plugin_id' => 'entity_aggregator',
'value_label' => new FormattableMarkup('@type nodes', array('@type' => $node_type->label())),
'category' => 'Content',
'status' => FALSE,
'caching_time' => 600,
'settings' => array(
'entity_type' => 'node',
'conditions' => array(
array('field' => 'type', 'value' => $node_type->id()),
),
'time_interval_field' => 'created',
'time_interval_value' => 60 * 60 * 24,
),
));
$sensor->save();
}
// Enable content sensors.
$sensor_manager->enableSensor('node_new_all');
$sensor_manager->enableSensor('node_new_page');
$sensor_manager->enableSensor('node_new_article');
$sensor_manager->enableSensor('comment_new');
// Generate watchdog entries.
// Watchdog sensors are enabled by default, no need to enable them here.
for ($i = 0; $i < 20; $i++) {
\Drupal::logger('sensor_demo')->error($random->name());
\Drupal::logger('sensor_demo')->notice($random->name());
}
for ($i = 0; $i < 10; $i++) {
\Drupal::logger('sensor_demo')->info($random->name());
\Drupal::logger('sensor_demo')->warning($random->name());
}
for ($i = 0; $i < 50; $i++) {
\Drupal::database()->insert('watchdog')->fields(array(
'type' => 'page not found',
'message' => '@uri',
'variables' => serialize(['@uri' => 'not/found']),
'location' => 'http://example.com/not/found',
'timestamp' => \Drupal::time()->getRequestTime(),
))->execute();
}
// Enable Enabled modules and Disappeared sensors for the "interactive" demo.
$sensor_manager->enableSensor('monitoring_installed_modules');
$sensor_manager->enableSensor('monitoring_disappeared_sensors');
// Generate some image style derivative errors.
$file = \Drupal::service('file.repository')->writeData($random->name(), 'public://');
/** @var \Drupal\file\FileUsage\FileUsageInterface $usage */
$usage = \Drupal::service('file.usage');
foreach ($nodes as $node) {
$usage->add($file, 'monitoring_test', 'node', $node->id());
// We use the logger.dblog service to be able to set the referer.
\Drupal::service('logger.dblog')->log(LOG_NOTICE,
'Source image at %source_image_path not found while trying to generate derivative image at %derivative_path.', [
'%source_image_path' => $file->getFileUri(),
'%derivative_path' => 'hash://styles/preview/1234.jpeg',
'request_uri' => '',
'uid' => 0,
'channel' => 'image',
'link' => '',
'referer' => 'http://example.com/node/' . $node->id(),
'ip' => '127.0.0.1',
'timestamp' => \Drupal::time()->getRequestTime(),
]
);
}
$file = \Drupal::service('file.repository')->writeData($random->name(), 'public://');
\Drupal::logger('image')->notice('Source image at %source_image_path not found while trying to generate derivative image at %derivative_path.', [
'%source_image_path' => $file->getFileUri(),
'%derivative_path' => 'hash://styles/preview/5678.jpeg',
]
);
}
/**
* Creates comments for testing purposes.
*
* @param array $settings
* Comment data.
*
* @return object
* Crated comment.
*/
function _monitoring_setup_create_comment($settings = array()) {
$random = new Random();
$settings += array(
'subject' => $random->name(),
'entity_id' => $settings['entity_id'],
'field_name' => 'comment',
'entity_type' => 'node',
'comment_type' => 'comment',
'comment_body' => $random->name(40),
);
$comment = Comment::create($settings);
$comment->save();
return $comment;
}
/**
* Creates nodes for testing purposes.
*
* @param array $settings
* Node data.
*
* @return \Drupal\node\Entity\Node
* Created node.
*/
function _monitoring_setup_create_node($settings = array()) {
$random = new Random();
// Populate defaults array.
$settings += array(
'body' => array(array()),
'title' => $random->name(8),
'revision' => 1,
'log' => '',
'status' => NodeInterface::PUBLISHED,
'type' => 'page',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
);
// If the node's user uid is not specified manually, use the currently
// logged in user if available, or else the user running the test.
if (!isset($settings['uid'])) {
$user = \Drupal::currentUser();
$settings['uid'] = $user->id();
}
// Merge body field value and format separately.
$settings['body'][0] += array(
'value' => $random->name(32),
'format' => filter_default_format(),
);
$node = Node::create($settings);
if (!empty($settings['revision'])) {
$node->setNewRevision();
}
$node->save();
return $node;
}
