Skip to content
Open
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
2 changes: 1 addition & 1 deletion composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ android {
isShrinkResources = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
"proguard-rules.pro",
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ import kotlinx.serialization.Serializable
data class OONINetTest(
@SerialName("test_name") val name: String,
@SerialName("inputs") val inputs: List<String>? = null,
@SerialName("is_background_run_enabled_default") val isBackgroundRunEnabled: Boolean = false,
@SerialName("is_manual_run_enabled_default") val isManualRunEnabled: Boolean = false,
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,25 @@ data class Descriptor(
val expirationDate: LocalDateTime?,
val netTests: List<NetTest>,
val longRunningTests: List<NetTest> = emptyList(),
val source: Source,
val source: InstalledTestDescriptorModel,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had mentioned this before, but keeping both the Descriptor and the InstalledTestDescriptorModel is just a big duplication of fields now. If we are to keep both because it helps with UpdateStatus, then we should remove the duplication:

  • The Descriptor class should just have the source and UpdateStatus
  • Optionally, InstalledTestDescriptorModel could be renamed to Descriptor and Descriptor could become a DescriptorItem or DescriptorWithUpdateStatus

val updateStatus: UpdateStatus,
val enabled: Boolean = true,
val summaryType: SummaryType,
) {
sealed interface Source {
data class Default(
val value: DefaultTestDescriptor,
) : Source

data class Installed(
val value: InstalledTestDescriptorModel,
) : Source
}

val isExpired get() = expirationDate != null && expirationDate < LocalDateTime.now()

val updatedDescriptor
get() = (updateStatus as? UpdateStatus.Updatable)?.updatedDescriptor

val key: String
get() = when (source) {
is Source.Default -> name
is Source.Installed -> source.value.id.value
get() {
val descriptorId = source.id.value
return if (isDefault()) {
OoniTest.fromId(descriptorId)?.key ?: descriptorId
} else {
descriptorId
}
}

val allTests get() = netTests + longRunningTests

val estimatedDuration
Expand All @@ -59,14 +52,19 @@ data class Descriptor(
get() =
allTests.size == 1 && allTests.first().test == TestType.WebConnectivity

val runLink get() = (source as? Source.Installed)?.value?.runLink
val runLink get() = source.runLink

val settingsPrefix: String?
get() = if (isDefault()) null else source.id.value

fun isDefault(): Boolean = source.isOoniDescriptor

companion object {
val SORT_COMPARATOR =
compareByDescending<Descriptor> { it.source is Source.Installed }
compareByDescending<Descriptor> { !it.isDefault() }
.thenBy { it.isExpired }
.thenByDescending { (it.source as? Source.Installed)?.value?.dateInstalled }
.thenByDescending { (it.source as? Source.Installed)?.value?.id?.value }
.thenByDescending { it.source.dateInstalled }
.thenBy { it.source.id.value }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ import kotlinx.datetime.format
import kotlinx.datetime.format.MonthNames
import kotlinx.datetime.format.char
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import ooniprobe.composeapp.generated.resources.Dashboard_Runv2_Overview_Description
import ooniprobe.composeapp.generated.resources.Dashboard_Runv2_Overview_LastUpdated
import ooniprobe.composeapp.generated.resources.Res
import ooniprobe.composeapp.generated.resources.TestResults_NotAvailable
import ooniprobe.composeapp.generated.resources.performance_datausage
import ooniprobe.composeapp.generated.resources.small_datausage
import ooniprobe.composeapp.generated.resources.test_circumvention
import ooniprobe.composeapp.generated.resources.test_experimental
import ooniprobe.composeapp.generated.resources.test_instant_messaging
import ooniprobe.composeapp.generated.resources.test_performance
import ooniprobe.composeapp.generated.resources.test_websites
import ooniprobe.composeapp.generated.resources.websites_datausage
import org.jetbrains.compose.resources.StringResource
import org.jetbrains.compose.resources.stringResource
import org.ooni.engine.models.SummaryType
import org.ooni.probe.config.OrganizationConfig
import org.ooni.probe.data.TestDescriptor
import org.ooni.probe.data.models.Descriptor.Source
import org.ooni.probe.shared.InstalledDescriptorIcons
import org.ooni.probe.shared.hexToColor
import org.ooni.probe.shared.now
Expand Down Expand Up @@ -60,6 +63,8 @@ data class InstalledTestDescriptorModel(
val revision: Long,
)

val isOoniDescriptor get() = OoniTest.isValidId(id.value)

val key get() = Key(id, revision)

val previousRevisions
Expand Down Expand Up @@ -93,15 +98,27 @@ fun InstalledTestDescriptorModel.toDescriptor(updateStatus: UpdateStatus = Updat
icon = icon?.let(InstalledDescriptorIcons::getIconFromValue),
color = color?.hexToColor(),
animation = icon?.let { determineAnimation(it) } ?: animation?.let(Animation::fromFileName),
dataUsage = { null },
dataUsage = { if (isOoniDescriptor) stringResource(getDataUsage()) else null },
expirationDate = expirationDate,
netTests = netTests.orEmpty(),
source = Descriptor.Source.Installed(this),
source = this,
updateStatus = updateStatus,
// In the future, this will become a DB field with a value provided by the back-end
summaryType = SummaryType.Anomaly,
)

fun InstalledTestDescriptorModel.getDataUsage(): StringResource =
when (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an enum class, so you can just do:

when (OoniTest.fromId(key.id.value)) {
    OoniTest.WEBSITES -> Res.string.websites_datausage
    ...
}

OoniTest.fromId(this.key.id.value)
) {
OoniTest.Websites -> Res.string.websites_datausage
OoniTest.InstantMessaging -> Res.string.small_datausage
OoniTest.Circumvention -> Res.string.small_datausage
OoniTest.Performance -> Res.string.performance_datausage
OoniTest.Experimental -> Res.string.TestResults_NotAvailable
else -> Res.string.TestResults_NotAvailable
}

private val iconAnimationMap = mapOf(
Res.drawable.test_websites to Animation.Websites,
Res.drawable.test_instant_messaging to Animation.InstantMessaging,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.ooni.probe.data.models

sealed class OoniTest(
val id: String,
val key: String
) {
object Websites : OoniTest("00104", "websites")
object InstantMessaging : OoniTest("00105", "instant_messaging")
object Circumvention : OoniTest("00106", "circumvention")
object Performance : OoniTest("00107", "performance")
object Experimental : OoniTest("00108", "experimental")

companion object {
fun fromId(id: String): OoniTest? = when (id) {
Websites.id -> Websites
InstantMessaging.id -> InstantMessaging
Circumvention.id -> Circumvention
Performance.id -> Performance
Experimental.id -> Experimental
else -> null
}

fun isValidId(id: String): Boolean = fromId(id) != null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,9 @@ sealed interface RunSpecification {

@Serializable
data class Test(
val source: Source,
val source: InstalledTestDescriptorModel.Id,
val netTests: List<NetTest>,
) {
@Serializable
sealed interface Source {
@Serializable
data class Default(
val name: String,
) : Source

@Serializable
data class Installed(
val id: InstalledTestDescriptorModel.Id,
) : Source

companion object {
fun fromDescriptor(descriptor: Descriptor) =
when (descriptor.source) {
is Descriptor.Source.Default -> Default(descriptor.name)
is Descriptor.Source.Installed -> Installed(descriptor.source.value.id)
}
}
}
}
)

/*
* Remove the URL inputs from the spec if we already have them in our database,
Expand Down Expand Up @@ -70,7 +49,7 @@ sealed interface RunSpecification {
}

private val Test.isWebsites
get() = (source as? Test.Source.Default)?.name == "websites"
get() = source.value == OoniTest.Websites.id

companion object {
fun buildForDescriptor(
Expand All @@ -80,13 +59,7 @@ sealed interface RunSpecification {
) = Full(
tests = listOf(
Test(
source = when (descriptor.source) {
is Descriptor.Source.Default ->
Test.Source.Default(descriptor.name)

is Descriptor.Source.Installed ->
Test.Source.Installed(descriptor.source.value.id)
},
source = descriptor.source.id,
netTests = descriptor.allTests,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,7 @@ class PreferenceRepository(
isAutoRun: Boolean,
) = getPreferenceKey(
name = netTest.test.preferenceKey,
prefix = (descriptor.source as? Descriptor.Source.Installed)
?.value
?.id
?.value,
prefix = descriptor.settingsPrefix,
autoRun = isAutoRun,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import org.ooni.probe.domain.FinishInProgressData
import org.ooni.probe.domain.GetAutoRunSettings
import org.ooni.probe.domain.GetAutoRunSpecification
import org.ooni.probe.domain.GetBootstrapTestDescriptors
import org.ooni.probe.domain.GetDefaultTestDescriptors
import org.ooni.probe.domain.GetEnginePreferences
import org.ooni.probe.domain.GetFirstRun
import org.ooni.probe.domain.GetLastResultOfDescriptor
Expand Down Expand Up @@ -313,7 +312,6 @@ class Dependencies(
private val getBootstrapTestDescriptors by lazy {
GetBootstrapTestDescriptors(readAssetFile, json, backgroundContext)
}
private val getDefaultTestDescriptors by lazy { GetDefaultTestDescriptors() }
private val getEnginePreferences by lazy {
GetEnginePreferences(
preferencesRepository = preferenceRepository,
Expand Down Expand Up @@ -376,7 +374,6 @@ class Dependencies(
@VisibleForTesting
val getTestDescriptors by lazy {
GetTestDescriptors(
getDefaultTestDescriptors = getDefaultTestDescriptors::invoke,
listAllInstalledTestDescriptors = testDescriptorRepository::listAll,
listLatestInstalledTestDescriptors = testDescriptorRepository::listLatest,
observeDescriptorsUpdateState = descriptorUpdateStateManager::observe,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class GetAutoRunSpecification(
return RunSpecification.Full(
tests = descriptors.map { descriptor ->
RunSpecification.Test(
source = RunSpecification.Test.Source.fromDescriptor(descriptor),
source = descriptor.source.id,
netTests = descriptor.netTests,
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class GetBootstrapTestDescriptors(
) {
suspend operator fun invoke(): List<InstalledTestDescriptorModel> =
withContext(backgroundContext) {

val descriptorsJson = Res.readBytes("files/assets/descriptors.json").decodeToString()
val descriptors =
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fun List<Descriptor>.forResult(result: ResultModel): Descriptor? =
result.descriptorKey
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can now stop using the concept of descriptorKey and start using InstalledTestDescriptorModel.Id everywhere.

?.let { key ->
firstOrNull {
it.source is Descriptor.Source.Installed && it.source.value.key == key
it.source?.key?.toString() == key.toString()
}
}
?: firstOrNull { it.name == result.descriptorName }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class RunDescriptors(
) {
val result = ResultModel(
descriptorName = descriptor.name,
descriptorKey = (descriptor.source as? Descriptor.Source.Installed)?.value?.key,
descriptorKey = descriptor.source?.key,
taskOrigin = taskOrigin,
)
val resultId = storeResult(result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class RunNetTest(
testTotal = spec.testTotal,
)
}
val installedDescriptorId =
(spec.descriptor.source as? Descriptor.Source.Installed)?.value?.id
// Ensure descriptor id is not available for ooni descriptors
val installedDescriptorId = if (spec.descriptor.isDefault()) null else spec.descriptor.source.id

try {
startTest(
Expand Down
Loading
Loading