Skip to content

Commit a4ba6a2

Browse files
committed
refactor: simplify log retention evaluation logic
1 parent 6e53ed8 commit a4ba6a2

File tree

1 file changed

+5
-34
lines changed

1 file changed

+5
-34
lines changed

frontend/src/models/logs.ts

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,6 @@ import { RootModel } from '.'
55

66
const DAY_MS = 24 * 60 * 60 * 1000
77

8-
const toTimestamp = (value?: Date | string) => (value ? new Date(value).getTime() : undefined)
9-
10-
const clamp = (value: number, min: number, max: number) => Math.min(Math.max(value, min), max)
11-
12-
const evaluateRetention = ({
13-
items,
14-
minDate,
15-
hasMore,
16-
allowedDays,
17-
}: {
18-
items: IEvent[]
19-
minDate?: Date
20-
hasMore?: boolean
21-
allowedDays: number
22-
}) => {
23-
const minDateMs = toTimestamp(minDate)
24-
const oldestMs = toTimestamp(items[items.length - 1]?.timestamp)
25-
const boundaryReady = Boolean(items.length && !hasMore && minDateMs !== undefined && oldestMs !== undefined)
26-
if (!boundaryReady) return { reached: false, near: false }
27-
28-
const reached = oldestMs! <= minDateMs!
29-
const thresholdDays = clamp(allowedDays > 0 ? allowedDays * 0.1 : 1, 0.25, 1)
30-
const near = !reached && oldestMs! - minDateMs! <= thresholdDays * DAY_MS
31-
32-
return { reached, near }
33-
}
34-
358
type ILogState = {
369
size: number
3710
after?: string
@@ -93,18 +66,16 @@ export default createModel<RootModel>()({
9366
items: mergedItems,
9467
}
9568

96-
const { reached, near } = evaluateRetention({
97-
items: mergedItems,
98-
minDate,
99-
hasMore: nextEvents.hasMore,
100-
allowedDays,
101-
})
69+
const hasReachedLimit = !nextEvents.hasMore && allowedDays > 0
70+
const firstLogMs = state.user.created?.getTime() || 0
71+
const logLimitMs = Date.now() - allowedDays * DAY_MS
72+
const planUpgrade = hasReachedLimit && firstLogMs < logLimitMs
10273

10374
set({
10475
events: nextEvents,
10576
fetching: false,
10677
fetchingMore: false,
107-
planUpgrade: reached || near,
78+
planUpgrade,
10879
})
10980
},
11081

0 commit comments

Comments
 (0)