Skip to content

Commit 0c6cfa3

Browse files
committed
Added template function to construct entire data structure on the backend
1 parent 176ae08 commit 0c6cfa3

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

static/pkg_info.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ $(document).ready(function() {
66
$("a[name=build-stages]").each(function() {
77
var text = $(this).text();
88
var id = text + ".log";
9-
var statusCode = urlData[id].statusCode;
9+
var statusCode = urlData[id].StatusCode;
1010
if (statusCode == 404) {
1111
$(this).addClass("disabled");
1212
}
@@ -16,10 +16,10 @@ $(document).ready(function() {
1616
e.preventDefault();
1717
var text = $(this).text();
1818
var id = text + ".log";
19-
var statusCode = urlData[id].statusCode;
20-
var url = new String(urlData[id].url);
19+
var statusCode = urlData[id].StatusCode;
20+
var url = new String(urlData[id].URL);
2121
var displayURL = url.length > 90 ? url.substring(0, 40) + " ... " + url.substring(url.length-40, url.length): url;
22-
var displayData = urlData[id].data === "" ? "No data available" : urlData[id].data;
22+
var displayData = urlData[id].Data === "" ? "No data available" : urlData[id].Data;
2323

2424
// Add shortcut unicode character to the displayURL
2525
displayURL = displayURL + " ⇗"; // Unicode character for link
@@ -35,9 +35,9 @@ $(document).ready(function() {
3535
activeLogName = text;
3636
}
3737

38-
$("#logFrame").text(urlData[id].error || displayData);
38+
$("#logFrame").text(urlData[id].Error || displayData);
3939
$("#logContainer").show();
40-
$("#original-logfile").attr("href", urlData[id].url);
40+
$("#original-logfile").attr("href", urlData[id].URL);
4141
$("#original-logfile").html(displayURL);
4242
});
4343

templates/pkg_info.html

+2-16
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,8 @@
3333
{{if eq .Res.BuildStatus 2}}
3434
<dt>Build Logs</dt>
3535
<script>
36-
// Create JavaScript object with URLData
37-
var urlData = {
38-
{{range .URLData}}
39-
{{.ID}}:
40-
{
41-
id: {{.ID}},
42-
url: {{.URL}},
43-
statusCode: {{.StatusCode}},
44-
{{if .Error}}
45-
error: {{.Error}},
46-
{{else}}
47-
data: {{.Data}},
48-
{{end}}
49-
},
50-
{{end}}
51-
};
36+
// URLData is used by pkg_info.js to load the logs
37+
var urlData = {{.URLDataJSON}};
5238
</script>
5339
<dd>
5440
<div class="btn-group btn-group-sm">

templates/templates.go

+30-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package templates
2323
import (
2424
"context"
2525
"embed"
26+
"encoding/json"
2627
"fmt"
2728
"html/template"
2829
"io"
@@ -142,6 +143,16 @@ func BulkBuildInfo(w io.Writer, b *ddao.Build) {
142143
t.ExecuteTemplate(w, "bulk_build_info.html", b)
143144
}
144145

146+
// marshalToJSON marshals the given data to a JSON string that can be used in JavaScript.
147+
// Returns a template.JS value to prevent HTML escaping of the JSON.
148+
func marshalToJSON(data interface{}) (template.JS, error) {
149+
jsonBytes, err := json.Marshal(data)
150+
if err != nil {
151+
return "", err
152+
}
153+
return template.JS(jsonBytes), nil
154+
}
155+
145156
func PkgInfo(w io.Writer, res ddao.GetSingleResultRow) {
146157
// Define what to fetch - these represent the stages of the build process
147158
// and are used to fetch the corresponding log files.
@@ -215,18 +226,32 @@ func PkgInfo(w io.Writer, res ddao.GetSingleResultRow) {
215226
// Wait for all requests to complete
216227
wg.Wait()
217228

229+
// Create a map of URL results for JSON marshalling
230+
urlDataMap := make(map[string]URLRequestResult)
231+
for _, result := range results {
232+
urlDataMap[result.ID] = result
233+
}
234+
235+
// Marshal URL data to JSON
236+
jsonData, err := marshalToJSON(urlDataMap)
237+
if err != nil {
238+
log.Errorf(ctx, "templates.PkgInfo: Error marshalling URL data: %v", err)
239+
}
240+
218241
// Create the structure to pass to the template
219242
s := struct {
220-
Res *ddao.GetSingleResultRow
221-
URLData []URLRequestResult
243+
Res *ddao.GetSingleResultRow
244+
URLData []URLRequestResult
245+
URLDataJSON template.JS
222246
bp
223247
}{
224-
Res: &res,
225-
URLData: results,
248+
Res: &res,
249+
URLData: results,
250+
URLDataJSON: jsonData,
226251
}
227252

228253
// Execute the template
229-
err := t.ExecuteTemplate(w, "pkg_info.html", s)
254+
err = t.ExecuteTemplate(w, "pkg_info.html", s)
230255
if err != nil {
231256
log.Errorf(ctx, "templates.PkgInfo: %v", err)
232257
}

0 commit comments

Comments
 (0)