Skip to content

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

added ui logic for new log polling, regex log search.

TzeYiing3899781421d2
diff --git a/studio/components/ui/Logs/LogPanel.tsx b/studio/components/ui/Logs/LogPanel.tsx
index 0d9a322d297..f9491482e47 100644
--- a/studio/components/ui/Logs/LogPanel.tsx
+++ b/studio/components/ui/Logs/LogPanel.tsx
@@ -19,7 +19,9 @@ const LogPanel = ({ onRefresh, isLoading, heading, children, onSearch }: Props)
<div className="flex items-center justify-between w-full" >
<Button type="secondary">Recent - All</Button>
<div className="flex flex-row gap-x-4">
- <Input className="max-w-32" placeholder="Search" onChange={e => onSearch ? onSearch(e.target.value) : null} />
+ <Input className="max-w-32" placeholder="Search" onChange={e => {
+ if(onSearch) onSearch(e.target.value)
+ }} />
<Button
type="outline"
loading={isLoading}
diff --git a/studio/pages/project/[ref]/settings/logs/[type].tsx b/studio/pages/project/[ref]/settings/logs/[type].tsx
index b94e3be193f..01bb5e9856f 100644
--- a/studio/pages/project/[ref]/settings/logs/[type].tsx
+++ b/studio/pages/project/[ref]/settings/logs/[type].tsx
@@ -1,4 +1,4 @@
-import React, { useEffect, useState } from 'react'
+import React, { useCallback, useEffect, useState } from 'react'
import { SettingsLayout } from 'components/layouts/'
import { useRouter } from 'next/router'
import LogPanel from 'components/ui/Logs/LogPanel'
@@ -9,8 +9,12 @@ import { get } from "lib/common/fetch"
import LogTable, { LogData } from 'components/ui/Logs/LogTable'
import useSWR from 'swr'
import { API_URL } from 'lib/constants'
-import { Typography, Input } from '@supabase/ui'
+import { Typography, Input, Button } from '@supabase/ui'
+import debounce from 'lodash/debounce'
+interface CountData {
+ count: number
+}
/**
* Acts as a container component for the entire log display
*/
@@ -18,26 +22,49 @@ export const LogPage = () => {
const router = useRouter()
const { ref, type } = router.query
const title = LOG_TYPE_LABEL_MAPPING[type as string]
- const [searchQuery, setSearchQuery] = useState("")
+ const [search, setSearch] = useState("")
+ const [queryParams, setQueryParams] = useState("")
+ const [latestRefresh, setLatestRefresh] = useState(new Date().toISOString())
// handle log fetching
const {
data,
isValidating: isLoading,
mutate,
- } = useSWR<{ data: LogData[], count: number, error: any }>(`${API_URL}/projects/${ref}/logs?type=${type}`, get, {
+ } = useSWR<{ data: LogData[], count: number, error: any }>(`${API_URL}/projects/${ref}/logs?${queryParams}`, get, {
revalidateOnFocus: false,
})
+ const debouncedSearch = debounce(setSearch, 500)
+
+ useEffect(() => {
+ const params = {
+ search_query: search,
+ path: `/${type}`
+ }
+ const qs = new URLSearchParams(params).toString()
+ setQueryParams(qs)
+ }, [search])
+
+ const countKey = `${API_URL}/projects/${ref}/logs?${queryParams}&count=true&period_start=${latestRefresh}`
+ const {
+ data: countData,
+ mutate: mutateCount
+ } = useSWR<{ data: [CountData] | [], error?: any }>(countKey, get, {
+ refreshInterval: 5000
+ })
+ const newCount = countData?.data[0]?.count
const { data: logData, error } = data || {}
- const filteredData = logData && searchQuery ? logData.filter(d => {
- return d.event_message.toLowerCase().includes(searchQuery.toLowerCase())
- }) : logData
+ const handleRefresh = () => {
+ setLatestRefresh(new Date().toISOString())
+ mutate()
+ }
return (
<SettingsLayout title={title} className="p-4">
- <LogPanel isLoading={isLoading} onRefresh={() => mutate()} onSearch={setSearchQuery}/>
+ <LogPanel isLoading={isLoading} onRefresh={handleRefresh} onSearch={debouncedSearch} />
{error && (<Typography.Text className="text-center w-full block">Could not fetch data</Typography.Text>)}
- <LogTable data={filteredData} />
+ {newCount && newCount > 0 && <Button onClick={handleRefresh}>Load new logs</Button>}
+ <LogTable data={logData} />
</SettingsLayout>
)
}
diff --git a/studio/tests/pages/projects/logs.test.js b/studio/tests/pages/projects/logs.test.js
index f6c9333f1f7..7a3a633c48a 100644
--- a/studio/tests/pages/projects/logs.test.js
+++ b/studio/tests/pages/projects/logs.test.js
@@ -21,7 +21,11 @@ useRouter.mockReturnValue({ query: { ref: "123", type: "auth" } })
import { LogPage } from 'pages/project/[ref]/settings/logs/[type]'
import { render, fireEvent, waitFor, screen } from '@testing-library/react'
import { SWRConfig } from 'swr'
+import userEvent from '@testing-library/user-event'
+beforeEach(() => {
+ get.mockReset()
+})
test('can display log data and metadata', async () => {
const data = [{
id: "seome-uuid",
@@ -42,6 +46,7 @@ test('can display log data and metadata', async () => {
})
test('Refresh', async () => {
+
const data = [{
id: "some-uuid",
timestamp: 1621323232312,
@@ -50,25 +55,71 @@ test('Refresh', async () => {
my_key: "something_value"
}
}]
- get.mockResolvedValue({ data })
- render(
- <SWRConfig value={{ dedupingInterval: null }}>
- <LogPage />
- </SWRConfig>
- )
+ get.mockResolvedValueOnce({ data }).mockResolvedValueOnce({ data: [] })
+ render(<LogPage />)
const row = screen.getByText(/happened/)
fireEvent.click(row)
await waitFor(() => screen.getByText(/my_key/))
- expect(get).toBeCalledTimes(1)
- get.mockResolvedValue({ data: [] })
- // simulate full refresh, loop because swr dedupe behaves weirdly
+ // simulate refresh
+ // wrap with a waitFor, and trigger twice, as button becomes disabled when Loading and may take time to reset
+ await waitFor(() => userEvent.click(screen.getByText(/Refresh/)))
+ // called by counter twice and refresh twice
+ await waitFor(() => expect(get).toBeCalledTimes(4), { timeout: 1000 })
+ // when log line unmounts and it was focused, should close focus panel
+ await waitFor(() => screen.queryByText(/my_key/) === null, { timeout: 1000 })
+ await waitFor(() => screen.queryByText(/happened/) === null, { timeout: 1000 })
+})
+
+
+test("Search will trigger a log refresh", async () => {
+ get.mockImplementation((url) => {
+
+ if (url.includes("search_query") && url.includes("something")) {
+ return {
+ data: [{
+ id: "some-uuid",
+ timestamp: 1621323232312,
+ event_message: "some event happened",
+ metadata: {}
+ }]
+ }
+ }
+ return { data: [] }
+ })
+ render(<LogPage />)
+
+ userEvent.type(screen.getByPlaceholderText(/Search/), "something")
await waitFor(() => {
- fireEvent.click(screen.getByText(/Refresh/))
- expect(get).toBeCalledTimes(2)
+ expect(get).toHaveBeenCalledWith(expect.stringContaining("search_query"))
+ expect(get).toHaveBeenCalledWith(expect.stringContaining("something"))
})
- // when log line unmounts and it was focused, should close focus panel
- await waitFor(() => expect(() => screen.getByText(/my_key/)).toThrow())
- await waitFor(() => expect(() => screen.getByText(/happened/)).toThrow())
+
+ await waitFor(() => screen.getByText(/happened/))
})
+
+test("poll count for new messages", async () => {
+ get.mockImplementation((url) => {
+ if (url.includes("count")) {
+ return { data: [{ count: 3 }] }
+ }
+ return {
+ data: [{
+ id: "some-uuid",
+ timestamp: 1621323232312,
+ event_message: "some event happened",
+ metadata: {}
+ }]
+ }
+ })
+ render(
+ <LogPage />
+ )
+ await waitFor(() => screen.queryByText(/happened/) === null)
+ await waitFor(() => screen.getByText(/Load new logs/))
+
+ userEvent.click(screen.getByText(/Load new logs/))
+ await waitFor(() => screen.queryByText(/Load new logs/) === null)
+ await waitFor(() => screen.getByText(/happened/))
+})
\ No newline at end of file