-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathrender_html.go
More file actions
808 lines (769 loc) · 26.8 KB
/
render_html.go
File metadata and controls
808 lines (769 loc) · 26.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
// Copyright (C) 2021-2025 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
package report
import (
"bytes"
"fmt"
"html"
htmltemplate "html/template"
"log/slog"
"perfspect/internal/table"
"strings"
texttemplate "text/template" // nosemgrep
)
// Package-level maps for custom HTML renderers
var customHTMLRenderers = map[string]table.HTMLTableRenderer{}
var customHTMLMultiTargetRenderers = map[string]table.HTMLMultiTargetTableRenderer{}
// getCustomHTMLRenderer returns the custom renderer for a table, or nil if no custom renderer exists
func getCustomHTMLRenderer(tableName string) table.HTMLTableRenderer {
return customHTMLRenderers[tableName]
}
// getCustomHTMLMultiTargetRenderer returns the custom multi-target renderer for a table, or nil if no custom renderer exists
func getCustomHTMLMultiTargetRenderer(tableName string) table.HTMLMultiTargetTableRenderer {
return customHTMLMultiTargetRenderers[tableName]
}
// RegisterHTMLRenderer allows external packages to register custom HTML renderers for specific tables
func RegisterHTMLRenderer(tableName string, renderer table.HTMLTableRenderer) {
customHTMLRenderers[tableName] = renderer
}
// RegisterHTMLMultiTargetRenderer allows external packages to register custom multi-target HTML renderers for specific tables
func RegisterHTMLMultiTargetRenderer(tableName string, renderer table.HTMLMultiTargetTableRenderer) {
customHTMLMultiTargetRenderers[tableName] = renderer
}
func getHtmlReportBegin() string {
var sb strings.Builder
sb.WriteString(`<!--
* Copyright (C) 2024 Intel Corporation
* SPDX-License-Identifier: MIT
-->
`)
sb.WriteString(`<!DOCTYPE html>
<html lang="en">
`)
sb.WriteString("<head>\n")
sb.WriteString(` <meta charset="UTF-8">
<title>Intel® PerfSpect</title>
<link rel="icon" type="image/x-icon" href="https://www.intel.com/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1">
`)
// link the style sheets and javascript
sb.WriteString(`
<link rel="stylesheet" href="https://unpkg.com/normalize.css@8.0.1/normalize.css" integrity="sha384-M86HUGbBFILBBZ9ykMAbT3nVb0+2C7yZlF8X2CiKNpDOQjKroMJqIeGZ/Le8N2Qp" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/purecss@3.0.0/build/pure-min.css" integrity="sha384-X38yfunGUhNzHpBaEBsWLO+A0HDYOQi8ufWDkZ0k9e0eXz/tH3II7uKZ9msv++Ls" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://unpkg.com/chart.js@3.7.1/dist/chart.min.js" integrity="sha384-7NrRHqlWUj2hJl3a/dZj/a1GxuQc56mJ3aYsEnydBYrY1jR+RSt6SBvK3sHfj+mJ" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/d3-flame-graph@4.1.3/dist/d3-flamegraph.css">
<script type="text/javascript" src="https://d3js.org/d3.v7.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/d3-flame-graph@4.1.3/dist/d3-flamegraph.min.js"></script>
`)
// add content class style
sb.WriteString(`
<style>
.content {
padding: 0 2em;
line-height: 1.6em;
}
.content h2 {
font-weight: 300;
color: #888;
}
.content h2:before {
content: '';
display: block;
position: relative;
width: 0;
height: 5em;
margin-top: -5em
}
</style>
`)
// add sidebar class styles
sb.WriteString(`
<style>
.sidebar {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.5s;
padding-top: 30px;
padding-left: 0px;
}
.sidebar h1 {
position: absolute;
top: 0;
padding: 0px 8px 8px 35px;
text-decoration: none;
color: #fff;
background-color: #1f8dd6;
display: block;
transition: 0.3s;
}
.sidebar h2 {
padding: 8px 4px 2px 35px;
text-decoration: none;
color: #818181;
display: block;
}
.sidebar a {
padding: 0px 4px 2px 35px;
text-decoration: none;
color: #818181;
display: block;
transition: 0.3s;
}
.sidebar a:hover {
color: #f1f1f1;
}
.sidebar .togglebtn {
position: absolute;
top: 0;
right: 0px;
font-size: 25px;
padding-left: 5px;
color: #ccc;
background-color: #1f8dd6;
}
.sidebar .togglebtn:hover {
color: #666;
}
.field-description {
position: relative;
display: inline-block;
margin-left: 5px;
cursor: help;
}
.field-description .tooltip-icon {
color: #fff;
font-size: 12px;
border: 1px solid #2196F3;
border-radius: 50%;
width: 16px;
height: 16px;
text-align: center;
line-height: 14px;
background-color: #2196F3;
transition: background-color 0.2s, border-color 0.2s;
}
.field-description:hover .tooltip-icon {
background-color: #1976D2;
border-color: #1976D2;
}
.field-description .tooltip-text {
visibility: hidden;
width: 250px;
background-color: #333;
color: #fff;
text-align: left;
border-radius: 6px;
padding: 8px;
position: absolute;
z-index: 1000;
bottom: 125%;
left: 50%;
margin-left: -125px;
opacity: 0;
transition: opacity 0.3s;
font-size: 12px;
box-shadow: 0px 0px 6px rgba(0,0,0,0.2);
}
.field-description .tooltip-text::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #333 transparent transparent transparent;
}
.field-description:hover .tooltip-text {
visibility: visible;
opacity: 1;
}
</style>
`)
sb.WriteString("</head>\n")
return sb.String()
}
func getHtmlReportMenu(allTableValues []table.TableValues) string {
var sb strings.Builder
// if none of the tables have menu labels, don't add the sidebar
hasMenuLabels := false
for _, tableValues := range allTableValues {
if tableValues.MenuLabel != "" {
hasMenuLabels = true
break
}
}
if hasMenuLabels {
sb.WriteString("<div id=\"mySidebar\" class=\"sidebar\">\n")
sb.WriteString("<a href=\"#\" style=\"position: absolute;top: 0; padding-left: 7px; padding-right: 117px; color: #fff; background-color: #1f8dd6\">CONTENTS</a>\n")
sb.WriteString("<a href=\"javascript:void(0)\" class=\"togglebtn\" onclick=\"toggleNav()\"><</a>\n")
// insert menu items into sidebar
for _, tableValues := range allTableValues {
if tableValues.MenuLabel != "" {
fmt.Fprintf(&sb, "<a href=\"#%s\">%s</a>\n", html.EscapeString(tableValues.Name), html.EscapeString(tableValues.MenuLabel))
}
}
sb.WriteString("</div>\n") // end of sidebar
}
return sb.String()
}
func getHtmlReportSidebarJavascript() string {
return `
<script>
const widthOpen="225px"
const widthClosed="30px"
function openNav() {
document.getElementById("mySidebar").style.width = widthOpen;
document.getElementById("myTables").style.marginLeft = widthOpen;
document.querySelector(".togglebtn").innerHTML="<"
}
function closeNav() {
document.getElementById("mySidebar").style.width = widthClosed;
document.getElementById("myTables").style.marginLeft= widthClosed;
document.querySelector(".togglebtn").innerHTML=">"
}
function toggleNav() {
if (document.getElementById("mySidebar").style.width !== widthOpen) {
openNav()
} else {
closeNav()
}
}
// open on startup
openNav()
</script>
`
}
func createHtmlReport(allTableValues []table.TableValues, targetName string) (out []byte, err error) {
var sb strings.Builder
sb.WriteString(getHtmlReportBegin())
// body starts here
sb.WriteString("<body>\n")
sb.WriteString("<main class=\"content\">\n")
// add the sidebar/menu
sb.WriteString(getHtmlReportMenu(allTableValues))
// add the tables
sb.WriteString("<div id=\"myTables\">\n")
sb.WriteString("<h1>Intel® PerfSpect</h1>\n")
sb.WriteString(`
<noscript>
<h3>JavaScript is disabled. Functionality is limited.</h3>
</noscript>
`)
for _, tableValues := range allTableValues {
// print the table name
fmt.Fprintf(&sb, "<h2 id=\"%[1]s\">%[1]s</h2>\n", html.EscapeString(tableValues.Name))
// if there's no data in the table, print a message and continue
if len(tableValues.Fields) == 0 || len(tableValues.Fields[0].Values) == 0 {
msg := NoDataFound
if tableValues.NoDataFound != "" {
msg = tableValues.NoDataFound
}
sb.WriteString("<p>" + msg + "</p>\n")
continue
}
// render the tables
if renderer := getCustomHTMLRenderer(tableValues.Name); renderer != nil { // custom table renderer
sb.WriteString(renderer(tableValues, targetName))
} else {
sb.WriteString(DefaultHTMLTableRendererFunc(tableValues))
}
}
sb.WriteString("</div>\n") // end of myTables
sb.WriteString("</main>\n")
// add the sidebar toggle function
sb.WriteString(getHtmlReportSidebarJavascript())
sb.WriteString("</body>\n")
sb.WriteString("</html>\n")
out = []byte(sb.String())
return
}
func createHtmlReportMultiTarget(allTargetsTableValues [][]table.TableValues, targetNames []string, allTableNames []string) (out []byte, err error) {
if len(allTargetsTableValues) == 0 {
return nil, fmt.Errorf("no target table values provided")
}
var sb strings.Builder
sb.WriteString(getHtmlReportBegin())
// body starts here
sb.WriteString("<body>\n")
sb.WriteString("<main class=\"content\">\n")
// add the sidebar/menu
sb.WriteString(getHtmlReportMenu(allTargetsTableValues[0]))
// add the tables
sb.WriteString("<div id=\"myTables\">\n")
sb.WriteString("<h1>Intel® PerfSpect</h1>\n")
sb.WriteString(`
<noscript>
<h3>JavaScript is disabled. Functionality is limited.</h3>
</noscript>
`)
// print the tables in the order they were passed in
for _, tableName := range allTableNames {
oneTableValuesForAllTargets := []table.TableValues{}
// build list of target names and table.TableValues for targets that have values for this table
tableTargets := []string{}
tableValues := []table.TableValues{}
for targetIndex, targetTableValues := range allTargetsTableValues {
tableIndex := findTableIndex(targetTableValues, tableName)
if tableIndex == -1 {
continue
}
tableTargets = append(tableTargets, targetNames[targetIndex])
tableValues = append(tableValues, targetTableValues[tableIndex])
}
// loop through the targets that have values for this table
for targetIndex, targetTableValues := range tableValues {
targetName := tableTargets[targetIndex]
// render the table for this target if it has a custom renderer
if shouldRenderTablePerTarget(targetTableValues) {
// print the table name only one time per table
if targetIndex == 0 {
fmt.Fprintf(&sb, "<h2 id=\"%[1]s\">%[1]s</h2>\n", html.EscapeString(targetTableValues.Name))
}
// print the target name
fmt.Fprintf(&sb, "<h3>%s</h3>\n", targetName)
// if there's no data in the table, print a message and continue
if len(targetTableValues.Fields) == 0 || len(targetTableValues.Fields[0].Values) == 0 {
sb.WriteString("<p>" + NoDataFound + "</p>\n")
continue
}
if renderer := getCustomHTMLRenderer(targetTableValues.Name); renderer != nil { // custom table renderer
sb.WriteString(renderer(targetTableValues, targetNames[targetIndex]))
} else {
sb.WriteString(DefaultHTMLTableRendererFunc(targetTableValues))
}
} else { // if the table doesn't have a custom renderer, add it to the list to render as a multi-target table
oneTableValuesForAllTargets = append(oneTableValuesForAllTargets, targetTableValues)
}
}
// print the multi-target table, if any
if len(oneTableValuesForAllTargets) > 0 {
fmt.Fprintf(&sb, "<h2 id=\"%[1]s\">%[1]s</h2>\n", html.EscapeString(oneTableValuesForAllTargets[0].Name))
if renderer := getCustomHTMLMultiTargetRenderer(oneTableValuesForAllTargets[0].Name); renderer != nil {
sb.WriteString(renderer(oneTableValuesForAllTargets, targetNames))
} else {
// render the multi-target table
sb.WriteString(RenderMultiTargetTableValuesAsHTML(oneTableValuesForAllTargets, targetNames))
}
}
}
sb.WriteString("</div>\n") // end of myTables
sb.WriteString("</main>\n")
// add the sidebar toggle function
sb.WriteString(getHtmlReportSidebarJavascript())
sb.WriteString("</body>\n")
sb.WriteString("</html>\n")
out = []byte(sb.String())
return
}
// shouldRenderTablePerTarget determines if a table should be rendered individually per target
// rather than as a combined multi-target comparison table.
// Renders per-target if:
// - The table has multiple rows (HasRows=true), OR
// - The table has a custom single-target renderer
//
// Unless a specific multi-target renderer exists for the table.
func shouldRenderTablePerTarget(tableValues table.TableValues) bool {
hasCustomRenderer := getCustomHTMLRenderer(tableValues.Name) != nil
hasMultiTargetRenderer := getCustomHTMLMultiTargetRenderer(tableValues.Name) != nil
return (tableValues.HasRows || hasCustomRenderer) && !hasMultiTargetRenderer
}
// findTableIndex
func findTableIndex(tableValues []table.TableValues, tableName string) int {
for i, tableValue := range tableValues {
if tableValue.Name == tableName {
return i
}
}
return -1
}
const datasetTemplate = `
{
label: '{{.Label}}',
data: [{{.Data}}],
backgroundColor: '{{.Color}}',
borderColor: '{{.Color}}',
borderWidth: 1,
showLine: true,
hidden: {{.Hidden}}
}
`
const lineChartTemplate = `<div class="chart-container" style="max-width: 900px">
<canvas id="{{.ID}}"></canvas>
</div>
<script>
new Chart(document.getElementById('{{.ID}}'), {
type: 'line',
data: {
labels: [{{.Labels}}],
datasets: [{{.Datasets}}]
},
options: {
aspectRatio: {{.AspectRatio}},
scales: {
x: {
beginAtZero: false,
title: {
text: "{{.XaxisText}}",
display: true
},
ticks: {
maxRotation: 90,
minRotation: 45
}
},
y: {
title: {
text: "{{.YaxisText}}",
display: true
},
suggestedMin: {{.SuggestedMin}},
suggestedMax: {{.SuggestedMax}},{{if .YaxisMax}}
max: {{.YaxisMax}},{{end}}
}
},
plugins: {
title: {
text: "{{.TitleText}}",
display: {{.DisplayTitle}},
font: {
size: 18
}
},
tooltip: {
callbacks: {
label: function(ctx) {
return ctx.dataset.label + " (" + ctx.parsed.x + ", " + ctx.parsed.y + ")";
}
}
},
legend: {
display: {{.DisplayLegend}},
onClick: function(e, legendItem, legend) {
const chart = legend.chart;
const datasetIndex = legendItem.datasetIndex;
const nativeEvent = e && e.native ? e.native : e;
const isCtrl = nativeEvent && (nativeEvent.ctrlKey || nativeEvent.metaKey);
if (isCtrl) {
const onlyThisVisible = chart.data.datasets.every(function(ds, idx) {
if (idx === datasetIndex) {
return chart.isDatasetVisible(idx);
}
return !chart.isDatasetVisible(idx);
});
chart.data.datasets.forEach(function(ds, idx) {
const visible = onlyThisVisible ? true : idx === datasetIndex;
chart.setDatasetVisibility(idx, visible);
});
chart.update();
return;
}
const visible = chart.isDatasetVisible(datasetIndex);
chart.setDatasetVisibility(datasetIndex, !visible);
chart.update();
}
}
}
}
});
</script>
`
const scatterChartTemplate = `<div class="chart-container" style="max-width: 900px">
<canvas id="{{.ID}}"></canvas>
</div>
<script>
new Chart(document.getElementById('{{.ID}}'), {
type: 'scatter',
data: {
datasets: [{{.Datasets}}]
},
options: {
aspectRatio: {{.AspectRatio}},
scales: {
x: {
beginAtZero: false,
title: {
text: "{{.XaxisText}}",
display: true
}
},
y: {
title: {
text: "{{.YaxisText}}",
display: true
},
suggestedMin: {{.SuggestedMin}},
suggestedMax: {{.SuggestedMax}},{{if .YaxisMax}}
max: {{.YaxisMax}},{{end}}
}
},
plugins: {
title: {
text: "{{.TitleText}}",
display: {{.DisplayTitle}},
font: {
size: 18
}
},
tooltip: {
callbacks: {
label: function(ctx) {
return ctx.dataset.label + " (" + ctx.parsed.x + ", " + ctx.parsed.y + ")";
}
}
},
legend: {
display: {{.DisplayLegend}},
onClick: function(e, legendItem, legend) {
const chart = legend.chart;
const datasetIndex = legendItem.datasetIndex;
const nativeEvent = e && e.native ? e.native : e;
const isCtrl = nativeEvent && (nativeEvent.ctrlKey || nativeEvent.metaKey);
if (isCtrl) {
const onlyThisVisible = chart.data.datasets.every(function(ds, idx) {
if (idx === datasetIndex) {
return chart.isDatasetVisible(idx);
}
return !chart.isDatasetVisible(idx);
});
chart.data.datasets.forEach(function(ds, idx) {
const visible = onlyThisVisible ? true : idx === datasetIndex;
chart.setDatasetVisibility(idx, visible);
});
chart.update();
return;
}
const visible = chart.isDatasetVisible(datasetIndex);
chart.setDatasetVisibility(datasetIndex, !visible);
chart.update();
}
}
}
}
});
</script>
`
type ChartTemplateStruct struct {
ID string
Labels string // only for line charts
Datasets string
XaxisText string
YaxisText string
TitleText string
DisplayTitle string
DisplayLegend string
AspectRatio string
SuggestedMin string
SuggestedMax string
YaxisMax string // hard max for Y-axis; "" means no constraint
}
// CreateFieldNameWithDescription creates HTML for a field name with optional description tooltip
func CreateFieldNameWithDescription(fieldName, description string) string {
if description == "" {
return htmltemplate.HTMLEscapeString(fieldName)
}
return htmltemplate.HTMLEscapeString(fieldName) + `<span class="field-description"><span class="tooltip-icon">?</span><span class="tooltip-text">` + htmltemplate.HTMLEscapeString(description) + `</span></span>`
}
func RenderHTMLTable(tableHeaders []string, tableValues [][]string, class string, valuesStyle [][]string) string {
return renderHTMLTableWithDescriptions(tableHeaders, nil, tableValues, class, valuesStyle)
}
// renderHTMLTableWithDescriptions renders an HTML table with optional header descriptions
func renderHTMLTableWithDescriptions(tableHeaders []string, headerDescriptions []string, tableValues [][]string, class string, valuesStyle [][]string) string {
var sb strings.Builder
sb.WriteString(`<table class="` + class + `">`)
if len(tableHeaders) > 0 {
sb.WriteString(`<thead>`)
sb.WriteString(`<tr>`)
for i, label := range tableHeaders {
var description string
if headerDescriptions != nil && i < len(headerDescriptions) {
description = headerDescriptions[i]
}
sb.WriteString(`<th>` + CreateFieldNameWithDescription(label, description) + `</th>`)
}
sb.WriteString(`</tr>`)
sb.WriteString(`</thead>`)
}
sb.WriteString(`<tbody>`)
for rowIdx, rowValues := range tableValues {
sb.WriteString(`<tr>`)
for colIdx, value := range rowValues {
var style string
if len(valuesStyle) > rowIdx && len(valuesStyle[rowIdx]) > colIdx {
style = ` style="` + valuesStyle[rowIdx][colIdx] + `"`
}
sb.WriteString(`<td` + style + `>` + value + `</td>`)
}
sb.WriteString(`</tr>`)
}
sb.WriteString(`</tbody>`)
sb.WriteString(`</table>`)
return sb.String()
}
func DefaultHTMLTableRendererFunc(tableValues table.TableValues) string {
if tableValues.HasRows { // print the field names as column headings across the top of the table
headers := []string{}
headerDescriptions := []string{}
for _, field := range tableValues.Fields {
headers = append(headers, field.Name)
headerDescriptions = append(headerDescriptions, field.Description)
}
values := [][]string{}
for row := range tableValues.Fields[0].Values {
rowValues := []string{}
for _, field := range tableValues.Fields {
rowValues = append(rowValues, htmltemplate.HTMLEscapeString(field.Values[row]))
}
values = append(values, rowValues)
}
return renderHTMLTableWithDescriptions(headers, headerDescriptions, values, "pure-table pure-table-striped", [][]string{})
} else { // print the field name followed by its value
values := [][]string{}
var tableValueStyles [][]string
for _, field := range tableValues.Fields {
rowValues := []string{}
rowValues = append(rowValues, CreateFieldNameWithDescription(field.Name, field.Description))
if len(field.Values) > 0 {
rowValues = append(rowValues, htmltemplate.HTMLEscapeString(field.Values[0]))
} else {
rowValues = append(rowValues, "")
}
values = append(values, rowValues)
tableValueStyles = append(tableValueStyles, []string{"font-weight:bold"})
}
return RenderHTMLTable([]string{}, values, "pure-table pure-table-striped", tableValueStyles)
}
}
// RenderMultiTargetTableValuesAsHTML renders a table for multiple targets
// tableValues is a slice of table.TableValues, each of which represents the same table from a single target
func RenderMultiTargetTableValuesAsHTML(tableValues []table.TableValues, targetNames []string) string {
if len(tableValues) == 0 {
return ""
}
values := [][]string{}
var tableValueStyles [][]string
for fieldIndex, field := range tableValues[0].Fields {
rowValues := []string{}
rowValues = append(rowValues, CreateFieldNameWithDescription(field.Name, field.Description))
for _, targetTableValues := range tableValues {
if len(targetTableValues.Fields) > fieldIndex && len(targetTableValues.Fields[fieldIndex].Values) > 0 {
rowValues = append(rowValues, htmltemplate.HTMLEscapeString(targetTableValues.Fields[fieldIndex].Values[0]))
} else {
rowValues = append(rowValues, "")
}
}
values = append(values, rowValues)
tableValueStyles = append(tableValueStyles, []string{"font-weight:bold"})
}
headers := []string{""}
headers = append(headers, targetNames...)
return RenderHTMLTable(headers, values, "pure-table pure-table-striped", tableValueStyles)
}
// RenderChart generates an HTML/JavaScript representation of a chart using the provided data and configuration.
// It supports different chart types (e.g., "line", "scatter") and uses Go templates to format the datasets and chart.
// Parameters:
// - chartType: the type of chart to render ("line", "scatter").
// - allFormattedPoints: a slice of strings, each representing formatted data points for a dataset.
// - datasetNames: a slice of dataset names corresponding to each dataset.
// - xAxisLabels: a slice of labels for the x-axis (used for line charts).
// - config: a chartTemplateStruct containing chart configuration and template variables.
// - datasetHiddenFlags: a slice of booleans indicating whether each dataset should be hidden initially.
//
// Returns:
// - A string containing the rendered chart HTML/JavaScript, or an error message if rendering fails.
func RenderChart(chartType string, allFormattedPoints []string, datasetNames []string, xAxisLabels []string, config ChartTemplateStruct, datasetHiddenFlags []bool) string {
datasets := []string{}
for dataIdx, formattedPoints := range allFormattedPoints {
specValues := formattedPoints
dst := texttemplate.Must(texttemplate.New("datasetTemplate").Parse(datasetTemplate))
buf := new(bytes.Buffer)
// determine hidden flag for this dataset
hidden := "false"
if datasetHiddenFlags != nil && dataIdx < len(datasetHiddenFlags) && datasetHiddenFlags[dataIdx] {
hidden = "true"
}
err := dst.Execute(buf, struct {
Label string
Data string
Color string
Hidden string
}{
Label: datasetNames[dataIdx],
Data: specValues,
Color: getColor(dataIdx),
Hidden: hidden,
})
if err != nil {
slog.Error("error executing template", slog.String("error", err.Error()))
return "Error rendering chart."
}
datasets = append(datasets, buf.String())
}
var chartTemplate string
switch chartType {
case "line":
chartTemplate = lineChartTemplate
case "scatter":
chartTemplate = scatterChartTemplate
default:
panic("unknown chart type")
}
sct := texttemplate.Must(texttemplate.New("chartTemplate").Parse(chartTemplate))
buf := new(bytes.Buffer)
config.Datasets = strings.Join(datasets, ",")
if chartType == "line" {
config.Labels = func() string {
var labels []string
for _, label := range xAxisLabels {
labels = append(labels, fmt.Sprintf("'%s'", label))
}
return strings.Join(labels, ",")
}()
}
err := sct.Execute(buf, config)
if err != nil {
slog.Error("error executing template", slog.String("error", err.Error()))
return "Error rendering chart."
}
out := buf.String()
out += "\n"
return out
}
type ScatterPoint struct {
X float64
Y float64
}
// RenderScatterChart generates an HTML string for a scatter chart using the provided data and configuration.
//
// Parameters:
//
// data - 2D slice of scatterPoint values, where each inner slice represents a dataset's data points.
// datasetNames - Slice of strings representing the names of each dataset.
// config - chartTemplateStruct containing chart configuration options.
//
// Returns:
//
// A string containing the rendered HTML for the scatter chart.
func RenderScatterChart(data [][]ScatterPoint, datasetNames []string, config ChartTemplateStruct) string {
allFormattedPoints := []string{}
for dataIdx := range data {
formattedPoints := []string{}
for _, point := range data[dataIdx] {
formattedPoints = append(formattedPoints, fmt.Sprintf("{x: %f, y: %f}", point.X, point.Y))
}
allFormattedPoints = append(allFormattedPoints, strings.Join(formattedPoints, ","))
}
return RenderChart("scatter", allFormattedPoints, datasetNames, nil, config, nil)
}
func getColor(idx int) string {
// color-blind safe palette from here: http://mkweb.bcgsc.ca/colorblind/palettes.mhtml#page-container
colors := []string{"#9F0162", "#009F81", "#FF5AAF", "#00FCCF", "#8400CD", "#008DF9", "#00C2F9", "#FFB2FD", "#A40122", "#E20134", "#FF6E3A", "#FFC33B"}
return colors[idx%len(colors)]
}