Skip to content

Commit

Permalink
update filter implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Nik Nasr <[email protected]>
  • Loading branch information
nikrooz committed Jan 2, 2025
1 parent 6aa0b71 commit b5854bc
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 14 deletions.
2 changes: 1 addition & 1 deletion libs/data-access/admin-api/src/lib/api/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion libs/data-access/admin-api/src/lib/api/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -2809,7 +2809,7 @@
},
"operation": {
"type": "string",
"enum": ["EQUALS", "NOT_EQUALS"]
"enum": ["EQUALS", "NOT_EQUALS", "CONTAINS"]
},
"value": {
"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion libs/data-access/admin-api/src/lib/api/query.json
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@
},
"operation": {
"type": "string",
"enum": ["EQUALS", "NOT_EQUALS"]
"enum": ["EQUALS", "NOT_EQUALS", "CONTAINS"]
},
"value": {
"type": "string"
Expand Down
143 changes: 138 additions & 5 deletions libs/data-access/query/src/lib/convertFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
}

Expand All @@ -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}%'`;
}
}

Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 3 additions & 6 deletions libs/data-access/query/src/lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,16 @@ 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
)}`,
{ 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,
Expand Down

0 comments on commit b5854bc

Please sign in to comment.