Skip to content

Commit 18500b8

Browse files
Implemented pagination for Duration Breakdown insight
1 parent 4350515 commit 18500b8

File tree

2 files changed

+129
-31
lines changed

2 files changed

+129
-31
lines changed

src/main/kotlin/org/digma/intellij/plugin/ui/list/insights/DurationBreakdownPanel.kt

Lines changed: 110 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,21 @@ import org.digma.intellij.plugin.model.rest.insights.SpanDurationBreakdownInsigh
1010
import org.digma.intellij.plugin.service.InsightsActionsService
1111
import org.digma.intellij.plugin.ui.common.*
1212
import org.digma.intellij.plugin.ui.list.PanelsLayoutHelper
13+
import org.digma.intellij.plugin.ui.panels.DigmaResettablePanel
1314
import java.awt.BorderLayout
14-
import java.awt.Dimension
1515
import java.awt.GridLayout
16-
import javax.swing.JComponent
17-
import javax.swing.JLabel
18-
import javax.swing.JPanel
19-
import javax.swing.SwingConstants
16+
import javax.swing.*
2017

21-
const val P_50: Float = 0.5F
18+
private const val P_50: Float = 0.5F
19+
private const val RECORDS_PER_PAGE = 3
20+
21+
private var lastPageNum = 0
22+
private var currPageNum = 0
23+
private var prev: ActionLink? = null
24+
private var next: ActionLink? = null
25+
private var resultBreakdownPanel: DigmaResettablePanel? = null
26+
private val paginationPanel = JPanel()
27+
val durationBreakdownEntriesToDisplay = ArrayList<SpanDurationBreakdown>()
2228

2329
fun spanDurationBreakdownPanel(
2430
project: Project,
@@ -27,25 +33,51 @@ fun spanDurationBreakdownPanel(
2733
panelsLayoutHelper: PanelsLayoutHelper
2834
): JPanel {
2935

30-
val resultBreakdownPanel = JBPanel<JBPanel<*>>()
31-
resultBreakdownPanel.layout = GridLayout(insight.breakdownEntries.size, 1, 0, 2)
32-
resultBreakdownPanel.isOpaque = false
36+
resultBreakdownPanel = object : DigmaResettablePanel() {
37+
override fun reset() {
38+
buildDurationBreakdownRowPanel(
39+
resultBreakdownPanel!!,
40+
durationBreakdownEntriesToDisplay,
41+
project,
42+
moreData,
43+
panelsLayoutHelper
44+
)
45+
buildPaginationPanel(paginationPanel)
46+
}
47+
}
3348

34-
insight.breakdownEntries
49+
val validBreakdownEntries = insight.breakdownEntries
3550
.filter { entry -> entry.percentiles.any { breakdown -> breakdown.percentile.equals(P_50) } }
3651
.sortedWith(compareByDescending { getValueOfPercentile(it, P_50) })
37-
.forEach { durationBreakdown: SpanDurationBreakdown ->
38-
val durationBreakdownPanel = durationBreakdownRowPanel(durationBreakdown, project, moreData, panelsLayoutHelper)
39-
resultBreakdownPanel.add(durationBreakdownPanel)
40-
}
4152

53+
//calculate how many pages there are
54+
lastPageNum = validBreakdownEntries.size / RECORDS_PER_PAGE + if (validBreakdownEntries.size % RECORDS_PER_PAGE != 0) 1 else 0
55+
currPageNum = if (lastPageNum > 0) 1 else 0
56+
57+
updateDurationBreakdownPanel(validBreakdownEntries)
4258
return createInsightPanel(
4359
"Duration Breakdown",
4460
"",
4561
Laf.Icons.Insight.DURATION,
4662
resultBreakdownPanel,
47-
null,
48-
panelsLayoutHelper)
63+
paginationRowPanel(validBreakdownEntries)
64+
)
65+
}
66+
67+
fun buildDurationBreakdownRowPanel(
68+
durationBreakdownPanel: DigmaResettablePanel,
69+
durationBreakdownEntries: List<SpanDurationBreakdown>,
70+
project: Project,
71+
moreData: HashMap<String, Any>,
72+
layoutHelper: PanelsLayoutHelper
73+
) {
74+
durationBreakdownPanel.removeAll()
75+
resultBreakdownPanel!!.layout = GridLayout(durationBreakdownEntries.size, 1, 0, 2)
76+
resultBreakdownPanel!!.isOpaque = false
77+
78+
durationBreakdownEntries.forEach { durationBreakdown: SpanDurationBreakdown ->
79+
resultBreakdownPanel!!.add(durationBreakdownRowPanel(durationBreakdown, project, moreData, layoutHelper))
80+
}
4981
}
5082

5183
fun durationBreakdownRowPanel(
@@ -66,6 +98,65 @@ fun durationBreakdownRowPanel(
6698
return durationBreakdownPanel
6799
}
68100

101+
fun paginationRowPanel(
102+
durationBreakdownEntries: List<SpanDurationBreakdown>
103+
): JPanel? {
104+
if (lastPageNum < 2) {
105+
return null
106+
}
107+
prev = ActionLink("Prev")
108+
prev!!.addActionListener {
109+
if (--currPageNum <= 0) currPageNum = 1
110+
updateDurationBreakdownPanel(durationBreakdownEntries)
111+
}
112+
next = ActionLink("Next")
113+
next!!.addActionListener {
114+
if (++currPageNum > lastPageNum) currPageNum = lastPageNum
115+
updateDurationBreakdownPanel(durationBreakdownEntries)
116+
}
117+
buildPaginationPanel(paginationPanel)
118+
return paginationPanel
119+
}
120+
121+
fun buildPaginationPanel(paginationPanel: JPanel) {
122+
paginationPanel.removeAll()
123+
124+
paginationPanel.layout = BorderLayout()
125+
paginationPanel.border = empty()
126+
paginationPanel.isOpaque = false
127+
128+
val paginationLabelText = "$currPageNum of $lastPageNum"
129+
val paginationLabel = JLabel(asHtml(paginationLabelText), SwingConstants.LEFT)
130+
paginationLabel.border = empty(0, 5, 0, 0)
131+
132+
prev?.let { paginationPanel.add(it, BorderLayout.WEST) }
133+
next?.let { paginationPanel.add(it, BorderLayout.CENTER) }
134+
paginationPanel.add(paginationLabel, BorderLayout.EAST)
135+
136+
val canGoBack = currPageNum > 1
137+
val canGoFwd = currPageNum != lastPageNum
138+
prev?.let { it.isEnabled = canGoBack }
139+
next?.let { it.isEnabled = canGoFwd }
140+
}
141+
142+
143+
private fun updateDurationBreakdownPanel(durationBreakdownEntries: List<SpanDurationBreakdown>) {
144+
durationBreakdownEntriesToDisplay.clear()
145+
146+
if (durationBreakdownEntries.isNotEmpty()) {
147+
val start = (currPageNum - 1) * RECORDS_PER_PAGE
148+
var end = start + RECORDS_PER_PAGE
149+
if (end >= durationBreakdownEntries.size) {
150+
end = durationBreakdownEntries.size
151+
}
152+
for (i in start until end) {
153+
durationBreakdownEntriesToDisplay.add(durationBreakdownEntries[i])
154+
}
155+
}
156+
157+
resultBreakdownPanel!!.reset()
158+
}
159+
69160
private fun getDurationBreakdownPanel(): JBPanel<JBPanel<*>> {
70161
val durationBreakdownPanel = JBPanel<JBPanel<*>>()
71162
durationBreakdownPanel.layout = BorderLayout(5, 0)
@@ -117,18 +208,7 @@ private fun getBreakdownDurationLabelPanel(
117208
pLabel.toolTipText = getTooltipForDurationLabel(durationBreakdown)
118209
boldFonts(pLabel)
119210

120-
val breakdownDurationLabelPanel = object : JPanel() {
121-
override fun getPreferredSize(): Dimension {
122-
val ps = super.getPreferredSize()
123-
if (ps == null) {
124-
return ps
125-
}
126-
val h = ps.height
127-
val w = ps.width
128-
addCurrentLargestWidthDurationPLabel(layoutHelper, w)
129-
return Dimension(getCurrentLargestWidthDurationPLabel(layoutHelper, w), h)
130-
}
131-
}
211+
val breakdownDurationLabelPanel = JPanel()
132212
breakdownDurationLabelPanel.layout = BorderLayout()
133213
breakdownDurationLabelPanel.border = empty()
134214
breakdownDurationLabelPanel.isOpaque = false
@@ -159,8 +239,8 @@ private fun getValueOfPercentile(breakdownEntry: SpanDurationBreakdown, requeste
159239
private fun getTooltipForDurationLabel(breakdownEntry: SpanDurationBreakdown): String {
160240
val sortedPercentiles = breakdownEntry.percentiles.sortedBy { it.percentile }
161241
var tooltip = "Percentage of time spent in span:".plus("<br>")
162-
for(p in sortedPercentiles){
163-
tooltip += "P${(p.percentile*100).toInt()}: ${p.duration.value} ${p.duration.unit}".plus("<br>")
242+
for (p in sortedPercentiles) {
243+
tooltip += "P${(p.percentile * 100).toInt()}: ${p.duration.value} ${p.duration.unit}".plus("<br>")
164244
}
165245
return asHtml(tooltip)
166246
}

src/main/kotlin/org/digma/intellij/plugin/ui/list/insights/InsightsCommon.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,15 @@ fun insightItemPanel(panel: JPanel): JPanel {
2424
return commonListItemPanel(panel)
2525
}
2626

27-
2827
fun createInsightPanel(title: String, description: String, icon: Icon, body: JComponent?, buttons: List<JButton?>?, panelsLayoutHelper: PanelsLayoutHelper): JPanel {
28+
return createInsightPanel(title, description, icon, body, buttons, null, panelsLayoutHelper)
29+
}
30+
31+
fun createInsightPanel(title: String, description: String, icon: Icon, body: JComponent?, footer: JComponent?): JPanel {
32+
return createInsightPanel(title, description, icon, body, null, footer, null)
33+
}
34+
35+
fun createInsightPanel(title: String, description: String, icon: Icon, body: JComponent?, buttons: List<JButton?>?, footer: JComponent?, panelsLayoutHelper: PanelsLayoutHelper?): JPanel {
2936

3037
// .-----------------------------------.
3138
// | title | icon |
@@ -70,6 +77,17 @@ fun createInsightPanel(title: String, description: String, icon: Icon, body: JCo
7077
bodyWrapper.add(buttonsList, BorderLayout.SOUTH)
7178
}
7279

80+
if(footer != null){
81+
val footerComponent = JPanel(FlowLayout(FlowLayout.LEFT, 0 ,0))
82+
footerComponent.isOpaque = false
83+
footerComponent.border = empty()
84+
85+
footerComponent.add(Box.createHorizontalStrut(5))
86+
footerComponent.add(footer)
87+
88+
bodyWrapper.add(footerComponent, BorderLayout.SOUTH)
89+
}
90+
7391
result.add(bodyWrapper,BorderLayout.SOUTH)
7492
}
7593

0 commit comments

Comments
 (0)