11package org.digma.intellij.plugin.ui.list.insights
22
3+ import com.google.common.io.CharStreams
34import com.intellij.openapi.fileEditor.impl.HTMLEditorProvider
45import com.intellij.openapi.project.Project
56import com.intellij.ui.components.ActionLink
@@ -31,6 +32,7 @@ import org.threeten.extra.AmountFormats
3132import java.awt.BorderLayout
3233import java.awt.Dimension
3334import java.awt.GridLayout
35+ import java.io.InputStreamReader
3436import java.sql.Timestamp
3537import java.util.Locale
3638import java.util.concurrent.TimeUnit
@@ -41,6 +43,21 @@ import javax.swing.SwingConstants
4143import kotlin.math.abs
4244import kotlin.math.max
4345
46+ class SpanPanels {
47+
48+ companion object {
49+ val JAEGER_EMBEDDED_HTML_TEMPLATE : String
50+
51+ init {
52+ this ::class .java.getResourceAsStream(" /templates/Jaeger-embedded-template.html" ).use {
53+ val loadedContent = CharStreams .toString(InputStreamReader (it))
54+ JAEGER_EMBEDDED_HTML_TEMPLATE = loadedContent
55+ }
56+ }
57+ }
58+
59+ }
60+
4461fun spanPanel (spanInsight : SpanInsight ): JPanel {
4562
4663 val title = JLabel (asHtml(spanBold(" Top Usage" )), SwingConstants .LEFT )
@@ -176,8 +193,8 @@ fun spanDurationPanel(
176193 }
177194
178195 val buttonToGraph = buildButtonToPercentilesGraph(project, spanDurationsInsight.span)
179- val settingsState = SettingsState .getInstance (project)
180- val iconPanel = buildIconPanelWithLinks(settingsState, traceSamples, buttonToGraph )
196+ val buttonToJaeger = buildButtonToJaeger (project, spanDurationsInsight.span, traceSamples )
197+ val iconPanel = buildIconPanelWithLinks(buttonToGraph, buttonToJaeger )
181198
182199 val result = JBPanel <JBPanel <* >>()
183200 result.layout = BorderLayout ()
@@ -189,10 +206,9 @@ fun spanDurationPanel(
189206}
190207
191208fun buildIconPanelWithLinks (
192- settingsState : SettingsState , traceSamples : List < TraceSample >, buttonToPercentilesGraph : JButton
209+ buttonToPercentilesGraph : JButton , buttonToJaeger : JButton ?
193210): JBPanel <* > {
194211
195- val jaegerUrl = buildLinkToJaeger(settingsState, traceSamples)
196212 val iconPanel = panel {
197213 row {
198214 icon(Laf .Icons .Insight .HISTOGRAM )
@@ -201,39 +217,58 @@ fun buildIconPanelWithLinks(
201217 row {
202218 cell(buttonToPercentilesGraph)
203219 }
204- if (jaegerUrl.isNotBlank() ) {
220+ if (buttonToJaeger != null ) {
205221 row {
206- val bl = browserLink(" Compare" , jaegerUrl)
207- bl.component.icon = null
208- bl.component.toolTipText = " Compare with Jaeger"
222+ cell(buttonToJaeger)
209223 }
210224 }
211225 }.andTransparent()
212226 return iconPanel
213227}
214228
215- fun buildLinkToJaeger (
216- settingsState : SettingsState , traceSamples : List <TraceSample >, embedLinks : Boolean = false
217- ): String {
229+ // if cannot create the button then would return null
230+ fun buildButtonToJaeger (
231+ project : Project , spanInfo : SpanInfo , traceSamples : List <TraceSample >
232+ ): JButton ? {
233+
234+ val settingsState = SettingsState .getInstance(project)
218235
219236 val jaegerBaseUrl = settingsState.jaegerUrl?.trim()?.trimEnd(' /' )
220237 if (jaegerBaseUrl.isNullOrBlank() || traceSamples.isNullOrEmpty()) {
221- return " "
238+ return null
222239 }
223240 val filtered = traceSamples.filter { x -> x.hasTraceId() }
224241 if (filtered.isNullOrEmpty()) {
225- return " "
242+ return null
226243 }
227- val embedPart = if (embedLinks) " &uiEmbed=v0" else " "
244+
245+ val caption: String
246+ val jaegerUrl: String
247+ val embedPart = " &uiEmbed=v0"
228248
229249 val trace1 = filtered[0 ].traceId?.lowercase()
230250 if (filtered.size == 1 ) {
231- return " ${jaegerBaseUrl} /trace/${trace1} ?cohort=${trace1}${embedPart} "
251+ caption = " A sample ${filtered[0 ].traceName} trace"
252+ jaegerUrl = " ${jaegerBaseUrl} /trace/${trace1} ?cohort=${trace1}${embedPart} "
253+ } else {
254+ // assuming it has (at least) size of 2
255+ val trace2 = filtered[1 ].traceId?.lowercase()
256+ caption = " Comparing: A sample ${filtered[0 ].traceName} trace with a ${filtered[1 ].traceName} trace"
257+ jaegerUrl = " ${jaegerBaseUrl} /trace/${trace1} ...${trace2} ?cohort=${trace1} &cohort=${trace2}${embedPart} "
232258 }
233259
234- // assuming it has (at least) size of 2
235- val trace2 = filtered[1 ].traceId?.lowercase()
236- return " ${jaegerBaseUrl} /trace/${trace1} ...${trace2} ?cohort=${trace1} &cohort=${trace2}${embedPart} "
260+ val htmlContent = SpanPanels .JAEGER_EMBEDDED_HTML_TEMPLATE
261+ .replace(" __JAEGER_EMBEDDED_URL__" , jaegerUrl)
262+ .replace(" __CAPTION__" , caption)
263+
264+ val editorTitle = " Jaeger sample traces of Span ${spanInfo.name} "
265+
266+ val button = ActionLink (" Compare" )
267+ button.addActionListener {
268+ HTMLEditorProvider .openEditor(project, editorTitle, htmlContent)
269+ }
270+
271+ return button
237272}
238273
239274fun buildButtonToPercentilesGraph (project : Project , span : SpanInfo ): ActionLink {
0 commit comments