diff --git a/libs/data-access/admin-api/src/lib/api/index.d.ts b/libs/data-access/admin-api/src/lib/api/index.d.ts index be683a9c..e2561daf 100644 --- a/libs/data-access/admin-api/src/lib/api/index.d.ts +++ b/libs/data-access/admin-api/src/lib/api/index.d.ts @@ -856,7 +856,7 @@ export interface components { /** @enum {string} */ type: 'STRING'; /** @enum {string} */ - operation: 'EQUALS' | 'NOT_EQUALS'; + operation: 'EQUALS' | 'NOT_EQUALS' | 'CONTAINS'; value?: string; }; FilterStringListItem: { diff --git a/libs/data-access/admin-api/src/lib/api/output.json b/libs/data-access/admin-api/src/lib/api/output.json index 0899fa2a..79651779 100644 --- a/libs/data-access/admin-api/src/lib/api/output.json +++ b/libs/data-access/admin-api/src/lib/api/output.json @@ -2809,7 +2809,7 @@ }, "operation": { "type": "string", - "enum": ["EQUALS", "NOT_EQUALS"] + "enum": ["EQUALS", "NOT_EQUALS", "CONTAINS"] }, "value": { "type": "string" diff --git a/libs/data-access/admin-api/src/lib/api/query.json b/libs/data-access/admin-api/src/lib/api/query.json index 10bdbf92..1a3b355b 100644 --- a/libs/data-access/admin-api/src/lib/api/query.json +++ b/libs/data-access/admin-api/src/lib/api/query.json @@ -862,7 +862,7 @@ }, "operation": { "type": "string", - "enum": ["EQUALS", "NOT_EQUALS"] + "enum": ["EQUALS", "NOT_EQUALS", "CONTAINS"] }, "value": { "type": "string" diff --git a/libs/data-access/query/src/lib/convertFilters.ts b/libs/data-access/query/src/lib/convertFilters.ts index 7c3fdc0e..38712ecf 100644 --- a/libs/data-access/query/src/lib/convertFilters.ts +++ b/libs/data-access/query/src/lib/convertFilters.ts @@ -11,13 +11,13 @@ function convertFilterNumberToSqlClause( ) { switch (filter.operation) { case 'EQUALS': - return `${filter.field} = '${filter.value}'`; + return `${filter.field} = ${filter.value}`; case 'NOT_EQUALS': - return `${filter.field} != '${filter.value}'`; + return `${filter.field} != ${filter.value}`; case 'GREATER_THAN': - return `${filter.field} > '${filter.value}'`; + return `${filter.field} > ${filter.value}`; case 'LESS_THAN': - return `${filter.field} < '${filter.value}'`; + return `${filter.field} < ${filter.value}`; } } @@ -29,6 +29,8 @@ function convertFilterStringToSqlClause( return `${filter.field} = '${filter.value}'`; case 'NOT_EQUALS': return `${filter.field} != '${filter.value}'`; + case 'CONTAINS': + return `${filter.field} LIKE '%${filter.value}%'`; } } @@ -71,8 +73,139 @@ function convertFilterToSqlClause(filter: FilterItem) { } } +function getStatusFilterString(value?: string): FilterItem[] { + switch (value) { + case 'succeeded': + return [ + { + type: 'STRING', + field: 'status', + operation: 'EQUALS', + value: 'completed', + }, + { + type: 'STRING', + field: 'completion_result', + operation: 'EQUALS', + value: 'success', + }, + ]; + case 'failed': + return [ + { + type: 'STRING', + field: 'status', + operation: 'EQUALS', + value: 'completed', + }, + { + type: 'STRING', + field: 'completion_result', + operation: 'EQUALS', + value: 'failure', + }, + ]; + case 'killed': + return [ + { + type: 'STRING', + field: 'status', + operation: 'EQUALS', + value: 'completed', + }, + { + type: 'STRING', + field: 'completion_result', + operation: 'EQUALS', + value: 'failure', + }, + { + type: 'STRING', + field: 'completion_failure', + operation: 'CONTAINS', + value: 'killed', + }, + ]; + case 'cancelled': + return [ + { + type: 'STRING', + field: 'status', + operation: 'EQUALS', + value: 'completed', + }, + { + type: 'STRING', + field: 'completion_result', + operation: 'EQUALS', + value: 'failure', + }, + { + type: 'STRING', + field: 'completion_failure', + operation: 'CONTAINS', + value: '[409]', + }, + ]; + case 'retrying': + return [ + { + type: 'STRING_LIST', + field: 'status', + operation: 'IN', + value: ['running', 'backing-off'], + }, + { + type: 'NUMBER', + field: 'retry_count', + operation: 'GREATER_THAN', + value: 1, + }, + ]; + + default: + return [ + { + type: 'STRING', + field: 'status', + operation: 'EQUALS', + value, + }, + ]; + } +} + export function convertFilters(filters: FilterItem[]) { - const mappedFilters = filters.map(convertFilterToSqlClause).filter(Boolean); + const statusFilter = filters.find((filter) => filter.field === 'status'); + + const mappedFilters = filters + .filter((filter) => filter.field !== 'status') + .map(convertFilterToSqlClause) + .filter(Boolean); + + if (statusFilter) { + if (statusFilter.type === 'STRING') { + mappedFilters.push( + getStatusFilterString(statusFilter.value) + .map(convertFilterToSqlClause) + .filter(Boolean) + .join(' AND ') + ); + } else if (statusFilter.type === 'STRING_LIST') { + mappedFilters.push( + `(${statusFilter.value + .map((value) => + getStatusFilterString(value) + .map(convertFilterToSqlClause) + .filter(Boolean) + .join(' AND ') + ) + .map((clause) => `(${clause})`) + .join(' OR ')})` + ); + } + } + if (mappedFilters.length === 0) { return ''; } else { diff --git a/libs/data-access/query/src/lib/query.ts b/libs/data-access/query/src/lib/query.ts index 91dfe345..dcba604c 100644 --- a/libs/data-access/query/src/lib/query.ts +++ b/libs/data-access/query/src/lib/query.ts @@ -28,11 +28,6 @@ async function listInvocations( headers: Headers, filters: FilterItem[] ) { - console.log( - `SELECT COUNT(*) AS total_count FROM sys_invocation ${convertFilters( - filters - )}` - ); const totalCountPromise = queryFetcher( `SELECT COUNT(*) AS total_count FROM sys_invocation ${convertFilters( filters @@ -40,7 +35,9 @@ async function listInvocations( { baseUrl, headers } ).then(({ rows }) => rows?.at(0)?.total_count as number); const invocationsPromise = queryFetcher( - `SELECT * FROM sys_invocation ORDER BY modified_at DESC LIMIT ${INVOCATIONS_LIMIT}`, + `SELECT * FROM sys_invocation ${convertFilters( + filters + )} ORDER BY modified_at DESC LIMIT ${INVOCATIONS_LIMIT}`, { baseUrl, headers,