You are here

abstract class DatabaseAggregatorSensorPluginBase in Monitoring 8

Base class for database aggregator sensors.

Defines sensor settings:

  • conditions: A list of conditions to apply to the query.

    • field: Name of the field to filter on. Configurable fields are supported using the field_name.column_name syntax.
    • value: The value to limit by, either an array or a scalar value.
    • operator: Any of the supported operators.
  • time_interval_field: Timestamp field name
  • time_interval_value: Number of seconds defining the period

Adds time interval to sensor settings form.

Hierarchy

Expanded class hierarchy of DatabaseAggregatorSensorPluginBase

2 files declare their use of DatabaseAggregatorSensorPluginBase
ContentEntityAggregatorSensorPlugin.php in src/Plugin/monitoring/SensorPlugin/ContentEntityAggregatorSensorPlugin.php
Contains \Drupal\monitoring\Plugin\monitoring\SensorPlugin\ContentEntityAggregatorSensorPlugin.
DatabaseAggregatorSensorPlugin.php in src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
Contains \Drupal\monitoring\Plugin\monitoring\SensorPlugin\DatabaseAggregatorSensorPlugin.

File

src/SensorPlugin/DatabaseAggregatorSensorPluginBase.php, line 25
Contains \Drupal\monitoring\SensorPlugin\DatabaseAggregatorSensorPluginBase.

Namespace

Drupal\monitoring\SensorPlugin
View source
abstract class DatabaseAggregatorSensorPluginBase extends SensorPluginBase {

  /**
   * Allows plugins to control if a timestamp field can be configured.
   *
   * @var bool
   */
  protected $configurableTimestampField = TRUE;

  /**
   * {@inheritdoc}
   */
  protected $configurableValueType = FALSE;

  /**
   * Gets conditions to be used in the select query.
   *
   * @return array
   *   List of conditions where each condition is an associative array:
   *   - field: Name of the field to filter on. Configurable fields are
   *     supported using the field_name.column_name syntax.
   *   - value: The value to limit by, either an array or a scalar value.
   *   - operator: Any of the supported operators.
   */
  protected function getConditions() {
    return $this->sensorConfig
      ->getSetting('conditions', array());
  }

  /**
   * Gets the time field.
   *
   * @return string
   *   Time interval field.
   */
  protected function getTimeIntervalField() {
    return $this->sensorConfig
      ->getSetting('time_interval_field');
  }

  /**
   * Gets the time interval value.
   *
   * @return int
   *   Time interval value.
   */
  protected function getTimeIntervalValue() {
    return $this->sensorConfig
      ->getTimeIntervalValue();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['aggregation'] = array(
      '#type' => 'fieldset',
      '#title' => 'Time Aggregation',
      // Give the aggregation settings a high weight, so that they show up
      // after other configuration by default.
      '#weight' => 50,
    );
    $form['aggregation']['time_interval_field'] = array(
      '#type' => 'textfield',
      '#title' => t('Timestamp field'),
      '#default_value' => $this->sensorConfig
        ->getSetting('time_interval_field'),
      '#access' => $this->configurableTimestampField,
    );
    $form['aggregation']['time_interval_value'] = array(
      '#type' => 'select',
      '#title' => t('Interval'),
      '#options' => $this
        ->getTimeIntervalOptions(),
      '#description' => t('Select the time interval for which the results will be aggregated.'),
      '#default_value' => $this
        ->getTimeIntervalValue(),
    );

    // Always show the interval value if a timestamp field is forced, otherwise
    // add states so it is only visible if something is entered.
    if ($this->configurableTimestampField) {
      $form['aggregation']['time_interval_value']['#states'] = array(
        'invisible' => array(
          ':input[name="settings[aggregation][time_interval_field]"]' => array(
            'value' => "",
          ),
        ),
      );
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);

    /** @var \Drupal\monitoring\Form\SensorForm $sensor_form */
    $sensor_form = $form_state
      ->getFormObject();

    /** @var \Drupal\monitoring\SensorConfigInterface $sensor_config */
    $sensor_config = $sensor_form
      ->getEntity();

    // Copy time interval field & value into settings if the field is specified.
    if ($interval_field = $form_state
      ->getValue(array(
      'settings',
      'aggregation',
      'time_interval_field',
    ))) {
      $sensor_config->settings['time_interval_field'] = $interval_field;
      $interval_value = $form_state
        ->getValue(array(
        'settings',
        'aggregation',
        'time_interval_value',
      ));
      $sensor_config->settings['time_interval_value'] = $interval_value;

      // Remove UI structure originated settings leftover.
    }
    else {

      // For consistency, empty the field + value setting if no field defined.
      unset($sensor_config->settings['time_interval_field']);
      unset($sensor_config->settings['time_interval_value']);
    }
    unset($sensor_config->settings['aggregation']);
  }

  /**
   * Returns time interval options.
   *
   * @return array
   *   Array with time interval options, keyed by time interval in seconds.
   */
  protected function getTimeIntervalOptions() {
    $time_intervals = array(
      600,
      900,
      1800,
      3600,
      7200,
      10800,
      21600,
      32400,
      43200,
      64800,
      86400,
      172800,
      259200,
      604800,
      1209600,
      2419200,
    );
    $date_formatter = \Drupal::service('date.formatter');
    return array_map(array(
      $date_formatter,
      'formatInterval',
    ), array_combine($time_intervals, $time_intervals)) + array(
      0 => t('No restriction'),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultConfiguration() {
    $default_config = array(
      'value_type' => 'number',
    );
    return $default_config;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatabaseAggregatorSensorPluginBase::$configurableTimestampField protected property Allows plugins to control if a timestamp field can be configured. 2
DatabaseAggregatorSensorPluginBase::$configurableValueType protected property Allows plugins to control if the value type can be configured. Overrides SensorPluginBase::$configurableValueType
DatabaseAggregatorSensorPluginBase::buildConfigurationForm public function Form constructor. Overrides SensorPluginBase::buildConfigurationForm 2
DatabaseAggregatorSensorPluginBase::getConditions protected function Gets conditions to be used in the select query.
DatabaseAggregatorSensorPluginBase::getDefaultConfiguration public function Default configuration for a sensor. Overrides SensorPluginBase::getDefaultConfiguration 2
DatabaseAggregatorSensorPluginBase::getTimeIntervalField protected function Gets the time field.
DatabaseAggregatorSensorPluginBase::getTimeIntervalOptions protected function Returns time interval options.
DatabaseAggregatorSensorPluginBase::getTimeIntervalValue protected function Gets the time interval value.
DatabaseAggregatorSensorPluginBase::submitConfigurationForm public function Form submission handler. Overrides SensorPluginBase::submitConfigurationForm 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
SensorPluginBase::$pluginDefinition protected property The plugin implementation definition.
SensorPluginBase::$pluginId protected property The plugin_id.
SensorPluginBase::$sensorConfig protected property Current sensor config object.
SensorPluginBase::$services protected property
SensorPluginBase::addService public function Service setter. Overrides SensorPluginInterface::addService
SensorPluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides SensorPluginInterface::calculateDependencies 4
SensorPluginBase::create public static function Creates an instance of the sensor with config. Overrides SensorPluginInterface::create 7
SensorPluginBase::getConfigurableValueType public function Configurable value type. Overrides SensorPluginInterface::getConfigurableValueType
SensorPluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
SensorPluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
SensorPluginBase::getSensorId public function Gets sensor name (not the label). Overrides SensorPluginInterface::getSensorId
SensorPluginBase::getService public function @todo: Replace with injection Overrides SensorPluginInterface::getService
SensorPluginBase::isEnabled public function Determines if sensor is enabled. Overrides SensorPluginInterface::isEnabled
SensorPluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 2
SensorPluginBase::__construct function Instantiates a sensor object. 8
SensorPluginInterface::runSensor public function Runs the sensor, updating $sensor_result. 22
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.