Skip to content

Commit ea6986d

Browse files
committed
[results.go ...] Results history
Closes #42 Results history is shown on the right sidebar with date and time of its execution, as well as the badge
1 parent 75f5636 commit ea6986d

File tree

5 files changed

+138
-15
lines changed

5 files changed

+138
-15
lines changed

assets/custom.css

+3
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ textarea#description {
339339
.ui.ginsearch > .results > :last-child {
340340
border-radius: 0em 0em 0.28571429rem 0.28571429rem;
341341
}
342+
#history {
343+
margin-bottom: 20px;
344+
}
342345
/*--------------
343346
Result
344347
---------------*/

internal/resources/templates/bids_results.go

+15
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ const BidsResults = `
2222
<div class="ui tabs divider"></div>
2323
</div>
2424
<div class="ui container">
25+
<div class="ui grid">
26+
<div class="column" style="width:20%">
27+
<div id="history">History:</div>
28+
{{ range $val := .Results }}
29+
<div>
30+
<a href="{{$val.Href}}" alt="{{$val.Alt}}">
31+
{{$val.Badge}}<br>
32+
<span class="tiny">{{$val.Text1}} {{$val.Text2}}</span>
33+
</a>
34+
</div>
35+
{{ end }}
36+
</div>
37+
<div class="column" style="width:80%">
2538
{{ range $val := .Issues.Errors }}
2639
<hr>
2740
<div>
@@ -64,6 +77,8 @@ const BidsResults = `
6477
<div>Size: {{ .Summary.Size }}</div>
6578
{{ end }}
6679
</div>
80+
</div>
81+
</div>
6782
</div>
6883
{{end}}
6984
`

internal/resources/templates/generic_results.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,26 @@ const GenericResults = `
2424
<div class="ui tabs divider"></div>
2525
</div>
2626
<div class="ui container">
27+
<div class="ui grid">
28+
<div class="column" style="width:20%">
29+
<div id="history">History:</div>
30+
{{ range $val := .Results }}
31+
<div>
32+
<a href="{{$val.Href}}" alt="{{$val.Alt}}">
33+
{{$val.Badge}}<br>
34+
<span class="tiny">{{$val.Text1}} {{$val.Text2}}</span>
35+
</a>
36+
</div>
37+
{{ end }}
38+
</div>
39+
<div class="column" style="width:80%">
2740
<hr>
2841
<div>
29-
<pre>{{.Content}}</pre>
42+
<pre style="white-space: pre-wrap">{{.Content}}</pre>
3043
</div>
3144
</div>
45+
</div>
46+
</div>
3247
</div>
3348
{{end}}
3449
`

internal/web/results.go

+83-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"html/template"
88
"io/ioutil"
99
"net/http"
10+
"os"
1011
"path/filepath"
12+
"sort"
1113
"strings"
1214
"time"
1315

@@ -18,6 +20,22 @@ import (
1820
"github.com/gorilla/mux"
1921
)
2022

23+
// ResultsHistoryStruct is the struct containing references to
24+
// all validations already performed
25+
type ResultsHistoryStruct struct {
26+
Results []Result
27+
}
28+
29+
// Result is the struct containing info about a single
30+
// validator run
31+
type Result struct {
32+
Href string
33+
Alt string
34+
Text1 string
35+
Text2 string
36+
Badge template.HTML
37+
}
38+
2139
// BidsResultStruct is the struct to parse a full BIDS validation json.
2240
type BidsResultStruct struct {
2341
Issues struct {
@@ -111,8 +129,14 @@ func Results(w http.ResponseWriter, r *http.Request) {
111129
srvcfg := config.Read()
112130
resID, ok := vars["id"]
113131
if !ok {
114-
fmt.Println("Results ID not specified: Rendering default")
115-
resID = srvcfg.Label.ResultsFolder
132+
resHistory := resultsHistory(validator, user, repo)
133+
if len(resHistory.Results) < 1 {
134+
log.ShowWrite("[Info] Results ID not specified: Rendering the default one")
135+
resID = srvcfg.Label.ResultsFolder
136+
} else {
137+
log.ShowWrite("[Info] Results ID not specified: Rendering the last one")
138+
resID = resHistory.Results[0].Alt
139+
}
116140
}
117141
resdir := filepath.Join(srvcfg.Dir.Result, validator, user, repo, resID)
118142

@@ -132,7 +156,7 @@ func Results(w http.ResponseWriter, r *http.Request) {
132156

133157
if string(content) == progressmsg {
134158
// validation in progress
135-
renderInProgress(w, r, badge, strings.ToUpper(validator), user, repo)
159+
renderInProgress(w, r, badge, validator, user, repo)
136160
return
137161
}
138162

@@ -166,16 +190,18 @@ func renderInProgress(w http.ResponseWriter, r *http.Request, badge []byte, vali
166190
}
167191

168192
// Parse results into html template and serve it
169-
head := fmt.Sprintf("%s validation for %s/%s", validator, user, repo)
193+
head := fmt.Sprintf("%s validation for %s/%s", strings.ToUpper(validator), user, repo)
170194
srvcfg := config.Read()
195+
resHistory := resultsHistory(validator, user, repo)
171196
year, _, _ := time.Now().Date()
172197
info := struct {
173198
Badge template.HTML
174199
Header string
175200
Content string
176201
GinURL string
177202
CurrentYear int
178-
}{template.HTML(badge), head, string(progressmsg), srvcfg.GINAddresses.WebURL, year}
203+
*ResultsHistoryStruct
204+
}{template.HTML(badge), head, string(progressmsg), srvcfg.GINAddresses.WebURL, year, &resHistory}
179205

180206
err = tmpl.ExecuteTemplate(w, "layout", info)
181207
if err != nil {
@@ -185,6 +211,49 @@ func renderInProgress(w http.ResponseWriter, r *http.Request, badge []byte, vali
185211
}
186212
}
187213

214+
func myReadDir(dirname string) ([]os.FileInfo, error) {
215+
f, err := os.Open(dirname)
216+
if err != nil {
217+
return nil, err
218+
}
219+
defer f.Close()
220+
list, err := f.Readdir(-1)
221+
if err != nil {
222+
return nil, err
223+
}
224+
sort.Slice(list, func(i, j int) bool { return list[i].ModTime().Unix() > list[j].ModTime().Unix() })
225+
return list, nil
226+
}
227+
228+
func resultsHistory(validator, user, repo string) ResultsHistoryStruct {
229+
var ret ResultsHistoryStruct
230+
srvcfg := config.Read()
231+
resdir := filepath.Join(srvcfg.Dir.Result, validator, user, repo)
232+
fileinfos, err := myReadDir(resdir)
233+
if err != nil {
234+
log.ShowWrite("[Error] cannot retrieve results history '%s/%s' result: %s\n", user, repo, err.Error())
235+
return ret
236+
}
237+
for _, i := range fileinfos {
238+
pth := filepath.Join("/results", strings.Split(resdir, "results/")[1], i.Name())
239+
fp := filepath.Join(resdir, i.Name(), srvcfg.Label.ResultsBadge)
240+
badge, err := ioutil.ReadFile(fp)
241+
if err != nil {
242+
badge = []byte("<svg></svg>")
243+
}
244+
if i.IsDir() && i.Name() != "." {
245+
var res Result
246+
res.Href = pth
247+
res.Alt = i.Name()
248+
res.Text1 = i.ModTime().Format("2006-01-02")
249+
res.Text2 = i.ModTime().Format("15:04:05")
250+
res.Badge = template.HTML(badge)
251+
ret.Results = append(ret.Results, res)
252+
}
253+
}
254+
return ret
255+
}
256+
188257
func renderBIDSResults(w http.ResponseWriter, r *http.Request, badge []byte, content []byte, user, repo string) {
189258
// Parse results file
190259
var resBIDS BidsResultStruct
@@ -214,13 +283,15 @@ func renderBIDSResults(w http.ResponseWriter, r *http.Request, badge []byte, con
214283
head := fmt.Sprintf("BIDS validation for %s/%s", user, repo)
215284
year, _, _ := time.Now().Date()
216285
srvcfg := config.Read()
286+
resHistory := resultsHistory("bids", user, repo)
217287
info := struct {
218288
Badge template.HTML
219289
Header string
220290
*BidsResultStruct
221291
GinURL string
222292
CurrentYear int
223-
}{template.HTML(badge), head, &resBIDS, srvcfg.GINAddresses.WebURL, year}
293+
*ResultsHistoryStruct
294+
}{template.HTML(badge), head, &resBIDS, srvcfg.GINAddresses.WebURL, year, &resHistory}
224295

225296
err = tmpl.ExecuteTemplate(w, "layout", info)
226297
if err != nil {
@@ -249,6 +320,7 @@ func renderNIXResults(w http.ResponseWriter, r *http.Request, badge []byte, cont
249320

250321
// Parse results into html template and serve it
251322
head := fmt.Sprintf("NIX validation for %s/%s", user, repo)
323+
resHistory := resultsHistory("nix", user, repo)
252324
year, _, _ := time.Now().Date()
253325
srvcfg := config.Read()
254326
info := struct {
@@ -257,7 +329,8 @@ func renderNIXResults(w http.ResponseWriter, r *http.Request, badge []byte, cont
257329
Content string
258330
GinURL string
259331
CurrentYear int
260-
}{template.HTML(badge), head, string(content), srvcfg.GINAddresses.WebURL, year}
332+
*ResultsHistoryStruct
333+
}{template.HTML(badge), head, string(content), srvcfg.GINAddresses.WebURL, year, &resHistory}
261334

262335
err = tmpl.ExecuteTemplate(w, "layout", info)
263336
if err != nil {
@@ -286,6 +359,7 @@ func renderODMLResults(w http.ResponseWriter, r *http.Request, badge []byte, con
286359

287360
// Parse results into html template and serve it
288361
head := fmt.Sprintf("odML validation for %s/%s", user, repo)
362+
resHistory := resultsHistory("odml", user, repo)
289363
srvcfg := config.Read()
290364
year, _, _ := time.Now().Date()
291365
info := struct {
@@ -294,7 +368,8 @@ func renderODMLResults(w http.ResponseWriter, r *http.Request, badge []byte, con
294368
Content string
295369
GinURL string
296370
CurrentYear int
297-
}{template.HTML(badge), head, string(content), srvcfg.GINAddresses.WebURL, year}
371+
*ResultsHistoryStruct
372+
}{template.HTML(badge), head, string(content), srvcfg.GINAddresses.WebURL, year, &resHistory}
298373

299374
err = tmpl.ExecuteTemplate(w, "layout", info)
300375
if err != nil {

internal/web/results_test.go

+21-6
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ func TestResultsODML(t *testing.T) {
5050
body := []byte("{}")
5151
router := mux.NewRouter()
5252
router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET")
53-
r, _ := http.NewRequest("GET", filepath.Join("/results/odml", username, "/", reponame, "/", id), bytes.NewReader(body))
53+
r, _ := http.NewRequest("GET", filepath.Join("/results/odml", username, reponame, id), bytes.NewReader(body))
5454
w := httptest.NewRecorder()
55-
resultfldr, _ := ioutil.TempDir("", "results")
55+
parent := os.TempDir()
56+
pth := filepath.Join(parent, "results")
57+
os.Mkdir(pth, 0755)
58+
resultfldr, _ := ioutil.TempDir(pth, "*-res")
5659
srvcfg := config.Read()
5760
srvcfg.Dir.Result = resultfldr
5861
config.Set(srvcfg)
@@ -78,7 +81,10 @@ func TestResultsNIX(t *testing.T) {
7881
router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET")
7982
r, _ := http.NewRequest("GET", filepath.Join("/results/nix", username, "/", reponame, "/", id), bytes.NewReader(body))
8083
w := httptest.NewRecorder()
81-
resultfldr, _ := ioutil.TempDir("", "results")
84+
parent := os.TempDir()
85+
pth := filepath.Join(parent, "results")
86+
os.Mkdir(pth, 0755)
87+
resultfldr, _ := ioutil.TempDir(pth, "*-res")
8288
srvcfg := config.Read()
8389
srvcfg.Dir.Result = resultfldr
8490
config.Set(srvcfg)
@@ -104,7 +110,10 @@ func TestResultsInJSON(t *testing.T) {
104110
router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET")
105111
r, _ := http.NewRequest("GET", filepath.Join("/results/bids", username, "/", reponame, "/", id), bytes.NewReader(body))
106112
w := httptest.NewRecorder()
107-
resultfldr, _ := ioutil.TempDir("", "results")
113+
parent := os.TempDir()
114+
pth := filepath.Join(parent, "results")
115+
os.Mkdir(pth, 0755)
116+
resultfldr, _ := ioutil.TempDir(pth, "*-res")
108117
srvcfg := config.Read()
109118
srvcfg.Dir.Result = resultfldr
110119
config.Set(srvcfg)
@@ -130,7 +139,10 @@ func TestResultsInProgress(t *testing.T) {
130139
router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET")
131140
r, _ := http.NewRequest("GET", filepath.Join("/results/bids", username, "/", reponame, "/", id), bytes.NewReader(body))
132141
w := httptest.NewRecorder()
133-
resultfldr, _ := ioutil.TempDir("", "results")
142+
parent := os.TempDir()
143+
pth := filepath.Join(parent, "results")
144+
os.Mkdir(pth, 0755)
145+
resultfldr, _ := ioutil.TempDir(pth, "*-res")
134146
srvcfg := config.Read()
135147
srvcfg.Dir.Result = resultfldr
136148
config.Set(srvcfg)
@@ -156,7 +168,10 @@ func TestResultsSomeResults(t *testing.T) {
156168
router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET")
157169
r, _ := http.NewRequest("GET", filepath.Join("/results/bids", username, "/", reponame, "/", id), bytes.NewReader(body))
158170
w := httptest.NewRecorder()
159-
resultfldr, _ := ioutil.TempDir("", "results")
171+
parent := os.TempDir()
172+
pth := filepath.Join(parent, "results")
173+
os.Mkdir(pth, 0755)
174+
resultfldr, _ := ioutil.TempDir(pth, "*-res")
160175
srvcfg := config.Read()
161176
srvcfg.Dir.Result = resultfldr
162177
config.Set(srvcfg)

0 commit comments

Comments
 (0)