@@ -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.
125133func (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.
181303func (s * Store ) InsertParseError (
182304 occurredAt time.Time ,
0 commit comments