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