Skip to content

Commit 640fddd

Browse files
authored
Merge pull request #346 from Gregory-Pereira/246-combined-out-html
creating carousel around data
2 parents 19a951e + 4f96e90 commit 640fddd

File tree

2 files changed

+137
-20
lines changed

2 files changed

+137
-20
lines changed

worker/cmd/generate.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func (w *Worker) runPrecheck(lab, outputDir, modelName string) error {
233233
}
234234
chatlogDir := path.Join(workDir, "data", "chatlogs")
235235
combinedYAMLPath := path.Join(outputDir, "combined_chatlogs.yaml")
236-
combinedLogPath := path.Join(outputDir, "combined_chatlogs.log")
236+
combinedYAMLHTMLPath := path.Join(outputDir, "combined_chatlogs.html")
237237

238238
defer func() {
239239
// Move everything from chatlogDir to outputDir
@@ -244,7 +244,7 @@ func (w *Worker) runPrecheck(lab, outputDir, modelName string) error {
244244
}
245245

246246
var combinedLogs []map[string]interface{}
247-
var combinedLogsText strings.Builder
247+
var fileNames []string
248248

249249
for _, file := range chatlogFiles {
250250
if strings.HasSuffix(file.Name(), ".yaml") {
@@ -261,27 +261,19 @@ func (w *Worker) runPrecheck(lab, outputDir, modelName string) error {
261261
continue
262262
}
263263
combinedLogs = append(combinedLogs, logData)
264-
} else if strings.HasSuffix(file.Name(), ".log") {
265-
// Read individual log files
266-
content, err := os.ReadFile(path.Join(chatlogDir, file.Name()))
267-
if err != nil {
268-
w.logger.Errorf("Could not read log file %s: %v", file.Name(), err)
269-
continue
270-
}
271-
// Add delimiter before each log
272-
combinedLogsText.WriteString(fmt.Sprintf("\n\n----- %s -----\n\n\n", file.Name()))
273-
combinedLogsText.Write(content)
274-
}
275264

265+
}
276266
// Move individual file to outputDir
277267
if err := os.Rename(path.Join(chatlogDir, file.Name()), path.Join(outputDir, file.Name())); err != nil {
278268
w.logger.Errorf("Could not move file %s: %v", file.Name(), err)
279269
continue
280270
}
271+
fileNames = append(fileNames, file.Name())
281272
}
282273

283274
// Write the combined YAML file
284275
if len(combinedLogs) > 0 {
276+
// standard combined yaml file
285277
combinedYAML, err := yaml.Marshal(combinedLogs)
286278
if err != nil {
287279
w.logger.Errorf("Could not marshal combined YAML data: %v", err)
@@ -292,15 +284,27 @@ func (w *Worker) runPrecheck(lab, outputDir, modelName string) error {
292284
return
293285
}
294286
w.logger.Infof("Combined YAML file written to %s", combinedYAMLPath)
295-
}
296287

297-
// Write the combined log file
298-
if combinedLogsText.Len() > 0 {
299-
if err := os.WriteFile(combinedLogPath, []byte(combinedLogsText.String()), 0644); err != nil {
300-
w.logger.Errorf("Could not write combined log file: %v", err)
301-
return
288+
combinedLogHtmlFile, err := os.Create(combinedYAMLHTMLPath)
289+
if err != nil {
290+
w.logger.Errorf("Could not create combined_yaml.html: %v", err)
291+
}
292+
defer combinedLogHtmlFile.Close()
293+
294+
// build yamlEntries array from files
295+
var yamlEntries []string
296+
var yamlFileBytes []byte
297+
for _, yamlFile := range combinedLogs {
298+
yamlFileBytes, err = yaml.Marshal(yamlFile)
299+
if err != nil {
300+
w.logger.Errorf("Could not create unmarshal map to yaml: %v", err)
301+
}
302+
yamlEntries = append(yamlEntries, string(yamlFileBytes))
303+
}
304+
if err := generateAllHTML(combinedLogHtmlFile, yamlEntries, fileNames); err != nil {
305+
w.logger.Errorf("Could not generate index.html: %v", err)
302306
}
303-
w.logger.Infof("Combined log file written to %s", combinedLogPath)
307+
w.logger.Infof("Combined log file written to %s", combinedLogHtmlFile)
304308
}
305309
}()
306310

worker/cmd/templates.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,119 @@ import (
1414
"sigs.k8s.io/yaml"
1515
)
1616

17+
func generateAllHTML(allFile *os.File, logEntries []string, fileNames []string) error {
18+
const INDEX_HTML = `
19+
<!DOCTYPE html>
20+
<html>
21+
<head>
22+
<style>
23+
:root {
24+
--primary-color: #007bff;
25+
--hover-color: #0056b3;
26+
--text-color: #333;
27+
--background-color: #f8f9fa;
28+
--link-color: #0066cc;
29+
--link-hover-color: #0044cc;
30+
}
31+
body {
32+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
33+
background-color: var(--background-color);
34+
margin: 0;
35+
padding: 20px;
36+
color: var(--text-color);
37+
}
38+
h1 {
39+
color: var(--primary-color);
40+
text-align: center;
41+
margin-bottom: 2rem;
42+
}
43+
a {
44+
color: var(--link-color);
45+
text-decoration: none;
46+
font-weight: 500;
47+
}
48+
a:hover {
49+
color: var(--link-hover-color);
50+
text-decoration: underline;
51+
}
52+
.item {
53+
min-height: 300px
54+
}
55+
.item p {
56+
max-width: fit-content;
57+
margin-left: auto;
58+
margin-right: auto;
59+
}
60+
.item h5 {
61+
max-width: fit-content;
62+
margin-left: auto;
63+
margin-right: auto;
64+
}
65+
</style>
66+
<meta charset="utf-8">
67+
<meta name="viewport" content="width=device-width, initial-scale=1">
68+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
69+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
70+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
71+
</head>
72+
<body>
73+
<div class="container">
74+
<h2>All Log Files as Carousel</h2>
75+
<div id="logfileCarousel" class="carousel slide" data-ride="carousel" data-interval="false">
76+
<ol class="carousel-indicators">
77+
{{ range $index, $value := .Files }}
78+
{{ if eq $index 0 }}
79+
<li data-target="#logfileCarousel" data-slide-to="{{$index}}" class="active"></li>
80+
{{ else }}
81+
<li data-target="#logfileCarousel" data-slide-to="{{$index}}"></li>
82+
{{ end}}
83+
{{ end }}
84+
</ol>
85+
<div class="carousel-inner">
86+
{{ $FileNames := .FileNames }}
87+
{{ range $index, $value := .Files }}
88+
{{ if eq $index 0 }}
89+
<div class="item active">
90+
<h5> {{ index $FileNames $index }} </h5>
91+
<pre> {{ $value }} </pre>
92+
</div>
93+
{{ else }}
94+
<div class="item">
95+
<h5> {{ index $FileNames $index }} </h5>
96+
<pre> {{ $value }} </pre>
97+
</div>
98+
{{ end}}
99+
{{ end }}
100+
</div>
101+
<a class="left carousel-control" href="#logfileCarousel" data-slide="prev">
102+
<span class="glyphicon glyphicon-chevron-left"></span>
103+
<span class="sr-only">Previous</span>
104+
</a>
105+
<a class="right carousel-control" href="#logfileCarousel" data-slide="next">
106+
<span class="glyphicon glyphicon-chevron-right"></span>
107+
<span class="sr-only">Next</span>
108+
</a>
109+
</div>
110+
</div>
111+
</body>
112+
</html>`
113+
114+
tmpl, err := template.New("index").Parse(INDEX_HTML)
115+
if err != nil {
116+
return fmt.Errorf("template parsing error: %w", err)
117+
}
118+
119+
data := struct {
120+
Files []string
121+
FileNames []string
122+
}{
123+
Files: logEntries,
124+
FileNames: fileNames,
125+
}
126+
127+
return tmpl.Execute(allFile, data)
128+
}
129+
17130
func generateIndexHTML(indexFile *os.File, prNumber string, presignedFiles []map[string]string) error {
18131
const INDEX_HTML = `
19132
<!DOCTYPE html>

0 commit comments

Comments
 (0)