diff options
| author | Shulhan <ms@kilabit.info> | 2025-08-01 02:21:20 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2025-08-01 02:21:20 +0700 |
| commit | 5338799c9fa5158031ebae53c6e64254175e7412 (patch) | |
| tree | 83ffde1d8bf34e4323c384f0877dfd21090a0ef3 | |
| parent | 5a90a6822fc0d6a0d0d07b4b4583f9dbc20ba0e6 (diff) | |
| download | lilin-5338799c9fa5158031ebae53c6e64254175e7412.tar.xz | |
all: record and display the history of failed scan
In the main Reports struct, we record the failed ScanReport in separate
slices.
Those slices then displayed on the main page under "Fail history"
section.
| -rw-r--r-- | _www/index.tmpl | 36 | ||||
| -rw-r--r-- | reports.go | 23 | ||||
| -rw-r--r-- | server.go | 24 | ||||
| -rw-r--r-- | worker.go | 11 |
4 files changed, 71 insertions, 23 deletions
diff --git a/_www/index.tmpl b/_www/index.tmpl index f7cebda..cf710db 100644 --- a/_www/index.tmpl +++ b/_www/index.tmpl @@ -7,20 +7,36 @@ </head> <body> <h2>{{.Title}}</h2> - {{ range.Services }} - <div> + + <h3>Service status</h3> + <div class="service-status"> + {{ range.Services }} <div> - <span>{{.Name}}</span> - {{if .Last.Success}} - <span class="success"> OK </span> - {{else}} - <span class="fail"> FAIL </span> + <div> + <span>{{.Name}}</span> + {{if .Last.Success}} + <span class="success"> OK </span> + {{else}} + <span class="fail"> FAIL </span> + {{ end }} + </div> + {{if not .Last.Success}} + <div>{{.Last.At}}: {{ .Last.Error }}</div> {{ end }} </div> - {{if not .Last.Success}} - <div>{{.Last.At}}: {{ .Last.Error }}</div> {{ end }} </div> - {{ end }} + + <h3>Fail history</h3> + <div class="fail-history"> + {{ $list := reverse .Fail }} + {{ range $list }} + <div class="fail-history-item"> + <span class="name">{{ .Name }}</span> + <span class="at">{{ .At }}</span> + <span class="error">{{ .Error }}</span> + </div> + {{ end }} + </div> </body> </html> @@ -3,8 +3,31 @@ package lilin +import "sync" + +const failKeepSize = 600 +const failMax = failKeepSize + (failKeepSize / 3) + // Reports contains the report for all services. type Reports struct { Services map[string]*ServiceReport Title string + Fail []ScanReport + sync.Mutex +} + +// Store the service scan report. +func (reports *Reports) Store(scanReport ScanReport) { + reports.Lock() + var svcReport = reports.Services[scanReport.Name] + svcReport.Store(scanReport) + if !scanReport.Success { + reports.Fail = append(reports.Fail, scanReport) + if len(reports.Fail) >= failMax { + var start = len(reports.Fail) - failKeepSize + copy(reports.Fail, reports.Fail[start:]) + reports.Fail = reports.Fail[:failKeepSize-1] + } + } + reports.Unlock() } @@ -58,7 +58,22 @@ func NewServer(opts ServerOptions) (srv *Server, err error) { return nil, fmt.Errorf(`%s: %w`, logp, err) } - srv.pageIndexTmpl, err = template.ParseFS(wwwFS, `_www/index.tmpl`) + srv.pageIndexTmpl = template.New("index.html").Funcs(template.FuncMap{ + `reverse`: func(rev []ScanReport) (ver []ScanReport) { + var size = len(rev) + ver = make([]ScanReport, size) + for x := 0; x < len(rev); x++ { + ver[size-x-1] = rev[x] + } + return ver + }, + }) + var tmplraw []byte + tmplraw, err = wwwFS.ReadFile(`_www/index.tmpl`) + if err != nil { + return nil, err + } + srv.pageIndexTmpl, err = srv.pageIndexTmpl.Parse(string(tmplraw)) if err != nil { return nil, err } @@ -154,15 +169,16 @@ func (srv *Server) update() { c = 0 srv.pageIndexMutex.Lock() - srv.worker.reportsMutex.Lock() + srv.worker.Reports.Lock() srv.pageIndexBody.Reset() - err = srv.pageIndexTmpl.Execute(&srv.pageIndexBody, &srv.worker.Reports) + err = srv.pageIndexTmpl.Execute(&srv.pageIndexBody, + &srv.worker.Reports) if err != nil { log.Printf(`update: pageIndexTmpl: %s`, err) } - srv.worker.reportsMutex.Unlock() + srv.worker.Reports.Unlock() srv.pageIndexMutex.Unlock() } } @@ -7,7 +7,6 @@ import ( "os" "path/filepath" "strings" - "sync" "git.sr.ht/~shulhan/pakakeh.go/lib/ini" ) @@ -19,8 +18,7 @@ type worker struct { reportq chan ScanReport - Reports Reports - reportsMutex sync.Mutex + Reports Reports } func newWorker(configDir string) (wrk *worker, err error) { @@ -114,12 +112,7 @@ func (wrk *worker) start(updateq chan<- struct{}) { if !ok { break } - - wrk.reportsMutex.Lock() - var svcReport = wrk.Reports.Services[scanReport.Name] - svcReport.Store(scanReport) - wrk.reportsMutex.Unlock() - + wrk.Reports.Store(scanReport) updateq <- struct{}{} } } |
