Skip to content

Garbage collection time window for URL Metrics posts could be increased and made customizable #1948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
westonruter opened this issue Mar 19, 2025 · 2 comments · Fixed by #1950
Assignees
Labels
[Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Enhancement A suggestion for improvement of an existing feature

Comments

@westonruter
Copy link
Member

westonruter commented Mar 19, 2025

When considering that a URL Metric can have an indefinite freshness TTL (#1940), where freshness then depends exclusively on the ETag, I think we should consider increasing the garbage collection time window to be greater than 1 month for od_url_metrics posts that haven't been updated:

/**
* Deletes posts that have not been modified in the past month.
*
* @since 0.1.0
*/
public static function delete_stale_posts(): void {
$one_month_ago = gmdate( 'Y-m-d H:i:s', strtotime( '-1 month' ) );
$query = new WP_Query(
array(
'post_type' => self::SLUG,
'posts_per_page' => 100,
'date_query' => array(
'column' => 'post_modified_gmt',
'before' => $one_month_ago,
),
)
);
foreach ( $query->posts as $post ) {
if ( $post instanceof WP_Post && self::SLUG === $post->post_type ) { // Sanity check.
wp_delete_post( $post->ID, true );
}
}
}

Consider sites that get very little traffic. Just because a page doesn't get visited once a month shouldn't result in the URL Metrics being purged. Maybe something like 3 months or 6 months would be more reasonable for the long tail of sites without much traffic.

The garbage collection TTL should probably have a filter to allow it to be customized (or disabled).

Note that if someone deletes a post there is no logic to attempt to delete any corresponding od_url_metrics posts, since there is no connection between the two. I don't think this is necessarily needed.

Docs will also need to be updated here:

When an `od_url_metrics` post has not been updated in a month then it is garbage-collected, since it is likely the original URL has gone away.

@westonruter westonruter added [Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Enhancement A suggestion for improvement of an existing feature labels Mar 19, 2025
@github-project-automation github-project-automation bot moved this to Not Started/Backlog 📆 in WP Performance 2025 Mar 19, 2025
@westonruter westonruter moved this from Not Started/Backlog 📆 to To Do 🔧 in WP Performance 2025 Mar 19, 2025
@hbhalodia
Copy link
Contributor

Hi @westonruter, I would like to pick this issue.

Just wanted some clarification, as suggested should we add the filter or provide some settings in the plugin, where we can set the time in months and just use that settings would be more customizable?

@westonruter
Copy link
Member Author

@hbhalodia The filter can be for the number of seconds, so it can default to something like 6 * MONTH_IN_SECONDS. ANd yes, it would be a filter, no settings UI.

@westonruter westonruter moved this from To Do 🔧 to In Progress 🚧 in WP Performance 2025 Mar 20, 2025
@github-project-automation github-project-automation bot moved this from In Progress 🚧 to Done 😃 in WP Performance 2025 Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Enhancement A suggestion for improvement of an existing feature
Projects
Status: Done 😃
2 participants