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
- class \Drupal\monitoring\SensorPlugin\SensorPluginBase implements SensorPluginInterface uses MessengerTrait, StringTranslationTrait
- class \Drupal\monitoring\SensorPlugin\DatabaseAggregatorSensorPluginBase
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\SensorPluginView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DatabaseAggregatorSensorPluginBase:: |
protected | property | Allows plugins to control if a timestamp field can be configured. | 2 |
DatabaseAggregatorSensorPluginBase:: |
protected | property |
Allows plugins to control if the value type can be configured. Overrides SensorPluginBase:: |
|
DatabaseAggregatorSensorPluginBase:: |
public | function |
Form constructor. Overrides SensorPluginBase:: |
2 |
DatabaseAggregatorSensorPluginBase:: |
protected | function | Gets conditions to be used in the select query. | |
DatabaseAggregatorSensorPluginBase:: |
public | function |
Default configuration for a sensor. Overrides SensorPluginBase:: |
2 |
DatabaseAggregatorSensorPluginBase:: |
protected | function | Gets the time field. | |
DatabaseAggregatorSensorPluginBase:: |
protected | function | Returns time interval options. | |
DatabaseAggregatorSensorPluginBase:: |
protected | function | Gets the time interval value. | |
DatabaseAggregatorSensorPluginBase:: |
public | function |
Form submission handler. Overrides SensorPluginBase:: |
2 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
SensorPluginBase:: |
protected | property | The plugin implementation definition. | |
SensorPluginBase:: |
protected | property | The plugin_id. | |
SensorPluginBase:: |
protected | property | Current sensor config object. | |
SensorPluginBase:: |
protected | property | ||
SensorPluginBase:: |
public | function |
Service setter. Overrides SensorPluginInterface:: |
|
SensorPluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides SensorPluginInterface:: |
4 |
SensorPluginBase:: |
public static | function |
Creates an instance of the sensor with config. Overrides SensorPluginInterface:: |
7 |
SensorPluginBase:: |
public | function |
Configurable value type. Overrides SensorPluginInterface:: |
|
SensorPluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
|
SensorPluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
SensorPluginBase:: |
public | function |
Gets sensor name (not the label). Overrides SensorPluginInterface:: |
|
SensorPluginBase:: |
public | function |
@todo: Replace with injection Overrides SensorPluginInterface:: |
|
SensorPluginBase:: |
public | function |
Determines if sensor is enabled. Overrides SensorPluginInterface:: |
|
SensorPluginBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
2 |
SensorPluginBase:: |
function | Instantiates a sensor object. | 8 | |
SensorPluginInterface:: |
public | function | Runs the sensor, updating $sensor_result. | 22 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |