-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
498 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
composeApp/src/commonMain/kotlin/org/ooni/engine/NetworkTypeFinder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.ooni.engine | ||
|
||
import org.ooni.engine.models.NetworkType | ||
|
||
fun interface NetworkTypeFinder { | ||
operator fun invoke(): NetworkType | ||
} |
122 changes: 122 additions & 0 deletions
122
composeApp/src/commonMain/kotlin/org/ooni/engine/TaskEventMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package org.ooni.engine | ||
|
||
import co.touchlab.kermit.Logger | ||
import kotlinx.serialization.json.Json | ||
import org.ooni.engine.models.TaskEvent | ||
import org.ooni.engine.models.TaskEventResult | ||
import kotlin.math.roundToInt | ||
|
||
class TaskEventMapper( | ||
private val networkTypeFinder: NetworkTypeFinder, | ||
private val json: Json, | ||
) { | ||
operator fun invoke(result: TaskEventResult): TaskEvent? { | ||
val key = result.key | ||
val value = result.value | ||
|
||
return when (key) { | ||
"bug.json_dump" -> | ||
value?.let { | ||
TaskEvent.BugJsonDump(value = value) | ||
} ?: run { | ||
Logger.d("Task Event $key missing 'value'") | ||
null | ||
} | ||
|
||
"failure.measurement_submission" -> | ||
TaskEvent.MeasurementSubmissionFailure( | ||
index = value?.idx ?: 0, | ||
message = value?.failure, | ||
) | ||
|
||
"failure.resolver_lookup" -> TaskEvent.ResolverLookupFailure(message = value?.failure) | ||
|
||
"failure.startup" -> TaskEvent.StartupFailure(message = value?.failure) | ||
|
||
"log" -> | ||
value?.message?.let { message -> | ||
TaskEvent.Log( | ||
level = value.logLevel, | ||
message = message, | ||
) | ||
} ?: run { | ||
Logger.d("Task Event $key missing 'message'") | ||
null | ||
} | ||
|
||
"measurement" -> | ||
value?.jsonStr?.let { jsonString -> | ||
TaskEvent.Measurement( | ||
index = value.idx, | ||
json = jsonString, | ||
result = | ||
try { | ||
json.decodeFromString(jsonString) | ||
} catch (e: Exception) { | ||
Logger.d("Could not deserialize $key 'jsonStr'", throwable = e) | ||
null | ||
}, | ||
) | ||
} ?: run { | ||
Logger.d("Task Event $key missing 'jsonStr'") | ||
null | ||
} | ||
|
||
"status.end" -> TaskEvent.End | ||
|
||
"status.geoip_lookup" -> | ||
TaskEvent.GeoIpLookup( | ||
networkName = value?.probeNetworkName, | ||
asn = value?.probeAsn, | ||
ip = value?.probeIp, | ||
countryCode = value?.probeCc, | ||
networkType = networkTypeFinder(), | ||
) | ||
|
||
"status.measurement_done" -> | ||
TaskEvent.MeasurementDone(index = value?.idx ?: 0) | ||
|
||
"status.measurement_start" -> | ||
value?.input?.ifEmpty { null }?.let { url -> | ||
TaskEvent.MeasurementStart( | ||
index = value.idx, | ||
url = url, | ||
) | ||
} ?: run { | ||
Logger.d("Task Event $key missing 'input'") | ||
null | ||
} | ||
|
||
"status.measurement_submission" -> | ||
TaskEvent.MeasurementSubmissionSuccessful(index = value?.idx ?: 0) | ||
|
||
"status.progress" -> | ||
value?.percentage?.let { percentageValue -> | ||
TaskEvent.Progress( | ||
percentage = (percentageValue * 100.0).roundToInt(), | ||
message = value.message, | ||
) | ||
} ?: run { | ||
Logger.d("Task Event $key missing 'percentage'") | ||
null | ||
} | ||
|
||
"status.report_create" -> | ||
value?.reportId?.let { | ||
TaskEvent.ReportCreate(reportId = it) | ||
} ?: run { | ||
Logger.d("Task Event $key missing 'reportId'") | ||
null | ||
} | ||
|
||
"status.started" -> TaskEvent.Started | ||
|
||
"task_terminated" -> TaskEvent.TaskTerminated | ||
|
||
else -> { | ||
Logger.d("Task Event $key ignored") | ||
null | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
composeApp/src/commonMain/kotlin/org/ooni/engine/models/MeasurementResult.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.ooni.engine.models | ||
|
||
import kotlinx.datetime.Instant | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import org.ooni.engine.models.serializers.InstantSerializer | ||
|
||
@Serializable | ||
data class MeasurementResult( | ||
@SerialName("probe_asn") | ||
val probeAsn: String? = null, | ||
@SerialName("probe_cc") | ||
val probeCountryCode: String? = null, | ||
@SerialName("test_start_time") | ||
@Serializable(with = InstantSerializer::class) | ||
val testStartTime: Instant? = null, | ||
@SerialName("measurement_start_time") | ||
@Serializable(with = InstantSerializer::class) | ||
val measurementStartTime: Instant? = null, | ||
@SerialName("test_runtime") | ||
val testRuntime: Double? = null, | ||
@SerialName("probe_ip") | ||
val probeIp: String? = null, | ||
@SerialName("report_id") | ||
val reportId: String? = null, | ||
@SerialName("input") | ||
val input: String? = null, | ||
/* | ||
Field `test_keys` is ignored because we're not planning on storing the measurement results | ||
as structured data. | ||
*/ | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 46 additions & 6 deletions
52
composeApp/src/commonMain/kotlin/org/ooni/engine/models/TaskEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,67 @@ | ||
package org.ooni.engine.models | ||
|
||
sealed interface TaskEvent { | ||
data class BugJsonDump( | ||
val value: TaskEventResult.Value, | ||
) : TaskEvent | ||
|
||
data object End : TaskEvent | ||
|
||
data class GeoIpLookup( | ||
val networkName: String?, | ||
val ip: String?, | ||
val asn: String?, | ||
val countryCode: String?, | ||
val networkType: NetworkType, | ||
) : TaskEvent | ||
|
||
data class Log( | ||
val level: String?, | ||
val message: String, | ||
) : TaskEvent | ||
|
||
data object Started : TaskEvent | ||
data class Measurement( | ||
val index: Int, | ||
val json: String, | ||
val result: MeasurementResult?, | ||
) : TaskEvent | ||
|
||
data class ReportCreate( | ||
val reportId: String, | ||
data class MeasurementDone( | ||
val index: Int, | ||
) : TaskEvent | ||
|
||
data class MeasurementStart( | ||
val index: Int, | ||
val url: String, | ||
) : TaskEvent | ||
|
||
data class MeasurementSubmissionSuccessful( | ||
val index: Int, | ||
) : TaskEvent | ||
|
||
data class MeasurementSubmissionFailure( | ||
val index: Int, | ||
val message: String?, | ||
) : TaskEvent | ||
|
||
data class Progress( | ||
val percentage: Int, | ||
val message: String?, | ||
) : TaskEvent | ||
|
||
data object StatusEnd : TaskEvent | ||
data class ReportCreate( | ||
val reportId: String, | ||
) : TaskEvent | ||
|
||
data object TaskTerminated : TaskEvent | ||
data class ResolverLookupFailure( | ||
val message: String?, | ||
) : TaskEvent | ||
|
||
data object Started : TaskEvent | ||
|
||
data class FailureStartup( | ||
data class StartupFailure( | ||
val message: String?, | ||
) : TaskEvent | ||
|
||
data object TaskTerminated : TaskEvent | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.