(w http.ResponseWriter, req bunrouter.Request)
| 53 | } |
| 54 | |
| 55 | func (h *UsageHandler) Show(w http.ResponseWriter, req bunrouter.Request) error { |
| 56 | ctx := req.Context() |
| 57 | |
| 58 | interval := 24 * time.Hour |
| 59 | timeLT := time.Now().Truncate(interval).Add(interval) |
| 60 | timeGTE := timeLT.AddDate(0, -1, 0) |
| 61 | |
| 62 | usage := new(Usage) |
| 63 | if err := h.CH.NewSelect(). |
| 64 | ColumnExpr("sum(rows) AS spans"). |
| 65 | ColumnExpr("sum(data_uncompressed_bytes) AS bytes"). |
| 66 | ColumnExpr("parseDateTime(partition, '%Y-%m-%d') AS time"). |
| 67 | TableExpr("system.parts"). |
| 68 | Where("database = ?", h.CH.Config().Database). |
| 69 | Where("table = ?", "spans_data"). |
| 70 | Where("min_time >= ?", timeGTE). |
| 71 | Where("active"). |
| 72 | GroupExpr("partition"). |
| 73 | OrderExpr("time ASC"). |
| 74 | ScanColumns(ctx, usage); err != nil { |
| 75 | return err |
| 76 | } |
| 77 | |
| 78 | usage.Spans = bunutil.Fill(usage.Spans, usage.Time, 0, timeGTE, timeLT, interval) |
| 79 | usage.Bytes = bunutil.Fill(usage.Bytes, usage.Time, 0, timeGTE, timeLT, interval) |
| 80 | usage.Time = bunutil.FillTime(usage.Time, timeGTE, timeLT, interval) |
| 81 | |
| 82 | subq := h.CH.NewSelect(). |
| 83 | ColumnExpr("60 * uniqCombined64(15)(d.attrs_hash) AS datapoints"). |
| 84 | ColumnExpr("d.time"). |
| 85 | ColumnExpr("60 AS minutes"). |
| 86 | TableExpr("datapoint_hours AS d"). |
| 87 | Where("d.time >= ?", timeGTE). |
| 88 | Where("d.time < ?", timeLT). |
| 89 | GroupExpr("d.project_id, d.time") |
| 90 | |
| 91 | usage2 := new(Usage2) |
| 92 | if err := h.CH.NewSelect(). |
| 93 | ColumnExpr("sum(d.datapoints) AS datapoints"). |
| 94 | ColumnExpr("toStartOfDay(d.time) AS time"). |
| 95 | ColumnExpr("sum(d.minutes) AS minutes"). |
| 96 | TableExpr("(?) AS d", subq). |
| 97 | GroupExpr("toStartOfDay(d.time)"). |
| 98 | OrderExpr("time ASC"). |
| 99 | ScanColumns(ctx, usage2); err != nil { |
| 100 | return err |
| 101 | } |
| 102 | |
| 103 | usage.Timeseries = make([]uint64, len(usage2.Datapoints)) |
| 104 | for i, datapoints := range usage2.Datapoints { |
| 105 | usage.Timeseries[i] = datapoints / usage2.Minutes[i] |
| 106 | } |
| 107 | usage.Timeseries = bunutil.Fill(usage.Timeseries, usage2.Time, 0, timeGTE, timeLT, interval) |
| 108 | |
| 109 | spans := sum(usage.Spans) |
| 110 | bytes := sum(usage.Bytes) |
| 111 | datapoints := sum(usage2.Datapoints) |
| 112 | minutes := sum(usage2.Minutes) |
nothing calls this directly
no test coverage detected