Skip to content

Commit 8d70ce4

Browse files
committed
Show chart option on Code References page where there is history available.
1 parent 3f826f5 commit 8d70ce4

File tree

1 file changed

+77
-2
lines changed

1 file changed

+77
-2
lines changed

invert-report/src/jsMain/kotlin/com/squareup/invert/common/pages/CodeReferencesReportPage.kt

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ import com.squareup.invert.common.DependencyGraph
88
import com.squareup.invert.common.InvertReportPage
99
import com.squareup.invert.common.ModuleOwnerAndCodeReference
1010
import com.squareup.invert.common.ReportDataRepo
11+
import com.squareup.invert.common.charts.ChartJsLineChartComposable
12+
import com.squareup.invert.common.charts.ChartsJs
1113
import com.squareup.invert.common.charts.PlotlyTreeMapComposable
1214
import com.squareup.invert.common.navigation.NavPage
1315
import com.squareup.invert.common.navigation.NavRouteRepo
1416
import com.squareup.invert.common.navigation.routes.BaseNavRoute
1517
import com.squareup.invert.common.pages.CodeReferencesNavRoute.Companion.parser
18+
import com.squareup.invert.common.utils.FormattingUtils.formatEpochToDate
1619
import com.squareup.invert.models.ExtraKey
1720
import com.squareup.invert.models.ModulePath
1821
import com.squareup.invert.models.OwnerName
1922
import com.squareup.invert.models.StatDataType
23+
import com.squareup.invert.models.StatKey
2024
import com.squareup.invert.models.js.StatTotalAndMetadata
2125
import org.jetbrains.compose.web.dom.A
2226
import org.jetbrains.compose.web.dom.H3
@@ -39,6 +43,7 @@ data class CodeReferencesNavRoute(
3943
val owner: String? = null,
4044
val module: String? = null,
4145
val treemap: Boolean? = null,
46+
val chart: Boolean? = null,
4247
) : BaseNavRoute(CodeReferencesReportPage.navPage) {
4348

4449
override fun toSearchParams(): Map<String, String> = toParamsWithOnlyPageId(this)
@@ -55,11 +60,15 @@ data class CodeReferencesNavRoute(
5560
treemap?.let {
5661
params[TREEMAP_PARAM] = treemap.toString()
5762
}
63+
chart?.let {
64+
params[CHART_PARAM] = chart.toString()
65+
}
5866
}
5967

6068
companion object {
6169

6270
private const val STATKEY_PARAM = "statkey"
71+
private const val CHART_PARAM = "chart"
6372
private const val OWNER_PARAM = "owner"
6473
private const val MODULE_PARAM = "module"
6574
private const val TREEMAP_PARAM = "treemap"
@@ -87,11 +96,19 @@ data class CodeReferencesNavRoute(
8796
null
8897
}
8998
}
99+
val chart = params[CHART_PARAM]?.trim()?.let {
100+
if (it.isNotBlank()) {
101+
it.toBoolean()
102+
} else {
103+
null
104+
}
105+
}
90106
return CodeReferencesNavRoute(
91107
statKey = statKey,
92108
owner = owner,
93109
module = module,
94110
treemap = treemap,
111+
chart = chart,
95112
)
96113
}
97114
}
@@ -119,15 +136,18 @@ fun CodeReferencesComposable(
119136
navRouteRepo: NavRouteRepo = DependencyGraph.navRouteRepo,
120137
) {
121138
val allModulesOrig by reportDataRepo.allModules.collectAsState(null)
139+
val historicalDataOrig by reportDataRepo.historicalData.collectAsState(null)
122140
val allOwnerNames by reportDataRepo.allOwnerNames.collectAsState(null)
123141
val moduleToOwnerMapFlowValue: Map<ModulePath, OwnerName>? by reportDataRepo.moduleToOwnerMap.collectAsState(null)
124142

125143
val statInfosOrig by reportDataRepo.statInfos.collectAsState(null)
126-
if (statInfosOrig == null || allOwnerNames == null) {
144+
if (statInfosOrig == null || allOwnerNames == null || historicalDataOrig == null) {
127145
BootstrapLoadingMessageWithSpinner("Loading Stats")
128146
return
129147
}
130148

149+
val historicalData = historicalDataOrig!!
150+
131151
val metadata by reportDataRepo.reportMetadata.collectAsState(null)
132152
BootstrapRow {
133153
BootstrapColumn(8) {
@@ -142,7 +162,6 @@ fun CodeReferencesComposable(
142162
}
143163
}
144164
BootstrapColumn(4) {
145-
146165
codeReferencesNavRoute.statKey?.let { statKey ->
147166
P {
148167
Ul {
@@ -155,6 +174,29 @@ fun CodeReferencesComposable(
155174
Text("View Grouped by Module")
156175
}
157176
}
177+
if (historicalData.size > 1) {
178+
Li {
179+
A("#", {
180+
onClick {
181+
navRouteRepo.updateNavRoute(
182+
codeReferencesNavRoute.copy(
183+
chart = if (codeReferencesNavRoute.chart != null) {
184+
!codeReferencesNavRoute.chart
185+
} else {
186+
true
187+
}
188+
)
189+
)
190+
}
191+
}) {
192+
if (codeReferencesNavRoute.chart == true) {
193+
Text("Hide Chart")
194+
} else {
195+
Text("Show Chart")
196+
}
197+
}
198+
}
199+
}
158200
Li {
159201
A("#", {
160202
onClick {
@@ -283,6 +325,39 @@ fun CodeReferencesComposable(
283325
}
284326
}
285327

328+
if (codeReferencesNavRoute.chart == true) {
329+
BootstrapRow {
330+
BootstrapColumn {
331+
val datasets = mutableListOf<ChartsJs.ChartJsDataset>()
332+
val currentHistoricalData = historicalData.last()
333+
val remainingStatKeys: List<StatKey> = listOf(statKey)
334+
remainingStatKeys.forEach { remainingStatKey ->
335+
val values: List<Int> = historicalData.map { historicalDataPoint ->
336+
historicalDataPoint.statTotalsAndMetadata.statTotals[remainingStatKey]?.total ?: 0
337+
}
338+
val remainingStat = currentHistoricalData.statTotalsAndMetadata.statTotals[remainingStatKey]!!.metadata
339+
datasets.add(
340+
ChartsJs.ChartJsDataset(
341+
label = remainingStat.description,
342+
data = values
343+
)
344+
)
345+
}
346+
347+
val chartJsData = ChartsJs.ChartJsData(
348+
labels = historicalData.map { formatEpochToDate(it.reportMetadata.latestCommitTime) },
349+
datasets = datasets,
350+
)
351+
352+
ChartJsLineChartComposable(
353+
data = chartJsData,
354+
onClick = { label, value ->
355+
356+
})
357+
}
358+
}
359+
}
360+
286361
val codeReferencesByOwner = allCodeReferencesForStat.groupBy { it.owner }
287362
val totalCodeReferenceCount = allCodeReferencesForStat.size
288363
BootstrapRow {

0 commit comments

Comments
 (0)