Problem/Motivation

Newly created redirects get a timestamp of their created date in last_access instead of 0 like was done in Drupal 7.

This gives a false reading of a last access even though the access_count is 0.
picture

This also is an issue when migrating sites that have redirects "never" been accessed. The value in the database for those is 0, and they migrate over that way just fine, but the view this module ships with tries to force a timestamp on them in the display.

For example I was getting Wed, 12/31/1969 - 7:00pm as a last access date for all the redirects that last_access of 0. I mentioned this in this comment.

This will also help in provide clarity in settings for deleting old aliases, in that it draws a distinction between redirects that were never accessed.

Since we have the Created date now anyway, there is no reason to timestamp the access until they actually get accessed.

Steps to reproduce

Add a new redirect. It has an access_count of 0, but the last_access is a timestamp from when it was created.

Proposed resolution

Set the value of a new redirect without a timestamp to 0 or NULL.

We should also conditionally rewrite the Last Accessed output in the view to "Never" when the last access value is 0 or NULL.

picture

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

  • 3213927 Comparechanges, plain diff MR !4
  • 1 hidden branch
  • 3213927- Comparecompare

Comments

tonytheferg created an issue. See original summary.

tonytheferg’s picture

Title: Support 0 with "never" in last_access for redirects with an access_count of 0 » Support 0 in last_access by displaying "never" for redirects with an access_count of 0
Issue summary: View changes
tonytheferg’s picture

Issue summary: View changes
tonytheferg’s picture

Title: Support 0 in last_access by displaying "never" for redirects with an access_count of 0 » Support 0 in last_access and display "never" for redirects with an access_count of 0
tonytheferg’s picture

Issue summary: View changes
tonytheferg’s picture

Ok so this is simple enough.

We can just set the $redirect->last_access->value to 0 for new redirects:

/**
 * Implements hook_ENTITY_TYPE_presave().
 */
function redirect_metrics_redirect_presave(Redirect $redirect) {
  // Initialise the default metrics when a new redirect is created.
  if ($redirect->isNew()) {
    // Set the access count if it's empty.
    if ($redirect->access_count->isEmpty()) {
      $redirect->access_count->value = 0;
    }
    // Set the access time if it's empty.
    if ($redirect->last_access->isEmpty()) {
      $redirect->last_access->value = 0;
    }
  }
}

Then use rewrite results of Redirect: Last accessed in the view so that Drupal doesn't try to put a date when the value is 0.

{% if last_access__value == "0"%}
   Never
{% else %}
   {{ last_access }}
{% endif %}
tonytheferg’s picture

Title: Support 0 in last_access and display "never" for redirects with an access_count of 0 » Set last_access->value to 0 for new redirects
tonytheferg’s picture

Status: Active » Needs review
Related issues: +#3205839: Metrics overridden when values set on save
StatusFileSize
new1.08 KB

Here is a patch built on the patch from this issue

You will need to run drush cim --partial --source=modules/contrib/redirect_metrics/config/install to pull in the changes on the view.

I thought about also changing ->setInitialValue(\Drupal::time()->getRequestTime()) to ->setInitialValue(0) in the for last_access in RedirectMetricsFieldDefinitions.php as well as I don't think time stamping a redirect as accessed, before it has been accessed is a good idea.

I can easily add that to the patch as well.

We will need to fix the tests. And maybe provide an update hook to set redirects last access that have an access count of 0 to zero.

Status: Needs review » Needs work
tonytheferg’s picture

Issue summary: View changes
tonytheferg’s picture

Issue summary: View changes
tonytheferg’s picture

I think in the test we need to change this line:

$this->assertEquals($test_request_time, $redirect->last_access->value);

to:
$this->assertEquals(0, $redirect->last_access->value);

tonytheferg’s picture

StatusFileSize
new1.68 KB

Curious if this fixes any of the broken tests...

tonytheferg’s picture

StatusFileSize
new2.06 KB

If I am looking at this right, $test_request_time = \Drupal::time()->getRequestTime(); is not needed in the tests if we are setting new redirects to 0.

tonytheferg’s picture

Status: Needs work » Needs review

looks like there is a problem with the tests?

There was 1 error:

1) Drupal\Tests\redirect_metrics\Functional\RedirectReportsTest::testRedirectReports
Behat\Mink\Exception\ResponseTextException: The text "foo" appears in the text of this page, but it should not.

And:

Unknown
fail: [run-tests.sh check] Line 0 of :
FATAL Drupal\Tests\redirect_metrics\Functional\RedirectReportsTest: test runner returned a non-zero error code (2).
tonytheferg’s picture

Category: Feature request » Bug report

Setting this to a bug report because it is a confusing UI/UX to mark a redirect that has never been accessed with a timestamp of having been accessed. This also causes issues in migrations, as I mentioned above.

We should write an update hook to set all redirects with a count of 0 to a last access of 0. The created date can be retained for when the redirect was created of course.

tonytheferg’s picture

StatusFileSize
new34.05 KB

Fixed the tests, and deprecated FilterResponseEvent. Added a view page for never accessed do distinguish them from stale.

tonytheferg’s picture

Status: Needs review » Needs work

The last submitted patch, 17: 3213927-17.patch, failed testing. View results

tonytheferg’s picture

Status: Needs work » Needs review
StatusFileSize
new32.35 KB

here is one without the deprecation changes.

tonytheferg’s picture

kala4ek’s picture

StatusFileSize
new32.35 KB

Updated patch to be compatible to latest dev version.

tonytheferg’s picture

Thanks! Can you provide an interdiff? I am not seeing what changed.

By the way, how is the patch working for you? It would be nice to get the maintainer to take a look at this issue.

Also might want an update hook to set redirects that have an access count of 0, to have a last access of 0 as well.

tonytheferg’s picture

Issue summary: View changes

Maybe NULL is better than 0. Either way, stale redirects can be calculated off the created date, so last access timestamp should only be populated when the redirect is actually accessed.

damienmckenna’s picture

Patch #22 is 100% identical to #20 (which means there was no need to reroll it) and should be ignored.

tonytheferg’s picture

Version: 8.x-1.x-dev » 2.0.1
tonytheferg’s picture

NULL would probably be better than zero so that we can use the default views output for empty instead of rewriting the field.

Also, an update script would be good to set last_access values with an access_count of 0 to NULL.

tonytheferg’s picture

StatusFileSize
new1.67 KB

Here is a patch with the function.

tonytheferg’s picture

StatusFileSize
new3.09 KB

Here are the basic tests added with NULL value.

The maintainer should decide what way this should be implemented in views so that those can be reworked if needed and so that tests can be rewritten.

tonytheferg’s picture

StatusFileSize
new4.76 KB

Whoops. here you go.

2.x branch needs dev release and test parameters.

tonytheferg’s picture

+ // Initial field values should be 0. Redirects should be considered

0 should be changed to NULL

tonytheferg’s picture

This is better for the reports test: (lines 51-57)

    // The popular report will show all aliases, ordered by the most used one.
    $this->drupalGet('admin/config/search/redirect/popular');
    $this->assertSession()->elementContains('css', 'tbody tr:first-child td:nth-child(2)', 'bar');
    $this->assertSession()->elementContains('css', 'tbody tr:first-child td:nth-child(6)', '5');
    $this->assertSession()->elementContains('css', 'tbody tr:last-child td:nth-child(2)', 'foo');
    $this->assertSession()->elementContains('css', 'tbody tr:last-child td:nth-child(5)', 'Never');
    $this->assertSession()->elementContains('css', 'tbody tr:last-child td:nth-child(6)', '0');
tonytheferg’s picture

Title: Set last_access->value to 0 for new redirects » Set last_access->value to 0 or NULL for new redirects

tonytheferg changed the visibility of the branch 3213927 to hidden.

tonytheferg changed the visibility of the branch 3213927 to active.

joelpittet’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new62.03 KB

This looks great, I needed to uninstall/re-install the module to get the new view code after patching (expected). And ran into #3420930: The field access_count has already been deleted and it is in the process of being purged. while doing the uninstall, but running cron to clean up the fields worked.

Screenshot of expected results

tonytheferg’s picture

Thanks for the review!
Yeah, I should write an update hook.

tonytheferg’s picture

Status: Reviewed & tested by the community » Needs review

Bumping back to NR for the update hook.

Should probably have a couple of people test it.

You have to install the module without the patch, then apply the patch, then run drush updb etc.
All redirects that have an access count of 0 will have the last_access timestamp set to NULL.

tonytheferg changed the visibility of the branch 3213927- to hidden.

tonytheferg’s picture

StatusFileSize
new5.69 KB

Here is a patch for those who prefer patches.

tonytheferg’s picture

joelpittet’s picture

Status: Needs review » Reviewed & tested by the community

I reviewed the addition, while it doesn't affect me it could help some scenarios. Thanks again @tonytheferg

mohit_aghera’s picture

PR and the implementation approach looks good to me, if possible can we address the phpcs related issues?
I understand that those are not related to the changes we did, however it would be good to address since those came across.
I can do it before merge otherwise.

mohit_aghera’s picture

mohit_aghera’s picture

Status: Reviewed & tested by the community » Fixed

Thanks everyone for the inputs and feedback.
I've merged this to 2.x and tagged for the releaes 2.0.2-rc1 https://www.drupal.org/project/redirect_metrics/releases/2.0.2-rc1

Status: Fixed » Closed (fixed)

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

kala4ek’s picture

Batch should be used for such update hooks. Because update took large amount of time for sites which have a lot of redirects (for example we have more then 46k).
Or instead of saving the whole entity, just simple sql query is preferable. Because during execution of the update hook site is unavailable (maintenance mode is enabled automatically when updates are executing).