Skip to content

Commit

Permalink
protocol commands
Browse files Browse the repository at this point in the history
  • Loading branch information
shalom938 committed Sep 25, 2024
1 parent ee81604 commit 073b22f
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>, 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<String, String>, 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
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,6 +38,7 @@ class DigmaProtocolCommand : JBProtocolCommand(DIGMA_COMMAND) {
}
}

//returns null on success, message on failure
private suspend fun executeImpl(target: String?, parameters: Map<String, String>, fragment: String?): String? {

if (target != DIGMA_PLUGIN_TARGET) {
Expand Down Expand Up @@ -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"

Expand All @@ -98,14 +96,14 @@ class DigmaProtocolCommand : JBProtocolCommand(DIGMA_COMMAND) {
}

Log.log(logger::trace, "executing action {}", action)
project.service<DigmaProtocolApi>().performAction(project, action, waitForJcef)
val result = project.service<DigmaProtocolApi>().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"

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {


Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
package org.digma.intellij.plugin.protocol

import com.intellij.util.text.nullize
import java.net.URLEncoder


fun Map<String, String>.toUrlQueryString() =
this.map {(k,v) -> "${URLEncoder.encode(k, "UTF-8")}=${URLEncoder.encode(v, "UTF-8")}" }
.joinToString("&")
.joinToString("&")


fun getActionFromParameters(parameters: Map<String, String>):String?{
return parameters[ACTION_PARAM_NAME]?.nullize(nullizeSpaces = true)
}


fun getCodeObjectIdFromParameters(parameters: Map<String, String>):String?{
return parameters[CODE_OBJECT_ID_PARAM_NAME]?.nullize(nullizeSpaces = true)
}

fun getEnvironmentIdFromParameters(parameters: Map<String, String>):String?{
return parameters[ENVIRONMENT_ID_PARAM_NAME]?.nullize(nullizeSpaces = true)
}


Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
Expand Down

0 comments on commit 073b22f

Please sign in to comment.