The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Move logic for checking for upgrade prompt to helper function
Terry Suttonfcb36fec86a4
diff --git a/studio/components/interfaces/Settings/Logs/Logs.constants.ts b/studio/components/interfaces/Settings/Logs/Logs.constants.tsindex aa915d032d2..55d5f904bf9 100644--- a/studio/components/interfaces/Settings/Logs/Logs.constants.ts+++ b/studio/components/interfaces/Settings/Logs/Logs.constants.ts@@ -21,7 +21,7 @@ export const TEMPLATES: LogTemplate[] = [description: 'Count of commits made by users on the database',mode: 'custom',searchString: `select- p.user_name,+ p.user_name,count(*) as countfrom postgres_logsleft join unnest(metadata) as m on true@@ -38,7 +38,7 @@ group bydescription: 'List all IP addresses that used the Supabase API',mode: 'custom',searchString: `select- cast(timestamp as datetime) as timestamp,+ cast(timestamp as datetime) as timestamp,h.x_real_ipfrom edge_logsleft join unnest(metadata) as m on true@@ -52,8 +52,8 @@ where h.x_real_ip is not nulllabel: 'Requests by Country',description: 'List all ISO 3166-1 alpha-2 country codes that used the Supabase API',mode: 'custom',- searchString: `select- cf.country,+ searchString: `select+ cf.country,count(*) as countfrom edge_logsleft join unnest(metadata) as m on true@@ -71,11 +71,11 @@ order bymode: 'custom',description: 'List all Supabase API requests that are slow',searchString: `select- cast(timestamp as datetime) as timestamp,+ cast(timestamp as datetime) as timestamp,event_message,r.origin_timefrom edge_logs- cross join unnest(metadata) as m+ cross join unnest(metadata) as mcross join unnest(m.response) as rwherer.origin_time > 1000@@ -90,11 +90,11 @@ limit 100description: 'List all Supabase API requests that responded witha 5XX status code',mode: 'custom',searchString: `select- cast(timestamp as datetime) as timestamp,+ cast(timestamp as datetime) as timestamp,event_message,r.status_codefrom edge_logs- cross join unnest(metadata) as m+ cross join unnest(metadata) as mcross join unnest(m.response) as rwherer.status_code >= 500@@ -113,11 +113,11 @@ limit 100r.search as params,count(timestamp) as cfrom edge_logs- cross join unnest(metadata) as m+ cross join unnest(metadata) as mcross join unnest(m.request) as r-group by+group bypath,- params+ paramsorder byc desclimit 100@@ -132,7 +132,7 @@ limit 100cast(timestamp as datetime) as timestamp,event_messagefrom edge_logs- cross join unnest(metadata) as m+ cross join unnest(metadata) as mcross join unnest(m.request) as rwherepath like '%rest/v1%'@@ -254,7 +254,7 @@ export const LOGS_TABLES = {functions: LogsTableName.FUNCTIONS,fn_edge: LogsTableName.FN_EDGE,auth: LogsTableName.AUTH,- realtime: LogsTableName.REALTIME+ realtime: LogsTableName.REALTIME,}export const LOGS_SOURCE_DESCRIPTION = {@@ -440,36 +440,37 @@ export const PREVIEWER_DATEPICKER_HELPERS: DatetimeHelper[] = [{text: 'Last hour',calcFrom: () => dayjs().subtract(1, 'hour').startOf('hour').toISOString(),- calcTo: () => '',+ calcTo: () => dayjs().toISOString(),default: true,},{text: 'Last 3 hours',calcFrom: () => dayjs().subtract(3, 'hour').startOf('hour').toISOString(),- calcTo: () => '',+ calcTo: () => dayjs().toISOString(),},{text: 'Last day',calcFrom: () => dayjs().subtract(1, 'day').startOf('day').toISOString(),- calcTo: () => '',+ calcTo: () => dayjs().toISOString(),},]export const EXPLORER_DATEPICKER_HELPERS: DatetimeHelper[] = [{text: 'Last day',+ //calcFrom: () => dayjs().subtract(1, 'day').startOf('day').toISOString(),calcFrom: () => dayjs().subtract(1, 'day').startOf('day').toISOString(),- calcTo: () => '',+ calcTo: () => dayjs().toISOString(),default: true,},{text: 'Last 3 days',calcFrom: () => dayjs().subtract(3, 'day').startOf('day').toISOString(),- calcTo: () => '',+ calcTo: () => dayjs().toISOString(),},{text: 'Last 7 days',calcFrom: () => dayjs().subtract(7, 'day').startOf('day').toISOString(),- calcTo: () => '',+ calcTo: () => dayjs().toISOString(),},]diff --git a/studio/components/interfaces/Settings/Logs/Logs.utils.ts b/studio/components/interfaces/Settings/Logs/Logs.utils.tsindex 95a373b8f8a..c53c6c7e92e 100644--- a/studio/components/interfaces/Settings/Logs/Logs.utils.ts+++ b/studio/components/interfaces/Settings/Logs/Logs.utils.ts@@ -130,10 +130,10 @@ export const genDefaultQuery = (table: LogsTableName, filters: Filters) => {`case 'postgres_logs':- return `select postgres_logs.timestamp, id, event_message, parsed.error_severity from ${table}- cross join unnest(metadata) as m- cross join unnest(m.parsed) as parsed- ${where}+ return `select postgres_logs.timestamp, id, event_message, parsed.error_severity from ${table}+ cross join unnest(metadata) as m+ cross join unnest(m.parsed) as parsed+ ${where}limit 100`@@ -145,7 +145,7 @@ export const genDefaultQuery = (table: LogsTableName, filters: Filters) => {`case 'function_edge_logs':- return `select id, ${table}.timestamp, event_message, response.status_code, response, request, request.method, m.function_id, m.execution_time_ms, m.deployment_id, m.version from ${table}+ return `select id, ${table}.timestamp, event_message, response.status_code, response, request, request.method, m.function_id, m.execution_time_ms, m.deployment_id, m.version from ${table}cross join unnest(metadata) as mcross join unnest(m.response) as responsecross join unnest(m.request) as request@@ -156,7 +156,7 @@ export const genDefaultQuery = (table: LogsTableName, filters: Filters) => {default:return `select id, ${table}.timestamp, event_message from ${table}${where}- limit 100+ limit 100`}}@@ -164,4 +164,22 @@ export const genDefaultQuery = (table: LogsTableName, filters: Filters) => {/*** SQL query to retrieve only one log*/-export const genSingleLogQuery = (table: LogsTableName, id: string) => `select id, timestamp, event_message, metadata from ${table} where id = '${id}' limit 1`\ No newline at end of file+export const genSingleLogQuery = (table: LogsTableName, id: string) =>+ `select id, timestamp, event_message, metadata from ${table} where id = '${id}' limit 1`++/**+ * Determine if we should show the user an updgrade prompt while browsing logs+ *+ * There's only 1440 minutes in a day, but when we select "Last day" from the dropdown,+ * it will set that to mean yesterday at midnight.+ * So the max possible time from that midnight to the upcoming midnight is 2880+ */+export const maybeShowUpgradePrompt = (from: any) => {+ const toTime = dayjs()+ const fromTime = dayjs(from)+ const hours = toTime.diff(fromTime, 'minutes')++ if (hours > 2880) {+ return true+ }+}diff --git a/studio/components/interfaces/Settings/Logs/PreviewFilterPanel.tsx b/studio/components/interfaces/Settings/Logs/PreviewFilterPanel.tsxindex bf1d12dad08..1fb245ccff3 100644--- a/studio/components/interfaces/Settings/Logs/PreviewFilterPanel.tsx+++ b/studio/components/interfaces/Settings/Logs/PreviewFilterPanel.tsx@@ -9,11 +9,18 @@ import {IconEye,IconEyeOff,} from '@supabase/ui'-import { Filters, LogSearchCallback, LogTemplate, PREVIEWER_DATEPICKER_HELPERS } from '.'+import {+ Filters,+ LogSearchCallback,+ LogTemplate,+ maybeShowUpgradePrompt,+ PREVIEWER_DATEPICKER_HELPERS,+} from '.'import { FILTER_OPTIONS, LogsTableName } from './Logs.constants'import LogsFilterPopover from './LogsFilterPopover'import DatePickers from './Logs.DatePickers'import CSVButton from 'components/ui/CSVButton'+import { StripeSubscription } from 'components/interfaces/Billing'interface Props {defaultSearchValue?: string@@ -33,7 +40,7 @@ interface Props {csvData?: unknown[]onFiltersChange: (filters: Filters) => voidfilters: Filters- tier: string | undefined+ tier: StripeSubscription['tier']['name'] | undefinedshowUpgradePrompt: booleansetShowUpgradePrompt: (showUpgradePrompt: boolean) => void}@@ -104,14 +111,10 @@ const PreviewFilterPanel: FC<Props> = ({</Button>)const handleSearch = (partial: Partial<Parameters<LogSearchCallback>[0]>) => {- // We want to show the upgrade prompt to Free plan users who go beyond the 1 day retention range- if (tier === 'FREE') {- // [Terry] The date range picker always returns a partial.from and a partial.to- // If we just use the last x hours dropdown, we only get a partial.to- if (partial.to) {- setShowUpgradePrompt(!showUpgradePrompt)- return- }+ const shouldShowUpgradePrompt = maybeShowUpgradePrompt(partial.from)++ if (shouldShowUpgradePrompt && tier === 'FREE') {+ setShowUpgradePrompt(!showUpgradePrompt)}onSearch({ query: search, to: partial?.to || null, from: partial?.from || null })diff --git a/studio/components/interfaces/Settings/Logs/UpgradePrompt.tsx b/studio/components/interfaces/Settings/Logs/UpgradePrompt.tsxindex e46d992789b..29688ed37dc 100644--- a/studio/components/interfaces/Settings/Logs/UpgradePrompt.tsx+++ b/studio/components/interfaces/Settings/Logs/UpgradePrompt.tsx@@ -1,8 +1,7 @@import Link from 'next/link'import dayjs from 'dayjs'-import { useState } from 'react'-import { Button, IconHelpCircle, IconLoader, Modal } from '@supabase/ui'-+import { Button, IconLoader, Modal } from '@supabase/ui'+import { SetStateAction } from 'react'import { TIER_QUERY_LIMITS } from '.'import { useProjectSubscription } from 'hooks'@@ -10,7 +9,7 @@ interface Props {projectRef: stringfrom: stringshowUpgradePrompt: boolean- setShowUpgradePrompt: (showUpgradePrompt: boolean) => void+ setShowUpgradePrompt: React.Dispatch<SetStateAction<boolean>>}const UpgradePrompt: React.FC<Props> = ({diff --git a/studio/pages/project/[ref]/logs-explorer/index.tsx b/studio/pages/project/[ref]/logs-explorer/index.tsxindex a9c2e206966..2cf6be722f6 100644--- a/studio/pages/project/[ref]/logs-explorer/index.tsx+++ b/studio/pages/project/[ref]/logs-explorer/index.tsx@@ -12,6 +12,7 @@ import {LOGS_LARGE_DATE_RANGE_DAYS_THRESHOLD,LogTable,LogTemplate,+ maybeShowUpgradePrompt,TEMPLATES,} from 'components/interfaces/Settings/Logs'import { uuidv4 } from 'lib/helpers'@@ -111,9 +112,10 @@ export const LogsExplorerPage: NextPageWithLayout = () => {}const handleDateChange = ({ to, from }: DatePickerToFrom) => {- if (tier?.key === 'FREE') {+ const shouldShowUpgradePrompt = maybeShowUpgradePrompt(from)++ if (shouldShowUpgradePrompt && tier?.key === 'FREE') {setShowUpgradePrompt(!showUpgradePrompt)- return}setParams((prev) => ({