-
Notifications
You must be signed in to change notification settings - Fork 30
feat: Add HTTP API to export captured network logs #526
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
Open
abdullahsohailcs
wants to merge
8
commits into
openflocon:main
Choose a base branch
from
abdullahsohailcs:feature/network-logs-json-export
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ecb27a9
feat: Add JSON export endpoint for network logs with device ID and ti…
abdullahsohailcs 971053c
refactor: Update ServerJvm to include NetworkRepository and integrate…
abdullahsohailcs 73d2f67
refactor: Update DI module to pass networkRepository to Server constr…
abdullahsohailcs be0d071
fix: Update ExportNetworkLogsAsJsonUseCase to use correct NetworkRepo…
abdullahsohailcs 79c4581
fix: Update ExportNetworkLogsAsJsonUseCase with correct NetworkFilter…
abdullahsohailcs 70f0d70
feat: Add network logs export HTTP API with three endpoints
abdullahsohailcs 17be260
fix: Push timestamp filtering to DB and address PR review comments
abdullahsohailcs 4a59893
fix: address PR review comments on network logs export
abdullahsohailcs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
41 changes: 41 additions & 0 deletions
41
...data/remote/src/commonMain/kotlin/com/flocon/data/remote/models/NetworkLogsExportModel.kt
This file contains hidden or 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,41 @@ | ||
| package com.flocon.data.remote.models | ||
|
|
||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| internal data class NetworkLogsExportResponse( | ||
| val data: List<NetworkCallExport>, | ||
| val metadata: ExportMetadata, | ||
| ) | ||
|
|
||
| @Serializable | ||
| internal data class ExportMetadata( | ||
| val exportedAt: Long, | ||
| val totalItems: Int, | ||
| val filteredBy: FilterCriteria?, | ||
| ) | ||
|
|
||
| @Serializable | ||
| internal data class FilterCriteria( | ||
| val deviceId: String? = null, | ||
| val startTimestamp: Long? = null, | ||
| val endTimestamp: Long? = null, | ||
| ) | ||
|
|
||
| @Serializable | ||
| internal data class NetworkCallExport( | ||
| val callId: String, | ||
| val method: String, | ||
| val url: String, | ||
| val startTime: Long, | ||
| val startTimeFormatted: String, | ||
| val statusCode: Int? = null, | ||
| val durationMs: Double? = null, | ||
| val requestHeaders: Map<String, String>, | ||
| val responseHeaders: Map<String, String>? = null, | ||
| val requestBody: String? = null, | ||
| val responseBody: String? = null, | ||
| val contentType: String? = null, | ||
| val deviceId: String, | ||
| val appInstance: Long, | ||
| ) |
This file contains hidden or 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
125 changes: 125 additions & 0 deletions
125
...data/remote/src/desktopMain/kotlin/com/flocon/data/remote/server/NetworkExportEndpoint.kt
This file contains hidden or 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,125 @@ | ||
| package com.flocon.data.remote.server | ||
|
|
||
| import co.touchlab.kermit.Logger | ||
| import com.flocon.data.remote.models.ExportMetadata | ||
| import com.flocon.data.remote.models.FilterCriteria | ||
| import com.flocon.data.remote.models.NetworkCallExport | ||
| import com.flocon.data.remote.models.NetworkLogsExportResponse | ||
| import io.github.openflocon.domain.network.models.FloconNetworkCallDomainModel | ||
| import io.github.openflocon.domain.network.models.NetworkCallWithDeviceId | ||
| import io.ktor.http.ContentType | ||
| import io.ktor.http.HttpStatusCode | ||
| import io.ktor.server.application.ApplicationCall | ||
| import io.ktor.server.response.respondText | ||
| import io.ktor.server.routing.Route | ||
| import io.ktor.server.routing.get | ||
| import kotlinx.serialization.builtins.MapSerializer | ||
| import kotlinx.serialization.json.Json | ||
| import kotlinx.serialization.serializer | ||
|
|
||
| fun Route.networkExportRoutes( | ||
| json: Json, | ||
| // Filtering is done at DB level — timestamps passed through to the query | ||
| getNetworkCalls: suspend (deviceId: String?, startTimestamp: Long?, endTimestamp: Long?) -> List<NetworkCallWithDeviceId>, | ||
| ) { | ||
| // 1. GET /api/network-logs — all calls from all devices | ||
| get("/api/network-logs") { | ||
| val calls = getNetworkCalls(null, null, null) | ||
| call.respondJson(json, calls, deviceId = null, startTimestamp = null, endTimestamp = null) | ||
| } | ||
|
|
||
| // 2. GET /api/network-logs/{deviceId} — all calls for a specific device | ||
| get("/api/network-logs/{deviceId}") { | ||
| val deviceId = call.parameters["deviceId"] | ||
| val calls = getNetworkCalls(deviceId, null, null) | ||
| call.respondJson(json, calls, deviceId = deviceId, startTimestamp = null, endTimestamp = null) | ||
| } | ||
|
|
||
| // 3. GET /api/network-logs/{deviceId}/filter?startTimestamp=&endTimestamp= — device + time range | ||
| get("/api/network-logs/{deviceId}/filter") { | ||
| val deviceId = call.parameters["deviceId"] | ||
| val startTimestamp = call.request.queryParameters["startTimestamp"]?.toLongOrNull() | ||
| val endTimestamp = call.request.queryParameters["endTimestamp"]?.toLongOrNull() | ||
|
|
||
| val calls = getNetworkCalls(deviceId, startTimestamp, endTimestamp) | ||
| call.respondJson(json, calls, deviceId = deviceId, startTimestamp = startTimestamp, endTimestamp = endTimestamp) | ||
| } | ||
| } | ||
|
|
||
| private suspend fun ApplicationCall.respondJson( | ||
| json: Json, | ||
| calls: List<NetworkCallWithDeviceId>, | ||
| deviceId: String?, | ||
| startTimestamp: Long?, | ||
| endTimestamp: Long?, | ||
| ) { | ||
| try { | ||
| val exportedCalls = calls.map { networkCallWithDeviceId -> | ||
| val networkCall = networkCallWithDeviceId.call | ||
| val storedDeviceId = networkCallWithDeviceId.deviceId | ||
| NetworkCallExport( | ||
| callId = networkCall.callId, | ||
| method = networkCall.request.method, | ||
| url = networkCall.request.url, | ||
| startTime = networkCall.request.startTime, | ||
| startTimeFormatted = networkCall.request.startTimeFormatted, | ||
| statusCode = when (val response = networkCall.response) { | ||
| is FloconNetworkCallDomainModel.Response.Success -> | ||
| when (val info = response.specificInfos) { | ||
| is FloconNetworkCallDomainModel.Response.Success.SpecificInfos.Http -> info.httpCode | ||
| else -> null | ||
| } | ||
| else -> null | ||
| }, | ||
| durationMs = networkCall.response?.durationMs, | ||
| requestHeaders = networkCall.request.headers, | ||
| responseHeaders = when (val response = networkCall.response) { | ||
| is FloconNetworkCallDomainModel.Response.Success -> response.headers | ||
| else -> null | ||
| }, | ||
| requestBody = networkCall.request.body, | ||
| responseBody = when (val response = networkCall.response) { | ||
| is FloconNetworkCallDomainModel.Response.Success -> response.body | ||
| else -> null | ||
| }, | ||
| contentType = when (val response = networkCall.response) { | ||
| is FloconNetworkCallDomainModel.Response.Success -> response.contentType | ||
| else -> null | ||
| }, | ||
| deviceId = storedDeviceId, | ||
| appInstance = networkCall.appInstance, | ||
| ) | ||
| } | ||
|
|
||
| val response = NetworkLogsExportResponse( | ||
| data = exportedCalls, | ||
| metadata = ExportMetadata( | ||
| exportedAt = System.currentTimeMillis(), | ||
| totalItems = exportedCalls.size, | ||
| filteredBy = if (deviceId != null || startTimestamp != null || endTimestamp != null) { | ||
| FilterCriteria( | ||
| deviceId = deviceId, | ||
| startTimestamp = startTimestamp, | ||
| endTimestamp = endTimestamp, | ||
| ) | ||
| } else null, | ||
| ), | ||
| ) | ||
|
|
||
| respondText( | ||
| json.encodeToString(NetworkLogsExportResponse.serializer(), response), | ||
| ContentType.Application.Json, | ||
| HttpStatusCode.OK, | ||
| ) | ||
| } catch (e: Exception) { | ||
| Logger.e("Error exporting network logs", e) | ||
| respondText( | ||
| json.encodeToString( | ||
| MapSerializer(serializer<String>(), serializer<String>()), | ||
| mapOf("error" to (e.message ?: "Unknown error")), | ||
| ), | ||
| ContentType.Application.Json, | ||
| HttpStatusCode.InternalServerError, | ||
| ) | ||
| } | ||
| } | ||
6 changes: 5 additions & 1 deletion
6
...esktop/data/remote/src/desktopMain/kotlin/com/flocon/data/remote/server/Server.desktop.kt
This file contains hidden or 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,5 +1,9 @@ | ||
| package com.flocon.data.remote.server | ||
|
|
||
| import io.github.openflocon.domain.network.repository.NetworkRepository | ||
| import kotlinx.serialization.json.Json | ||
|
|
||
| actual fun getServer(json: Json): Server = ServerJvm(json) | ||
| actual fun getServer( | ||
| json: Json, | ||
| networkRepository: Lazy<NetworkRepository>, | ||
| ): Server = ServerJvm(json, networkRepository) |
This file contains hidden or 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
6 changes: 6 additions & 0 deletions
6
...c/commonMain/kotlin/io/github/openflocon/domain/network/models/NetworkCallWithDeviceId.kt
This file contains hidden or 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,6 @@ | ||
| package io.github.openflocon.domain.network.models | ||
|
|
||
| data class NetworkCallWithDeviceId( | ||
| val deviceId: String, | ||
| val call: FloconNetworkCallDomainModel, | ||
| ) |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.