Skip to content

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

Fix for nested log filters when set to false

TzeYiing64f9f77409c2
diff --git a/studio/components/interfaces/Settings/Logs/Logs.utils.ts b/studio/components/interfaces/Settings/Logs/Logs.utils.ts
index 3dac12eca1b..e9c2965d47c 100644
--- a/studio/components/interfaces/Settings/Logs/Logs.utils.ts
+++ b/studio/components/interfaces/Settings/Logs/Logs.utils.ts
@@ -1,37 +1,37 @@
-import { get } from "lodash"
-import { Filters, LogsTableName, SQL_FILTER_TEMPLATES } from "."
+import { get } from 'lodash'
+import { Filters, LogsTableName, SQL_FILTER_TEMPLATES } from '.'
/**
* Recursively retrieve all nested object key paths.
- *
+ *
* TODO: move to utils
- *
+ *
* @param obj any object
* @param parent a string representing the parent key
* @returns string[] all dot paths for keys.
*/
const getDotKeys = (obj: { [k: string]: unknown }, parent?: string): string[] => {
- const keys = Object.keys(obj).filter(k => obj[k])
- return keys.flatMap(k => {
- const currKey = parent ? `${parent}.${k}` : k
- if (typeof obj[k] === 'object') {
- return getDotKeys(obj[k] as any, currKey)
- } else {
- return [currKey]
- }
- })
+ const keys = Object.keys(obj).filter((k) => obj[k])
+ return keys.flatMap((k) => {
+ const currKey = parent ? `${parent}.${k}` : k
+ if (typeof obj[k] === 'object') {
+ return getDotKeys(obj[k] as any, currKey)
+ } else {
+ return [currKey]
+ }
+ })
}
/**
* Root keys in the filter object are considered to be AND filters.
* Nested keys under a root key are considered to be OR filters.
- *
+ *
* For example:
* ```
* {my_value: 'something', nested: {id: 123, test: 123 }}
* ```
* This would be converted into `WHERE (my_value = 'something') and (id = 123 or test = 123)
- *
+ *
* The template of the filter determines the actual filter statement. If no template is provided, a generic equality statement will be used.
* This only applies for root keys of the filter.
* For example:
@@ -39,60 +39,71 @@ const getDotKeys = (obj: { [k: string]: unknown }, parent?: string): string[] =>
* {'my.nested.value': 123}
* ```
* with no template, it will be converted into `WHERE (my.nested.value = 123)
- *
+ *
* @returns a whewhere statement with WHERE clause.
*/
const _genWhereStatement = (table: LogsTableName, filters: Filters) => {
- const keys = Object.keys(filters)
- const filterTemplates = SQL_FILTER_TEMPLATES[table]
- const _resolveTemplateToStatement = (dotKey: string): string | null => {
- const template = filterTemplates[dotKey]
- const value = get(filters, dotKey)
- if (value !== undefined && typeof template === 'function') {
- return template(value)
- } else if (template === undefined) {
- // resolve unknwon filters (possibly from filter overrides)
- // no template, set a default
- if (typeof value === 'string') {
- return `${dotKey} = '${value}'`
- } else {
- return `${dotKey} = ${value}`
- }
- } else if (value === undefined && typeof template === 'function') {
- return null
- } else {
- return template
- }
+ const keys = Object.keys(filters)
+ const filterTemplates = SQL_FILTER_TEMPLATES[table]
+ const _resolveTemplateToStatement = (dotKey: string): string | null => {
+ const template = filterTemplates[dotKey]
+ const value = get(filters, dotKey)
+ if (value !== undefined && typeof template === 'function') {
+ return template(value)
+ } else if (template === undefined) {
+ // resolve unknwon filters (possibly from filter overrides)
+ // no template, set a default
+ if (typeof value === 'string') {
+ return `${dotKey} = '${value}'`
+ } else {
+ return `${dotKey} = ${value}`
+ }
+ } else if (value === undefined && typeof template === 'function') {
+ return null
+ } else if (template && value === false) {
+ // template present, but value is false
+ return null
+ } else {
+ return template
}
+ }
- const statement = keys.map(rootKey => {
- if (typeof filters[rootKey] === 'object') {
- // join all statements with an OR
- const nestedStatement = getDotKeys(filters[rootKey] as Filters, rootKey).map(_resolveTemplateToStatement).filter(Boolean).join(" or ")
- return `(${nestedStatement})`
+ const statement = keys
+ .map((rootKey) => {
+ if (typeof filters[rootKey] === 'object') {
+ // join all statements with an OR
+ const nestedStatements = getDotKeys(filters[rootKey] as Filters, rootKey)
+ .map(_resolveTemplateToStatement)
+ .filter(Boolean)
+
+ if (nestedStatements.length > 0) {
+ return `(${nestedStatements.join(' or ')})`
} else {
- const nestedStatement = _resolveTemplateToStatement(rootKey)
- if (nestedStatement === null) return null
- return `(${nestedStatement})`
+ return null
}
+ } else {
+ const nestedStatement = _resolveTemplateToStatement(rootKey)
+ if (nestedStatement === null) return null
+ return `(${nestedStatement})`
+ }
+ })
+ .filter(Boolean)
+ // join all root statements with AND
+ .join(' and ')
- }).filter(Boolean)
- // join all root statements with AND
- .join(" and ")
-
- if (statement) {
- return 'where ' + statement
- } else {
- return ''
- }
+ if (statement) {
+ return 'where ' + statement
+ } else {
+ return ''
+ }
}
export const genDefaultQuery = (table: LogsTableName, filters: Filters) => {
- const where = _genWhereStatement(table, filters)
+ const where = _genWhereStatement(table, filters)
- switch (table) {
- case 'edge_logs':
- return `select id, timestamp, event_message, metadata, request, response, request.method, request.path, response.status_code
+ switch (table) {
+ case 'edge_logs':
+ return `select id, timestamp, event_message, metadata, request, response, request.method, request.path, response.status_code
from ${table}
cross join unnest(metadata) as m
cross join unnest(m.request) as request
@@ -100,27 +111,27 @@ export const genDefaultQuery = (table: LogsTableName, filters: Filters) => {
${where}
limit 100
`
- break
+ break
- case 'postgres_logs':
- return `select postgres_logs.timestamp, id, event_message, metadata, metadataparsed.error_severity from ${table}
+ case 'postgres_logs':
+ return `select postgres_logs.timestamp, id, event_message, metadata, metadataparsed.error_severity from ${table}
cross join unnest(metadata) as m
cross join unnest(m.parsed) as metadataparsed
${where}
limit 100
`
- break
+ break
- case 'function_logs':
- return `select id, ${table}.timestamp, event_message, metadata.event_type, metadata.function_id, metadata.level, metadata from ${table}
+ case 'function_logs':
+ return `select id, ${table}.timestamp, event_message, metadata.event_type, metadata.function_id, metadata.level, metadata from ${table}
cross join unnest(metadata) as metadata
${where}
limit 100
`
- break
+ break
- 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}
+ 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}
cross join unnest(metadata) as m
cross join unnest(m.response) as response
cross join unnest(m.request) as request
@@ -128,8 +139,8 @@ export const genDefaultQuery = (table: LogsTableName, filters: Filters) => {
limit 100
`
- default:
- return ""
- break
- }
-}
\ No newline at end of file
+ default:
+ return ''
+ break
+ }
+}
diff --git a/studio/tests/pages/projects/Logs.utils.test.js b/studio/tests/pages/projects/Logs.utils.test.js
index 053d45e670a..b74f812e17b 100644
--- a/studio/tests/pages/projects/Logs.utils.test.js
+++ b/studio/tests/pages/projects/Logs.utils.test.js
@@ -1,6 +1,20 @@
-import { genDefaultQuery, LogsTableName } from 'components/interfaces/Settings/Logs'
+import {
+ genDefaultQuery,
+ LogsTableName,
+ SQL_FILTER_TEMPLATES,
+} from 'components/interfaces/Settings/Logs'
describe.each(Object.values(LogsTableName))('%s', (table) => {
+ const templates = SQL_FILTER_TEMPLATES[table]
+ const stringTemplateKey = Object.keys(templates).find((key) => {
+ if (typeof templates[key] === 'string' && key.split('.').length === 2) {
+ return true
+ } else {
+ false
+ }
+ })
+ const [root, child] = (stringTemplateKey || '').split('.')
+ console.log(stringTemplateKey, templates[stringTemplateKey])
describe.each([
{ filter: { search_query: '' } },
{ filter: { search_query: undefined } },
@@ -27,15 +41,27 @@ describe.each(Object.values(LogsTableName))('%s', (table) => {
filter: { 'override.nested': 'something' },
contains: ['override.nested', 'something'],
},
- ])('generates sql for filter $filter', ({ filter, contains = [] }) => {
+ // check for when string templates are set to false.
+ ...(stringTemplateKey
+ ? [
+ {
+ filter: { [root]: { [child]: false } },
+ excludes: [root, child, stringTemplateKey, templates[stringTemplateKey], '()'],
+ },
+ ]
+ : []),
+ ])('generates sql for filter $filter', ({ filter, contains = [], excludes = [] }) => {
test('generates query correctly', async () => {
const generated = genDefaultQuery(table, filter)
// should not contain functions
expect(generated).not.toMatch(/function[ A-Za-z_-]+\(.+\).+\{.+return.+\}/)
expect(generated).not.toMatch(/\=\>|\$\{.+\}/)
expect(generated).not.toContain('return')
-
+ if (stringTemplateKey) {
+ console.log(generated)
+ }
contains.forEach((str) => expect(generated).toContain(str))
+ excludes.forEach((str) => expect(generated).not.toContain(str))
})
})
})