getUsage returns usage aggregated since the last ack — one row per key (org, team, query_source, worker size) per UTC day — plus the watermark window. watermark_low is the server cursor (what billing last acked; billing should cross-check it against its own record), watermark_high is what billing ac
(c *gin.Context)
| 48 | // interval), so it is safe to serve — and later delete on ack. |
| 49 | func (h *billingAPIHandler) latestClosedBucket() time.Time { |
| 50 | return h.now().UTC().Add(-computeBucketWidth - computeBucketGrace).Truncate(computeBucketWidth) |
| 51 | } |
| 52 | |
| 53 | // getUsage returns usage aggregated since the last ack — compute: one row per |
| 54 | // key (org, team, query_source, worker size) per UTC day; storage: one row |
| 55 | // per (org, team) per UTC day — plus the shared watermark window. watermark_low is the server cursor (what billing last acked; |
| 56 | // billing should cross-check it against its own record), watermark_high is |
| 57 | // what billing acks after processing. |
| 58 | func (h *billingAPIHandler) getUsage(c *gin.Context) { |
| 59 | low, _, err := h.store.ComputeBillingCursor() |
| 60 | if err != nil { |
| 61 | c.JSON(http.StatusInternalServerError, gin.H{"error": "read billing cursor: " + err.Error()}) |
| 62 | return |
| 63 | } |
| 64 | low = low.UTC() // zero time (never acked) serves everything buffered |
| 65 | |
| 66 | high := h.latestClosedBucket() |
| 67 | if !high.After(low) { |
| 68 | // Nothing closed beyond the cursor yet (fresh deploy or a pull racing |
| 69 | // right behind an ack). An empty window with high == low is a valid |
| 70 | // response: billing acks it as a no-op. |
| 71 | c.JSON(http.StatusOK, gin.H{ |
| 72 | "watermark_low": low.Format(time.RFC3339), |
| 73 | "watermark_high": low.Format(time.RFC3339), |
| 74 | "usage": []configstore.ComputeUsageRow{}, |
| 75 | "storage": []configstore.StorageUsageRow{}, |
| 76 | }) |
| 77 | return |
| 78 | } |
| 79 | |
| 80 | rows, err := h.store.AggregateComputeUsage(low, high) |
| 81 | if err != nil { |
| 82 | c.JSON(http.StatusInternalServerError, gin.H{"error": "aggregate usage: " + err.Error()}) |
| 83 | return |
| 84 | } |
| 85 | if rows == nil { |
| 86 | rows = []configstore.ComputeUsageRow{} |
| 87 | } |
nothing calls this directly
no test coverage detected