site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

dateRangeLength must be a positive integer dashboard error

Open jamesozzie opened this issue 5 years ago • 9 comments

Bug Description

There have been recent reports of the below error message, with one user stating this occurs when clicking on the Search console tab.

dateRangeLength must be a positive integer.

    in LegacySearchConsoleDashboardWidgetSiteStats
    in withData(LegacySearchConsoleDashboardWidgetSiteStats)
    in Unknown
    in WithSelect(withData(LegacySearchConsoleDashboardWidgetSiteStats))
    in div
    in Layout
    in div
    in div
    in div
    in div
    in GoogleSitekitSearchConsoleDashboardWidget
    in FilteredComponent
    in WithFilters(ModuleApp)
    in ModuleApp
    in GoogleSitekitModule
    in RestoreSnapshots
    in ErrorHandler
    in Root
  • https://wordpress.org/support/topic/site-kit-encountered-an-error-36/ | Open | SH info provided
  • https://wordpress.org/support/topic/error-search-google-in-site-kit/ | Open | SH info provided
  • https://wordpress.org/support/topic/my-site-kit-has-occured-an-error/ | Open
  • https://wordpress.org/support/topic/gsuite-shows-a-error-what-does-it-means | Resolved | SH info within topic | randomly started working as expected once more
  • https://wordpress.org/support/topic/error-occur-to-site-kit/#post-14510879 | Open | SH info provided
  • https://wordpress.org/support/topic/problem-with-search-console-in-google-site-kit/ | Open
  • https://wordpress.org/support/topic/site-kit-encountered-an-error-38/#post-14516532 | Open

Troubleshooting ongoing, looking at the error message provided this looks similar to #3255


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • extractSearchConsoleDashboardData should be updated to accept dateRangeLength as a parameter rather than inferring this from the length of the data given as it does now
  • All (4) instances of components using extractSearchConsoleDashboardData should be updated to retrieve the current date range length (e.g. getDateRangeNumberOfDays) to provide to this utility

Implementation Brief

  • Merge #3478

Test Coverage

  • Add and update tests for changes to partitionReport

Visual Regression Changes

  • Update reference images for Search Console chart due to change in date range (essentially undoes a change in #3348)

QA Brief

  • Set up Site Kit using a property that has less-than-full data in Search Console (e.g. added in the last few weeks – @jamesozzie has such a property)
  • Check Search Console dashboards (widget + non-widget based)
  • Check all date ranges
  • For date ranges that touch the maximum age of the property in Search Console (e.g. Last 28 days with the property added in Search Console 25 days ago) any larger date range should show same/similar metrics since larger date ranges include previous

Changelog entry

  • Fix potential dateRangeLength must be a positive integer. error for Search Console reports with missing dates.

jamesozzie avatar May 26 '21 13:05 jamesozzie

Additional 2 reports added to issue description. No common plugins or obvious cause so far, all 3 sites do have a migration or demo content importer plugins active. Troubleshooting ongoing,

jamesozzie avatar May 28 '21 14:05 jamesozzie

More reports of this from other users in an existing support topic. One of the users reported that since reporting the issue no longer occurs. The same user reported that from a Chrome browser incognito window the progress bar remained in a loading position.

jamesozzie avatar May 31 '21 11:05 jamesozzie

Some findings from my investigation:

I first tried to generate a more meaningful stack trace:

  • The error is thrown by the partitionReport function in this file.
  • The partitionReport function is used inside the extractSearchConsoleDashboardData function here,
  • which in turn is used by the LegacySearchConsoleDashboardWidgetSiteStats component here.

I then managed to find one way to recreate the issue: it looks like if the report has only one row, the error is thrown.

extractSearchConsoleDashboardData tries to infer the range length from the number of rows:

export const extractSearchConsoleDashboardData = ( rows ) => {
	// Infer range length from total number of rows.
	const dateRangeLength = Math.floor( rows.length / 2 );
	const { compareRange, currentRange } = partitionReport( rows, { dateRangeLength } );
	// ...

And then partitionReport evaluates whether this is an integer greater than 1:

export const partitionReport = ( report, { dateRangeLength } ) => {
	invariant( Array.isArray( report ), 'report must be an array to partition.' );
	invariant( Number.isInteger( dateRangeLength ) && dateRangeLength > 0, 'dateRangeLength must be a positive integer.' );

This means that if the report only has one row, partitionReport is called with dateRangeLength of 0.5, which would then generate the error. LegacySearchConsoleDashboardWidgetSiteStats checks if the report is not undefined or empty, but a report with one row will be evaluated:

	render() {
		const { data, selectedStats } = this.props;

		if ( ! data || ! data.length ) {
			return null;
		}
+		const subset = [ data[ 0 ] ];

		const options = this.setOptions();
+		const processedData = extractSearchConsoleDashboardData( subset );

Based on this, a suggested fix might be to add a conditional to extractSearchConsoleDashboardData to account for cases where there is only one row of data.

johnPhillips avatar Jun 03 '21 15:06 johnPhillips

This error also occurs if the number of rows in the report is 0 (zero) in extractSearchConsoleDashboardData. A conditional check in that function to see if the number of rows is less than 2 seems to solve the problem. If it is, setting dateRangeLength to 1 displays the widgets without error.

Hazlitte avatar Jun 03 '21 16:06 Hazlitte

@Hazlitte Just a side note: LegacySearchConsoleDashboardWidgetSiteStats seems to check if the report is empty:

https://github.com/google/site-kit-wp/blob/045409d71d8f36315f97a0d47f49d39fcb9bdf0c/assets/js/modules/search-console/components/dashboard/LegacySearchConsoleDashboardWidgetSiteStats.js#L117-L119

Once the widgets.moduleScreens feature is turned on and we stop using the legacy widgets, we will still be calling extractSearchConsoleDashboardData:

https://github.com/google/site-kit-wp/blob/045409d71d8f36315f97a0d47f49d39fcb9bdf0c/assets/js/modules/search-console/components/module/ModuleOverviewWidget/Overview.js#L45-L47

In that scenario too, empty reports seem to be caught (via isZeroReport).

That was why I thought it might only be reports of length 1 that cause this issue. Thought it might be worth pointing out.

Either way, your > 2 fix suggestion would work, I think.

johnPhillips avatar Jun 03 '21 16:06 johnPhillips

Thanks guys – let's see if we can get this fixed for the release.

aaemnnosttv avatar Jun 03 '21 16:06 aaemnnosttv

Here is a possible solution in extractSearchConsoleDashboardData:

const dateRangeLength = rows.length > 1 ? Math.floor( rows.length / 2 ) : 1;

Hazlitte avatar Jun 03 '21 16:06 Hazlitte

After looking into this a bit more, I've found this is due to other problems related to how the Search Console API works which I will open a new issue for. For now, this issue is just about avoiding the error by using the utility in the same way it is used everywhere else.

aaemnnosttv avatar Jun 03 '21 18:06 aaemnnosttv

QA ✅

Ran through a series of tests on a site with has less-than-full data in Search Console.

Was unable to reproduce this error. image

image

This is fixed on my end. Sending to approval.

cole10up avatar Jun 06 '21 23:06 cole10up