Skip to content

Commit 0c03146

Browse files
authored
chore: entitlement audit logs (supabase#40022)
1 parent 587f55e commit 0c03146

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

apps/studio/components/interfaces/Settings/Logs/Logs.utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,19 @@ export const maybeShowUpgradePrompt = (from: string | null | undefined, planId?:
291291
)
292292
}
293293

294+
/**
295+
* Determine if we should show the user an upgrade prompt while browsing logs
296+
* This method should replace maybeShowUpgradePrompt once we have migrated all usage to the Entitlements API.
297+
*/
298+
export const maybeShowUpgradePromptIfNotEntitled = (
299+
from: string | null | undefined,
300+
entitledToDays: number | undefined
301+
) => {
302+
if (!entitledToDays) return false
303+
const day = Math.abs(dayjs().diff(dayjs(from), 'day'))
304+
return day > entitledToDays
305+
}
306+
294307
export const genCountQuery = (table: LogsTableName, filters: Filters): string => {
295308
let where = genWhereStatement(table, filters)
296309
// pg_cron logs are a subset of postgres logs

apps/studio/components/interfaces/Settings/Logs/LogsPreviewer.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ import {
2626
PREVIEWER_DATEPICKER_HELPERS,
2727
} from './Logs.constants'
2828
import type { Filters, LogSearchCallback, LogTemplate, QueryType } from './Logs.types'
29-
import { maybeShowUpgradePrompt } from './Logs.utils'
29+
import { maybeShowUpgradePromptIfNotEntitled } from './Logs.utils'
3030
import { PreviewFilterPanelWithUniversal } from './PreviewFilterPanelWithUniversal'
3131
import UpgradePrompt from './UpgradePrompt'
32+
import { useCheckEntitlements } from 'hooks/misc/useCheckEntitlements'
3233

3334
/**
3435
* Calculates the appropriate time range for bar click filtering based on the current time range duration.
@@ -206,15 +207,20 @@ export const LogsPreviewer = ({
206207
refresh()
207208
}
208209

210+
const { getEntitlementNumericValue } = useCheckEntitlements('log.retention_days')
211+
const entitledToAuditLogDays = getEntitlementNumericValue()
212+
209213
const handleSearch: LogSearchCallback = async (event, { query, to, from }) => {
210214
if (event === 'search-input-change') {
211215
setSearch(query || '')
212216
setSelectedLogId(null)
213217
} else if (event === 'event-chart-bar-click') {
214218
setTimeRange(from || '', to || '')
215219
} else if (event === 'datepicker-change') {
216-
const shouldShowUpgradePrompt = maybeShowUpgradePrompt(from || '', organization?.plan?.id)
217-
220+
const shouldShowUpgradePrompt = maybeShowUpgradePromptIfNotEntitled(
221+
from || '',
222+
entitledToAuditLogDays
223+
)
218224
if (shouldShowUpgradePrompt) {
219225
setShowUpgradePrompt(!showUpgradePrompt)
220226
} else {
@@ -226,7 +232,10 @@ export const LogsPreviewer = ({
226232
// Show the prompt on page load based on query params
227233
useEffect(() => {
228234
if (timestampStart) {
229-
const shouldShowUpgradePrompt = maybeShowUpgradePrompt(timestampStart, organization?.plan?.id)
235+
const shouldShowUpgradePrompt = maybeShowUpgradePromptIfNotEntitled(
236+
timestampStart,
237+
entitledToAuditLogDays
238+
)
230239
if (shouldShowUpgradePrompt) {
231240
setShowUpgradePrompt(!showUpgradePrompt)
232241
}

apps/studio/hooks/misc/useUpgradePrompt.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { maybeShowUpgradePrompt } from 'components/interfaces/Settings/Logs/Logs.utils'
1+
import { maybeShowUpgradePromptIfNotEntitled } from 'components/interfaces/Settings/Logs/Logs.utils'
22
import { useEffect, useState } from 'react'
3-
import { useSelectedOrganizationQuery } from './useSelectedOrganization'
3+
import { useCheckEntitlements } from './useCheckEntitlements'
44

55
export const useUpgradePrompt = (from: string) => {
6-
const { data: organization } = useSelectedOrganizationQuery()
76
const [showUpgradePrompt, setShowUpgradePrompt] = useState(false)
7+
const { getEntitlementNumericValue } = useCheckEntitlements('log.retention_days')
8+
const entitledToAuditLogDays = getEntitlementNumericValue()
89

9-
const shouldShowUpgradePrompt = maybeShowUpgradePrompt(from, organization?.plan?.id)
10+
const shouldShowUpgradePrompt = maybeShowUpgradePromptIfNotEntitled(from, entitledToAuditLogDays)
1011

1112
useEffect(() => {
1213
if (shouldShowUpgradePrompt) {

apps/studio/pages/project/[ref]/logs/explorer/index.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
LogsWarning,
1919
} from 'components/interfaces/Settings/Logs/Logs.types'
2020
import {
21-
maybeShowUpgradePrompt,
21+
maybeShowUpgradePromptIfNotEntitled,
2222
useEditorHints,
2323
} from 'components/interfaces/Settings/Logs/Logs.utils'
2424
import LogsQueryPanel from 'components/interfaces/Settings/Logs/LogsQueryPanel'
@@ -52,6 +52,7 @@ import {
5252
ResizablePanel,
5353
ResizablePanelGroup,
5454
} from 'ui'
55+
import { useCheckEntitlements } from 'hooks/misc/useCheckEntitlements'
5556

5657
const LOCAL_PLACEHOLDER_QUERY =
5758
'select\n timestamp, event_message, metadata\n from edge_logs limit 5'
@@ -66,7 +67,6 @@ export const LogsExplorerPage: NextPageWithLayout = () => {
6667
const { profile } = useProfile()
6768
const { ref, q, queryId } = useParams()
6869
const projectRef = ref as string
69-
const { data: organization } = useSelectedOrganizationQuery()
7070
const { logsShowMetadataIpTemplate } = useIsFeatureEnabled(['logs:show_metadata_ip_template'])
7171

7272
const allTemplates = useMemo(() => {
@@ -93,6 +93,9 @@ export const LogsExplorerPage: NextPageWithLayout = () => {
9393
[]
9494
)
9595

96+
const { getEntitlementNumericValue } = useCheckEntitlements('log.retention_days')
97+
const entitledToAuditLogDays = getEntitlementNumericValue()
98+
9699
const { data: content } = useContentQuery({
97100
projectRef: ref,
98101
type: 'log_sql',
@@ -253,7 +256,10 @@ export const LogsExplorerPage: NextPageWithLayout = () => {
253256
}
254257

255258
const handleDateChange = ({ to, from }: DatePickerToFrom) => {
256-
const shouldShowUpgradePrompt = maybeShowUpgradePrompt(from, organization?.plan?.id)
259+
const shouldShowUpgradePrompt = maybeShowUpgradePromptIfNotEntitled(
260+
from,
261+
entitledToAuditLogDays
262+
)
257263

258264
if (shouldShowUpgradePrompt) {
259265
setShowUpgradePrompt(!showUpgradePrompt)
@@ -288,12 +294,15 @@ export const LogsExplorerPage: NextPageWithLayout = () => {
288294
// Show the prompt on page load based on query params
289295
useEffect(() => {
290296
if (timestampStart) {
291-
const shouldShowUpgradePrompt = maybeShowUpgradePrompt(timestampStart, organization?.plan?.id)
297+
const shouldShowUpgradePrompt = maybeShowUpgradePromptIfNotEntitled(
298+
timestampStart,
299+
entitledToAuditLogDays
300+
)
292301
if (shouldShowUpgradePrompt) {
293302
setShowUpgradePrompt(!showUpgradePrompt)
294303
}
295304
}
296-
}, [timestampStart, organization])
305+
}, [timestampStart, entitledToAuditLogDays])
297306

298307
return (
299308
<div className="w-full h-full mx-auto">

0 commit comments

Comments
 (0)