From 00378c20e4ef45f599d682537f0b55b3e8c98b5f Mon Sep 17 00:00:00 2001 From: vojtasmrcek Date: Fri, 12 Jun 2020 10:04:36 +0200 Subject: [PATCH 1/3] Log progress of fetchVisits call to investigate stale data issue --- .../store/stats/time/VisitsAndViewsStore.kt | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/store/stats/time/VisitsAndViewsStore.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/store/stats/time/VisitsAndViewsStore.kt index eec7f5f9ae..4846a6052d 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/store/stats/time/VisitsAndViewsStore.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/store/stats/time/VisitsAndViewsStore.kt @@ -12,6 +12,7 @@ import org.wordpress.android.fluxc.store.StatsStore.OnStatsFetched import org.wordpress.android.fluxc.store.StatsStore.StatsError import org.wordpress.android.fluxc.store.StatsStore.StatsErrorType.INVALID_RESPONSE import org.wordpress.android.fluxc.tools.CoroutineEngine +import org.wordpress.android.fluxc.utils.AppLogWrapper import org.wordpress.android.fluxc.utils.CurrentTimeProvider import org.wordpress.android.fluxc.utils.SiteUtils import org.wordpress.android.util.AppLog.T.STATS @@ -26,7 +27,8 @@ class VisitsAndViewsStore private val timeStatsMapper: TimeStatsMapper, private val statsUtils: StatsUtils, private val currentTimeProvider: CurrentTimeProvider, - private val coroutineEngine: CoroutineEngine + private val coroutineEngine: CoroutineEngine, + private val appLogWrapper: AppLogWrapper ) { suspend fun fetchVisits( site: SiteModel, @@ -38,7 +40,11 @@ class VisitsAndViewsStore currentTimeProvider.currentDate, SiteUtils.getNormalizedTimezone(site.timezone) ) + logProgress("Site timezone: ${site.timezone}") + logProgress("Current date: ${currentTimeProvider.currentDate}") + logProgress("Fetching for date with applied timezone: $dateWithTimeZone") if (!forced && sqlUtils.hasFreshRequest(site, granularity, dateWithTimeZone, limitMode.limit)) { + logProgress("Loading cached data") return@withDefaultContext OnStatsFetched( getVisits(site, granularity, limitMode, dateWithTimeZone), cached = true @@ -46,19 +52,31 @@ class VisitsAndViewsStore } val payload = restClient.fetchVisits(site, granularity, dateWithTimeZone, limitMode.limit, forced) return@withDefaultContext when { - payload.isError -> OnStatsFetched(payload.error) + payload.isError -> { + logProgress("Error fetching data: ${payload.error}") + OnStatsFetched(payload.error) + } payload.response != null -> { + logProgress("Data fetched correctly") sqlUtils.insert(site, payload.response, granularity, dateWithTimeZone, limitMode.limit) val overviewResponse = timeStatsMapper.map(payload.response, limitMode) - if (overviewResponse.period.isBlank() || overviewResponse.dates.isEmpty()) + if (overviewResponse.period.isBlank() || overviewResponse.dates.isEmpty()) { + logProgress("Invalid response") OnStatsFetched(StatsError(INVALID_RESPONSE, "Overview: Required data 'period' or 'dates' missing")) - else + } else { + logProgress("Valid response returned for period: ${overviewResponse.period}") + logProgress("Last data item for: ${overviewResponse.dates.lastOrNull()?.period}") OnStatsFetched(overviewResponse) + } } else -> OnStatsFetched(StatsError(INVALID_RESPONSE)) } } + private fun logProgress(message: String) { + appLogWrapper.d(STATS, "fetchVisits: $message") + } + fun getVisits( site: SiteModel, granularity: StatsGranularity, From 221033bfcb05a53a1d4a19a70b6534a346553a29 Mon Sep 17 00:00:00 2001 From: vojtasmrcek Date: Fri, 12 Jun 2020 10:08:30 +0200 Subject: [PATCH 2/3] Fix test --- .../fluxc/store/stats/time/VisitsAndViewsStoreTest.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/example/src/test/java/org/wordpress/android/fluxc/store/stats/time/VisitsAndViewsStoreTest.kt b/example/src/test/java/org/wordpress/android/fluxc/store/stats/time/VisitsAndViewsStoreTest.kt index 0b3f404b1d..e2f35e7d9e 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/store/stats/time/VisitsAndViewsStoreTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/store/stats/time/VisitsAndViewsStoreTest.kt @@ -27,6 +27,7 @@ import org.wordpress.android.fluxc.store.StatsStore.StatsError import org.wordpress.android.fluxc.store.StatsStore.StatsErrorType.API_ERROR import org.wordpress.android.fluxc.test import org.wordpress.android.fluxc.tools.initCoroutineEngine +import org.wordpress.android.fluxc.utils.AppLogWrapper import org.wordpress.android.fluxc.utils.CurrentTimeProvider import java.util.Date import kotlin.test.assertEquals @@ -44,6 +45,7 @@ class VisitsAndViewsStoreTest { @Mock lateinit var statsUtils: StatsUtils @Mock lateinit var currentTimeProvider: CurrentTimeProvider @Mock lateinit var mapper: TimeStatsMapper + @Mock lateinit var appLogWrapper: AppLogWrapper private lateinit var store: VisitsAndViewsStore @Before fun setUp() { @@ -53,7 +55,8 @@ class VisitsAndViewsStoreTest { mapper, statsUtils, currentTimeProvider, - initCoroutineEngine() + initCoroutineEngine(), + appLogWrapper ) val currentDate = Date(0) whenever(currentTimeProvider.currentDate).thenReturn(currentDate) From 8f3b8f040022c89a0f6c8fa94ad9d06b1bb3e801 Mon Sep 17 00:00:00 2001 From: vojtasmrcek Date: Fri, 12 Jun 2020 10:15:01 +0200 Subject: [PATCH 3/3] Add granularity to the logs --- .../store/stats/time/VisitsAndViewsStore.kt | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/store/stats/time/VisitsAndViewsStore.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/store/stats/time/VisitsAndViewsStore.kt index 4846a6052d..b940dd8c65 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/store/stats/time/VisitsAndViewsStore.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/store/stats/time/VisitsAndViewsStore.kt @@ -40,11 +40,11 @@ class VisitsAndViewsStore currentTimeProvider.currentDate, SiteUtils.getNormalizedTimezone(site.timezone) ) - logProgress("Site timezone: ${site.timezone}") - logProgress("Current date: ${currentTimeProvider.currentDate}") - logProgress("Fetching for date with applied timezone: $dateWithTimeZone") + logProgress(granularity, "Site timezone: ${site.timezone}") + logProgress(granularity, "Current date: ${currentTimeProvider.currentDate}") + logProgress(granularity, "Fetching for date with applied timezone: $dateWithTimeZone") if (!forced && sqlUtils.hasFreshRequest(site, granularity, dateWithTimeZone, limitMode.limit)) { - logProgress("Loading cached data") + logProgress(granularity, "Loading cached data") return@withDefaultContext OnStatsFetched( getVisits(site, granularity, limitMode, dateWithTimeZone), cached = true @@ -53,19 +53,19 @@ class VisitsAndViewsStore val payload = restClient.fetchVisits(site, granularity, dateWithTimeZone, limitMode.limit, forced) return@withDefaultContext when { payload.isError -> { - logProgress("Error fetching data: ${payload.error}") + logProgress(granularity, "Error fetching data: ${payload.error}") OnStatsFetched(payload.error) } payload.response != null -> { - logProgress("Data fetched correctly") + logProgress(granularity, "Data fetched correctly") sqlUtils.insert(site, payload.response, granularity, dateWithTimeZone, limitMode.limit) val overviewResponse = timeStatsMapper.map(payload.response, limitMode) if (overviewResponse.period.isBlank() || overviewResponse.dates.isEmpty()) { - logProgress("Invalid response") + logProgress(granularity, "Invalid response") OnStatsFetched(StatsError(INVALID_RESPONSE, "Overview: Required data 'period' or 'dates' missing")) } else { - logProgress("Valid response returned for period: ${overviewResponse.period}") - logProgress("Last data item for: ${overviewResponse.dates.lastOrNull()?.period}") + logProgress(granularity, "Valid response returned for period: ${overviewResponse.period}") + logProgress(granularity, "Last data item for: ${overviewResponse.dates.lastOrNull()?.period}") OnStatsFetched(overviewResponse) } } @@ -73,8 +73,8 @@ class VisitsAndViewsStore } } - private fun logProgress(message: String) { - appLogWrapper.d(STATS, "fetchVisits: $message") + private fun logProgress(granularity: StatsGranularity, message: String) { + appLogWrapper.d(STATS, "fetchVisits for $granularity: $message") } fun getVisits(