You are here

function monitoring_demo_install in Monitoring 8

Implements hook_install().

Executes setup of monitoring sensors to provide the demo functionality.

File

modules/demo/monitoring_demo.install, line 22
Install file of the monitoring_demo module.

Code

function monitoring_demo_install() {
  $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 = file_save_data($random
    ->name());

  /** @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 = file_save_data($random
    ->name());
  \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',
  ]);
}