Debian Codegraph @

service/internal/zpages/templates.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package zpages // import "go.opentelemetry.io/collector/service/internal/zpages"
5
6 import (
7 _ "embed"
8 "html/template"
9 "io"
10 "log"
11 )
12
13 var (
14 templateFunctions = template.FuncMap{
15 "even": even,
16 "getKey": getKey,
17 "getValue": getValue,
18 }
19
20 //go:embed templates/component_header.html
21 componentHeaderBytes []byte
22 componentHeaderTemplate = parseTemplate("component_header", componentHeaderBytes)
23
24 //go:embed templates/extensions_table.html
25 extensionsTableBytes []byte
26 extensionsTableTemplate = parseTemplate("extensions_table", extensionsTableBytes)
27
28 //go:embed templates/page_header.html
29 headerBytes []byte
30 headerTemplate = parseTemplate("header", headerBytes)
31
32 //go:embed templates/page_footer.html
33 footerBytes []byte
34 footerTemplate = parseTemplate("footer", footerBytes)
35
36 //go:embed templates/pipelines_table.html
37 pipelinesTableBytes []byte
38 pipelinesTableTemplate = parseTemplate("pipelines_table", pipelinesTableBytes)
39
40 //go:embed templates/properties_table.html
41 propertiesTableBytes []byte
42 propertiesTableTemplate = parseTemplate("properties_table", propertiesTableBytes)
43
44 //go:embed templates/features_table.html
45 featuresTableBytes []byte
46 featuresTableTemplate = parseTemplate("features_table", featuresTableBytes)
47 )
48
49 func parseTemplate(name string, bytes []byte) *template.Template {
50 return template.Must(template.New(name).Funcs(templateFunctions).Parse(string(bytes)))
51 }
52
53 // HeaderData contains data for the header template.
54 type HeaderData struct {
55 Title string
56 }
57
58 // WriteHTMLPageHeader writes the header.
59 func WriteHTMLPageHeader(w io.Writer, hd HeaderData) {
60 if err := headerTemplate.Execute(w, hd); err != nil {
61 log.Printf("zpages: executing template: %v", err)
62 }
63 }
64
65 // SummaryExtensionsTableData contains data for extensions summary table template.
66 type SummaryExtensionsTableData struct {
67 Rows []SummaryExtensionsTableRowData
68 }
69
70 // SummaryExtensionsTableRowData contains data for one row in extensions summary table template.
71 type SummaryExtensionsTableRowData struct {
72 FullName string
73 Enabled bool
74 }
75
76 // WriteHTMLExtensionsSummaryTable writes the summary table for one component type (receivers, processors, exporters).
77 // Id does not write the header or footer.
78 func WriteHTMLExtensionsSummaryTable(w io.Writer, spd SummaryExtensionsTableData) {
79 if err := extensionsTableTemplate.Execute(w, spd); err != nil {
80 log.Printf("zpages: executing template: %v", err)
81 }
82 }
83
84 // SummaryPipelinesTableData contains data for pipelines summary table template.
85 type SummaryPipelinesTableData struct {
86 Rows []SummaryPipelinesTableRowData
87 }
88
89 // SummaryPipelinesTableRowData contains data for one row in pipelines summary table template.
90 type SummaryPipelinesTableRowData struct {
91 FullName string
92 InputType string
93 MutatesData bool
94 Receivers []string
95 Processors []string
96 Exporters []string
97 }
98
99 // WriteHTMLPipelinesSummaryTable writes the summary table for one component type (receivers, processors, exporters).
100 // Id does not write the header or footer.
101 func WriteHTMLPipelinesSummaryTable(w io.Writer, spd SummaryPipelinesTableData) {
102 if err := pipelinesTableTemplate.Execute(w, spd); err != nil {
103 log.Printf("zpages: executing template: %v", err)
104 }
105 }
106
107 // ComponentHeaderData contains data for component header template.
108 type ComponentHeaderData struct {
109 Name string
110 ComponentEndpoint string
111 Link bool
112 }
113
114 // WriteHTMLComponentHeader writes the header for components.
115 func WriteHTMLComponentHeader(w io.Writer, chd ComponentHeaderData) {
116 if err := componentHeaderTemplate.Execute(w, chd); err != nil {
117 log.Printf("zpages: executing template: %v", err)
118 }
119 }
120
121 // PropertiesTableData contains data for properties table template.
122 type PropertiesTableData struct {
123 Name string
124 Properties [][2]string
125 }
126
127 // WriteHTMLPropertiesTable writes the HTML for properties table.
128 func WriteHTMLPropertiesTable(w io.Writer, chd PropertiesTableData) {
129 if err := propertiesTableTemplate.Execute(w, chd); err != nil {
130 log.Printf("zpages: executing template: %v", err)
131 }
132 }
133
134 // WriteHTMLPageFooter writes the footer.
135 func WriteHTMLPageFooter(w io.Writer) {
136 if err := footerTemplate.Execute(w, nil); err != nil {
137 log.Printf("zpages: executing template: %v", err)
138 }
139 }
140
141 func even(x int) bool {
142 return x%2 == 0
143 }
144
145 func getKey(row [2]string) string {
146 return row[0]
147 }
148
149 func getValue(row [2]string) string {
150 return row[1]
151 }
152
153 // FeatureGateTableData contains data for feature gate table template.
154 type FeatureGateTableData struct {
155 Rows []FeatureGateTableRowData
156 }
157
158 // FeatureGateTableRowData contains data for one row in feature gate table template.
159 type FeatureGateTableRowData struct {
160 ID string
161 Enabled bool
162 Description string
163 Stage string
164 FromVersion string
165 ToVersion string
166 ReferenceURL string
167 }
168
169 // WriteHTMLFeaturesTable writes a table summarizing registered feature gates.
170 func WriteHTMLFeaturesTable(w io.Writer, ftd FeatureGateTableData) {
171 if err := featuresTableTemplate.Execute(w, ftd); err != nil {
172 log.Printf("zpages: executing template: %v", err)
173 }
174 }