Problem/Motivation

#1446932: Improve statistics performance by adding a swappable backend deprecated statistics_get() before deprecation testing was a thing and it never removed all the usages.

Proposed resolution

  • Remove all usages
  • Add @trigger_error
  • Add a test

Remaining tasks

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

N/a

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alexpott created an issue. See original summary.

alexpott’s picture

Status: Active » Needs review
FileSize
6.94 KB
timmillwood’s picture

Status: Needs review » Reviewed & tested by the community

The long @expectedDeprecation feels wrong, but it looks logically correct. 😉

Looks fine to me.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 2: 3068060-2.patch, failed testing. View results

timmillwood’s picture

Ah yes, for backwards compatibility statistics_get returns an array, where as the newer service returns a StatisticsViewsResult object.

alexpott’s picture

Status: Needs work » Needs review
FileSize
5.9 KB
8.56 KB

Ah the return values are not the same.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Except nit (which probably out of scope) it looks good

+++ b/core/modules/statistics/statistics.tokens.inc
@@ -40,25 +40,22 @@ function statistics_tokens($type, $tokens, array $data, array $options, Bubbleab
+        $replacements[$original] = \Drupal::service('date.formatter')->format($statistics->getTimestamp());

maybe move service access out of loop?

alexpott’s picture

@timmillwood yep this patch has nothing to do with the \Drupal::service('date.formatter')->format() part so shouldn't be touching that. Thanks for the reviews :)

catch’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/statistics/statistics.tokens.inc
@@ -40,25 +40,22 @@ function statistics_tokens($type, $tokens, array $data, array $options, Bubbleab
+    $statistics = \Drupal::service('statistics.storage.node')->fetchView($node->id());

This is fetching the statistics for the node up front, even if none of these tokens are used. We should just do a 1-1 replacement of the existing logic instead.

alexpott’s picture

Status: Needs work » Needs review
FileSize
1.86 KB
8.66 KB

Sure we can do that. I guess it means multiple queries if multiple tokens are used. But that seems an acceptable trade off. And this patch shouldn't be making any changes on that scale.

alexpott’s picture

+++ b/core/modules/statistics/statistics.tokens.inc
@@ -40,25 +40,22 @@ function statistics_tokens($type, $tokens, array $data, array $options, Bubbleab
+    /** @var \Drupal\statistics\StatisticsStorageInterface $stats_storage */
+    $stats_storage = \Drupal::service('statistics.storage.node');

Note this doesn't result in any queries - just getting an object from the container.

mikelutz’s picture

Berdir’s picture

  1. +++ b/core/modules/statistics/statistics.tokens.inc
    @@ -40,25 +40,22 @@ function statistics_tokens($type, $tokens, array $data, array $options, Bubbleab
     
       if ($type == 'node' & !empty($data['node'])) {
         $node = $data['node'];
    -
    +    /** @var \Drupal\statistics\StatisticsStorageInterface $stats_storage */
    +    $stats_storage = \Drupal::service('statistics.storage.node');
         foreach ($tokens as $name => $original) {
    

    Yeah, this is still a tiny overhead since most calls to this probably don't hav statistics tokens, but that's not going to be measurable.

  2. +++ b/core/modules/statistics/tests/src/Functional/StatisticsTokenReplaceTest.php
    index 56e18eefad..556593fea6 100644
    --- a/core/modules/statistics/tests/src/Functional/Views/IntegrationTest.php
    
    --- a/core/modules/statistics/tests/src/Functional/Views/IntegrationTest.php
    +++ b/core/modules/statistics/tests/src/Functional/Views/IntegrationTest.php
    
    +++ b/core/modules/statistics/tests/src/Functional/Views/IntegrationTest.php
    @@ -78,11 +78,15 @@ public function testNodeCounterIntegration() {
    -    $expected = statistics_get($this->node->id());
    -    // Convert the timestamp to year, to match the expected output of the date
    -    // handler.
    -    $expected['timestamp'] = date('Y', $expected['timestamp']);
    -
    +    /** @var \Drupal\statistics\StatisticsViewsResult $statistics */
    +    $statistics = \Drupal::service('statistics.storage.node')->fetchView($this->node->id());
    +    $expected = [
    +      'totalcount' => $statistics->getTotalCount(),
    +      'daycount' => $statistics->getDayCount(),
    +      // Convert the timestamp to year, to match the expected output of the date
    +      // handler.
    +      'timestamp' => date('Y', $statistics->getTimestamp()),
    +    ];
         foreach ($expected as $field => $value) {
           $xpath = "//div[contains(@class, views-field-$field)]/span[@class = 'field-content']";
           $this->assertFieldByXpath($xpath, $value, "The $field output matches the expected.");
    

    Hm. Feels like it would actually be easier and less code to just drop the foreach and have 3 different xpath + assertFieldByXpath() lines? Was a neat trick when we had an array with (mostly) matching keys/values, but seems weird now.

    I also just noticed that there is a second loop below, but that just checks that the fields don't exist at all, so the specific value shouldn't matter and we could simplify that too? Multiple combined contains() for example like https://stackoverflow.com/questions/12152890/xpath-contains-one-of-multi...?

alexpott’s picture

Updated the test. It changes the test to be more inline with a functional test and test what the user sees rather than the underlying HTML. Makes for a simpler and easier to grok test.

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

Works for me :)

catch’s picture

Status: Reviewed & tested by the community » Fixed

  • catch committed af4492f on 8.8.x
    Issue #3068060 by alexpott, timmillwood, Berdir: Properly deprecate...
catch’s picture

Status: Fixed » Closed (fixed)

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