Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame^] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | |
| 8 | "code.hackerspace.pl/hscloud/go/mirko" |
| 9 | "code.hackerspace.pl/hscloud/go/statusz" |
| 10 | "github.com/golang/glog" |
| 11 | ) |
| 12 | |
| 13 | const invoicesFragment = ` |
| 14 | <style type="text/css"> |
| 15 | .table td,th { |
| 16 | background-color: #eee; |
| 17 | padding: 0.2em 0.4em 0.2em 0.4em; |
| 18 | } |
| 19 | .table th { |
| 20 | background-color: #c0c0c0; |
| 21 | } |
| 22 | .table { |
| 23 | background-color: #fff; |
| 24 | border-spacing: 0.2em; |
| 25 | margin-left: auto; |
| 26 | margin-right: auto; |
| 27 | } |
| 28 | </style> |
| 29 | <div> |
| 30 | {{ .Msg }} |
| 31 | <table class="table"> |
| 32 | <tr> |
| 33 | <th>Internal ID</th> |
| 34 | <th>Number</th> |
| 35 | <th>Customer</th> |
| 36 | <th>Amount (net)</th> |
| 37 | <th>Actions</th> |
| 38 | </tr> |
| 39 | {{ range .Invoices }} |
| 40 | {{ if .Sealed }} |
| 41 | <tr> |
| 42 | {{ else }} |
| 43 | <tr style="opacity: 0.5"> |
| 44 | {{ end }} |
| 45 | <td>{{ .ID }}</td> |
| 46 | <td>{{ .Number }}</td> |
| 47 | <td>{{ index .Proto.CustomerBilling 0 }}</td> |
| 48 | <td>{{ .TotalNetPretty }}</td> |
| 49 | <td> |
| 50 | <a href="/debug/view?id={{ .ID }}">View</a> |
| 51 | </td> |
| 52 | </tr> |
| 53 | {{ end }} |
| 54 | </table> |
| 55 | </div> |
| 56 | ` |
| 57 | |
| 58 | type templateInvoice struct { |
| 59 | invoice |
| 60 | TotalNetPretty string |
| 61 | } |
| 62 | |
| 63 | func (s *service) setupStatusz(m *mirko.Mirko) { |
| 64 | statusz.AddStatusPart("Invoices", invoicesFragment, func(ctx context.Context) interface{} { |
| 65 | var res struct { |
| 66 | Invoices []templateInvoice |
| 67 | Msg string |
| 68 | } |
| 69 | invoices, err := s.m.getInvoices(ctx) |
| 70 | res.Invoices = make([]templateInvoice, len(invoices)) |
| 71 | for i, inv := range invoices { |
| 72 | res.Invoices[i] = templateInvoice{ |
| 73 | invoice: inv, |
| 74 | TotalNetPretty: fmt.Sprintf("%.2f %s", float64(inv.TotalNet)/100, inv.Proto.Unit), |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | if err != nil { |
| 79 | glog.Errorf("Could not get invoices for statusz: %v", err) |
| 80 | res.Msg = fmt.Sprintf("Could not get invoices: %v", err) |
| 81 | } |
| 82 | return res |
| 83 | }) |
| 84 | |
| 85 | m.HTTPMux().HandleFunc("/debug/view", func(w http.ResponseWriter, r *http.Request) { |
| 86 | rendered, err := s.invoicePDF(r.Context(), r.URL.Query().Get("id")) |
| 87 | if err != nil { |
| 88 | fmt.Fprintf(w, "error: %v", err) |
| 89 | } |
| 90 | w.Header().Set("Content-type", "application/pdf") |
| 91 | w.Write(rendered) |
| 92 | }) |
| 93 | } |