Skip to content

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

added timestamp filter checking for query range

TzeYiingfdb923d4fe3f
diff --git a/studio/components/interfaces/Settings/Logs/LogPanel.tsx b/studio/components/interfaces/Settings/Logs/LogPanel.tsx
new file mode 100644
index 00000000000..9b142437ea0
--- /dev/null
+++ b/studio/components/interfaces/Settings/Logs/LogPanel.tsx
@@ -0,0 +1,245 @@
+import React, { FC, useEffect, useState } from 'react'
+import {
+ Button,
+ Input,
+ Dropdown,
+ Typography,
+ IconChevronDown,
+ IconRefreshCw,
+ IconX,
+ Toggle,
+ IconSearch,
+ IconClock,
+ Popover,
+} from '@supabase/ui'
+import { LogSearchCallback, LogTemplate, TIER_QUERY_LIMITS } from '.'
+import dayjs from 'dayjs'
+import utc from 'dayjs/plugin/utc'
+import { Tier } from 'hooks/misc/useProjectSubscription'
+interface Props {
+ defaultSearchValue?: string
+ defaultFromValue?: string
+ templates?: any
+ isLoading: boolean
+ isCustomQuery: boolean
+ newCount: number
+ onRefresh?: () => void
+ onSearch?: LogSearchCallback
+ onCustomClick?: () => void
+ onSelectTemplate: (template: LogTemplate) => void
+ isShowingEventChart: boolean
+ onToggleEventChart: () => void
+ tier?: Tier
+}
+
+dayjs.extend(utc)
+
+/**
+ * Logs control panel header + wrapper
+ */
+const LogPanel: FC<Props> = ({
+ templates = [],
+ isLoading,
+ isCustomQuery,
+ newCount,
+ onRefresh,
+ onSearch = () => {},
+ defaultSearchValue = '',
+ defaultFromValue = '',
+ onCustomClick,
+ onSelectTemplate,
+ isShowingEventChart,
+ onToggleEventChart,
+ tier,
+}) => {
+ const [search, setSearch] = useState('')
+ const [from, setFrom] = useState({ value: '', error: '' })
+ const [defaultTimestamp, setDefaultTimestamp] = useState(dayjs().utc().toISOString())
+
+ // Sync local state with provided default value
+ useEffect(() => {
+ if (search !== defaultSearchValue) {
+ setSearch(defaultSearchValue)
+ }
+ }, [defaultSearchValue])
+
+ useEffect(() => {
+ if (from.value !== defaultFromValue) {
+ setFrom({ value: defaultFromValue, error: '' })
+ }
+ }, [defaultFromValue])
+
+ const handleFromChange = (e: React.ChangeEvent<HTMLInputElement>) => {
+ const value = e.target.value
+ const parsed = Date.parse(value)
+ const now = new Date().getTime()
+ const tierQueryLimit = TIER_QUERY_LIMITS[tier?.key || 'FREE']
+ if (value !== '' && isNaN(parsed)) {
+ setFrom({ value, error: 'Invalid ISO 8601 timestamp' })
+ } else if (new Date(value).getTime() < now - tierQueryLimit.windowSeconds * 1000) {
+ setFrom({ value, error: `Query window is limited to ${tierQueryLimit.text}` })
+ } else {
+ setFrom({ value, error: '' })
+ }
+ }
+ const handleFromReset = async () => {
+ setFrom({ value: '', error: '' })
+ const value = dayjs().utc().toISOString()
+ setDefaultTimestamp(value)
+ onSearch({ query: search, from: '' })
+ }
+
+ const handleSearch = () => onSearch({ query: search, from: from.value })
+
+ const showFromReset = from.value !== ''
+ return (
+ <div className="bg-panel-header-light dark:bg-panel-header-dark">
+ <div className="px-2 py-1 flex items-center justify-between w-full">
+ <div className="flex flex-row gap-x-4 items-center">
+ <Button
+ type="text"
+ icon={
+ <div className="relative">
+ {newCount > 0 && (
+ <div
+ className={[
+ 'absolute flex items-center justify-center -top-3 right-3',
+ 'h-3 w-3 z-50',
+ ].join(' ')}
+ >
+ <div className="absolute z-20">
+ <Typography.Text style={{ fontSize: '0.7rem' }} className="opacity-80">
+ {newCount}
+ </Typography.Text>
+ </div>
+ <div className="bg-green-600 rounded-full w-full h-full animate-ping opacity-40"></div>
+ <div className="absolute z-60 top-0 right-0 bg-green-900 opacity-60 rounded-full w-full h-full "></div>
+ </div>
+ )}
+ <IconRefreshCw />
+ </div>
+ }
+ loading={isLoading}
+ onClick={onRefresh}
+ >
+ Refresh
+ </Button>
+ <Dropdown
+ side="bottom"
+ align="start"
+ overlay={templates.map((template: LogTemplate) => (
+ <Dropdown.Item key={template.label} onClick={() => onSelectTemplate(template)}>
+ <Typography.Text>{template.label}</Typography.Text>
+ </Dropdown.Item>
+ ))}
+ >
+ <Button as="span" type="text" iconRight={<IconChevronDown />}>
+ Templates
+ </Button>
+ </Dropdown>
+
+ <div className="flex items-center space-x-2">
+ <Typography.Text type="secondary" small>
+ Search logs via query
+ </Typography.Text>
+ <Toggle size="tiny" checked={isCustomQuery} onChange={onCustomClick} />
+ </div>
+ </div>
+ <div className="flex items-center gap-x-4">
+ {!isCustomQuery && (
+ <>
+ <div className="flex flex-row">
+ <Popover
+ side="bottom"
+ align="end"
+ portalled
+ overlay={
+ <Input
+ label="From"
+ labelOptional="UTC"
+ value={from.value === '' ? defaultTimestamp : from.value}
+ onChange={handleFromChange}
+ error={from.error}
+ className="w-72 p-3"
+ actions={[
+ <Button
+ key="set"
+ size="tiny"
+ title="Set"
+ type="secondary"
+ onClick={handleSearch}
+ >
+ Set
+ </Button>,
+ ]}
+ />
+ }
+ >
+ <Button
+ as="span"
+ size="tiny"
+ className={showFromReset ? '!rounded-r-none' : ''}
+ type={showFromReset ? 'outline' : 'text'}
+ icon={<IconClock size="tiny" />}
+ >
+ {from.value ? 'Custom' : 'Now'}
+ </Button>
+ </Popover>
+ {showFromReset && (
+ <Button
+ size="tiny"
+ className={showFromReset ? '!rounded-l-none' : ''}
+ icon={<IconX size="tiny" />}
+ type="outline"
+ title="Clear timestamp filter"
+ onClick={handleFromReset}
+ />
+ )}
+ </div>
+ {!isCustomQuery && (
+ <div className="flex items-center space-x-2">
+ <Typography.Text type="secondary" small>
+ Show event chart
+ </Typography.Text>
+ <Toggle size="tiny" checked={isShowingEventChart} onChange={onToggleEventChart} />
+ </div>
+ )}
+ {/* wrap with form so that if user presses enter, the search value will submit automatically */}
+ <form
+ id="log-panel-search"
+ onSubmit={(e) => {
+ // prevent redirection
+ e.preventDefault()
+ handleSearch()
+ }}
+ >
+ <Input
+ placeholder="Search events"
+ onChange={(e) => setSearch(e.target.value)}
+ value={search}
+ actions={[
+ search && (
+ <IconX
+ key="clear-search"
+ size="tiny"
+ className="cursor-pointer mx-1"
+ title="Clear search"
+ onClick={() => setSearch('')}
+ />
+ ),
+
+ <Button key="go" size="tiny" title="Go" type="secondary" onClick={handleSearch}>
+ <IconSearch size={16} />
+ </Button>,
+ ]}
+ />
+ </form>
+ </>
+ )}
+ </div>
+ </div>
+ </div>
+ )
+}
+
+export default LogPanel
diff --git a/studio/components/interfaces/Settings/Logs/Logs.constants.ts b/studio/components/interfaces/Settings/Logs/Logs.constants.ts
index a3bb609b3ce..f8b15fe60c4 100644
--- a/studio/components/interfaces/Settings/Logs/Logs.constants.ts
+++ b/studio/components/interfaces/Settings/Logs/Logs.constants.ts
@@ -462,3 +462,9 @@ export const EXPLORER_DATEPICKER_HELPERS: DatetimeHelper[] = [
export const getDefaultHelper = (helpers: DatetimeHelper[]) =>
helpers.find((helper) => helper.default) || helpers[0]
+
+export const TIER_QUERY_LIMITS = {
+ FREE: { text: '1 day', windowSeconds: 3600 * 24 },
+ PRO: { text: '7 days', windowSeconds: 3600 * 24 * 7 },
+ PAYG: { text: '3 months', windowSeconds: 3600 * 24 * 30 * 3 },
+}
diff --git a/studio/hooks/misc/useProjectSubscription.tsx b/studio/hooks/misc/useProjectSubscription.tsx
index 4b9a2a553f4..beb32f9ab71 100644
--- a/studio/hooks/misc/useProjectSubscription.tsx
+++ b/studio/hooks/misc/useProjectSubscription.tsx
@@ -2,13 +2,14 @@ import { get } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import useSWR from 'swr'
+export interface Tier {
+ name: string
+ prod_id: string
+ key: 'FREE' | 'PRO' | 'PAYG'
+ unit_amount: number
+}
interface Data {
- tier: {
- name: string
- prod_id: string
- key: 'FREE' | 'PRO' | 'PAYG'
- unit_amount: number
- }
+ tier: Tier
addons: any[]
billing: {
billing_cycle_anchor: number
diff --git a/studio/pages/project/[ref]/settings/logs/[type].tsx b/studio/pages/project/[ref]/settings/logs/[type].tsx
index a27a14be23b..442a56723da 100644
--- a/studio/pages/project/[ref]/settings/logs/[type].tsx
+++ b/studio/pages/project/[ref]/settings/logs/[type].tsx
@@ -29,6 +29,7 @@ import {
LogData,
LogSearchCallback,
LOG_TYPE_LABEL_MAPPING,
+ TIER_QUERY_LIMITS,
} from 'components/interfaces/Settings/Logs'
import { uuidv4 } from 'lib/helpers'
import useSWRInfinite, { SWRInfiniteKeyLoader } from 'swr/infinite'
@@ -70,13 +71,10 @@ export const LogPage: NextPage = () => {
value.toLowerCase().includes('select') ? true : false
const isSelectQuery = checkIfSelectQuery(editorValue)
const [sub] = useProjectSubscription(ref as string)
- const tier = sub ? sub?.tier.key : 'FREE'
- const tierName = sub ? sub?.tier.name : 'Free tier'
- const tierQueryWindow = {
- FREE: '1 day',
- PRO: '7 days',
- PAYG: '3 months',
- }[tier]
+ const tierData = sub && sub?.tier ? sub.tier : undefined
+ const tier = tierData ? tierData?.key : 'FREE'
+ const tierName = tierData ? sub?.tier.name : 'Free tier'
+ const tierQueryLimit = TIER_QUERY_LIMITS[tier]
useEffect(() => {
setParams({ ...params, type: type as string })
@@ -247,6 +245,7 @@ export const LogPage: NextPage = () => {
<SettingsLayout title={title}>
<div className="h-full flex flex-col flex-grow">
<LogPanel
+ tier={tierData}
isShowingEventChart={showChart}
onToggleEventChart={() => setShowChart(!showChart)}
isCustomQuery={mode === 'custom'}
@@ -355,7 +354,7 @@ export const LogPage: NextPage = () => {
title={
<div className="flex flex-row gap-2 items-center">
<Typography.Text>
- {`${tierName} is limited to a ${tierQueryWindow} query window`}
+ {`${tierName} is limited to a ${tierQueryLimit.text} query window`}
</Typography.Text>
{tier !== 'PAYG' && (
<Link href={`/project/${ref}/settings/billing`}>