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.

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.

| Comment | File | Size | Author |
|---|---|---|---|
| #41 | 3213927-41.patch | 5.69 KB | tonytheferg |
| Screenshot (201).png | 8.69 KB | tonytheferg | |
| Screenshot (200).png | 30.93 KB | tonytheferg |
Issue fork redirect_metrics-3213927
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:
Comments
Comment #2
tonytheferg commentedComment #3
tonytheferg commentedComment #4
tonytheferg commentedComment #5
tonytheferg commentedComment #6
tonytheferg commentedOk so this is simple enough.
We can just set the
$redirect->last_access->valueto0for new redirects: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.Comment #7
tonytheferg commentedComment #8
tonytheferg commentedHere is a patch built on the patch from this issue
You will need to run
drush cim --partial --source=modules/contrib/redirect_metrics/config/installto pull in the changes on the view.I thought about also changing
->setInitialValue(\Drupal::time()->getRequestTime())to->setInitialValue(0)in the forlast_accessin 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.
Comment #10
tonytheferg commentedComment #11
tonytheferg commentedComment #12
tonytheferg commentedI 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);Comment #13
tonytheferg commentedCurious if this fixes any of the broken tests...
Comment #14
tonytheferg commentedIf 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.Comment #15
tonytheferg commentedlooks like there is a problem with the tests?
And:
Comment #16
tonytheferg commentedSetting 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
0to a last access of0. The created date can be retained for when the redirect was created of course.Comment #17
tonytheferg commentedFixed the tests, and deprecated
FilterResponseEvent. Added a view page for never accessed do distinguish them from stale.Comment #18
tonytheferg commentedComment #20
tonytheferg commentedhere is one without the deprecation changes.
Comment #21
tonytheferg commentedComment #22
kala4ekUpdated patch to be compatible to latest dev version.
Comment #23
tonytheferg commentedThanks! 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.
Comment #24
tonytheferg commentedMaybe
NULLis better than0. 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.Comment #25
damienmckennaPatch #22 is 100% identical to #20 (which means there was no need to reroll it) and should be ignored.
Comment #26
tonytheferg commentedComment #27
tonytheferg commentedNULL 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_accessvalues with anaccess_countof0toNULL.Comment #28
tonytheferg commentedHere is a patch with the function.
Comment #29
tonytheferg commentedHere 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.
Comment #30
tonytheferg commentedWhoops. here you go.
2.x branch needs dev release and test parameters.
Comment #31
tonytheferg commented+ // Initial field values should be 0. Redirects should be considered0should be changed toNULLComment #32
tonytheferg commentedThis is better for the reports test: (lines 51-57)
Comment #33
tonytheferg commentedComment #37
joelpittetThis 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.
Comment #38
tonytheferg commentedThanks for the review!
Yeah, I should write an update hook.
Comment #39
tonytheferg commentedBumping 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 updbetc.All redirects that have an access count of
0will have the last_access timestamp set toNULL.Comment #41
tonytheferg commentedHere is a patch for those who prefer patches.
Comment #42
tonytheferg commentedComment #43
joelpittetI reviewed the addition, while it doesn't affect me it could help some scenarios. Thanks again @tonytheferg
Comment #44
mohit_aghera commentedPR 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.
Comment #45
mohit_aghera commentedComment #46
mohit_aghera commentedThanks 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
Comment #48
kala4ekBatch 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).