feat(quota): give CI/CD usage its own tab, charted by day #101
No reviewers
Labels
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
codefloe/forgejo-codefloe!101
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/ci-usage-tab"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Follows #100, which landed the CI minutes ledger and put a CI card on the storage overview.
CI time is not storage, and that tab is gated on
[quota] ENABLED, so the card was unreachable on an instance that caps no storage. This moves it out into a tab of its own and gives the numbers room to be read.Rebased onto #105, so it sits on top of the whole of phase 2 rather than on #100 alone. The per-class breakdown #105 added to the card is carried into the new panel rather than dropped with it.
CI/CD usage tab
/user/settings/ci_usageand<org>/settings/ci_usage, gated only on[quota.ci] ENABLED. The card and its data loading come back out oftemplates/shared/quota_overview.tmplandStorageOverview, so the storage overview is once again only about storage.Four panels:
Limits — the rules that apply to CI time, each with a bar filled to used against limit. Now that #100 has landed
time:ci:*subjects and[quota.ci.default] TOTAL, there is a real denominator to fill against.Minutes by CI engine — one fold per producer, behind a chevron that turns on open, with the per-class breakdown inside. The
<details>was already there in the card, but the storage overview's styling hides the disclosure marker, so nothing said the row opened.Minutes by day — the period charted one column per day, stacked by the repositories that ran that day, with an axis tick on every day. Columns are scaled against the busiest day, printed above the plot. Hovering a segment names just that segment: the repository, its minutes, and the day. Days the period has not reached are a dotted baseline, so an empty tail reads as "not yet" rather than "nothing ran". Columns carry no track behind the bar; the limit bars above are what fills against a quota.
Minutes by repository — the five biggest, each with a bar and share, doubling as the chart's legend.
Units
Every duration reads in whole minutes as a plain number: "1797 minutes / 2400 minutes", not "29h 57m" and not "1,797". That is the unit the quota is written in and the one usage is communicated in.
Minutes are a display unit only. The ledger keeps exact wall-clock seconds, which is what accumulates without drift, and the API keeps reporting seconds as #93 specified, so machine consumers and phase 3's external reporters keep a lossless contract.
Every figure is rendered from those seconds rather than from a number that has already been rounded, because the two zeroes have to read differently: an account that has run nothing shows "0 minutes", while a run too short to round up to one still says "less than a minute". Ranking, shares and the "other" total are taken from the same seconds, so no decision is made on a figure that has lost the precision to make it.
The figure in the header and the rules beside it count work that is still running, so a long job is visible while it burns time. The breakdowns below are drawn from the ledger, which only has finished runs in it. Rather than leave the two to disagree by however long the jobs have been going, the page says how much of the total is still in flight, and only while there is any.
Colours
Series are coloured from viridis at five steps:
#440154 #3B528B #21908C #5DC863 #FDE725, with#A8A8A8for "other".Checked with a colour-blindness validator over every pair rather than only adjacent ones, which is the right check because a stacked column can put any two series next to each other. Every pair clears both the colour-blind separation floor (worst 9.2 ΔE) and the normal-vision floor (worst 16.7 ΔE), on the light and the dark surface alike, "other" included. The neutral is deliberately light: a mid grey lands on viridis' teal and fails.
Series take palette slots in fixed order by name and are never cycled. The sixth repository onwards folds into the neutral, so the chart never shows a colour the legend cannot explain.
That invariant is held in seconds as well. A tail of runs that each round to no minutes still adds up to the legend's grey row, and a run that started and stopped inside the same second is dropped once, before either the chart or the legend sees it, so neither can show what the other does not.
The chart is plain server-rendered markup and CSS, no charting library and no new JS. Its styles live in
web_src/css/codefloe/ci-usage-chart.css, reached through the fork's own@importlist, so upstream's CSS is untouched.Admin view
/admin/users/{id}gets the same four panels, read-only, with the period total in the header. Upstream exposes admin quota over the API only, so this has no upstream equivalent to mirror.Three gaps in the ledger
All fixed here rather than left for later:
quota_ci_usageandquota_ci_recordare now dropped with the owner, in the existingDeleteBeanscalls. #92 lists this under Risks. Both paths are covered: organizations never reachservices/user/delete.go, they go throughorg_model.DeleteOrganization, and they are the owners that accumulate the most CI time.quota_ci_recordsingle-column indexes onowner_idand onperiod, but every read of the detail rows asks for both at once. v11 adds the composite. The aggregate table needs nothing: itsUNIQUE(usage_scope)already starts(owner_id, period).v10_test.goasserts both tables and both unique indexes exist, and that re-running the sync is a no-op.v11_test.godoes the same for the new index, starting from the schema v10 leaves behind.Model
One addition:
PeriodBreakdown, which projects a period's records to(repo_id, stopped, seconds). The chart and the repository list are both computed from that single query, and every repository it names is resolved in one more, shared by both panels. Bucketing is done in Go rather than SQL because truncating a timestamp to a day differs across the three supported databases, and a period holds at most one month of one owner's records.TopReposgoes: the storage card was its only caller, and itsGROUP BY ... LIMIT 5bounded the rows returned rather than the rows scanned, so what changed withPeriodBreakdownis the transfer, not the work the database does. v11's composite index is what makes that scan an index range over exactly the matching rows.The new index is declared on the model as well as in the migration. Startup runs the migrations and then syncs the models, and that sync reconciles each table's indexes against its model rather than adding to them, so an index only the migration knows about is dropped again before the instance has finished starting.
v11_test.goasserts that too.Upstream merge surface
Seven upstream files, all small:
routers/web/web.goctxDataSetentryrouters/web/admin/users.goservices/user/delete.gomodels/organization/org.gotemplates/admin/user/view.tmpltemplates/user/settings/navbar.tmpl,templates/org/settings/navbar.tmpltemplates/shared/quota_overview.tmplandrouters/web/shared/storage_overview.gogo back to what upstream has, minus #100's own additions, which is a net reduction in surface.Tests
[quota] ENABLEDoffEvery test added or changed during review was also checked against the unfixed code — the defect put back, the test confirmed to fail — so none of them passes for the wrong reason. Two did not, at first: one asserted on a fixture where the tail absorbed the case it was meant to catch, and one synced a struct whose name mapped to a different table than the one it was asserting on.
Verification
All pass. Two notes on running them:
lint-backendneeds thetoolchainversion pinned ingo.modrather than a newer system Go, which is what makes it runnable at all. It is also the target that catcheslint-editorconfig, which the shorterlint-godoes not.GITEA_ROOTandGITEA_CONF, the way the Makefile passes them. Without those they fail with "no database host defined", which looks like an environment they cannot run in and is not.860173b09374c19a05eb74c19a05eb24788e0fdd24788e0fddf76deb4839Reviewed at
f76deb4839. Ten findings, each verified against the branch.Correctness
1. Zero minutes renders as "less than a minute" —
templates/shared/codefloe_ci_minutes.tmpl:7{{- if .}}...{{else}}under_a_minute{{end -}}—0is falsy in Go templates, so there is no way to print "0 minutes".A user with no CI records sees "less than a minute / Unlimited"; same for the admin header total and any rule whose limit is 0.
Needs an explicit
{{if lt . 1}}-style split that distinguishes 0 from 0<x<1.2. Deleting an organization still orphans the ledger —
services/user/delete.go:107/models/organization/org.go:408The
quota_ci_model.Usage/Recordbeans were added only to the user path.services/org/org.gocallsorg_model.DeleteOrganizationdirectly and never reachesservices/user/delete.go, so an org'squota_ci_usage/quota_ci_recordrows survive forever.DeleteRecordsBeforeprunes records by retention, but aggregates are never pruned at all.Since orgs are the owners that actually accumulate CI time, this is the bigger half of the gap the PR sets out to close.
3. The chart paints colours the legend cannot explain —
routers/web/shared/codefloe_ci_usage.go:352The "other repositories" legend row is gated on
if other > 0, whereothersums already-rounded minutes.The chart's
label()(line 397) independently maps any repo missing fromcolorsto the greyotherColor, using raw seconds.Six-plus repositories where the tail rounds to 0 minutes gives grey segments in the chart and no grey row in the legend, which breaks the invariant the description states.
4. Repository shares divide by a total that includes in-flight work —
routers/web/shared/codefloe_ci_usage.go:348percentOf(named[i].Minutes, minutesOf(total))withtotal = info.Used.Total.GetInfo's own comment says that figure "includes an estimate for work that is still running", whilePeriodBreakdownreads only finishedquota_ci_recordrows.Shares silently under-sum whenever a job is in flight.
classTotalsworks around exactly this mismatch for the class breakdown; the repository panel did not get the same treatment.5. A rule with a limit of 0 draws an empty bar —
routers/web/shared/codefloe_ci_usage.go:201, viapercentOf'swhole <= 0guard at line 289A rule that denies CI outright gets
Percent == 0, so it renders as unused rather than as maxed out.6. The chart panel disappears on sub-minute periods —
templates/shared/codefloe_ci_usage.tmpl:16{{if .CIUsageChart.PeakMinutes}}gates on rounded minutes, so a period of only short runs loses the chart while the repository panel below still lists those same repositories.Same root cause as finding 1: rounded minutes used as a truthiness test.
7. Rounded corners land on the wrong end of the column —
web_src/css/codefloe/ci-usage-chart.css:70.ci-usage-chart-stackisflex-direction: column-reverse, so the first DOM child renders at the bottom.:first-childshould be:last-child.Invisible on the codefloe themes (
--border-radius: 0), visible on upstream ones.8.
time.Now()instead of the timeutil clock —routers/web/shared/codefloe_ci_usage.go:429The rest of the ledger goes through timeutil, so
Futureand the dotted-baseline behaviour are wrong under a mocked clock and cannot be covered by a test.Efficiency
9.
PeriodBreakdownis an unbounded SELECT —models/codefloe/quota_ci/quota_ci.go:395It replaces a
GROUP BY ... LIMIT 5with every record in the period, now run on the settings tab and on every/admin/users/{id}view.The doc comment's justification ("at most one month of one owner's records") is fine for a personal account and thin for a busy org.
10. 2N point queries per render —
routers/web/shared/codefloe_ci_usage.go:310and:397Every distinct repository is resolved via
GetRepositoryByIDinciUsageReposeven though only five are named, then resolved again by the chart through its ownnamescache.One batched lookup shared by both would do.
Checked and fine
Permissions are sound: the org route sits inside the owner-gated settings group, the user route uses
ctx.Doer.ID, and the handler re-checkssetting.QuotaCI.Enabledrather than trusting the nav gate.The
sortedCopyhelperv10_test.gouses is already defined inv9_test.goin the same package.The quota_ci unit tests and the new integration tests pass.
The migration tests fail in my environment for a pre-existing reason (no DB host configured) and fail identically on v8 and v9.
I would treat findings 1, 2 and 3 as blocking: 2 because it is the risk the PR claims to close, 1 and 3 because they contradict behaviour the description promises.
f76deb48396648e7392b6648e7392b6b6d66fb806b6d66fb8054ad33766a