Skip to content

Commit

Permalink
fix based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Dec 18, 2024
1 parent 0cb840b commit ed18a39
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package org.ooni.probe.data.models

import kotlin.time.Duration.Companion.seconds

data class ResultCount(val total: Long, val failed: Long?, val succeeded: Long?)
data class ResultCount(val total: Long, val succeeded: Long) {
val failed get() = total - succeeded
}

data class ResultItem(
val result: ResultModel,
val descriptor: Descriptor,
val network: NetworkModel?,
val measurements: List<MeasurementWithUrl>,
val measurementCounts: ResultCount,
) {
val anyMeasurementMissingUpload =
result.isDone && measurements.any { it.measurement.isDoneAndMissingUpload }
Expand All @@ -19,4 +20,12 @@ data class ResultItem(
val canBeRerun get() = descriptor.name == "websites" && result.isDone && urlCount > 0

val urlCount get() = measurements.count { it.url != null }

val measurementCounts
get() = ResultCount(
total = measurements.size.toLong(),
succeeded = measurements.count {
!it.measurement.isFailed && !it.measurement.isAnomaly && it.measurement.isDone
}.toLong(),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.ooni.probe.data.repositories

import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
import app.cash.sqldelight.coroutines.mapToOneNotNull
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
Expand All @@ -14,7 +13,6 @@ import org.ooni.probe.data.Url
import org.ooni.probe.data.models.InstalledTestDescriptorModel
import org.ooni.probe.data.models.MeasurementModel
import org.ooni.probe.data.models.MeasurementWithUrl
import org.ooni.probe.data.models.ResultCount
import org.ooni.probe.data.models.ResultModel
import org.ooni.probe.data.models.UrlModel
import org.ooni.probe.shared.toEpoch
Expand Down Expand Up @@ -56,12 +54,6 @@ class MeasurementRepository(
.mapToList(backgroundContext)
.map { list -> list.mapNotNull { it.toModel() } }

fun countByResultId(resultId: ResultModel.Id): Flow<ResultCount> {
return database.measurementQueries.countByResultId(resultId.value).asFlow()
.mapToOneNotNull(backgroundContext)
.map { ResultCount(it.total_measurements, it.failed_measurements, it.ok_measurements) }
}

suspend fun createOrUpdate(model: MeasurementModel): MeasurementModel.Id =
withContext(backgroundContext) {
database.transactionWithResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ class Dependencies(
getResultById = resultRepository::getById,
getTestDescriptors = getTestDescriptors.invoke(),
getMeasurementsByResultId = measurementRepository::listByResultId,
countByResultId = measurementRepository::countByResultId,
)
}
private val clearStorage by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,26 @@ import kotlinx.coroutines.flow.combine
import org.ooni.probe.data.models.Descriptor
import org.ooni.probe.data.models.MeasurementWithUrl
import org.ooni.probe.data.models.NetworkModel
import org.ooni.probe.data.models.ResultCount
import org.ooni.probe.data.models.ResultItem
import org.ooni.probe.data.models.ResultModel

class GetResult(
private val getResultById: (ResultModel.Id) -> Flow<Pair<ResultModel, NetworkModel?>?>,
private val getTestDescriptors: Flow<List<Descriptor>>,
private val getMeasurementsByResultId: (ResultModel.Id) -> Flow<List<MeasurementWithUrl>>,
private val countByResultId: (ResultModel.Id) -> Flow<ResultCount>,
) {
operator fun invoke(resultId: ResultModel.Id): Flow<ResultItem?> =
combine(
getResultById(resultId),
getTestDescriptors,
getMeasurementsByResultId(resultId),
countByResultId(resultId),
) { resultWithNetwork, descriptors, measurements, measurementCounts ->
) { resultWithNetwork, descriptors, measurements ->
val result = resultWithNetwork?.first ?: return@combine null
ResultItem(
result = result,
descriptor = descriptors.forResult(result) ?: return@combine null,
network = resultWithNetwork.second,
measurements = measurements,
measurementCounts = measurementCounts,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,6 @@ selectByResultIdWithUrl:
SELECT * FROM Measurement
LEFT JOIN Url ON Measurement.url_id = Url.id
WHERE Measurement.result_id = ?;

countByResultId:
SELECT
COUNT(*) AS total_measurements,
SUM(CASE WHEN is_rerun = 0 AND is_done = 1 AND is_failed = 0 AND is_anomaly = 0 THEN 1 ELSE 0 END) AS ok_measurements,
SUM(CASE WHEN is_rerun = 0 AND is_done = 1 AND is_failed = 1 THEN 1 ELSE 0 END) AS failed_measurements
FROM Measurement
WHERE result_id = ?;

selectAllNotUploaded:
SELECT * FROM Measurement
WHERE Measurement.is_done = 1
Expand Down

0 comments on commit ed18a39

Please sign in to comment.