Skip to content

Commit

Permalink
add open chat event telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
reymondzzzz committed Dec 11, 2024
1 parent 854e990 commit fc1a3c8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/kotlin/com/smallcloud/refactai/Resources.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ object Resources {
val defaultCodeCompletionUrlSuffix = URI("v1/code-completion")
val cloudUserMessage: URI = defaultCloudUrl.resolve("/v1/user-message")
val defaultReportUrlSuffix: URI = URI("v1/telemetry-network")
val defaultChatReportUrlSuffix: URI = URI("v1/telemetry-chat")
val defaultSnippetAcceptedUrlSuffix: URI = URI("v1/snippet-accepted")
val version: String = getVersion()
const val client: String = "jetbrains"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package com.smallcloud.refactai.code_lens

import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.service
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.LogicalPosition
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.wm.ToolWindowManager
import com.smallcloud.refactai.Resources
import com.smallcloud.refactai.panes.RefactAIToolboxPaneFactory
import com.smallcloud.refactai.statistic.UsageStatistic
import com.smallcloud.refactai.statistic.UsageStats
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.io.path.relativeTo

Expand Down Expand Up @@ -50,6 +53,7 @@ class CodeLensAction(
chat?.activate {
RefactAIToolboxPaneFactory.chat?.requestFocus()
RefactAIToolboxPaneFactory.chat?.executeCodeLensCommand(formatMessage(), sendImmediately, openNewTab)
editor.project?.service<UsageStats>()?.addChatStatistic(true, UsageStatistic("openChatByCodelens"), "")
}

// If content is empty, then it's "Open Chat" instruction, selecting range of code in active tab
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package com.smallcloud.refactai.panes.sharedchat

import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.components.service
import com.intellij.openapi.wm.ToolWindowManager
import com.smallcloud.refactai.Resources
import com.smallcloud.refactai.panes.RefactAIToolboxPaneFactory
import com.smallcloud.refactai.statistic.UsageStatistic
import com.smallcloud.refactai.statistic.UsageStats
import com.smallcloud.refactai.utils.getLastUsedProject

class ChatPaneInvokeAction: AnAction(Resources.Icons.LOGO_RED_16x16) {
Expand All @@ -14,8 +17,9 @@ class ChatPaneInvokeAction: AnAction(Resources.Icons.LOGO_RED_16x16) {

fun actionPerformed() {
val chat = ToolWindowManager.getInstance(getLastUsedProject()).getToolWindow("Refact")
chat?.activate{
chat?.activate {
RefactAIToolboxPaneFactory.focusChat()
getLastUsedProject().service<UsageStats>().addChatStatistic(true, UsageStatistic("openChatByShortcut"), "")
}
}
}
43 changes: 43 additions & 0 deletions src/main/kotlin/com/smallcloud/refactai/statistic/UsageStats.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import com.intellij.util.concurrency.AppExecutorUtil
import com.smallcloud.refactai.Resources.defaultChatReportUrlSuffix
import com.smallcloud.refactai.Resources.defaultReportUrlSuffix
import com.smallcloud.refactai.Resources.defaultSnippetAcceptedUrlSuffix
import com.smallcloud.refactai.io.sendRequest
Expand Down Expand Up @@ -93,6 +94,48 @@ class UsageStats(private val project: Project): Disposable {
}
}
}

fun addChatStatistic(
positive: Boolean,
stat: UsageStatistic,
errorMessage: Any
) {
var errorMessageStr = errorMessage.toString()
val gson = Gson()
if (errorMessageStr.length > 200) {
errorMessageStr = errorMessageStr.substring(0, 200) + ""
}

val errorMessageJson = gson.toJson(errorMessageStr)
var scope = stat.scope
if (stat.subScope.isNotEmpty()) {
scope += ":" + stat.subScope
}

val scopeJson = gson.toJson(scope)
val body = gson.toJson(
mapOf(
"success" to positive,
"error_message" to errorMessageJson,
"scope" to scopeJson,
)
)
val url = getLSPProcessHolder(project)!!.url.resolve(defaultChatReportUrlSuffix)
execService.submit {
try {
val res = sendRequest(url, "POST", body=body)
if (res.body.isNullOrEmpty()) return@submit

val json = gson.fromJson(res.body, JsonObject::class.java)
val success = if (json.has("success")) json.get("success").asInt else null
if (success != null && success != 1) {
throw Exception(json.get("human_readable_message").asString)
}
} catch (e: Exception) {
Logger.getInstance(UsageStats::class.java).warn("report to $url failed: $e")
}
}
}

companion object {
@JvmStatic
Expand Down

0 comments on commit fc1a3c8

Please sign in to comment.