Skip to content

Commit 1d9f846

Browse files
committed
Snapshot: server-side plateau compaction (bd-f7ag)
When a snapshot arrives marked continuous and matches every plateau field of the latest row from the same source, slide that row's observed_at and received_at forward in place rather than inserting a duplicate. The slide is suppressed when the prior row is itself an explicit start (continuous_with_prev = 0), so a fresh page load always anchors a new row. raw_json is preserved on the surviving row as an audit artifact. Built with Raymond (Agent Orchestrator)
1 parent 8b8c1d0 commit 1d9f846

4 files changed

Lines changed: 577 additions & 0 deletions

File tree

docs/data-model.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,29 @@ Storing NULL on absence and treating NULL as "start"/"unknown" keeps
9090
older clients (which never set this field) safe by default — the
9191
engine will not assume continuity it cannot prove.
9292

93+
**Write-time plateau compaction.** When a snapshot arrives with
94+
`continuous_with_prev = 1` and every "match" field
95+
(`session_used`, `weekly_used`, `session_window_ends`,
96+
`weekly_window_ends`, `session_active`) is identical to the most
97+
recent row from the same `source`, the existing row's `observed_at`
98+
and `received_at` are slid forward in place instead of inserting a
99+
duplicate. The slide is suppressed when the prior row is itself an
100+
explicit start (`continuous_with_prev = 0`), so a fresh page load
101+
always anchors a new row.
102+
103+
The audit-trail rules:
104+
105+
- `observed_at` is refreshed so the chart sees the latest sighting —
106+
otherwise plateaus would visually end at the first observation.
107+
- `received_at` is refreshed so freshness queries
108+
(`last_snapshot_age_seconds`, slack-gate freshness) stay honest on
109+
a stable plateau.
110+
- `raw_json` is preserved as the original payload's audit artifact;
111+
it is not overwritten across slides.
112+
113+
The slide is scoped to a single `source` so unrelated ingestion
114+
paths (userscript vs. headless) cannot collapse onto each other.
115+
93116
### `windows`
94117

95118
Derived/cached state for the current and recent session and weekly windows. Maintained by

internal/store/store.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ func (s *Store) InsertUsageEvent(
122122
// session_used and weekly_used are 0–100 percentages.
123123
// sessionActive is nil when the source did not report it; persisted as NULL.
124124
// continuousWithPrev is nil when absent; persisted as NULL.
125+
//
126+
// Plateau compaction: when the arrival is marked continuous and every
127+
// "match" field is identical to the latest row from the same source, the
128+
// existing row's observed_at and received_at are slid forward in place
129+
// instead of inserting a duplicate. raw_json is preserved on the surviving
130+
// row as an audit artifact. The slide is suppressed when the latest row is
131+
// itself an explicit start (continuous_with_prev = 0), so a fresh page
132+
// load always anchors a new row.
125133
func (s *Store) InsertQuotaSnapshot(
126134
observedAt, receivedAt time.Time,
127135
source string,
@@ -149,6 +157,22 @@ func (s *Store) InsertQuotaSnapshot(
149157
continuousWithPrevArg = 0
150158
}
151159
}
160+
161+
if continuousWithPrev != nil && *continuousWithPrev {
162+
slidID, slid, err := s.tryPlateauSlide(
163+
observedAt, receivedAt, source,
164+
sessionUsed, sessionWindowEnds,
165+
weeklyUsed, weeklyWindowEnds,
166+
sessionActive,
167+
)
168+
if err != nil {
169+
return 0, err
170+
}
171+
if slid {
172+
return slidID, nil
173+
}
174+
}
175+
152176
result, err := s.db.Exec(`
153177
INSERT INTO quota_snapshots (
154178
observed_at, received_at, source,
@@ -177,6 +201,104 @@ func (s *Store) InsertQuotaSnapshot(
177201
return id, nil
178202
}
179203

204+
// tryPlateauSlide refreshes the latest row's timestamps in place when the
205+
// new arrival continues an identical plateau. Returns (id, true, nil) if a
206+
// slide happened; (0, false, nil) means the caller should insert as usual.
207+
func (s *Store) tryPlateauSlide(
208+
observedAt, receivedAt time.Time,
209+
source string,
210+
sessionUsed *float64,
211+
sessionWindowEnds *time.Time,
212+
weeklyUsed *float64,
213+
weeklyWindowEnds *time.Time,
214+
sessionActive *bool,
215+
) (int64, bool, error) {
216+
var (
217+
prevID int64
218+
prevSessionUsed sql.NullFloat64
219+
prevWeeklyUsed sql.NullFloat64
220+
prevSessionWindowEnds sql.NullString
221+
prevWeeklyWindowEnds sql.NullString
222+
prevSessionActive sql.NullInt64
223+
prevContinuousWithPrev sql.NullInt64
224+
)
225+
err := s.db.QueryRow(`
226+
SELECT id, session_used, weekly_used,
227+
session_window_ends, weekly_window_ends,
228+
session_active, continuous_with_prev
229+
FROM quota_snapshots
230+
WHERE source = ?
231+
ORDER BY observed_at DESC
232+
LIMIT 1
233+
`, source).Scan(
234+
&prevID, &prevSessionUsed, &prevWeeklyUsed,
235+
&prevSessionWindowEnds, &prevWeeklyWindowEnds,
236+
&prevSessionActive, &prevContinuousWithPrev,
237+
)
238+
if err == sql.ErrNoRows {
239+
return 0, false, nil
240+
}
241+
if err != nil {
242+
return 0, false, fmt.Errorf("failed to read latest snapshot for slide: %w", err)
243+
}
244+
245+
// Don't slide on top of a row that is itself an explicit start.
246+
if prevContinuousWithPrev.Valid && prevContinuousWithPrev.Int64 == 0 {
247+
return 0, false, nil
248+
}
249+
250+
if !nullableFloatEqual(prevSessionUsed, sessionUsed) ||
251+
!nullableFloatEqual(prevWeeklyUsed, weeklyUsed) ||
252+
!nullableTimeEqual(prevSessionWindowEnds, sessionWindowEnds) ||
253+
!nullableTimeEqual(prevWeeklyWindowEnds, weeklyWindowEnds) ||
254+
!nullableBoolEqual(prevSessionActive, sessionActive) {
255+
return 0, false, nil
256+
}
257+
258+
if _, err := s.db.Exec(`
259+
UPDATE quota_snapshots
260+
SET observed_at = ?, received_at = ?
261+
WHERE id = ?
262+
`, FormatTime(observedAt), FormatTime(receivedAt), prevID); err != nil {
263+
return 0, false, fmt.Errorf("failed to slide quota snapshot: %w", err)
264+
}
265+
return prevID, true, nil
266+
}
267+
268+
func nullableFloatEqual(a sql.NullFloat64, b *float64) bool {
269+
if a.Valid != (b != nil) {
270+
return false
271+
}
272+
if !a.Valid {
273+
return true
274+
}
275+
return a.Float64 == *b
276+
}
277+
278+
func nullableTimeEqual(a sql.NullString, b *time.Time) bool {
279+
if a.Valid != (b != nil) {
280+
return false
281+
}
282+
if !a.Valid {
283+
return true
284+
}
285+
return a.String == FormatTime(*b)
286+
}
287+
288+
func nullableBoolEqual(a sql.NullInt64, b *bool) bool {
289+
if a.Valid != (b != nil) {
290+
return false
291+
}
292+
if !a.Valid {
293+
return true
294+
}
295+
var bv int64
296+
if *b {
297+
bv = 1
298+
}
299+
return a.Int64 == bv
300+
}
301+
180302
// InsertParseError inserts a parse error record.
181303
func (s *Store) InsertParseError(
182304
occurredAt time.Time,

0 commit comments

Comments
 (0)