Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/added-14762-launch-cost-history.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- `GET /api/launch-cost/history` returns a persisted daily per-ref rollup (`share_usd`, `launches` per UTC day) of launch attribution, so cost per merged PR can be measured over 14+ days instead of the ~1-day `/api/launch-cost` window ([#14762](https://github.com/hivecommons/hive/issues/14762)).
55 changes: 55 additions & 0 deletions dashboard/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -27912,6 +27912,61 @@
}
}
},
"/api/launch-cost/history": {
"get": {
"tags": [
"System"
],
"summary": "Get the daily per-ref launch-cost rollup",
"description": "Returns the per-UTC-day rollup of launch attribution kept by the launch-cost collector beyond the /api/launch-cost accounting window, so cost per issue / PR can be measured over many days (#14762). Each day lists the work items launched for, how many kicks named them and the even-split share of spend. Empty before the collector's first tick. Attribution only — never a budget gate.",
"responses": {
"200": {
"description": "Daily launch-cost rollup",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"daily_rollup": {
"type": "array",
"items": {
"type": "object",
"properties": {
"day": {
"type": "string",
"description": "UTC calendar day, YYYY-MM-DD"
},
"by_ref": {
"type": "array",
"items": {
"type": "object",
"properties": {
"ref": {
"type": "string"
},
"kind": {
"type": "string"
},
"launches": {
"type": "integer"
},
"share_usd": {
"type": "number"
}
}
}
}
}
}
}
}
}
}
}
}
}
}
},
"/api/repos/pause": {
"post": {
"tags": [
Expand Down
467 changes: 234 additions & 233 deletions src/docs/api-reference.md

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/docs/dashboard-history-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ existing ordering. `by_launch_total`, `by_ref_total` and `truncated` disclose
omissions after filtering. Aggregate totals and unattributed/unsupported buckets
always describe the entire accounting window, even when details are filtered.

`GET /api/launch-cost/history` returns `{"daily_rollup": [...]}`, ascending by
UTC `day`. Each day lists `by_ref[]` with `ref`, `kind`, `launches` and `share_usd`
(each launch's spend split evenly across the refs it named). `launches` counts
every delivered kick that named the ref that day, including kicks that received
no usage, matching `/api/launch-cost` `by_ref[].launches`. It is folded from
the launch-cost snapshot on every collection and persisted with it, so it
outlives the `/api/launch-cost` accounting window. Each launch is tracked
individually while the join can still see it (its share follows usage that
keeps landing on it) and is added to its day's settled totals exactly once when
it leaves the window or the launch ring, so a day sums every launch even when
the ring turns over in under 24h. It has no sampling parameters.

All four endpoints, `/api/status` and `/api/status/summary` emit weak ETags with
`Cache-Control: private, no-cache`. Send `If-None-Match` to receive an empty 304
when the selected representation is unchanged. Status field projections retain
Expand Down
1 change: 1 addition & 0 deletions src/pkg/dashboard/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func (s *Server) RegisterAPI(deps *Dependencies) {
s.mux.HandleFunc("GET /api/repo-cost", s.handleRepoCost)
// #12128: per-issue/PR/head-SHA launch attribution over the same collector.
s.mux.HandleFunc("GET /api/launch-cost", s.handleLaunchCost)
s.mux.HandleFunc("GET /api/launch-cost/history", s.handleLaunchCostHistory)
s.mux.HandleFunc("GET /api/cost/history", s.handleCostHistory)
// #4298: "for every recent reset, how much of the budget had been used".
s.mux.HandleFunc("GET /api/budget/history", s.handleBudgetHistory)
Expand Down
52 changes: 43 additions & 9 deletions src/pkg/dashboard/collect/launch_cost_collector.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package collect

import (
Expand Down Expand Up @@ -30,11 +30,22 @@
snap LaunchCostResponse
ready bool
collectedAt time.Time
// rollup holds settled per-day, per-ref totals of launches the join no
// longer sees; rollupLive holds the launches it still sees, keyed by
// launch, so each is folded into rollup exactly once when it ages out.
rollup map[string]map[string]LaunchCostRollupRef
rollupLive map[string]launchCostLiveLaunch
rollupDayCache map[string][]LaunchCostRollupRef
}

type persistedLaunchCost struct {
Snapshot LaunchCostResponse `json:"snapshot"`
CollectedAt time.Time `json:"collected_at"`
// DailyRollup is absent in files written before the rollup existed.
DailyRollup []LaunchCostDailyRollup `json:"daily_rollup,omitempty"`
// LiveLaunches are the launches still visible to the join at write time,
// keyed by agent@launched_at; they are not yet part of DailyRollup.
LiveLaunches map[string]launchCostLiveLaunch `json:"live_launches,omitempty"`
}

// NewLaunchCostCollector builds a collector over the token collector. A nil
Expand Down Expand Up @@ -66,6 +77,7 @@
}
return
}
lc.seedRollupLocked(stored.DailyRollup, stored.LiveLaunches)
if stored.CollectedAt.IsZero() {
return
}
Expand All @@ -74,23 +86,39 @@
lc.ready = true
}

func (lc *LaunchCostCollector) persistLocked() {
// persistSnapshotLocked captures what persistLocked will write. It runs under
// lc.mu; the marshal and file I/O in writePersist do not, so a tick holds the
// lock only for the map-to-list fold of the days it changed.
func (lc *LaunchCostCollector) persistSnapshotLocked() (persistedLaunchCost, bool) {
if lc.persistPath == "" {
return
return persistedLaunchCost{}, false
}
live := make(map[string]launchCostLiveLaunch, len(lc.rollupLive))
for k, v := range lc.rollupLive {
live[k] = v
}
data, err := json.Marshal(persistedLaunchCost{Snapshot: lc.snap, CollectedAt: lc.collectedAt})
return persistedLaunchCost{
Snapshot: lc.snap,
CollectedAt: lc.collectedAt,
DailyRollup: lc.settledRollupListLocked(),
LiveLaunches: live,
}, true
}

func (lc *LaunchCostCollector) writePersist(path string, stored persistedLaunchCost) {
data, err := json.Marshal(stored)
if err != nil {
return
}
tmp := lc.persistPath + ".tmp"
tmp := path + ".tmp"
if err := os.WriteFile(tmp, data, 0o600); err != nil {
if lc.logger != nil {
lc.logger.Warn("failed to write launch-cost store", "path", lc.persistPath, "error", err)
lc.logger.Warn("failed to write launch-cost store", "path", path, "error", err)
}
return
}
if err := os.Rename(tmp, lc.persistPath); err != nil && lc.logger != nil {
lc.logger.Warn("failed to replace launch-cost store", "path", lc.persistPath, "error", err)
if err := os.Rename(tmp, path); err != nil && lc.logger != nil {
lc.logger.Warn("failed to replace launch-cost store", "path", path, "error", err)
}
}

Expand All @@ -114,13 +142,19 @@

func (lc *LaunchCostCollector) collect() {
now := lc.nowFn()
resp := ComputeLaunchCost(lc.tokens.Summary(), lc.tokens.Launches(), now)
launches := lc.tokens.Launches()
resp := ComputeLaunchCost(lc.tokens.Summary(), launches, now)
lc.mu.Lock()
lc.snap = resp
lc.collectedAt = now
lc.ready = resp.Ready
lc.persistLocked()
lc.mergeLaunchCostRollupLocked(resp, launches, now)
stored, ok := lc.persistSnapshotLocked()
path := lc.persistPath
lc.mu.Unlock()
if ok {
lc.writePersist(path, stored)
}
}

// Snapshot returns the last computed response and whether a collect has ever
Expand Down
Loading
Loading