Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Merge branch 'master' into chore/show-upgrade-prompt-in-logs-explorer

Terry Suttond72db308796c
diff --cc studio/components/interfaces/Settings/Logs/Logs.utils.ts
index fb7273ce1cb,2c2f2f58200..3a754010138
--- a/studio/components/interfaces/Settings/Logs/Logs.utils.ts
+++ b/studio/components/interfaces/Settings/Logs/Logs.utils.ts
@@@ -167,19 -167,71 +167,87 @@@ export const genDefaultQuery = (table:
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 upgrade 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: string) => {
+ const toTime = dayjs()
+ const fromTime = dayjs(from)
+ const hours = toTime.diff(fromTime, 'minutes')
+
+ if (hours > 2880) {
+ return true
+ }
+}
++
+ export const genCountQuery = (table: string): string => `SELECT count(*) as count FROM ${table}`
+
+ /** calculates how much the chart start datetime should be offset given the current datetime filter params */
+ export const calcChartStart = (params: Partial<LogsEndpointParams>): [Dayjs, string] => {
+ const ite = params.iso_timestamp_end ? dayjs(params.iso_timestamp_end) : dayjs()
+ const its = params.iso_timestamp_start ? dayjs(params.iso_timestamp_start) : dayjs()
+ let trunc = 'minute'
+ let extendValue = 60 * 6
+ const minuteDiff = ite.diff(its, 'minute')
+ const hourDiff = ite.diff(its, 'hour')
- if (minuteDiff > (60 * 12)) {
++ if (minuteDiff > 60 * 12) {
+ trunc = 'hour'
+ extendValue = 24 * 3
+ } else if (hourDiff > 24 * 5) {
+ trunc = 'day'
+ extendValue = 7
+ }
+ return [its.add(-extendValue, trunc), trunc]
+ }
+
+ /**
+ *
+ * generates log event chart query
+ */
+ export const genChartQuery = (
+ table: LogsTableName,
+ params: LogsEndpointParams,
+ filters: Filters
+ ) => {
+ const [startOffset, trunc] = calcChartStart(params)
+ const where = _genWhereStatement(table, filters)
+ return `
+ SELECT
+ timestamp_trunc(t.timestamp, ${trunc}) as timestamp,
+ count(timestamp) as count
+ FROM
+ ${table} t
-${where ? where + ` and t.timestamp > '${startOffset.toISOString()}'` : ""}
++${where ? where + ` and t.timestamp > '${startOffset.toISOString()}'` : ''}
+ GROUP BY
+ timestamp
+ ORDER BY
+ timestamp ASC
+ `
+ }
+
-
+ type TsPair = [string | '', string | '']
-export const ensureNoTimestampConflict = ([initialStart, initialEnd]: TsPair, [nextStart, nextEnd]: TsPair): TsPair => {
++export const ensureNoTimestampConflict = (
++ [initialStart, initialEnd]: TsPair,
++ [nextStart, nextEnd]: TsPair
++): TsPair => {
+ if (initialStart && initialEnd && nextEnd && !nextStart) {
+ const resolvedDiff = dayjs(nextEnd).diff(dayjs(initialStart))
+ let start = dayjs(initialStart)
+
+ if (resolvedDiff <= 0) {
+ // start ts is definitely before end ts
- const currDiff = Math.abs(
- dayjs(initialEnd).diff(start, 'minute')
- )
++ const currDiff = Math.abs(dayjs(initialEnd).diff(start, 'minute'))
+ // shift start ts backwards by the current ts difference
+ start = dayjs(nextEnd).subtract(currDiff, 'minute')
+ }
+ return [start.toISOString(), nextEnd]
+ } else if (!nextEnd && nextStart) {
+ return [nextStart, initialEnd]
+ } else {
+ return [nextStart, nextEnd]
+ }
-
-}
++}
diff --cc studio/components/interfaces/Settings/Logs/LogsQueryPanel.tsx
index f8e35cd8b36,c88af9d5b15..6c415eb126c
--- a/studio/components/interfaces/Settings/Logs/LogsQueryPanel.tsx
+++ b/studio/components/interfaces/Settings/Logs/LogsQueryPanel.tsx
@@@ -48,110 -42,141 +42,141 @@@ const LogsQueryPanel: React.FC<Props>
defaultTo,
onDateChange,
warnings,
- }) => (
- <div
- className="
- border
- border-panel-border-light dark:border-panel-border-dark rounded rounded-bl-none rounded-br-none
- bg-panel-header-light dark:bg-panel-header-dark
+ }) => {
+ const { ui } = useStore()
+ const canCreateLogQuery = checkPermissions(PermissionAction.CREATE, 'user_content', {
+ resource: { type: 'log_sql', owner_id: ui.profile?.id },
+ subject: { id: ui.profile?.id },
+ })
- "
- >
- <div className="px-5 py-2 flex items-center justify-between w-full">
- <div className="flex flex-row gap-x-4 items-center justify-between w-full">
- <div className="flex items-center gap-2">
- <Dropdown
- side="bottom"
- align="start"
- overlay={Object.values(LogsTableName)
- .sort((a, b) => a.localeCompare(b))
- .map((source) => (
- <Dropdown.Item key={source} onClick={() => onSelectSource(source)}>
- <div className="flex flex-col gap-1">
- <span className="font-mono text-scale-1100 font-bold">{source}</span>
- <span className="text-scale-1100">{LOGS_SOURCE_DESCRIPTION[source]}</span>
- </div>
- </Dropdown.Item>
- ))}
- >
- <Button as="span" type="default" iconRight={<IconChevronDown />}>
- Insert source
- </Button>
- </Dropdown>
+ return (
+ <div
+ className="
+ border
+ border-panel-border-light dark:border-panel-border-dark rounded rounded-bl-none rounded-br-none
+ bg-panel-header-light dark:bg-panel-header-dark
-
+
- <Dropdown
- side="bottom"
- align="start"
- overlay={templates
- .sort((a, b) => a.label!.localeCompare(b.label!))
- .map((template: LogTemplate) => (
- <Dropdown.Item key={template.label} onClick={() => onSelectTemplate(template)}>
- <Typography.Text>{template.label}</Typography.Text>
- </Dropdown.Item>
- ))}
- >
- <Button as="span" type="default" iconRight={<IconChevronDown />}>
- Templates
- </Button>
- </Dropdown>
- <DatePickers
- to={defaultTo}
- from={defaultFrom}
- onChange={onDateChange}
- helpers={EXPLORER_DATEPICKER_HELPERS}
- />
- <div className="overflow-hidden">
- <div
- className={` transition-all duration-300 ${
- warnings.length > 0 ? 'opacity-100' : 'invisible w-0 h-0 opacity-0'
- }`}
- >
- <Popover
- portalled
- overlay={
- <Alert variant="warning" title="">
- <div className="flex flex-col gap-3">
- {warnings.map((warning, index) => (
- <p key={index}>
- {warning.text}{' '}
- {warning.link && (
- <Link href={warning.link}>{warning.linkText || 'View'}</Link>
- )}
- </p>
- ))}
+ "
+ >
+ <div className="px-5 py-2 flex items-center justify-between w-full">
+ <div className="flex flex-row gap-x-4 items-center justify-between w-full">
+ <div className="flex items-center gap-2">
+ <Dropdown
+ side="bottom"
+ align="start"
+ overlay={Object.values(LogsTableName)
+ .sort((a, b) => a.localeCompare(b))
+ .map((source) => (
+ <Dropdown.Item key={source} onClick={() => onSelectSource(source)}>
+ <div className="flex flex-col gap-1">
+ <span className="font-mono text-scale-1100 font-bold">{source}</span>
+ <span className="text-scale-1100">{LOGS_SOURCE_DESCRIPTION[source]}</span>
</div>
- </Alert>
- }
+ </Dropdown.Item>
+ ))}
+ >
+ <Button as="span" type="default" iconRight={<IconChevronDown />}>
+ Insert source
+ </Button>
+ </Dropdown>
+
+ <Dropdown
+ side="bottom"
+ align="start"
+ overlay={templates
+ .sort((a, b) => a.label!.localeCompare(b.label!))
+ .map((template: LogTemplate) => (
+ <Dropdown.Item key={template.label} onClick={() => onSelectTemplate(template)}>
+ <p>{template.label}</p>
+ </Dropdown.Item>
+ ))}
+ >
+ <Button as="span" type="default" iconRight={<IconChevronDown />}>
+ Templates
+ </Button>
+ </Dropdown>
+ <DatePickers
+ to={defaultTo}
+ from={defaultFrom}
+ onChange={onDateChange}
+ helpers={EXPLORER_DATEPICKER_HELPERS}
+ />
+ <div className="overflow-hidden">
+ <div
+ className={` transition-all duration-300 ${
+ warnings.length > 0 ? 'opacity-100' : 'invisible w-0 h-0 opacity-0'
+ }`}
>
- <Badge color="yellow">
- {warnings.length} {warnings.length > 1 ? 'warnings' : 'warning'}
- </Badge>
- </Popover>
+ <Popover
+ portalled
+ overlay={
+ <Alert variant="warning" title="">
+ <div className="flex flex-col gap-3">
+ {warnings.map((warning, index) => (
+ <p key={index}>
+ {warning.text}{' '}
+ {warning.link && (
+ <Link href={warning.link}>{warning.linkText || 'View'}</Link>
+ )}
+ </p>
+ ))}
+ </div>
+ </Alert>
+ }
+ >
+ <Badge color="yellow">
+ {warnings.length} {warnings.length > 1 ? 'warnings' : 'warning'}
+ </Badge>
+ </Popover>
+ </div>
</div>
</div>
- </div>
- <div>
- <div className="flex items-center gap-4">
- <div className="flex items-center gap-2">
- <Button type="default" onClick={onClear}>
- Clear query
- </Button>
- {onSave && (
- <Button type="default" onClick={() => onSave()} disabled={!hasEditorValue}>
- Save query
+ <div>
+ <div className="flex items-center gap-4">
+ <div className="flex items-center gap-2">
+ <Button type="default" onClick={onClear}>
+ Clear query
</Button>
- )}
- </div>
+ {onSave && (
+ <Tooltip.Root delayDuration={0}>
+ <Tooltip.Trigger>
+ <Button
+ type="default"
+ onClick={() => onSave()}
+ disabled={!canCreateLogQuery || !hasEditorValue}
+ >
+ Save query
+ </Button>
+ </Tooltip.Trigger>
+ {!canCreateLogQuery && (
+ <Tooltip.Content side="bottom">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'bg-scale-100 rounded py-1 px-2 leading-none shadow',
+ 'border-scale-200 border',
+ ].join(' ')}
+ >
+ <span className="text-scale-1200 text-xs">
+ You need additional permissions to save your query
+ </span>
+ </div>
+ </Tooltip.Content>
+ )}
+ </Tooltip.Root>
+ )}
+ </div>
- <Button
- type={hasEditorValue ? 'primary' : 'alternative'}
- disabled={!hasEditorValue}
- onClick={onRun}
- iconRight={<IconPlay />}
- loading={isLoading}
- >
- Run
- </Button>
+ <Button
+ type={hasEditorValue ? 'primary' : 'alternative'}
+ disabled={!hasEditorValue}
+ onClick={onRun}
+ iconRight={<IconPlay />}
+ loading={isLoading}
+ >
+ Run
+ </Button>
+ </div>
</div>
</div>
</div>
diff --cc studio/pages/project/[ref]/logs-explorer/index.tsx
index 4ed6abe6a22,e0c7ed91d7c..f5c1f8aeeff
--- a/studio/pages/project/[ref]/logs-explorer/index.tsx
+++ b/studio/pages/project/[ref]/logs-explorer/index.tsx
@@@ -12,19 -21,9 +21,11 @@@ import
LOGS_LARGE_DATE_RANGE_DAYS_THRESHOLD,
LogTable,
LogTemplate,
+ maybeShowUpgradePrompt,
TEMPLATES,
} from 'components/interfaces/Settings/Logs'
- import { uuidv4 } from 'lib/helpers'
- import useLogsQuery from 'hooks/analytics/useLogsQuery'
- import { LogsExplorerLayout } from 'components/layouts'
- import ShimmerLine from 'components/ui/ShimmerLine'
- import LoadingOpacity from 'components/ui/LoadingOpacity'
- import { NextPageWithLayout, UserContent } from 'types'
- import toast from 'react-hot-toast'
- import dayjs from 'dayjs'
import UpgradePrompt from 'components/interfaces/Settings/Logs/UpgradePrompt'
+import { useProjectSubscription } from 'hooks'
export const LogsExplorerPage: NextPageWithLayout = () => {
const router = useRouter()