Skip to content

Commit 7f6ca8a

Browse files
authored
filter by variables (#349)
FLPATH-1991 https://issues.redhat.com/browse/FLPATH-1991 Signed-off-by: Yaron Dayagi <ydayagi@redhat.com>
1 parent 002f7c9 commit 7f6ca8a

14 files changed

Lines changed: 428 additions & 265 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@red-hat-developer-hub/backstage-plugin-orchestrator-backend': patch
3+
'@red-hat-developer-hub/backstage-plugin-orchestrator-common': patch
4+
---
5+
6+
add filter by variables and nested variables

workspaces/orchestrator/plugins/orchestrator-backend/src/helpers/filterBuilder.ts

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
Filter,
2121
IntrospectionField,
2222
LogicalFilter,
23+
NestedFilter,
2324
TypeName,
2425
} from '@red-hat-developer-hub/backstage-plugin-orchestrator-common';
2526

@@ -64,6 +65,10 @@ function isLogicalFilter(filter: Filter): filter is LogicalFilter {
6465
return (filter as LogicalFilter).filters !== undefined;
6566
}
6667

68+
function isNestedFilter(filter: Filter): filter is NestedFilter {
69+
return (filter as NestedFilter).nested !== undefined;
70+
}
71+
6772
function handleLogicalFilter(
6873
introspection: IntrospectionField[],
6974
type: ProcessType,
@@ -78,6 +83,21 @@ function handleLogicalFilter(
7883
return `${filter.operator.toLowerCase()}: {${subClauses.join(', ')}}`;
7984
}
8085

86+
function handleNestedFilter(
87+
introspection: IntrospectionField[],
88+
type: ProcessType,
89+
filter: NestedFilter,
90+
): string {
91+
const subClauses = buildFilterCondition(
92+
introspection,
93+
type,
94+
filter.nested,
95+
true,
96+
);
97+
98+
return `${filter.field}: {${subClauses}}`;
99+
}
100+
81101
function handleBetweenOperator(filter: FieldFilter): string {
82102
if (!Array.isArray(filter.value) || filter.value.length !== 2) {
83103
throw new Error('Between operator requires an array of two elements');
@@ -114,7 +134,7 @@ function isValidEnumOperator(operator: FieldFilterOperatorEnum): boolean {
114134

115135
function handleBinaryOperator(
116136
binaryFilter: FieldFilter,
117-
fieldDef: IntrospectionField,
137+
fieldDef: IntrospectionField | undefined,
118138
type: 'ProcessDefinition' | 'ProcessInstance',
119139
): string {
120140
if (isEnumFilter(binaryFilter.field, type)) {
@@ -138,11 +158,16 @@ export function buildFilterCondition(
138158
introspection: IntrospectionField[],
139159
type: ProcessType,
140160
filters?: Filter,
161+
isNested?: boolean,
141162
): string {
142163
if (!filters) {
143164
return '';
144165
}
145166

167+
if (isNestedFilter(filters)) {
168+
return handleNestedFilter(introspection, type, filters);
169+
}
170+
146171
if (isLogicalFilter(filters)) {
147172
return handleLogicalFilter(introspection, type, filters);
148173
}
@@ -153,16 +178,21 @@ export function buildFilterCondition(
153178
);
154179
}
155180

156-
const fieldDef = introspection.find(f => f.name === filters.field);
157-
if (!fieldDef) {
158-
throw new Error(`Can't find field "${filters.field}" definition`);
159-
}
181+
let fieldDef;
160182

161-
if (!isOperatorAllowedForField(filters.operator, fieldDef, type)) {
162-
const allowedOperators = supportedOperatorsByType[fieldDef.type.name] || [];
163-
throw new Error(
164-
`Unsupported operator ${filters.operator} for field "${fieldDef.name}" of type "${fieldDef.type.name}". Allowed operators are: ${allowedOperators.join(', ')}`,
165-
);
183+
if (!isNested) {
184+
fieldDef = introspection.find(f => f.name === filters.field);
185+
if (!fieldDef) {
186+
throw new Error(`Can't find field "${filters.field}" definition`);
187+
}
188+
189+
if (!isOperatorAllowedForField(filters.operator, fieldDef, type)) {
190+
const allowedOperators =
191+
supportedOperatorsByType[fieldDef.type.name] || [];
192+
throw new Error(
193+
`Unsupported operator ${filters.operator} for field "${fieldDef.name}" of type "${fieldDef.type.name}". Allowed operators are: ${allowedOperators.join(', ')}`,
194+
);
195+
}
166196
}
167197

168198
switch (filters.operator) {
@@ -221,9 +251,13 @@ function convertToBoolean(value: any): boolean {
221251
function formatValue(
222252
fieldName: string,
223253
fieldValue: any,
224-
fieldDef: IntrospectionField,
254+
fieldDef: IntrospectionField | undefined,
225255
type: ProcessType,
226256
): string {
257+
if (!fieldDef) {
258+
return `"${fieldValue}"`;
259+
}
260+
227261
if (!isFieldFilterSupported) {
228262
throw new Error(`Unsupported field type ${fieldDef.type.name}`);
229263
}

workspaces/orchestrator/plugins/orchestrator-common/report.api.md

Lines changed: 276 additions & 251 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cb305ab5d700a2949eb303ad16653ed83d86e53c
1+
64f078a27298ae8c5f83d7f244f3ab7cd6c9e39c

workspaces/orchestrator/plugins/orchestrator-common/src/generated/api/definition.ts

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

workspaces/orchestrator/plugins/orchestrator-common/src/generated/client/api.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export type FieldFilterValue = any | boolean | number | string;
162162
* @type Filter
163163
* @export
164164
*/
165-
export type Filter = FieldFilter | LogicalFilter;
165+
export type Filter = FieldFilter | LogicalFilter | NestedFilter;
166166

167167
/**
168168
*
@@ -230,6 +230,31 @@ export const LogicalFilterOperatorEnum = {
230230

231231
export type LogicalFilterOperatorEnum = typeof LogicalFilterOperatorEnum[keyof typeof LogicalFilterOperatorEnum];
232232

233+
/**
234+
*
235+
* @export
236+
* @interface NestedFilter
237+
*/
238+
export interface NestedFilter {
239+
/**
240+
*
241+
* @type {string}
242+
* @memberof NestedFilter
243+
*/
244+
'field': string;
245+
/**
246+
*
247+
* @type {NestedFilterNested}
248+
* @memberof NestedFilter
249+
*/
250+
'nested': NestedFilterNested;
251+
}
252+
/**
253+
* @type NestedFilterNested
254+
* @export
255+
*/
256+
export type NestedFilterNested = FieldFilter | NestedFilter;
257+
233258
/**
234259
*
235260
* @export

workspaces/orchestrator/plugins/orchestrator-common/src/generated/docs/api-doc/orchestrator-api.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,19 @@ spec:
702702
oneOf:
703703
- $ref: '#/components/schemas/LogicalFilter'
704704
- $ref: '#/components/schemas/FieldFilter'
705+
- $ref: '#/components/schemas/NestedFilter'
706+
NestedFilter:
707+
type: object
708+
required:
709+
- field
710+
- nested
711+
properties:
712+
field:
713+
type: string
714+
nested:
715+
oneOf:
716+
- $ref: '#/components/schemas/FieldFilter'
717+
- $ref: '#/components/schemas/NestedFilter'
705718
LogicalFilter:
706719
type: object
707720
required:

workspaces/orchestrator/plugins/orchestrator-common/src/generated/docs/html/index.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,8 @@
948948
"$ref" : "#/components/schemas/LogicalFilter"
949949
}, {
950950
"$ref" : "#/components/schemas/FieldFilter"
951+
}, {
952+
"$ref" : "#/components/schemas/NestedFilter"
951953
} ]
952954
};
953955
defs["GetOverviewsRequestParams"] = {
@@ -984,6 +986,24 @@
984986
}
985987
}
986988
}
989+
};
990+
defs["NestedFilter"] = {
991+
"required" : [ "field", "nested" ],
992+
"properties" : {
993+
"field" : {
994+
"type" : "string"
995+
},
996+
"nested" : {
997+
"$ref" : "#/components/schemas/NestedFilter_nested"
998+
}
999+
}
1000+
};
1001+
defs["NestedFilter_nested"] = {
1002+
"oneOf" : [ {
1003+
"$ref" : "#/components/schemas/FieldFilter"
1004+
}, {
1005+
"$ref" : "#/components/schemas/NestedFilter"
1006+
} ]
9871007
};
9881008
defs["NodeInstanceDTO"] = {
9891009
"required" : [ "id" ],

workspaces/orchestrator/plugins/orchestrator-common/src/generated/docs/markdown/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Models/Filter.md
1111
Models/GetOverviewsRequestParams.md
1212
Models/InputSchemaResponseDTO.md
1313
Models/LogicalFilter.md
14+
Models/NestedFilter.md
15+
Models/NestedFilter_nested.md
1416
Models/NodeInstanceDTO.md
1517
Models/PaginationInfoDTO.md
1618
Models/ProcessInstanceDTO.md

workspaces/orchestrator/plugins/orchestrator-common/src/generated/docs/markdown/Models/Filter.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
| **filters** | [**oas_any_type_not_mapped**](.md) | | [default to null] |
88
| **field** | [**oas_any_type_not_mapped**](.md) | | [default to null] |
99
| **value** | [**FieldFilter_value**](FieldFilter_value.md) | | [default to null] |
10+
| **nested** | [**NestedFilter_nested**](NestedFilter_nested.md) | | [default to null] |
1011

1112
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
1213

0 commit comments

Comments
 (0)