Sergiusz Bazanski | 1fad2e5 | 2019-08-01 20:16:27 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "sort" |
| 7 | |
| 8 | humanize "github.com/dustin/go-humanize" |
| 9 | ) |
| 10 | |
| 11 | const processorsFragment = ` |
| 12 | <style type="text/css"> |
| 13 | .table td,th { |
| 14 | background-color: #eee; |
| 15 | padding: 0.2em 0.4em 0.2em 0.4em; |
| 16 | } |
| 17 | .table th { |
| 18 | background-color: #c0c0c0; |
| 19 | } |
| 20 | .table { |
| 21 | background-color: #fff; |
| 22 | border-spacing: 0.2em; |
| 23 | margin-left: auto; |
| 24 | margin-right: auto; |
| 25 | } |
| 26 | </style> |
| 27 | <div> |
| 28 | <table class="table"> |
| 29 | <tr> |
| 30 | <th>Name</th> |
| 31 | <th>Status</th> |
| 32 | <th>Last Run</th> |
| 33 | <th>Next Run</th> |
| 34 | </tr> |
| 35 | {{range .Processors }} |
| 36 | <tr> |
| 37 | <td>{{ .Name }}</td> |
| 38 | {{ if ne .Status "OK" }} |
| 39 | <td style="background-color: #ff3030;">{{ .Status }}</td> |
| 40 | {{ else }} |
| 41 | <td>{{ .Status }}</td> |
| 42 | {{ end }} |
| 43 | <td>{{ .LastRun }}</td> |
| 44 | <td>{{ .NextRun }}</td> |
| 45 | </tr> |
| 46 | {{end}} |
| 47 | </table> |
| 48 | </div> |
| 49 | ` |
| 50 | |
| 51 | type processorsFragmentEntry struct { |
| 52 | Name string |
| 53 | Status string |
| 54 | LastRun string |
| 55 | NextRun string |
| 56 | } |
| 57 | |
| 58 | func (s *service) statuszProcessors(ctx context.Context) interface{} { |
| 59 | s.processorsMu.RLock() |
| 60 | defer s.processorsMu.RUnlock() |
| 61 | |
| 62 | res := struct { |
| 63 | Processors []*processorsFragmentEntry |
| 64 | }{ |
| 65 | Processors: make([]*processorsFragmentEntry, len(s.processors)), |
| 66 | } |
| 67 | |
| 68 | i := 0 |
| 69 | for _, processor := range s.processors { |
| 70 | lastRun := "never" |
| 71 | if processor.lastRun != nil { |
| 72 | lastRun = humanize.Time(*processor.lastRun) |
| 73 | } |
| 74 | nextRun := "any second now" |
| 75 | if nr := processor.nextRun(); nr != nil { |
| 76 | nextRun = humanize.Time(*nr) |
| 77 | } |
| 78 | status := "OK" |
| 79 | if processor.lastErr != nil { |
| 80 | status = fmt.Sprintf("%v", processor.lastErr) |
| 81 | } |
| 82 | res.Processors[i] = &processorsFragmentEntry{ |
| 83 | Name: processor.name, |
| 84 | Status: status, |
| 85 | LastRun: lastRun, |
| 86 | NextRun: nextRun, |
| 87 | } |
| 88 | i += 1 |
| 89 | } |
| 90 | |
| 91 | sort.Slice(res.Processors, func(i, j int) bool { return res.Processors[i].Name < res.Processors[j].Name }) |
| 92 | |
| 93 | return res |
| 94 | } |