Skip to content
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

Log progress of fetchVisits call to investigate stale data issue #1604

Merged
merged 3 commits into from
Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -53,7 +55,8 @@ class VisitsAndViewsStoreTest {
mapper,
statsUtils,
currentTimeProvider,
initCoroutineEngine()
initCoroutineEngine(),
appLogWrapper
)
val currentDate = Date(0)
whenever(currentTimeProvider.currentDate).thenReturn(currentDate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -38,27 +40,43 @@ class VisitsAndViewsStore
currentTimeProvider.currentDate,
SiteUtils.getNormalizedTimezone(site.timezone)
)
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(granularity, "Loading cached data")
return@withDefaultContext OnStatsFetched(
getVisits(site, granularity, limitMode, dateWithTimeZone),
cached = true
)
}
val payload = restClient.fetchVisits(site, granularity, dateWithTimeZone, limitMode.limit, forced)
return@withDefaultContext when {
payload.isError -> OnStatsFetched(payload.error)
payload.isError -> {
logProgress(granularity, "Error fetching data: ${payload.error}")
OnStatsFetched(payload.error)
}
payload.response != null -> {
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())
if (overviewResponse.period.isBlank() || overviewResponse.dates.isEmpty()) {
logProgress(granularity, "Invalid response")
OnStatsFetched(StatsError(INVALID_RESPONSE, "Overview: Required data 'period' or 'dates' missing"))
else
} else {
logProgress(granularity, "Valid response returned for period: ${overviewResponse.period}")
logProgress(granularity, "Last data item for: ${overviewResponse.dates.lastOrNull()?.period}")
OnStatsFetched(overviewResponse)
}
}
else -> OnStatsFetched(StatsError(INVALID_RESPONSE))
}
}

private fun logProgress(granularity: StatsGranularity, message: String) {
appLogWrapper.d(STATS, "fetchVisits for $granularity: $message")
}

fun getVisits(
site: SiteModel,
granularity: StatsGranularity,
Expand Down