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

Correct order for showing measurements #358

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -22,8 +22,8 @@ import org.ooni.probe.data.models.MeasurementWithUrl
import org.ooni.probe.data.models.NetTest
import org.ooni.probe.data.models.ResultItem
import org.ooni.probe.data.models.ResultModel
import org.ooni.probe.data.models.RunSpecification
import org.ooni.probe.data.models.RunBackgroundState
import org.ooni.probe.data.models.RunSpecification

class ResultViewModel(
resultId: ResultModel.Id,
Expand All @@ -46,34 +46,17 @@ class ResultViewModel(
combine(
getResult(resultId),
expandedDescriptorsKeys,
::Pair,
)
.onEach { (result, expandedDescriptorsKeys) ->
var groupedMeasurements = listOf<MeasurementGroupItem>()
result?.measurements?.let { measurements ->
groupedMeasurements = measurements.groupBy { it.measurement.test }.flatMap { (key, itemList) ->
when {
itemList.size == 1 -> listOf(MeasurementGroupItem.Single(itemList.first()))
itemList.size > 1 && itemList.size == measurements.size -> itemList.map { MeasurementGroupItem.Single(it) }
else -> {
listOf(
MeasurementGroupItem.Group(
test = key,
measurements = itemList,
isExpanded = expandedDescriptorsKeys.contains(key),
),
)
}
}
}
}
_state.update {
it.copy(result = result, groupedMeasurements = groupedMeasurements)
}
if (result?.result?.isViewed == false) {
markResultAsViewed(resultId)
}
) { result, expandedDescriptorsKeys ->
_state.update {
it.copy(
result = result,
groupedMeasurements = groupMeasurements(result, expandedDescriptorsKeys),
)
}
if (result?.result?.isViewed == false) {
markResultAsViewed(resultId)
}
}
.launchIn(viewModelScope)

combine(
Expand Down Expand Up @@ -141,6 +124,34 @@ class ResultViewModel(
events.tryEmit(event)
}

private fun groupMeasurements(
result: ResultItem?,
expandedDescriptorsKeys: List<TestType>,
) = result?.measurements?.let { measurements ->
measurements
.groupBy { it.measurement.test }
.flatMap { (key, itemList) ->
if (itemList.size == 1 || itemList.size == measurements.size) {
itemList.sort().map { MeasurementGroupItem.Single(it) }
} else {
listOf(
MeasurementGroupItem.Group(
test = key,
measurements = itemList.sort(),
isExpanded = expandedDescriptorsKeys.contains(key),
),
)
}
}
}.orEmpty()

private fun List<MeasurementWithUrl>.sort() =
sortedWith(
compareByDescending<MeasurementWithUrl> { it.measurement.isFailed }
.thenByDescending { it.measurement.isAnomaly }
.thenBy { it.measurement.startTime },
)

private fun getRerunSpecification(): RunSpecification? {
val item = _state.value.result ?: return null
return RunSpecification.Full(
Expand Down