Sergiusz Bazanski | a9a266c | 2019-04-06 01:21:25 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | mirko "code.hackerspace.pl/hscloud/go/mirko" |
| 7 | "code.hackerspace.pl/hscloud/go/statusz" |
| 8 | ) |
| 9 | |
| 10 | const statuszFragment = ` |
| 11 | <style type="text/css"> |
| 12 | .table td,th { |
| 13 | background-color: #eee; |
| 14 | padding: 0.2em 0.4em 0.2em 0.4em; |
| 15 | } |
| 16 | .table th { |
| 17 | background-color: #c0c0c0; |
| 18 | } |
| 19 | .table { |
| 20 | background-color: #fff; |
| 21 | border-spacing: 0.2em; |
| 22 | } |
| 23 | </style> |
| 24 | <div> |
| 25 | <b>Current leases:</b> {{ .Leases | len }}<br /> |
| 26 | <table class="table"> |
| 27 | <tr> |
| 28 | <th>IP Address</th> |
| 29 | <th>MAC Address</th> |
| 30 | <th>Start</th> |
| 31 | <th>End</th> |
| 32 | </tr> |
| 33 | {{range .Leases }} |
| 34 | <tr> |
| 35 | <td>{{ .IP }}</td> |
| 36 | <td>{{ .MAC }}</td> |
| 37 | <td>{{ .Start }}</td> |
| 38 | <td>{{ .End }}</td> |
| 39 | </tr> |
| 40 | {{end}} |
| 41 | </table> |
| 42 | </div> |
| 43 | ` |
| 44 | |
| 45 | type szLeases struct { |
| 46 | IP string |
| 47 | MAC string |
| 48 | Start string |
| 49 | End string |
| 50 | } |
| 51 | |
| 52 | func (s *service) setupStatusz(m *mirko.Mirko) { |
| 53 | statusz.AddStatusPart("Leases", statuszFragment, func(ctx context.Context) interface{} { |
| 54 | c := make(chan []lease) |
| 55 | s.leaseC <- c |
| 56 | leases := <-c |
| 57 | |
| 58 | ls := make([]szLeases, len(leases)) |
| 59 | |
| 60 | for i, l := range leases { |
| 61 | ls[i].IP = l.ip.String() |
| 62 | ls[i].MAC = l.hardware.String() |
| 63 | ls[i].Start = l.from.String() |
| 64 | ls[i].End = l.to.String() |
| 65 | } |
| 66 | |
| 67 | return struct { |
| 68 | Leases []szLeases |
| 69 | }{ |
| 70 | Leases: ls, |
| 71 | } |
| 72 | }) |
| 73 | } |