Commit 7b83e922 authored by Alessio Placitelli's avatar Alessio Placitelli
Browse files

Regenerate the client id / first run date if needed

Old builds of products using glean, which were already
executed once, once updated, might not correctly report
the client id and the first run date. Since these are
likely low number of developer builds, we simply regenerate
these fields.
parent 227c932c
......@@ -216,8 +216,17 @@ open class GleanInternalAPI internal constructor () {
val newClientId = uuidClientInfoSnapshot?.get("client_id")
if (newClientId == null) {
val uuidPingInfoSnapshot = UuidsStorageEngine.getSnapshot("glean_ping_info", false)
uuidPingInfoSnapshot?.get("client_id")?.let {
UuidsStorageEngine.record(GleanInternalMetrics.clientId, it)
val legacyClientId = uuidPingInfoSnapshot?.get("client_id")
if (legacyClientId != null) {
UuidsStorageEngine.record(GleanInternalMetrics.clientId, legacyClientId)
} else {
// Apparently, this is a very old build that was already run
// at least once and has a different name for the underlying
// UUID storage engine persistence (i.e. UuidStorageEngine.xml
// vs using the full class name with the namespace).
// This should be mostly devs and super early adopters, so just regenerate
// the data.
UuidsStorageEngine.record(GleanInternalMetrics.clientId, UUID.randomUUID())
}
}
......@@ -225,11 +234,23 @@ open class GleanInternalAPI internal constructor () {
val newFirstRunDate = datetimeClientInfoSnapshot?.get("first_run_date")
if (newFirstRunDate == null) {
val datetimePingInfoSnapshot = DatetimesStorageEngine.getSnapshot("glean_ping_info", false)
var firstRunDateSet = false
datetimePingInfoSnapshot?.get("first_run_date")?.let {
parseISOTimeString(it)?.let {
DatetimesStorageEngine.set(GleanInternalMetrics.firstRunDate, it)
parseISOTimeString(it)?.let { parsedDate ->
DatetimesStorageEngine.set(GleanInternalMetrics.firstRunDate, parsedDate)
firstRunDateSet = true
}
}
if (!firstRunDateSet) {
// Apparently, this is a very old build that was already run
// at least once and has a different name for the underlying
// Datetime storage engine persistence (i.e. DatetimeStorageEngine.xml
// vs using the full class name with the namespace).
// This should be mostly devs and super early adopters, so just regenerate
// the data.
DatetimesStorageEngine.set(GleanInternalMetrics.firstRunDate)
}
}
}
......
......@@ -15,6 +15,7 @@ import kotlinx.coroutines.ObsoleteCoroutinesApi
import kotlinx.coroutines.runBlocking
import mozilla.components.service.glean.GleanMetrics.GleanInternalMetrics
import mozilla.components.service.glean.config.Configuration
import mozilla.components.service.glean.firstrun.FileFirstRunDetector
import mozilla.components.service.glean.private.DatetimeMetricType
import mozilla.components.service.glean.private.EventMetricType
import mozilla.components.service.glean.private.Lifetime
......@@ -394,6 +395,35 @@ class GleanTest {
assertNotEquals(firstRunDateMetric.testGetValue(), GleanInternalMetrics.firstRunDate.testGetValue())
}
@Test
fun `client_id and first_run_date must be generated if not available after the first start`() {
// 1539480 BACKWARD COMPATIBILITY HACK
// The resetGlean called right before this function will add client_id
// and first_run_date to the new location in glean_client_info. We
// need to clear those out again so we can test what happens when they
// are missing.
StorageEngineManager(
applicationContext = ApplicationProvider.getApplicationContext()
).clearAllStores()
assertFalse(GleanInternalMetrics.clientId.testHasValue())
assertFalse(GleanInternalMetrics.firstRunDate.testHasValue())
// Set this to be a non-first start with missing clientId/firstRunDate.
val gleanDataDir =
File(ApplicationProvider.getApplicationContext<Context>().applicationInfo.dataDir, Glean.GLEAN_DATA_DIR)
val firstRunDetector = FileFirstRunDetector(gleanDataDir)
firstRunDetector.createFirstRunFile()
// This should copy the values to their new locations
Glean.initialized = false
Glean.initialize(ApplicationProvider.getApplicationContext())
assertTrue(GleanInternalMetrics.clientId.testHasValue())
assertTrue(GleanInternalMetrics.firstRunDate.testHasValue())
}
@Test
fun `getLanguageTag() reports the tag for the default locale`() {
val defaultLanguageTag = getLocaleTag()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment