|
| 1 | +package scheduler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + _ "embed" |
| 6 | + "html/template" |
| 7 | + "net/http" |
| 8 | + "slices" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/twmb/franz-go/pkg/kadm" |
| 12 | +) |
| 13 | + |
| 14 | +//go:embed status.gohtml |
| 15 | +var defaultPageContent string |
| 16 | +var defaultPageTemplate = template.Must(template.New("webpage").Funcs(template.FuncMap{ |
| 17 | + "durationSince": func(t time.Time) string { return time.Since(t).Truncate(time.Second).String() }, |
| 18 | +}).Parse(defaultPageContent)) |
| 19 | + |
| 20 | +type jobQueue interface { |
| 21 | + ListPendingJobs() []JobWithMetadata |
| 22 | + ListInProgressJobs() []JobWithMetadata |
| 23 | +} |
| 24 | + |
| 25 | +type offsetReader interface { |
| 26 | + GroupLag(ctx context.Context, lookbackPeriod time.Duration) (map[int32]kadm.GroupMemberLag, error) |
| 27 | +} |
| 28 | + |
| 29 | +type partitionInfo struct { |
| 30 | + Partition int32 |
| 31 | + Lag int64 |
| 32 | + EndOffset int64 |
| 33 | + CommittedOffset int64 |
| 34 | +} |
| 35 | + |
| 36 | +type statusPageHandler struct { |
| 37 | + jobQueue jobQueue |
| 38 | + offsetReader offsetReader |
| 39 | + lookbackPeriod time.Duration |
| 40 | +} |
| 41 | + |
| 42 | +func newStatusPageHandler(jobQueue jobQueue, offsetReader offsetReader, lookbackPeriod time.Duration) *statusPageHandler { |
| 43 | + return &statusPageHandler{jobQueue: jobQueue, offsetReader: offsetReader, lookbackPeriod: lookbackPeriod} |
| 44 | +} |
| 45 | + |
| 46 | +func (h *statusPageHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) { |
| 47 | + offsets, err := h.offsetReader.GroupLag(context.Background(), h.lookbackPeriod) |
| 48 | + if err != nil { |
| 49 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + pendingJobs := h.jobQueue.ListPendingJobs() |
| 54 | + slices.SortFunc(pendingJobs, func(a, b JobWithMetadata) int { |
| 55 | + return b.Priority - a.Priority // Higher priority first |
| 56 | + }) |
| 57 | + |
| 58 | + inProgressJobs := h.jobQueue.ListInProgressJobs() |
| 59 | + slices.SortFunc(inProgressJobs, func(a, b JobWithMetadata) int { |
| 60 | + return int(a.StartTime.Sub(b.StartTime)) // Earlier start time First |
| 61 | + }) |
| 62 | + |
| 63 | + data := struct { |
| 64 | + PendingJobs []JobWithMetadata |
| 65 | + InProgressJobs []JobWithMetadata |
| 66 | + Now time.Time |
| 67 | + PartitionInfo []partitionInfo |
| 68 | + }{ |
| 69 | + Now: time.Now(), |
| 70 | + PendingJobs: pendingJobs, |
| 71 | + InProgressJobs: inProgressJobs, |
| 72 | + } |
| 73 | + |
| 74 | + for _, partitionOffset := range offsets { |
| 75 | + // only include partitions having lag |
| 76 | + if partitionOffset.Lag > 0 { |
| 77 | + data.PartitionInfo = append(data.PartitionInfo, partitionInfo{ |
| 78 | + Partition: partitionOffset.Partition, |
| 79 | + Lag: partitionOffset.Lag, |
| 80 | + EndOffset: partitionOffset.End.Offset, |
| 81 | + CommittedOffset: partitionOffset.Commit.At, |
| 82 | + }) |
| 83 | + } |
| 84 | + } |
| 85 | + slices.SortFunc(data.PartitionInfo, func(a, b partitionInfo) int { |
| 86 | + return int(a.Partition - b.Partition) |
| 87 | + }) |
| 88 | + |
| 89 | + w.Header().Set("Content-Type", "text/html") |
| 90 | + if err := defaultPageTemplate.Execute(w, data); err != nil { |
| 91 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 92 | + } |
| 93 | +} |
0 commit comments