Skip to content

Commit

Permalink
Added in even more query templates, added template filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziinc authored and joshenlim committed Feb 15, 2022
1 parent fc67028 commit b4e0fbb
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 29 deletions.
4 changes: 2 additions & 2 deletions studio/components/interfaces/Home/ProjectUsage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ const ProjectUsage: FC<Props> = ({ project }) => {
if (timestampDigits < 16) {
// pad unix timestamp with additional 0 and then forward
const paddedTimestamp = String(timestamp) + '0'.repeat(16 - timestampDigits)
router.push(`/project/${ref}/settings/logs/rest?te=${paddedTimestamp}&s=${search}`)
router.push(`/project/${ref}/settings/logs/api?te=${paddedTimestamp}`)
} else {
router.push(`/project/${ref}/settings/logs/rest?te=${timestamp}&s=${search}`)
router.push(`/project/${ref}/settings/logs/api?te=${timestamp}`)
}
}
return (
Expand Down
48 changes: 43 additions & 5 deletions studio/components/interfaces/Settings/Logs/Logs.constants.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,63 @@
import { LogTemplate } from '.'
import { LogTemplate, LogType } from '.'

export const TEMPLATES: LogTemplate[] = [
{ label: 'Recent Errors', mode: 'simple', searchString: '[Ee]rror|\\s[45][0-9][0-9]\\s' },
{
label: 'Recent Errors',
mode: 'simple',
searchString: '[Ee]rror|\\s[45][0-9][0-9]\\s',
for: ['api'],
},
{
label: 'Commits',
mode: 'simple',
searchString: 'COMMIT',
for: ['database'],
},

{
label: '10 Minutes Ago',
mode: 'simple',
searchString: 'timestamp < TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 10 MINUTE)',
for: ['database', 'api'],
},
{
label: 'POST or PATCH',
mode: 'custom',
searchString:
"REGEXP_CONTAINS(event_message, 'POST') OR REGEXP_CONTAINS(event_message, 'PATCH')",
for: ['api'],
},
{
label: 'Metadata IP',
mode: 'custom',
searchString:
`SELECT timestamp, h.x_real_ip
searchString: `SELECT timestamp, h.x_real_ip
FROM edge_logs
LEFT JOIN UNNEST(metadata) as m ON TRUE
LEFT JOIN UNNEST(m.request) AS r ON TRUE
LEFT JOIN UNNEST(r.headers) AS h ON TRUE
WHERE h.x_real_ip IS NOT NULL
`,
for: ['database'],
},
{
label: 'Requests by Country',
mode: 'custom',
searchString: `SELECT
cf.country, count(*) as count
FROM edge_logs
LEFT JOIN UNNEST(metadata) as m ON TRUE
LEFT JOIN UNNEST(m.request) AS r ON TRUE
LEFT JOIN UNNEST(r.cf) AS cf ON TRUE
GROUP BY
cf.country
ORDER BY
DESC count
`,
for: ['api'],
},
]

export const DEFAULT_QUERY = 'SELECT timestamp, event_message FROM postgres_logs;'
export const LOG_TYPE_LABEL_MAPPING: { [k: LogType]: string } = {
api: 'API',
database: 'Database',
}
7 changes: 6 additions & 1 deletion studio/components/interfaces/Settings/Logs/Logs.types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
interface Metadata {
[key: string]: string | number | Object | Object[]
}
export type LogSearchCallback = (filters: { query: string; from?: string, fromMicro?: number }) => void
export type LogSearchCallback = (filters: {
query: string
from?: string
fromMicro?: number
}) => void
export interface LogData {
id: string
timestamp: number
Expand All @@ -12,6 +16,7 @@ export interface LogData {
export interface LogTemplate {
label?: string
mode: 'custom' | 'simple'
for?: LogType[]
searchString: string
}

Expand Down
19 changes: 9 additions & 10 deletions studio/components/layouts/SettingsLayout/SettingsMenu.utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { LogType, LOG_TYPE_LABEL_MAPPING } from 'components/interfaces/Settings/Logs'
import { ProductMenuGroup } from 'components/ui/ProductMenu/ProductMenu.types'
import { LOG_TYPE_LABEL_MAPPING } from 'lib/constants'

export const generateSettingsMenu = (ref: string): ProductMenuGroup[] => {
const logTypes: LogType[] = ['database', 'api']

return [
{
title: 'Project settings',
Expand All @@ -20,15 +22,12 @@ export const generateSettingsMenu = (ref: string): ProductMenuGroup[] => {
},
{
title: 'Logs',
// items: ['database', 'auth', 'realtime', 'rest', 'storage'].map((type: string) => {
items: ['database', 'rest'].map((type: string) => {
return {
name: LOG_TYPE_LABEL_MAPPING[type],
key: `logs-${type}`,
url: `/project/${ref}/settings/logs/${type}`,
items: [],
}
}),
items: logTypes.map((type: string) => ({
name: LOG_TYPE_LABEL_MAPPING[type],
key: `logs-${type}`,
url: `/project/${ref}/settings/logs/${type}`,
items: [],
})),
},
]
}
8 changes: 0 additions & 8 deletions studio/lib/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,6 @@ export const POLICY_MODAL_VIEWS = {
REVIEW: 'REVIEW',
}

export const LOG_TYPE_LABEL_MAPPING: { [k: string]: string } = {
rest: 'API',
realtime: 'Realtime',
auth: 'Auth',
storage: 'Storage',
database: 'Database',
}

export const GOTRUE_ERRORS = {
UNVERIFIED_GITHUB_USER: 'Error sending confirmation mail',
}
8 changes: 5 additions & 3 deletions studio/pages/project/[ref]/settings/logs/[type].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {

import { withAuth } from 'hooks'
import { get } from 'lib/common/fetch'
import { API_URL, LOG_TYPE_LABEL_MAPPING } from 'lib/constants'
import { API_URL } from 'lib/constants'
import { SettingsLayout } from 'components/layouts/'
import CodeEditor from 'components/ui/CodeEditor'
import {
Expand All @@ -28,6 +28,8 @@ import {
TEMPLATES,
LogData,
LogSearchCallback,
LogType,
LOG_TYPE_LABEL_MAPPING,
} from 'components/interfaces/Settings/Logs'
import { uuidv4 } from 'lib/helpers'
import useSWRInfinite from 'swr/infinite'
Expand Down Expand Up @@ -64,7 +66,7 @@ export const LogPage: NextPage = () => {
timestamp_start: '',
timestamp_end: '',
})
const title = `Logs - ${LOG_TYPE_LABEL_MAPPING[type as string]}`
const title = `Logs - ${LOG_TYPE_LABEL_MAPPING[type as LogType]}`
const checkIfSelectQuery = (value: string) =>
value.toLowerCase().includes('select') ? true : false
const isSelectQuery = checkIfSelectQuery(editorValue)
Expand Down Expand Up @@ -246,7 +248,7 @@ export const LogPage: NextPage = () => {
isCustomQuery={mode === 'custom'}
isLoading={isValidating}
newCount={newCount}
templates={TEMPLATES}
templates={TEMPLATES.filter((template) => template.for?.includes(type as LogType))}
onRefresh={handleRefresh}
onSearch={handleSearch}
defaultSearchValue={params.search_query}
Expand Down

0 comments on commit b4e0fbb

Please sign in to comment.