diff --git a/ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/DigmaProtocolApi.kt b/ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/DigmaProtocolApi.kt index af1f5eb50..0f606b45f 100644 --- a/ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/DigmaProtocolApi.kt +++ b/ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/DigmaProtocolApi.kt @@ -9,28 +9,94 @@ import kotlinx.coroutines.launch import org.digma.intellij.plugin.common.DisposableAdaptor import org.digma.intellij.plugin.errorreporting.ErrorReporter import org.digma.intellij.plugin.log.Log +import org.digma.intellij.plugin.scope.ScopeManager +import org.digma.intellij.plugin.scope.SpanScope -const val ACTION_ASSETS = "assets" +const val ACTION_PARAM_NAME = "action" +const val ACTION_SHOW_ASSET_PARAM_NAME = "showAsset" +const val CODE_OBJECT_ID_PARAM_NAME = "codeObjectId" +const val ENVIRONMENT_ID_PARAM_NAME = "environmentId" +const val ACTION_SHOW_ASSETS_TAB_PARAM_NAME = "showAssetsTab" @Service(Service.Level.PROJECT) class DigmaProtocolApi(val cs: CoroutineScope) : DisposableAdaptor { private val logger: Logger = Logger.getInstance(this::class.java) - fun performAction(project: Project, action: String, waitForJcef: Boolean) { + /** + * return null on success. + * error message on failure + */ + fun performAction(project: Project, parameters: Map, waitForJcef: Boolean): String? { try { + + val action = getActionFromParameters(parameters) ?: return "DigmaProtocolCommand no action in request" + Log.log(logger::trace, "perform action {}, thread {}", action, Thread.currentThread().name) - cs.launch { - if (waitForJcef) { - delay(5000) + return when (action) { + ACTION_SHOW_ASSET_PARAM_NAME -> { + showAsset(project, parameters, waitForJcef) + } + + ACTION_SHOW_ASSETS_TAB_PARAM_NAME -> { + showAssetTab(project, action, waitForJcef) + } + + else -> { + "DigmaProtocolCommand unknown action in request $action" } - project.messageBus.syncPublisher(ProtocolCommandEvent.PROTOCOL_COMMAND_TOPIC).protocolCommand(action) } + } catch (e: Throwable) { ErrorReporter.getInstance().reportError("DigmaProtocolApi.performAction", e) + return "DigmaProtocolCommand error $e" } } + + + private fun showAsset(project: Project, parameters: Map, waitForJcef: Boolean): String? { + + val codeObjectId = getCodeObjectIdFromParameters(parameters) + val environmentId = getEnvironmentIdFromParameters(parameters) + + Log.log( + logger::trace, + "showing asset, codeObjectId='{}', environment='{}', thread='{}'", + codeObjectId, + environmentId, + Thread.currentThread().name + ) + + if (codeObjectId == null) { + return "DigmaProtocolCommand no code object id in request" + } + + cs.launch { + if (waitForJcef) { + delay(5000) + } + + val scope = SpanScope(codeObjectId) + + ScopeManager.getInstance(project).changeScope(scope, null, environmentId) + } + + return null + } + + + private fun showAssetTab(project: Project, action: String, waitForJcef: Boolean): String? { + cs.launch { + if (waitForJcef) { + delay(5000) + } + project.messageBus.syncPublisher(ProtocolCommandEvent.PROTOCOL_COMMAND_TOPIC).protocolCommand(action) + } + return null + } + + } \ No newline at end of file diff --git a/ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/DigmaProtocolCommand.kt b/ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/DigmaProtocolCommand.kt index 6c62a044d..138972004 100644 --- a/ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/DigmaProtocolCommand.kt +++ b/ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/DigmaProtocolCommand.kt @@ -12,18 +12,15 @@ import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.JBProtocolCommand import com.intellij.openapi.components.service import com.intellij.openapi.diagnostic.Logger -import com.intellij.util.text.nullize import org.digma.intellij.plugin.common.findActiveProject import org.digma.intellij.plugin.errorreporting.ErrorReporter import org.digma.intellij.plugin.log.Log import org.digma.intellij.plugin.ui.ToolWindowShower import java.nio.file.Path -import java.util.concurrent.CompletableFuture -import java.util.concurrent.Future const val DIGMA_COMMAND = "digma" const val DIGMA_PLUGIN_TARGET = "plugin" -const val ACTION_NAME_KEY = "action" + //jetbrains://idea/digma/plugin?action=assets //jetbrains://idea/digma/plugin?project=spring-petclinic&action=assets @@ -41,6 +38,7 @@ class DigmaProtocolCommand : JBProtocolCommand(DIGMA_COMMAND) { } } + //returns null on success, message on failure private suspend fun executeImpl(target: String?, parameters: Map, fragment: String?): String? { if (target != DIGMA_PLUGIN_TARGET) { @@ -82,7 +80,7 @@ class DigmaProtocolCommand : JBProtocolCommand(DIGMA_COMMAND) { Log.log(logger::trace, "got project {}", project.name) - val action = parameters[ACTION_NAME_KEY]?.nullize(nullizeSpaces = true) + val action = getActionFromParameters(parameters) // ?: return CompletableFuture.completedFuture("DigmaProtocolCommand no action in request") ?: return "DigmaProtocolCommand no action in request" @@ -98,14 +96,14 @@ class DigmaProtocolCommand : JBProtocolCommand(DIGMA_COMMAND) { } Log.log(logger::trace, "executing action {}", action) - project.service().performAction(project, action, waitForJcef) + val result = project.service().performAction(project, parameters, waitForJcef) Log.log(logger::trace, "after execute action {}", action) // return CompletableFuture.completedFuture(null) - return null + return result } // return CompletableFuture.completedFuture("DigmaProtocolCommand can not find project") - return "DigmaProtocolCommand can not find project" + return "DigmaProtocolCommand can not open any project" } diff --git a/ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/ProtocolCommandEvent.kt b/ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/ProtocolCommandEvent.kt index 78a9e97dd..69c702681 100644 --- a/ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/ProtocolCommandEvent.kt +++ b/ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/ProtocolCommandEvent.kt @@ -7,6 +7,7 @@ import com.intellij.util.messages.Topic * this is an application event that should fire when we change the api client, * usually when user changes the api url in settings. */ +//todo: maybe we don't need it fun interface ProtocolCommandEvent { diff --git a/ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/protocol-utils.kt b/ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/protocol-utils.kt index 1d957e958..7183433d3 100644 --- a/ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/protocol-utils.kt +++ b/ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/protocol-utils.kt @@ -1,8 +1,25 @@ package org.digma.intellij.plugin.protocol +import com.intellij.util.text.nullize import java.net.URLEncoder fun Map.toUrlQueryString() = this.map {(k,v) -> "${URLEncoder.encode(k, "UTF-8")}=${URLEncoder.encode(v, "UTF-8")}" } - .joinToString("&") \ No newline at end of file + .joinToString("&") + + +fun getActionFromParameters(parameters: Map):String?{ + return parameters[ACTION_PARAM_NAME]?.nullize(nullizeSpaces = true) +} + + +fun getCodeObjectIdFromParameters(parameters: Map):String?{ + return parameters[CODE_OBJECT_ID_PARAM_NAME]?.nullize(nullizeSpaces = true) +} + +fun getEnvironmentIdFromParameters(parameters: Map):String?{ + return parameters[ENVIRONMENT_ID_PARAM_NAME]?.nullize(nullizeSpaces = true) +} + + diff --git a/src/main/kotlin/org/digma/intellij/plugin/ui/mainapp/MainAppService.kt b/src/main/kotlin/org/digma/intellij/plugin/ui/mainapp/MainAppService.kt index 778cc0ae2..6da0e1d8c 100644 --- a/src/main/kotlin/org/digma/intellij/plugin/ui/mainapp/MainAppService.kt +++ b/src/main/kotlin/org/digma/intellij/plugin/ui/mainapp/MainAppService.kt @@ -11,7 +11,7 @@ import kotlinx.datetime.toKotlinInstant import org.digma.intellij.plugin.engagement.EngagementScoreService import org.digma.intellij.plugin.log.Log import org.digma.intellij.plugin.persistence.PersistenceService -import org.digma.intellij.plugin.protocol.ACTION_ASSETS +import org.digma.intellij.plugin.protocol.ACTION_SHOW_ASSETS_TAB_PARAM_NAME import org.digma.intellij.plugin.protocol.ProtocolCommandEvent import org.digma.intellij.plugin.scheduling.disposingPeriodicTask import org.digma.intellij.plugin.ui.jcef.JCefComponent @@ -36,7 +36,7 @@ class MainAppService(private val project: Project) : Disposable { jCefComponent?.let { jcefComp -> when (action) { - ACTION_ASSETS -> { + ACTION_SHOW_ASSETS_TAB_PARAM_NAME -> { Log.log(logger::trace,"sending generic event to open assets page") sendGenericPluginEvent(project, jcefComp.jbCefBrowser.cefBrowser, "first asset notification link click") }