Problem/Motivation

When a Watchdog Agreggator Sensor is added an exception appears The table does not exist in the database %database
It seems that no table(watchdog) is set by default.

Steps to reproduce:

Go to admin/config/system/monitoring/sensors/add
Select Watchdog aggregator plugin
Set a label
Click on save

Proposed resolution

Set the table watchdog before the sensor is saved.

Remaining tasks

Review patch, commit.

User interface changes

None

API changes

None

Data model changes

None

Comments

giancarlosotelo created an issue. See original summary.

giancarlosotelo’s picture

Title: Error when a Wwtchdog sensor is tried to be added. » Error when a Watchdog sensor is tried to be added.
Assigned: Unassigned » giancarlosotelo
berdir’s picture

Sounds like that's my fault. I wasn't aware that the watchdog sensor is addable.

This was broken by #2543886: Allow database sensor plugins to control which configuration elements are shown. We need what we discussed there, the concept of default configuration so that when you add a new one of a given type, it starts with that set of settings. We will need to discuss this.

giancarlosotelo’s picture

Status: Active » Needs review
StatusFileSize
new2.41 KB

Here is a patch adding a default configuration. I am wondering if we need to create test for this issue.

miro_dietiker’s picture

Status: Needs review » Needs work

;-)

No reason to wonder. It's a bug and it has no test coverage. It definitively needs test coverage by our definition of done.
In cases of a bug, i recommend you to once start with writing the (failing) test first and then start writing code to fix the test.

miro_dietiker’s picture

  1. +++ b/src/Form/SensorForm.php
    @@ -237,6 +237,12 @@ class SensorForm extends EntityForm {
    +    if (!empty($plugin->getDefaultConfiguration())) {
    +      $default_config = $plugin->getDefaultConfiguration();
    

    Don't call this twice.

  2. +++ b/src/Plugin/monitoring/SensorPlugin/WatchdogAggregatorSensorPlugin.php
    @@ -50,4 +50,15 @@ class WatchdogAggregatorSensorPlugin extends DatabaseAggregatorSensorPlugin impl
    +       'table' => 'watchdog',
    +       'time_interval_field' => 'timestamp',
    

    Wrong indentation.

  3. +++ b/src/SensorPlugin/SensorPluginInterface.php
    @@ -30,6 +30,14 @@ interface SensorPluginInterface extends PluginInspectionInterface, PluginFormInt
    +  public function getDefaultConfiguration();
    

    Mind the core ConfigurablePluginInterface. Should we reuse it here?

berdir’s picture

3. No, we can't. That assumes configuration is an array, but we use the sensor config entity directly. Would be weird to mix that.

giancarlosotelo’s picture

Testonly patch and patch with comments on #6 and test

giancarlosotelo’s picture

Status: Needs work » Needs review
juanse254’s picture

+++ b/src/Tests/MonitoringUITest.php
@@ -177,6 +177,16 @@ class MonitoringUITest extends MonitoringTestBase {
+
+    $this->drupalPostForm(NULL, array(), t('Save'));
+    $this->assertText(SafeMarkup::format('Sensor @label saved.', array('@label' => 'Watchdog Sensor')));

Empty line here, and the SafeMarkup::format is no needed here, guess that t() should be enough. have a look at

juanse254’s picture

giancarlosotelo’s picture

Status: Needs work » Needs review
StatusFileSize
new511 bytes
new3.14 KB

Done and removed @label, it probably is not worth it.

miro_dietiker’s picture

Status: Needs review » Needs work
  1. +++ b/src/Form/SensorForm.php
    @@ -237,6 +237,11 @@ class SensorForm extends EntityForm {
    +    if ($default_config = $plugin->getDefaultConfiguration()) {
    +      $form_state->setValue(['settings', 'table'], $default_config['table']);
    +      $form_state->setValue(['settings', 'aggregation', 'time_interval_field'], $default_config['time_interval_field']);
    
    +++ b/src/Plugin/monitoring/SensorPlugin/WatchdogAggregatorSensorPlugin.php
    @@ -50,4 +50,15 @@ class WatchdogAggregatorSensorPlugin extends DatabaseAggregatorSensorPlugin impl
    +    $default_config = [
    +      'table' => 'watchdog',
    +      'time_interval_field' => 'timestamp',
    

    Use the same structure in default configuration as in the settings. Just set the entity settings and it should work.

  2. +++ b/src/Tests/MonitoringUITest.php
    @@ -177,6 +177,15 @@ class MonitoringUITest extends MonitoringTestBase {
    +    // Test the creation of a Watchdog sensor.
    

    Plz mention that we are testing default configuration here.

giancarlosotelo’s picture

Status: Needs work » Needs review
StatusFileSize
new1.72 KB
new3.07 KB

Done, much better now.

miro_dietiker’s picture

Status: Needs review » Needs work

Much better. Almost. ;-)

+++ b/src/Form/SensorForm.php
@@ -237,6 +237,10 @@ class SensorForm extends EntityForm {
+    if ($default_config = $plugin->getDefaultConfiguration()) {
+      $form_state->setValue('settings', $default_config);
+    }

This should not be in validateForm().
Default config is about default values, before the form is displayed. So the form should represent the default values.

Set the entity settings on the entity in form() around:

  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);

    $sensor_config = $this->entity;
giancarlosotelo’s picture

StatusFileSize
new849 bytes
new3.17 KB

Ok I change the location of the default config and it has to be after .

if (isset($sensor_config->plugin_id) && $plugin = $sensor_config->getPlugin())

Because a selected plugin is needed and the settings have to be set in that way.

giancarlosotelo’s picture

Status: Needs work » Needs review
miro_dietiker’s picture

Status: Needs review » Needs work

Exactly. Now one last thing more to make test coverage happy.

+++ b/src/Tests/MonitoringUITest.php
@@ -177,6 +177,15 @@ class MonitoringUITest extends MonitoringTestBase {
+    $this->drupalGet('admin/config/system/monitoring/sensors/add');

Here you should check that the default values are filled out in the form. :-)

berdir’s picture

Sorry, but miro is wrong :)

Your code *overwrites* existing settings every time the form is displayed.

As discussed, the correct place to set this is submitSelectPlugin(), which is where the plugin *changes*. Then we need to reset the settings back to the defaults of that plugin type. Always, even if they are empty.

Also, you can't test this in the UI since those settings aren't visible. You need to actually run the sensor to test it.

giancarlosotelo’s picture

Status: Needs work » Needs review
StatusFileSize
new1.52 KB
new3.13 KB

Well now it is in the right place and also small fix with the timestamp to be displayed in the right way (was wrong).

giancarlosotelo’s picture

StatusFileSize
new1.9 KB
new3.56 KB

Added an assert after the sensor is created in test and a better approach to set default configuration.

giancarlosotelo’s picture

StatusFileSize
new541 bytes
new3.56 KB

An extra space removed.

juanse254’s picture

The code seems fine, still waiting for the test to pass.

miro_dietiker’s picture

Much nice now. Monitoring HEAD is broken, so we can wait forever until this is green... ;-)

Status: Needs review » Needs work

The last submitted patch, 22: default_config_added-2556621-22.patch, failed testing.

miro_dietiker’s picture

Status: Needs work » Fixed

Awesome, working default configuration with nice test coverage!
Fails unrelated, the UI test passes.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

The last submitted patch, 4: default_config_added-2556621-4.patch, failed testing.

The last submitted patch, 8: default_config_added-2556621ONLYTEST.patch, failed testing.

The last submitted patch, 8: default_config_added-2556621-7.patch, failed testing.

The last submitted patch, 12: default_config_added-2556621-12.patch, failed testing.

The last submitted patch, 14: default_config_added-2556621-14.patch, failed testing.

The last submitted patch, 16: default_config_added-2556621-16.patch, failed testing.

The last submitted patch, 20: default_config_added-2556621-19.patch, failed testing.

The last submitted patch, 21: default_config_added-2556621-21.patch, failed testing.

The last submitted patch, 4: default_config_added-2556621-4.patch, failed testing.

The last submitted patch, 8: default_config_added-2556621ONLYTEST.patch, failed testing.

The last submitted patch, 8: default_config_added-2556621-7.patch, failed testing.

The last submitted patch, 12: default_config_added-2556621-12.patch, failed testing.

The last submitted patch, 14: default_config_added-2556621-14.patch, failed testing.

The last submitted patch, 16: default_config_added-2556621-16.patch, failed testing.

The last submitted patch, 20: default_config_added-2556621-19.patch, failed testing.

The last submitted patch, 21: default_config_added-2556621-21.patch, failed testing.

Status: Closed (fixed) » Needs work

The last submitted patch, 22: default_config_added-2556621-22.patch, failed testing.

giancarlosotelo’s picture

Status: Needs work » Closed (fixed)