Skip to content

Commit

Permalink
Merge pull request #13058 from woocommerce/hotfix/recover-from-data-s…
Browse files Browse the repository at this point in the history
…tore-corruption

[Dashboard] Add corruption handlers to Data Stores using Protocol Buffer schemas.
  • Loading branch information
malinajirka authored Dec 4, 2024
2 parents 496d044 + 5bfed0d commit 47ed84d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.woocommerce.android.datastore
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.core.DataStoreFactory
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
import androidx.datastore.dataStoreFile
import com.automattic.android.tracks.crashlogging.CrashLogging
import com.woocommerce.android.di.SiteComponent
import com.woocommerce.android.di.SiteCoroutineScope
import com.woocommerce.android.di.SiteScope
Expand All @@ -23,12 +25,17 @@ object DashboardDataStoreModule {
@SiteScope
fun provideDashboardDataStore(
appContext: Context,
crashLogging: CrashLogging,
@SiteCoroutineScope siteCoroutineScope: CoroutineScope,
site: SiteModel
): DataStore<DashboardDataModel> = DataStoreFactory.create(
produceFile = {
appContext.dataStoreFile("dashboard_configuration_${site.id}")
},
corruptionHandler = ReplaceFileCorruptionHandler {
crashLogging.recordEvent("Corrupted data store. DataStore Type: DASHBOARD")
DashboardDataModel.getDefaultInstance()
},
scope = CoroutineScope(siteCoroutineScope.coroutineContext + Dispatchers.IO),
serializer = DashboardSerializer
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ package com.woocommerce.android.datastore
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.core.DataStoreFactory
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.emptyPreferences
import androidx.datastore.preferences.preferencesDataStoreFile
import com.automattic.android.tracks.crashlogging.CrashLogging
import com.woocommerce.android.datastore.DataStoreType.ANALYTICS_CONFIGURATION
import com.woocommerce.android.datastore.DataStoreType.ANALYTICS_UI_CACHE
import com.woocommerce.android.datastore.DataStoreType.COUPONS
import com.woocommerce.android.datastore.DataStoreType.DASHBOARD_STATS
import com.woocommerce.android.datastore.DataStoreType.LAST_UPDATE
import com.woocommerce.android.datastore.DataStoreType.TOP_PERFORMER_PRODUCTS
import com.woocommerce.android.datastore.DataStoreType.TRACKER
import com.woocommerce.android.di.AppCoroutineScope
Expand All @@ -30,11 +35,16 @@ class DataStoreModule {
@DataStoreQualifier(TRACKER)
fun provideTrackerDataStore(
appContext: Context,
crashLogging: CrashLogging,
@AppCoroutineScope appCoroutineScope: CoroutineScope
): DataStore<Preferences> = PreferenceDataStoreFactory.create(
produceFile = {
appContext.preferencesDataStoreFile("tracker")
},
corruptionHandler = ReplaceFileCorruptionHandler {
crashLogging.recordEvent("Corrupted data store. DataStore Type: ${TRACKER.name}")
emptyPreferences()
},
scope = CoroutineScope(appCoroutineScope.coroutineContext + Dispatchers.IO)
)

Expand All @@ -43,11 +53,16 @@ class DataStoreModule {
@DataStoreQualifier(ANALYTICS_UI_CACHE)
fun provideAnalyticsDataStore(
appContext: Context,
crashLogging: CrashLogging,
@AppCoroutineScope appCoroutineScope: CoroutineScope
): DataStore<Preferences> = PreferenceDataStoreFactory.create(
produceFile = {
appContext.preferencesDataStoreFile("analytics")
},
corruptionHandler = ReplaceFileCorruptionHandler {
crashLogging.recordEvent("Corrupted data store. DataStore Type: ${ANALYTICS_UI_CACHE.name}")
emptyPreferences()
},
scope = CoroutineScope(appCoroutineScope.coroutineContext + Dispatchers.IO)
)

Expand All @@ -56,11 +71,16 @@ class DataStoreModule {
@DataStoreQualifier(ANALYTICS_CONFIGURATION)
fun provideAnalyticsConfigurationDataStore(
appContext: Context,
crashLogging: CrashLogging,
@AppCoroutineScope appCoroutineScope: CoroutineScope
): DataStore<Preferences> = PreferenceDataStoreFactory.create(
produceFile = {
appContext.preferencesDataStoreFile("analytics_configuration")
},
corruptionHandler = ReplaceFileCorruptionHandler {
crashLogging.recordEvent("Corrupted data store. DataStore Type: ${ANALYTICS_CONFIGURATION.name}")
emptyPreferences()
},
scope = CoroutineScope(appCoroutineScope.coroutineContext + Dispatchers.IO)
)

Expand All @@ -69,11 +89,16 @@ class DataStoreModule {
@DataStoreQualifier(DASHBOARD_STATS)
fun provideCustomDateRangeDataStore(
appContext: Context,
crashLogging: CrashLogging,
@AppCoroutineScope appCoroutineScope: CoroutineScope
): DataStore<CustomDateRange> = DataStoreFactory.create(
produceFile = {
appContext.preferencesDataStoreFile("custom_date_range_configuration")
},
corruptionHandler = ReplaceFileCorruptionHandler {
crashLogging.recordEvent("Corrupted data store. DataStore Type: ${DASHBOARD_STATS.name}")
CustomDateRange.getDefaultInstance()
},
scope = CoroutineScope(appCoroutineScope.coroutineContext + Dispatchers.IO),
serializer = CustomDateRangeSerializer
)
Expand All @@ -83,39 +108,54 @@ class DataStoreModule {
@DataStoreQualifier(TOP_PERFORMER_PRODUCTS)
fun provideTopPerformersCustomDateRangeDataStore(
appContext: Context,
crashLogging: CrashLogging,
@AppCoroutineScope appCoroutineScope: CoroutineScope
): DataStore<CustomDateRange> = DataStoreFactory.create(
produceFile = {
appContext.preferencesDataStoreFile("top_performers_custom_date_range_configuration")
},
corruptionHandler = ReplaceFileCorruptionHandler {
crashLogging.recordEvent("Corrupted data store. DataStore Type: ${TOP_PERFORMER_PRODUCTS.name}")
CustomDateRange.getDefaultInstance()
},
scope = CoroutineScope(appCoroutineScope.coroutineContext + Dispatchers.IO),
serializer = CustomDateRangeSerializer
)

@Provides
@Singleton
@DataStoreQualifier(DataStoreType.COUPONS)
@DataStoreQualifier(COUPONS)
fun provideCouponsCustomDateRangeDataStore(
appContext: Context,
crashLogging: CrashLogging,
@AppCoroutineScope appCoroutineScope: CoroutineScope
): DataStore<CustomDateRange> = DataStoreFactory.create(
produceFile = {
appContext.preferencesDataStoreFile("dashboard_coupons_custom_date_range_configuration")
},
corruptionHandler = ReplaceFileCorruptionHandler {
crashLogging.recordEvent("Corrupted data store. DataStore Type: ${COUPONS.name}")
CustomDateRange.getDefaultInstance()
},
scope = CoroutineScope(appCoroutineScope.coroutineContext + Dispatchers.IO),
serializer = CustomDateRangeSerializer
)

@Provides
@Singleton
@DataStoreQualifier(DataStoreType.LAST_UPDATE)
@DataStoreQualifier(LAST_UPDATE)
fun provideLastUpdateDataStore(
appContext: Context,
crashLogging: CrashLogging,
@AppCoroutineScope appCoroutineScope: CoroutineScope
) = PreferenceDataStoreFactory.create(
produceFile = {
appContext.preferencesDataStoreFile("update")
},
corruptionHandler = ReplaceFileCorruptionHandler {
crashLogging.recordEvent("Corrupted data store. DataStore Type: ${LAST_UPDATE.name}")
emptyPreferences()
},
scope = CoroutineScope(appCoroutineScope.coroutineContext + Dispatchers.IO)
)
}

0 comments on commit 47ed84d

Please sign in to comment.