Skip to content

Commit

Permalink
refactor(analyze): Rename Flow Files to Debug files along with non-pr…
Browse files Browse the repository at this point in the history
…imitive usage of debug files
  • Loading branch information
luistak committed Jan 27, 2025
1 parent 8b41130 commit 481ab04
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 58 deletions.
30 changes: 3 additions & 27 deletions maestro-cli/src/main/java/maestro/cli/api/ApiClient.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package maestro.cli.api

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonTypeInfo
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.github.michaelbull.result.Err
Expand All @@ -11,7 +9,7 @@ import com.github.michaelbull.result.Result
import maestro.cli.CliError
import maestro.cli.analytics.Analytics
import maestro.cli.analytics.AnalyticsReport
import maestro.cli.insights.FlowFiles
import maestro.cli.insights.AnalysisDebugFiles
import maestro.cli.model.FlowStatus
import maestro.cli.runner.resultview.AnsiResultView
import maestro.cli.util.CiUtils
Expand Down Expand Up @@ -460,32 +458,10 @@ class ApiClient(

fun analyze(
authToken: String,
flowFiles: List<FlowFiles>,
debugFiles: AnalysisDebugFiles,
): AnalyzeResponse {
if (flowFiles.isEmpty()) throw CliError("Missing flow files to analyze")

val screenshots = mutableListOf<Pair<String, ByteArray>>()
val logs = mutableListOf<Pair<String, ByteArray>>()

flowFiles.forEach { flowFile ->
flowFile.imageFiles.forEach { (imageData, path) ->
val imageName = path.fileName.toString()
screenshots.add(Pair(imageName, imageData))
}

flowFile.textFiles.forEach { (textData, path) ->
val textName = path.fileName.toString()
logs.add(Pair(textName, textData))
}
}

val requestBody = mapOf(
"screenshots" to screenshots,
"logs" to logs
)

val mediaType = "application/json; charset=utf-8".toMediaType()
val body = JSON.writeValueAsString(requestBody).toRequestBody(mediaType)
val body = JSON.writeValueAsString(debugFiles).toRequestBody(mediaType)

val url = "$baseUrl/v2/analyze"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import maestro.cli.api.RobinUploadResponse
import maestro.cli.api.UploadStatus
import maestro.cli.auth.Auth
import maestro.cli.device.Platform
import maestro.cli.insights.FlowFiles
import maestro.cli.insights.AnalysisDebugFiles
import maestro.cli.model.FlowStatus
import maestro.cli.model.RunningFlow
import maestro.cli.model.RunningFlows
Expand Down Expand Up @@ -498,15 +498,15 @@ class CloudInteractor(

fun analyze(
apiKey: String?,
flowFiles: List<FlowFiles>,
debugFiles: AnalysisDebugFiles,
debugOutputPath: Path,
): Int {
val authToken = getAuthToken(apiKey)

PrintUtils.info("\n\uD83D\uDD0E Analyzing Flow(s)...")

try {
val response = client.analyze(authToken, flowFiles)
val response = client.analyze(authToken, debugFiles)

if (response.htmlReport.isNullOrEmpty()) {
PrintUtils.info(response.output)
Expand All @@ -528,5 +528,4 @@ class CloudInteractor(
return 1
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,20 @@ import kotlin.io.path.exists
import kotlin.io.path.readText
import kotlin.io.path.writeText

data class FlowFiles(
val imageFiles: List<Pair<ByteArray, Path>>,
val textFiles: List<Pair<ByteArray, Path>>
data class AnalysisScreenshot (
val data: ByteArray,
val path: Path,
)

data class AnalysisLog (
val data: ByteArray,
val path: Path,
)

data class AnalysisDebugFiles(
val screenshots: List<AnalysisScreenshot>,
val logs: List<AnalysisLog>,
val commands: List<AnalysisLog>,
)

class TestAnalysisManager(private val apiUrl: String, private val apiKey: String?) {
Expand All @@ -29,61 +40,60 @@ class TestAnalysisManager(private val apiUrl: String, private val apiKey: String
}

fun runAnalysis(debugOutputPath: Path): Int {
val flowFiles = processFilesByFlowName(debugOutputPath)
if (flowFiles.isEmpty()) {
val debugFiles = processDebugFiles(debugOutputPath)
if (debugFiles == null) {
PrintUtils.warn("No screenshots or debug artifacts found for analysis.")
return 0;
}

return CloudInteractor(apiclient).analyze(
apiKey = apiKey,
flowFiles = flowFiles,
debugFiles = debugFiles,
debugOutputPath = debugOutputPath
)
}

private fun processFilesByFlowName(outputPath: Path): List<FlowFiles> {
private fun processDebugFiles(outputPath: Path): AnalysisDebugFiles? {
val files = Files.walk(outputPath)
.filter(Files::isRegularFile)
.collect(Collectors.toList())

return if (files.isNotEmpty()) {
val (imageFiles, textFiles) = getDebugFiles(files)
listOf(
FlowFiles(
imageFiles = imageFiles,
textFiles = textFiles
)
)
} else {
emptyList()
if (files.isEmpty()) {
return null
}

return getDebugFiles(files)
}

private fun getDebugFiles(files: List<Path>): Pair<List<Pair<ByteArray, Path>>, List<Pair<ByteArray, Path>>> {
val imageFiles = mutableListOf<Pair<ByteArray, Path>>()
val textFiles = mutableListOf<Pair<ByteArray, Path>>()
private fun getDebugFiles(files: List<Path>): AnalysisDebugFiles {
val logs = mutableListOf<AnalysisLog>()
val commands = mutableListOf<AnalysisLog>()
val screenshots = mutableListOf<AnalysisScreenshot>()

files.forEach { filePath ->
val content = Files.readAllBytes(filePath)
val fileName = filePath.fileName.toString().lowercase()
files.forEach { path ->
val data = Files.readAllBytes(path)
val fileName = path.fileName.toString().lowercase()

when {
fileName.endsWith(".png") || fileName.endsWith(".jpg") || fileName.endsWith(".jpeg") -> {
imageFiles.add(content to filePath)
screenshots.add(AnalysisScreenshot(data = data, path = path))
}

fileName.startsWith("commands") -> {
textFiles.add(content to filePath)
commands.add(AnalysisLog(data = data, path = path))
}

fileName == "maestro.log" -> {
textFiles.add(content to filePath)
logs.add(AnalysisLog(data = data, path = path))
}
}
}

return Pair(imageFiles, textFiles)
return AnalysisDebugFiles(
logs = logs,
commands = commands,
screenshots = screenshots,
)
}

/**
Expand Down

0 comments on commit 481ab04

Please sign in to comment.