Problem/Motivation

It would be great, if it was possible to reset the Drupal core Statistics counter enabled under admin/config/system/statistics via Drush. The alternative would be to uninstall and reinstall the Drupal core Statistics module, which might have unintended consequences.

Proposed resolution

Perhaps add something like drush core:statistics:reset?

Remaining tasks

Update the code, adding the Drush command.

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

ressa created an issue. See original summary.

cilefen’s picture

Status: Active » Closed (won't fix)

Hi @ressa,

That's an interesting idea but Drush feature requests are taken on the Drush project.

-Chris

ressa’s picture

Thanks for a fast reply @cilefen, but modules are free to define their own Drush commands. See for example Search API's Drush commands.

Or do you mean that Drupal core modules specifically won't be allowed to include Drush commands?

ressa’s picture

Status: Closed (won't fix) » Needs review
StatusFileSize
new2.99 KB

Here is a patch which can be used as a starting point, even if it isn't going to get implemented here, but has to be worked through as a feature requests on the Drush project.

The fields in the Statistics node_counter table are nid, totalcount, daycount and timestamp:

$ drush sql-query "SELECT * FROM node_counter"
1 3 3 1585666771
2 1 1 1585666767

$ drush statistics:reset:day
 [success] Statistics day count reset.

$ drush sql-query "SELECT * FROM node_counter"
1 3 0 1585666771
2 1 0 1585666767

$ drush statistics:reset:total
 [success] Statistics total count reset.

$ drush sql-query "SELECT * FROM node_counter"
1 0 0 1585666771
2 0 0 1585666767

Aliases

$ drush srd
 [success] Statistics day count reset.

$ drush srt
 [success] Statistics total count reset.

Status: Needs review » Needs work

The last submitted patch, 4: drupal_core_statistics-reset-counter-with-drush-3123772-4-D9.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

ressa’s picture

Status: Needs work » Needs review
StatusFileSize
new3.02 KB

I want at least to make it pass the test-bot :-)

msuthars’s picture

Assigned: Unassigned » msuthars
ressa’s picture

Another great improvement for the Drupal core Statistics counter would be adding more granular statistics, like visits per current week, month and year. That should probably be done in another issue, though.

ressa’s picture

cilefen’s picture

@ressa:

Fair enough, but there are evidently none as of today:

$ git grep 'DrushCommands' origin/9.1.x
# nothing

Also, the core project doesn't depend on drush/drush.

ressa’s picture

Yes @cilefen, I noticed that there are none yet. Also, the requirement for a custom Drush command of adding a composer.json in the root of a module is something to consider.

Perhaps adding the reset option as a button firstly ("Reset statistics" ?) and adding a Drush command which can trigger the action which clicking that "Reset statistics" button does is a viable plan?

ressa’s picture

Hi @cilefen: I have thought more about it, and you are probably right that it is a Drush issue.

I have updated the patch for Drupal 9 in #54798: "Clear all recorded statistics" button, since the correct way would probably be to have that ready, and then hook in to it with Drush.

msuthars’s picture

Assigned: msuthars » Unassigned
xjm’s picture

Version: 9.0.x-dev » 8.9.x-dev
Status: Needs review » Closed (won't fix)

Yep, Drupal core does not include Drush commands. This can be requested in the Drush queue instead. Thanks!

fgm’s picture

Project: Drupal core » Statistics
Version: 8.9.x-dev » 1.0.0
Component: statistics.module » Code
Status: Closed (won't fix) » Active

There is no such limitation now that the module is in contrib: reopening.

demma10’s picture

StatusFileSize
new3.22 KB

Attached is a patch ported to the contrib module and upgraded for Drupal 11.

Will create an MR against the repo when I get some time.

fgm’s picture

The drush.services.yml is a tad optimistic, don't you think ? Otherwise LGTM. I'll verify the MR when you submit it so CI can run it.