mirror of
https://github.com/openflocon/Flocon.git
synced 2026-05-06 05:18:41 +00:00
refact: [NETWORK] lite models (#71)
* Revert "Fix an issue with the release yml (#58)"
This reverts commit 1707aebf0f.
* refact: [NETWORK] lite models
---------
Co-authored-by: Florent Champigny <florent@bere.al>
This commit is contained in:
parent
a457055354
commit
64dafa8789
9 changed files with 124 additions and 13 deletions
|
|
@ -32,8 +32,8 @@ class NetworkRepositoryImpl(
|
|||
|
||||
override val pluginName = listOf(Protocol.FromDevice.Network.Plugin)
|
||||
|
||||
override fun observeRequests(deviceId: String) = networkLocalDataSource
|
||||
.observeRequests(deviceId = deviceId)
|
||||
override fun observeRequests(deviceId: String, lite: Boolean) = networkLocalDataSource
|
||||
.observeRequests(deviceId = deviceId, lite = lite)
|
||||
.flowOn(dispatcherProvider.data)
|
||||
|
||||
override fun observeRequest(
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import androidx.room.OnConflictStrategy
|
|||
import androidx.room.Query
|
||||
import io.github.openflocon.flocondesktop.DeviceId
|
||||
import io.github.openflocon.flocondesktop.features.network.data.datasource.local.model.FloconHttpRequestEntity
|
||||
import io.github.openflocon.flocondesktop.features.network.data.datasource.local.model.FloconHttpRequestEntityLite
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
|
|
@ -20,6 +21,17 @@ interface FloconHttpRequestDao {
|
|||
)
|
||||
fun observeRequests(deviceId: String): Flow<List<FloconHttpRequestEntity>>
|
||||
|
||||
@Query(
|
||||
"""
|
||||
SELECT *
|
||||
FROM FloconHttpRequestEntity
|
||||
WHERE deviceId = :deviceId
|
||||
ORDER BY startTime ASC
|
||||
""",
|
||||
)
|
||||
fun observeRequestsLite(deviceId: String): Flow<List<FloconHttpRequestEntityLite>>
|
||||
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun upsertRequest(request: FloconHttpRequestEntity)
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import io.github.openflocon.flocondesktop.features.network.domain.model.FloconHt
|
|||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface NetworkLocalDataSource {
|
||||
fun observeRequests(deviceId: DeviceId): Flow<List<FloconHttpRequestDomainModel>>
|
||||
fun observeRequests(deviceId: DeviceId, lite: Boolean): Flow<List<FloconHttpRequestDomainModel>>
|
||||
|
||||
fun observeRequest(
|
||||
deviceId: DeviceId,
|
||||
|
|
|
|||
|
|
@ -22,7 +22,8 @@ class NetworkLocalDataSourceRoom(
|
|||
init {
|
||||
if (Fakes.Enabled) {
|
||||
applicationScope.launch(dispatcherProvider.data) {
|
||||
val fakeRequests = FloconHttpRequestGenerator.generateDynamicFakeFloconHttpRequests(count = 10)
|
||||
val fakeRequests =
|
||||
FloconHttpRequestGenerator.generateDynamicFakeFloconHttpRequests(count = 10)
|
||||
fakeRequests.forEach { domainModel ->
|
||||
floconHttpRequestDao.upsertRequest(domainModel.toEntity(Fakes.FakeDeviceId))
|
||||
}
|
||||
|
|
@ -30,11 +31,20 @@ class NetworkLocalDataSourceRoom(
|
|||
}
|
||||
}
|
||||
|
||||
override fun observeRequests(deviceId: DeviceId): Flow<List<FloconHttpRequestDomainModel>> = floconHttpRequestDao
|
||||
.observeRequests(deviceId)
|
||||
.map { entities ->
|
||||
entities.mapNotNull { it.toDomainModel() }
|
||||
}.flowOn(dispatcherProvider.data)
|
||||
override fun observeRequests(
|
||||
deviceId: DeviceId,
|
||||
lite: Boolean
|
||||
): Flow<List<FloconHttpRequestDomainModel>> = floconHttpRequestDao
|
||||
.let {
|
||||
if (lite)
|
||||
it.observeRequestsLite(deviceId).map { entities ->
|
||||
entities.mapNotNull { it.toDomainModel() }
|
||||
}
|
||||
else it.observeRequests(deviceId).map { entities ->
|
||||
entities.mapNotNull { it.toDomainModel() }
|
||||
}
|
||||
}
|
||||
.flowOn(dispatcherProvider.data)
|
||||
|
||||
override suspend fun save(
|
||||
deviceId: DeviceId,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package io.github.openflocon.flocondesktop.features.network.data.datasource.local.mapper
|
||||
|
||||
import io.github.openflocon.flocondesktop.features.network.data.datasource.local.model.FloconHttpRequestEntity
|
||||
import io.github.openflocon.flocondesktop.features.network.data.datasource.local.model.FloconHttpRequestEntityLite
|
||||
import io.github.openflocon.flocondesktop.features.network.data.datasource.local.model.FloconHttpRequestInfosEntity
|
||||
import io.github.openflocon.flocondesktop.features.network.domain.model.FloconHttpRequestDomainModel
|
||||
|
||||
|
|
@ -89,3 +90,43 @@ fun FloconHttpRequestEntity.toDomainModel(): FloconHttpRequestDomainModel? {
|
|||
},
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
fun FloconHttpRequestEntityLite.toDomainModel(): FloconHttpRequestDomainModel? {
|
||||
return FloconHttpRequestDomainModel(
|
||||
uuid = this.uuid,
|
||||
url = this.url,
|
||||
durationMs = this.durationMs,
|
||||
request = FloconHttpRequestDomainModel.Request(
|
||||
method = this.method,
|
||||
startTime = this.startTime,
|
||||
headers = emptyMap(), // removed for lite
|
||||
body = null, // removed for lite
|
||||
byteSize = 0L, // removed for lite
|
||||
),
|
||||
response = FloconHttpRequestDomainModel.Response(
|
||||
contentType = null, // removed for lite
|
||||
body = null, // removed for lite
|
||||
headers = emptyMap(), // removed for lite
|
||||
byteSize = 0L, // removed for lite
|
||||
),
|
||||
type = when {
|
||||
this.graphql != null -> FloconHttpRequestDomainModel.Type.GraphQl(
|
||||
query = this.graphql.query,
|
||||
operationType = this.graphql.operationType,
|
||||
isSuccess = this.graphql.isSuccess,
|
||||
httpCode = this.graphql.responseHttpCode,
|
||||
)
|
||||
|
||||
this.http != null -> FloconHttpRequestDomainModel.Type.Http(
|
||||
httpCode = this.http.responseHttpCode,
|
||||
)
|
||||
|
||||
this.grpc != null -> FloconHttpRequestDomainModel.Type.Grpc(
|
||||
responseStatus = this.grpc.responseStatus,
|
||||
)
|
||||
|
||||
else -> return null
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,46 @@
|
|||
package io.github.openflocon.flocondesktop.features.network.data.datasource.local.model
|
||||
|
||||
import androidx.room.Embedded
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
data class FloconHttpRequestEntityLite(
|
||||
val uuid: String,
|
||||
val deviceId: String, // To associate with a device
|
||||
// if it's a graphql method, this item is not null
|
||||
@Embedded(prefix = "graphql_")
|
||||
val graphql: GraphQlEmbedded?,
|
||||
|
||||
@Embedded(prefix = "http_")
|
||||
val http: HttpEmbedded?,
|
||||
|
||||
@Embedded(prefix = "grpc_")
|
||||
val grpc: GrpcEmbedded?,
|
||||
|
||||
val url: String,
|
||||
val method: String,
|
||||
val startTime: Long,
|
||||
val durationMs: Double,
|
||||
// removed val requestHeaders: Map<String, String>,
|
||||
// removed val requestBody: String?,
|
||||
// removed val requestByteSize: Long,
|
||||
// removed val responseContentType: String?,
|
||||
// removed val responseBody: String?,
|
||||
// removed val responseHeaders: Map<String, String>,
|
||||
// removed val responseByteSize: Long,
|
||||
) {
|
||||
data class GraphQlEmbedded(
|
||||
val query: String,
|
||||
val operationType: String,
|
||||
val isSuccess: Boolean,
|
||||
val responseHttpCode: Int,
|
||||
)
|
||||
|
||||
data class HttpEmbedded(
|
||||
val responseHttpCode: Int,
|
||||
)
|
||||
|
||||
data class GrpcEmbedded(
|
||||
val responseStatus: String,
|
||||
)
|
||||
}
|
||||
|
|
@ -12,12 +12,13 @@ class ObserveHttpRequestsUseCase(
|
|||
private val networkRepository: NetworkRepository,
|
||||
private val observeCurrentDeviceIdUseCase: ObserveCurrentDeviceIdUseCase,
|
||||
) {
|
||||
operator fun invoke(): Flow<List<FloconHttpRequestDomainModel>> = observeCurrentDeviceIdUseCase()
|
||||
// lite : exclude headers, sizes, body
|
||||
operator fun invoke(lite: Boolean): Flow<List<FloconHttpRequestDomainModel>> = observeCurrentDeviceIdUseCase()
|
||||
.flatMapLatest { deviceId ->
|
||||
if (deviceId == null) {
|
||||
flowOf(emptyList())
|
||||
} else {
|
||||
networkRepository.observeRequests(deviceId)
|
||||
networkRepository.observeRequests(deviceId = deviceId, lite = lite)
|
||||
}
|
||||
}.distinctUntilChanged()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,8 @@ import io.github.openflocon.flocondesktop.features.network.domain.model.FloconHt
|
|||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface NetworkRepository {
|
||||
fun observeRequests(deviceId: String): Flow<List<FloconHttpRequestDomainModel>>
|
||||
// lite : exclude headers, sizes, body
|
||||
fun observeRequests(deviceId: String, lite: Boolean): Flow<List<FloconHttpRequestDomainModel>>
|
||||
|
||||
fun observeRequest(
|
||||
deviceId: String,
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ class NetworkViewModel(
|
|||
.stateIn(viewModelScope, started = SharingStarted.WhileSubscribed(5_000), null)
|
||||
|
||||
private val filteredItems = combine(
|
||||
observeHttpRequestsUseCase().map { list ->
|
||||
observeHttpRequestsUseCase(lite = true).map { list ->
|
||||
list.map {
|
||||
Pair(
|
||||
it,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue