Problem/Motivation

The verbose extra fields have been implemented for the DB sensor with a text area, using a line per verbose extra field
#2495089: Entity aggregator: Verbose extra fields

Proposed resolution

Should we use separate text fields per extra field with an "Add item" pattern?

As an example, the entity aggregator could offer a dropdown instead of a free text field.

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#55 separate_text_fields-2525432-55-interdiff.txt975 bytesberdir
#55 separate_text_fields-2525432-55.patch29.45 KBberdir
#53 separate_text_fields-2525432-51-interdiff.txt902 bytesberdir
#53 separate_text_fields-2525432-51.patch28.69 KBberdir
#46 Screen Shot 2015-07-24 at 15.39.40.png23.91 KBLKS90
#45 separate_text_fields-2525432-45.patch27.32 KBLKS90
#45 interdiff_39-45.txt3.1 KBLKS90
#40 interdiff_35-39.txt3.08 KBLKS90
#39 separate_text_fields-2525432-39.patch26.63 KBLKS90
#35 separate_text_fields-2525432-35.patch23.54 KBLKS90
#35 interdiff_31-35.txt2.68 KBLKS90
#31 interdiff_29-31.txt2.9 KBLKS90
#31 separate_text_fields-2525432-31.patch23.59 KBLKS90
#29 interdiff_27-29.txt774 bytesLKS90
#29 separate_text_fields-2525432-29.patch23.1 KBLKS90
#27 interdiff_22-27.txt2.84 KBLKS90
#27 separate_text_fields-2525432-27.patch23.12 KBLKS90
#22 separate_text_fields-2525432-22.patch23.12 KBLKS90
#22 interdiff_20-22.txt1.89 KBLKS90
#20 interdiff_18-20.txt2.95 KBLKS90
#20 separate_text_fields-2525432-20.patch21.23 KBLKS90
#18 interdiff_16-18.txt5.84 KBLKS90
#18 separate_text_fields-2525432-18.patch21.9 KBLKS90
#16 separate_text_fields-2525432-16.patch21.25 KBLKS90
#14 interdiff_12-14.txt1.82 KBLKS90
#14 separate_text_fields-2525432-14.patch21.18 KBLKS90
#12 interdiff_10-12.txt16.12 KBLKS90
#12 separate_text_fields-2525432-12.patch19.55 KBLKS90
#10 separate_text_fields-2525432-10.patch9.14 KBLKS90
#6 interdiff_3-6.txt7.53 KBLKS90
#6 separate_text_fields-2525432-6.patch9.14 KBLKS90
#3 Screen Shot 2015-07-08 at 13.04.17.png19.84 KBLKS90
#1 separate_text_fields-2525432-1.patch16.33 KBLKS90

Comments

LKS90’s picture

Status: Active » Needs review
StatusFileSize
new16.33 KB

Here is a start, no text area anymore, validation has moved to the validateConfigurationForm() function, renamed all 'keys' instances to 'fields'. Adding and deleting fields is possible, fields are validated before saving, so requesting a non-existing field won't break the whole sensor.

miro_dietiker’s picture

Status: Needs review » Needs work

UI issue, so screenshot please.

LKS90’s picture

Issue summary: View changes
StatusFileSize
new19.84 KB

Here is a screenshot:

LKS90’s picture

Status: Needs work » Needs review
miro_dietiker’s picture

Status: Needs review » Needs work

Already needs a reroll.

  1. +++ b/config/install/monitoring.sensor_config.user_failed_logins.yml
    @@ -16,7 +16,7 @@ settings:
    -  keys:
    +  fields:
    

    Dunno if i'm happy with the new key. :-)

  2. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -273,24 +273,27 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    +        'Add fields to filter the verbose output.'
    

    Filter what?

  3. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -348,6 +351,38 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    +    $fields = $settings['fields'];
    +    if (empty($fields)) {
    +      $fields = [];
    

    We can expect valid defaults. Remove this health check / fix.

  4. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -348,6 +351,38 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    +      '#value' => t('Add more fields'),
    

    The core pattern is more "Add another xyz". So we should switch and also alter the other form button for a similar option on this form. ;-)

  5. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -470,6 +519,30 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    +    foreach ($fields as $key => $field) {
    ...
    +        $settings['fields'] = [$field['field']];
    +        $this->sensorConfig->set('settings', $settings);
    ...
    +          $this->getQuery()->range(0, 1)->execute();
    

    This is inacceptable. You temporarily set the sensor settings to a "wrong" value just for being able to execute the query to validate it... sensorConfig is then in an invalid state!

LKS90’s picture

Status: Needs work » Needs review
StatusFileSize
new9.14 KB
new7.53 KB

1. Back to keys it is.

2. I went over all comments and checked if they make sense/are still copy&paste.

3. Removed this health check as well as the equivalent for the conditions.

4. Changed the button accordingly for keys and conditions.

5. $database->schema()->fieldExists($table, $key_name) is way easier than configuring a sensor and running it. Also added validation for the table (because I use that for later validation).

The last submitted patch, 1: separate_text_fields-2525432-1.patch, failed testing.

The last submitted patch, 1: separate_text_fields-2525432-1.patch, failed testing.

The last submitted patch, 1: separate_text_fields-2525432-1.patch, failed testing.

LKS90’s picture

StatusFileSize
new9.14 KB

Rebased, and this time the issue and the patch belong together.

miro_dietiker’s picture

Status: Needs review » Needs work

5. That is one option. But schema is not complete (Berdir said)! Best is to check Schema and if it does not work, try running the query.

As mentioned in the related issue: i was not happy with keys => fields since it's about verbose only. I would be more for verbose_fields or similar.

LKS90’s picture

Status: Needs work » Needs review
StatusFileSize
new19.55 KB
new16.12 KB

New version, switched to verbose_fields, re-added try/catch block which runs a query on that table with the field (and it's a simple query now instead of running a modified sensor).

Status: Needs review » Needs work

The last submitted patch, 12: separate_text_fields-2525432-12.patch, failed testing.

LKS90’s picture

Status: Needs work » Needs review
StatusFileSize
new21.18 KB
new1.82 KB

Corrected the schema, should have run a test locally first : /.

miro_dietiker’s picture

Status: Needs review » Needs work
  1. +++ b/config/schema/monitoring.schema.yml
    @@ -122,6 +122,24 @@ monitoring.settings.entity_aggregator:
    +    conditions:
    ...
    +    fields:
    
    +++ b/modules/test/config/install/monitoring.sensor_config.watchdog_aggregate_test.yml
    @@ -9,7 +9,7 @@ settings:
    +  verbose_fields:
    
    +++ b/modules/test/config/optional/monitoring.sensor_config.entity_aggregate_test.yml
    @@ -15,6 +15,9 @@ settings:
    +  fields:
    +    - id
    +    - label
    

    fields?

  2. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -464,12 +509,31 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    +    foreach ($fields as $key => $field) {
    ...
    +            $query->range(0, 1)->execute();
    

    You are now executing the whole query for each field (without schema) again and again...

  3. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -464,12 +509,31 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    +          try{
    

    style

LKS90’s picture

StatusFileSize
new21.25 KB

Not finished, just uploading a patch so I can continue from home :).

Edit: Some feedback for the validation would be nice though :). (reverting the fields is unnecessary, as the form won't continue until the form_state is error free)

miro_dietiker’s picture

  1. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -240,7 +233,7 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    -      '#default_value' => $this->sensorConfig->getSetting('table'),
    +      '#default_value' => $settings['table'],
    
    @@ -270,30 +263,8 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    -    $conditions = $this->sensorConfig->getSetting('conditions');
    ...
    +    $conditions = $settings['conditions'];
    

    Yeah, $settings is unused, but i think we should consistently use getSetting($key) and not do $settings array access.

  2. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -398,24 +419,13 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    +    foreach ($form_state->getValue('verbose_fields') as $delta => $field) {
    

    Drop $delta.

  3. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -457,6 +497,11 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    +    if (!$database->schema()->tableExists($table)) {
    +      $form_state->setErrorByName('settings][table', t('The table %table does not exist in the database %database', ['%table' => $table, '%database' => $database->getConnectionOptions()['database']]));
    

    Hm, if schema is incomplete, will it even contain that table or just miss fields?

  4. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -464,12 +509,33 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    +        $new_fields[] = (array_pop($fields[$key]));
    ...
    +    catch (\Exception $e) {
    ...
    +      $new_fields = $old_fields;
    

    So in case of a wrong line, all verbose field changes are dropped? Bad. I thought we just drop the single change.

LKS90’s picture

Status: Needs work » Needs review
StatusFileSize
new21.9 KB
new5.84 KB

Removed the $settings array and the usage where possible (I use it once to save the submitted settings), removed the unused delta, made another query for the table in case the schema says the table doesn't exist, fixed the fields query.

miro_dietiker’s picture

Status: Needs review » Needs work
+++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
@@ -464,12 +512,34 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
+    foreach ($fields as $key => $field) {
...
+        $new_fields[] = (array_pop($fields[$key]));
...
+            unset($new_fields[$key]);

You should not do an array_pop in a foreach.

Anyway, instead of adding early and later unsetting, just add the field after all checks passed.

LKS90’s picture

Status: Needs work » Needs review
StatusFileSize
new21.23 KB
new2.95 KB

Removed the array_pop (the whole line was rather pointless) and cleaned up the validation/saving of new fields.

Status: Needs review » Needs work

The last submitted patch, 20: separate_text_fields-2525432-20.patch, failed testing.

LKS90’s picture

Status: Needs work » Needs review
StatusFileSize
new1.89 KB
new23.12 KB

Fixed the test fails which are related to this issue in MonitroingCoreWebTest. Couldn't reproduce the payment test fail though.

Status: Needs review » Needs work

The last submitted patch, 22: separate_text_fields-2525432-22.patch, failed testing.

Status: Needs work » Needs review

The last submitted patch, 16: separate_text_fields-2525432-16.patch, failed testing.

mbovan’s picture

Status: Needs review » Needs work
  1. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -127,10 +120,10 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    +    $fields = $this->sensorConfig->getSetting('verbose_fields');
    +    if (!empty($fields)) {
    +      foreach ($this->sensorConfig->getSetting('verbose_fields') as $field) {
    +        $query->addField($this->sensorConfig->getSetting('table'), $field);
           }
         }
    

    Why not use $fields in foreach?

  2. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -361,6 +331,57 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    +    $form['output_table'] = array(
    ...
    +      '#tree' => FALSE,
    +    );
    

    Is it #tree false by default?

  3. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -361,6 +331,57 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    +      '#empty' => t(
    +        'Add keys to display in the verbose output.'
    +      ),
    

    One line?

  4. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -398,37 +419,24 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    -    $settings['conditions'] = [];
    +    $conditions = [];
    ...
    -        $settings['conditions'][] = $condition;
    +        $conditions[] = $condition;
    ...
    +    $settings['conditions'] = $conditions;
    

    Do we need this change? If we do like before then we don't need $settings['conditions'] = $conditions; line.

  5. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -398,37 +419,24 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    +    $verbose_fields = [];
    +    foreach ($form_state->getValue('verbose_fields') as $field) {
    +      if (!empty($field['field'])) {
    +        $verbose_fields[] = $field['field'];
    +      }
         }
    ...
    +    $settings['verbose_fields'] = $verbose_fields;
    

    Same here, you can use $settings['verbose_fields'][] instead of $verbose_fields[].

  6. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -477,10 +526,26 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    -          t('The specified time interval field %name does not exist.', array('%name' => $field_name)));
    +          t('The specified time interval field %name does not exist in table %table.', array('%name' => $field_name, $table)));
    

    %table array key is missing.

LKS90’s picture

Status: Needs work » Needs review
StatusFileSize
new23.12 KB
new2.84 KB

1. Done

2. It's not, removing it breaks saving of fields.

3. Done

4. & 5. Done

6. Done

mbovan’s picture

Re #27 (3): You changed different empty message. That's good, but this one is still here. :)

+++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
@@ -361,6 +329,57 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
+      '#empty' => t(
+        'Add keys to display in the verbose output.'
+      ),
LKS90’s picture

StatusFileSize
new23.1 KB
new774 bytes

Thanks! Fixed the second #empty message.

miro_dietiker’s picture

Status: Needs review » Needs work
  1. +++ b/modules/test/config/optional/monitoring.sensor_config.entity_aggregate_test.yml
    @@ -18,6 +18,9 @@ settings:
    +    - id
    
    +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -361,6 +329,55 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    +        'field' => array(
    

    This key level seems unneeded.

  2. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -397,12 +414,7 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    +    $settings = $this->sensorConfig->getSettings();
    

    Oh wow, is this item now always properly updated? I remember we needed the different code to get access to the entity updated by the form state / submissions...

  3. +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php
    @@ -412,23 +424,14 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase
    -    // Update the verbose output keys.
    

    Don't drop the comment.

LKS90’s picture

Status: Needs work » Needs review
StatusFileSize
new23.59 KB
new2.9 KB

Readded the comment, removed one empty line in a docblock and changed the header for the verbose fields and conditions table slightly.

The 'field' nesting is necessary because every verbose field is a row in a table with only one column (the 'field' column). I tried helping alleviate the confusion by adding a comment and renaming the column field to field_key (so the key for a field is the content of that column). I also changed the header for the conditions to also say 'Field key', so both are consistent.

Status: Needs review » Needs work

The last submitted patch, 31: separate_text_fields-2525432-31.patch, failed testing.

The last submitted patch, 31: separate_text_fields-2525432-31.patch, failed testing.

LKS90’s picture

Status: Needs work » Needs review
StatusFileSize
new2.68 KB
new23.54 KB

The tests did need some updating with the new column key, couldn't reproduce in the beginning because I was in the completly wrong branch... :D

Here is the patch with the updated tests.

miro_dietiker’s picture

When i create a new sensor, i still only get "id" and "label" is missing as verbose field.

miro_dietiker’s picture

Also the conceptual fields like id / label / ... are not on the field list, making me confused as a user.
We should list them separately.

miro_dietiker’s picture

Status: Needs review » Needs work

I tried to add a "path" field to a node sensor. Column was empty and when executing, the sensor has thrown an error.

Notice: Undefined index: default_formatter in Drupal\Core\Field\FormatterPluginManager->prepareConfiguration() (line 155 of core/lib/Drupal/Core/Field/FormatterPluginManager.php).
Drupal\Core\Field\FormatterPluginManager->prepareConfiguration('path', Array)
Drupal\Core\Entity\EntityDisplayBase->setComponent('path', Array)
Drupal\Core\Entity\EntityViewBuilder->getSingleFieldDisplay(Object, 'path', Array)
Drupal\Core\Entity\EntityViewBuilder->viewField(Object, Array)
LKS90’s picture

Status: Needs work » Needs review
StatusFileSize
new26.63 KB

When i create a new sensor, i still only get "id" and "label" is missing as verbose field.

Stupid mistake, fixed.

Also the conceptual fields like id / label / ... are not on the field list, making me confused as a user.
We should list them separately.

I put the synonymous key in brackets after the field now, see screenshot.

I tried to add a "path" field to a node sensor. Column was empty and when executing, the sensor has thrown an error.

Finally managed to fix that according to berdir's proposal, I check if there is a field default_formatter with the FieldTypePluginManager. If not, I don't try and 'view()' the field but keep the $value as set previously ($entity->$field->$property).

LKS90’s picture

StatusFileSize
new3.08 KB

Here is the interdiff, thought I clicked on upload, apparently not.

Status: Needs review » Needs work

The last submitted patch, 39: separate_text_fields-2525432-39.patch, failed testing.

The last submitted patch, 39: separate_text_fields-2525432-39.patch, failed testing.

berdir’s picture

  1. +++ b/src/Plugin/monitoring/SensorPlugin/EntityAggregatorSensorPlugin.php
    @@ -243,8 +245,15 @@ class EntityAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase im
    +            $ui_definitions = $field_type_manager->getUiDefinitions();
    +            $value = $entity->$field->$property;
    +            if (!empty($ui_definitions[$field_type])) {
    +              if (isset($ui_definitions[$field_type]['default_formatter'])) {
    

    I don't think you need UiDefinitions() here ? Those are already filtered, so path will never even be in there.

    Instead, you could just write if (isset($field_type_manager->getDefinition($field_type)['default_formatter']

  2. +++ b/src/Plugin/monitoring/SensorPlugin/EntityAggregatorSensorPlugin.php
    @@ -414,16 +423,24 @@ class EntityAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase im
    +    $keys = array_flip($entity_type->getKeys());
    +    foreach ($available_fields as $delta => $field) {
    +      if (isset($keys[$field])) {
    +        if ($field != $keys[$field]) {
    +          $available_fields[$delta] = $field . ' (' . $keys[$field] . ')';
    +        }
    +      }
    +    }
    

    Do we really support all the keys her? Stuff like langcode and status?

    I'd just hardcode those that you explicitly support.

LKS90’s picture

Status: Needs work » Needs review
StatusFileSize
new3.1 KB
new27.32 KB

1. Done

2. Except for Changed which caused some problems (I could add a case for changed and handle it correctly though) all fields seemed fined. I switched to getting the keys from the entity_type directly. Less keys are displayed now (but more are supported).

There is another problem though with some entities: File for example has for key label the field filename, the field label has a special case for rendering a link, the File doesn't have a canonical Url though and getUrlInfo() does not work. I fixed it now by switching the from field key to the field itself in case that is possible (as it is with File), if there is no field set for the key label, I use the case 'label': instead of the default case which renders the linked label to the entity.

LKS90’s picture

StatusFileSize
new23.91 KB

Here is the screenshot I was talking about up there, it's from the latest version with less keys.

LKS90’s picture

Status: Needs review » Postponed

Status: Postponed » Needs work

The last submitted patch, 45: separate_text_fields-2525432-45.patch, failed testing.

The last submitted patch, 45: separate_text_fields-2525432-45.patch, failed testing.

miro_dietiker’s picture

miro_dietiker’s picture

Priority: Minor » Major

From minor to major: This contains a config schema change. ;-)

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new28.69 KB
new902 bytes

Not so sure about the direction of the last patches, we lost all the real fields.

I dropped the keys stuff almost completely. Instead, just hardcoding id and label as suggested above. We have a small overlap but that makes defaults a lot easier. Also simplified the label case a bit and made sure it doesn't fail when we don't have a canonical link template.

We have quite some checks and special cases now. And not that much test coverage for them.

I'm going to commit this when it is green. And I'll create a follow-up to test creating sensors for nodes and files with all available fields and make sure the output is correct and doesn't fail anywhere.

Status: Needs review » Needs work

The last submitted patch, 53: separate_text_fields-2525432-51.patch, failed testing.

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new29.45 KB
new975 bytes

Fixed that test.

  • Berdir committed 9bc58cd on 8.x-1.x authored by LKS90
    Issue #2525432 by LKS90, Berdir, miro_dietiker: Separate text fields per...
berdir’s picture

Status: Needs review » Fixed

Committed.

Status: Fixed » Closed (fixed)

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