most of the process instance filters are working. still need to add back in support for reports w/ burnettk

This commit is contained in:
jasquat 2023-04-27 11:47:48 -04:00
parent d29a4ca0ae
commit e49734b9ef
2 changed files with 102 additions and 118 deletions

View File

@ -412,11 +412,16 @@ class ProcessInstanceReportService:
) -> list[dict]: ) -> list[dict]:
"""Add_metadata_columns_to_process_instance.""" """Add_metadata_columns_to_process_instance."""
results = [] results = []
non_metadata_columns = cls.non_metadata_columns()
for process_instance_row in process_instance_sqlalchemy_rows: for process_instance_row in process_instance_sqlalchemy_rows:
process_instance_mapping = process_instance_row._mapping process_instance_mapping = process_instance_row._mapping
process_instance_dict = process_instance_row[0].serialized process_instance_dict = process_instance_row[0].serialized
# import pdb; pdb.set_trace()
for metadata_column in metadata_columns: for metadata_column in metadata_columns:
# if metadata_column["accessor"] not in non_metadata_columns:
# if metadata_column["accessor"] not in cls.process_instance_stock_columns():
if metadata_column["accessor"] not in process_instance_dict: if metadata_column["accessor"] not in process_instance_dict:
# import pdb; pdb.set_trace()
process_instance_dict[metadata_column["accessor"]] = process_instance_mapping[ process_instance_dict[metadata_column["accessor"]] = process_instance_mapping[
metadata_column["accessor"] metadata_column["accessor"]
] ]
@ -477,6 +482,14 @@ class ProcessInstanceReportService:
"""Get_column_names_for_model.""" """Get_column_names_for_model."""
return [i.name for i in model.__table__.columns] return [i.name for i in model.__table__.columns]
@classmethod
def process_instance_stock_columns(cls) -> list[str]:
return cls.get_column_names_for_model(ProcessInstanceModel)
@classmethod
def non_metadata_columns(cls) -> list[str]:
return cls.process_instance_stock_columns() + ['process_initiator_username']
@classmethod @classmethod
def builtin_column_options(cls) -> list[dict]: def builtin_column_options(cls) -> list[dict]:
"""Builtin_column_options.""" """Builtin_column_options."""
@ -663,16 +676,15 @@ class ProcessInstanceReportService:
process_instance_query = process_instance_query.filter(UserGroupAssignmentModel.user_id == user.id) process_instance_query = process_instance_query.filter(UserGroupAssignmentModel.user_id == user.id)
instance_metadata_aliases = {} instance_metadata_aliases = {}
report_column_list = cls.get_filter_value(filters, 'report_column_list') # print(f"report_metadata['columns']: {report_metadata['columns']}")
report_filter_by_list = cls.get_filter_value(filters, 'report_filter_by_list') # import pdb; pdb.set_trace()
stock_columns = cls.get_column_names_for_model(ProcessInstanceModel) if report_metadata['columns'] and len(report_metadata['columns']) > 0:
if isinstance(report_column_list, list): process_instance_report.report_metadata["columns"] = report_metadata['columns']
process_instance_report.report_metadata["columns"] = report_column_list if report_metadata['filter_by'] and len(report_metadata['filter_by']) > 0:
if isinstance(report_filter_by_list, list): process_instance_report.report_metadata["filter_by"] = report_metadata['filter_by']
process_instance_report.report_metadata["filter_by"] = report_filter_by_list
for column in process_instance_report.report_metadata["columns"]: for column in process_instance_report.report_metadata["columns"]:
if column["accessor"] in stock_columns: if column["accessor"] in cls.non_metadata_columns():
continue continue
instance_metadata_alias = aliased(ProcessInstanceMetadataModel) instance_metadata_alias = aliased(ProcessInstanceMetadataModel)
instance_metadata_aliases[column["accessor"]] = instance_metadata_alias instance_metadata_aliases[column["accessor"]] = instance_metadata_alias
@ -699,32 +711,33 @@ class ProcessInstanceReportService:
instance_metadata_alias, and_(*conditions), isouter=isouter instance_metadata_alias, and_(*conditions), isouter=isouter
).add_columns(func.max(instance_metadata_alias.value).label(column["accessor"])) ).add_columns(func.max(instance_metadata_alias.value).label(column["accessor"]))
order_by_query_array = [] # order_by_query_array = []
order_by_array = process_instance_report.report_metadata["order_by"] # order_by_array = process_instance_report.report_metadata["order_by"]
if len(order_by_array) < 1: # if len(order_by_array) < 1:
order_by_array = ProcessInstanceReportModel.default_order_by() # order_by_array = ProcessInstanceReportModel.default_order_by()
for order_by_option in order_by_array: # for order_by_option in order_by_array:
attribute = re.sub("^-", "", order_by_option) # attribute = re.sub("^-", "", order_by_option)
if attribute in stock_columns: # if attribute in cls.process_instance_stock_columns():
if order_by_option.startswith("-"): # if order_by_option.startswith("-"):
order_by_query_array.append(getattr(ProcessInstanceModel, attribute).desc()) # order_by_query_array.append(getattr(ProcessInstanceModel, attribute).desc())
else: # else:
order_by_query_array.append(getattr(ProcessInstanceModel, attribute).asc()) # order_by_query_array.append(getattr(ProcessInstanceModel, attribute).asc())
elif attribute in instance_metadata_aliases: # elif attribute in instance_metadata_aliases:
if order_by_option.startswith("-"): # if order_by_option.startswith("-"):
order_by_query_array.append(func.max(instance_metadata_aliases[attribute].value).desc()) # order_by_query_array.append(func.max(instance_metadata_aliases[attribute].value).desc())
else: # else:
order_by_query_array.append(func.max(instance_metadata_aliases[attribute].value).asc()) # order_by_query_array.append(func.max(instance_metadata_aliases[attribute].value).asc())
process_instances = ( process_instances = (
process_instance_query.group_by(ProcessInstanceModel.id) process_instance_query.group_by(ProcessInstanceModel.id)
.add_columns(ProcessInstanceModel.id) .add_columns(ProcessInstanceModel.id)
.order_by(*order_by_query_array) # .order_by(*order_by_query_array)
.paginate(page=page, per_page=per_page, error_out=False) .paginate(page=page, per_page=per_page, error_out=False)
) )
results = cls.add_metadata_columns_to_process_instance( results = cls.add_metadata_columns_to_process_instance(
process_instances.items, process_instance_report.report_metadata["columns"] process_instances.items, process_instance_report.report_metadata["columns"]
) )
# import pdb; pdb.set_trace()
for value in cls.check_filter_value(filters, 'oldest_open_human_task_fields'): for value in cls.check_filter_value(filters, 'oldest_open_human_task_fields'):
results = cls.add_human_task_fields(results, value) results = cls.add_human_task_fields(results, value)

View File

@ -128,7 +128,9 @@ export default function ProcessInstanceListTable({
const permissionRequestData: PermissionsToCheck = { const permissionRequestData: PermissionsToCheck = {
[targetUris.userSearch]: ['GET'], [targetUris.userSearch]: ['GET'],
}; };
const { ability } = usePermissionFetcher(permissionRequestData); const { ability, permissionsLoaded } = usePermissionFetcher(
permissionRequestData
);
const canSearchUsers: boolean = ability.can('GET', targetUris.userSearch); const canSearchUsers: boolean = ability.can('GET', targetUris.userSearch);
const [processInstances, setProcessInstances] = useState([]); const [processInstances, setProcessInstances] = useState([]);
@ -301,6 +303,10 @@ export default function ProcessInstanceListTable({
// eslint-disable-next-line sonarjs/cognitive-complexity // eslint-disable-next-line sonarjs/cognitive-complexity
useEffect(() => { useEffect(() => {
if (!permissionsLoaded) {
return;
}
// we apparently cannot use a state set in a useEffect from within that same useEffect // we apparently cannot use a state set in a useEffect from within that same useEffect
// so use a variable instead // so use a variable instead
let processModelSelectionItemsForUseEffect: ProcessModel[] = []; let processModelSelectionItemsForUseEffect: ProcessModel[] = [];
@ -311,7 +317,6 @@ export default function ProcessInstanceListTable({
setProcessInstances(processInstancesFromApi); setProcessInstances(processInstancesFromApi);
setPagination(result.pagination); setPagination(result.pagination);
setProcessInstanceFilters(result.filters); setProcessInstanceFilters(result.filters);
setReportMetadata(result.report.report_metadata); setReportMetadata(result.report.report_metadata);
if (result.report.id) { if (result.report.id) {
setProcessInstanceReportSelection(result.report); setProcessInstanceReportSelection(result.report);
@ -350,6 +355,10 @@ export default function ProcessInstanceListTable({
setProcessStatusSelection( setProcessStatusSelection(
(reportFilter.field_value || '').split(',') (reportFilter.field_value || '').split(',')
); );
setShowFilterOptions(true);
} else if (reportFilter.field_name === 'process_initiator_username') {
searchForProcessInitiator(reportFilter.field_value || '');
setShowFilterOptions(true);
} else if (reportFilter.field_name === 'process_model_identifier') { } else if (reportFilter.field_name === 'process_model_identifier') {
selectedProcessModelIdentifier = selectedProcessModelIdentifier =
reportFilter.field_value || undefined; reportFilter.field_value || undefined;
@ -377,16 +386,11 @@ export default function ProcessInstanceListTable({
(processModel: ProcessModel) => { (processModel: ProcessModel) => {
if (processModel.id === selectedProcessModelIdentifier) { if (processModel.id === selectedProcessModelIdentifier) {
setProcessModelSelection(processModel); setProcessModelSelection(processModel);
setShowFilterOptions(true);
} }
} }
); );
// const postBody: ReportMetadata = {
// columns: [],
// filter_by: [],
// order_by: [],
// };
//
// if (searchParams.get('report_id')) { // if (searchParams.get('report_id')) {
// queryParamString += `&report_id=${searchParams.get('report_id')}`; // queryParamString += `&report_id=${searchParams.get('report_id')}`;
// } else if (reportIdentifier) { // } else if (reportIdentifier) {
@ -402,40 +406,6 @@ export default function ProcessInstanceListTable({
// postBody.columns = reportColumnsList; // postBody.columns = reportColumnsList;
// } // }
// } // }
// if (searchParams.get('report_filter_by')) {
// const reportFilterByBase64 = searchParams.get('report_filter_by');
// if (reportFilterByBase64) {
// const reportFilterByList = JSON.parse(
// decodeBase64(reportFilterByBase64)
// );
// postBody.filter_by = reportFilterByList;
// }
// }
//
// Object.keys(parametersToGetFromSearchParams).forEach(
// (paramName: string) => {
// if (
// paramName === 'process_model_identifier' &&
// processModelFullIdentifier
// ) {
// postBody.filter_by.push({
// field_name: 'process_model_identifier',
// field_value: processModelFullIdentifier,
// });
// } else if (searchParams.get(paramName)) {
// // @ts-expect-error TS(7053) FIXME:
// const functionToCall = parametersToGetFromSearchParams[paramName];
// postBody.filter_by.push({
// field_name: paramName,
// field_value: searchParams.get(paramName),
// });
// if (functionToCall !== null) {
// functionToCall(searchParams.get(paramName) || '');
// }
// setShowFilterOptions(true);
// }
// }
// );
// eslint-disable-next-line prefer-const // eslint-disable-next-line prefer-const
let { page, perPage } = getPageInfoFromSearchParams( let { page, perPage } = getPageInfoFromSearchParams(
@ -460,7 +430,7 @@ export default function ProcessInstanceListTable({
failureCallback: stopRefreshing, failureCallback: stopRefreshing,
onUnauthorized: stopRefreshing, onUnauthorized: stopRefreshing,
postBody: { postBody: {
report_metadata: reportMetadataBody, report_metadata: reportMetadataBodyToUse,
}, },
}); });
} }
@ -533,6 +503,7 @@ export default function ProcessInstanceListTable({
reportIdentifier, reportIdentifier,
additionalParams, additionalParams,
processInstanceApiSearchPath, processInstanceApiSearchPath,
permissionsLoaded,
]); ]);
// // This sets the filter data using the saved reports returned from the initial instance_list query. // // This sets the filter data using the saved reports returned from the initial instance_list query.
@ -682,14 +653,17 @@ export default function ProcessInstanceListTable({
const reportColumns = () => { const reportColumns = () => {
if (reportMetadata) { if (reportMetadata) {
return (reportMetadata as any).columns; return reportMetadata.columns;
} }
return null; return [];
}; };
const reportFilterBy = () => { // const reportFilterBy = () => {
return (reportMetadata as any).filter_by; // if (reportMetadata) {
}; // return reportMetadata.filter_by;
// }
// return null
// };
const navigateToNewReport = (queryParamString: string) => { const navigateToNewReport = (queryParamString: string) => {
removeError(); removeError();
@ -698,6 +672,16 @@ export default function ProcessInstanceListTable({
navigate(`${processInstanceListPathPrefix}?${queryParamString}`); navigate(`${processInstanceListPathPrefix}?${queryParamString}`);
}; };
const removeFieldFromReportMetadata = (
postBody: ReportMetadata,
fieldName: string
) => {
const filtersToKeep = postBody.filter_by.filter(
(rf: ReportFilter) => rf.field_name !== fieldName
);
postBody.filter_by = filtersToKeep;
};
const addFieldValueToReportMetadata = ( const addFieldValueToReportMetadata = (
postBody: ReportMetadata, postBody: ReportMetadata,
fieldName: string, fieldName: string,
@ -708,6 +692,8 @@ export default function ProcessInstanceListTable({
field_name: fieldName, field_name: fieldName,
field_value: fieldValue, field_value: fieldValue,
}); });
} else {
removeFieldFromReportMetadata(postBody, fieldName);
} }
}; };
@ -721,8 +707,6 @@ export default function ProcessInstanceListTable({
undefined, undefined,
paginationQueryParamPrefix paginationQueryParamPrefix
); );
// let queryParamString = `per_page=${perPage}&page=${page}&user_filter=true`;
const queryParamString = `per_page=${perPage}&page=${page}`;
const { const {
valid, valid,
startFromSeconds, startFromSeconds,
@ -735,11 +719,17 @@ export default function ProcessInstanceListTable({
return; return;
} }
const postBody: ReportMetadata = { let postBody = null;
columns: [], if (reportMetadata) {
filter_by: [], postBody = { ...reportMetadata };
order_by: [], }
}; if (!postBody) {
postBody = {
columns: [],
filter_by: [],
order_by: [],
};
}
addFieldValueToReportMetadata(postBody, 'start_from', startFromSeconds); addFieldValueToReportMetadata(postBody, 'start_from', startFromSeconds);
addFieldValueToReportMetadata(postBody, 'start_to', startToSeconds); addFieldValueToReportMetadata(postBody, 'start_to', startToSeconds);
@ -751,6 +741,8 @@ export default function ProcessInstanceListTable({
'process_status', 'process_status',
processStatusSelection.join(',') processStatusSelection.join(',')
); );
} else {
removeFieldFromReportMetadata(postBody, 'process_status');
} }
if (processModelSelection) { if (processModelSelection) {
@ -759,17 +751,17 @@ export default function ProcessInstanceListTable({
'process_model_identifier', 'process_model_identifier',
processModelSelection.id processModelSelection.id
); );
} else {
removeFieldFromReportMetadata(postBody, 'process_model_identifier');
} }
if (processInstanceReportSelection) { // if (processInstanceReportSelection) {
addFieldValueToReportMetadata( // addFieldValueToReportMetadata(
postBody, // postBody,
'report_id', // 'report_id',
processInstanceReportSelection.id.toString() // processInstanceReportSelection.id.toString()
); // );
} // }
postBody.columns = reportColumns();
if (processInitiatorSelection) { if (processInitiatorSelection) {
addFieldValueToReportMetadata( addFieldValueToReportMetadata(
@ -777,39 +769,17 @@ export default function ProcessInstanceListTable({
'process_initiator_username', 'process_initiator_username',
processInitiatorSelection.username processInitiatorSelection.username
); );
// navigateToNewReport(queryParamString); } else {
removeFieldFromReportMetadata(postBody, 'process_initiator_username');
} }
// } else if (processInitiatorText) {
// HttpService.makeCallToBackend({
// path: targetUris.userExists,
// httpMethod: 'POST',
// postBody: { username: processInitiatorText },
// successCallback: (result: any) => {
// if (result.user_found) {
// queryParamString += `&process_initiator_username=${processInitiatorText}`;
// // navigateToNewReport(queryParamString);
// } else {
// setProcessInitiatorNotFoundErrorText(
// `The provided username is invalid. Please type the exact username.`
// );
// }
// },
// });
// } else {
// navigateToNewReport(queryParamString);
// }
// http://localhost:7001/admin/process-instances/for-me?per_page=50&page=1&report_metadata_base64=eyJjb2x1bW5zIjpudWxsLCJmaWx0ZXJfYnkiOlt7ImZpZWxkX25hbWUiOiJwcm9jZXNzX3N0YXR1cyIsImZpZWxkX3ZhbHVlIjoiY29tcGxldGUifV0sIm9yZGVyX2J5IjpbXX0%3D
// const queryParamString = `per_page=${perPage}&page=${page}`;
setListHasBeenFiltered(true); setListHasBeenFiltered(true);
setReportMetadata(postBody);
searchParams.set('per_page', perPage.toString()); searchParams.set('per_page', perPage.toString());
searchParams.set('page', page.toString()); searchParams.set('page', page.toString());
// const reportMetadataBase64 = encodeBase64(JSON.stringify(postBody));
// searchParams.set('report_metadata_base64', reportMetadataBase64);
setSearchParams(searchParams); setSearchParams(searchParams);
const queryParamString = `per_page=${perPage}&page=${page}`;
HttpService.makeCallToBackend({ HttpService.makeCallToBackend({
path: `${processInstanceApiSearchPath}?${queryParamString}`, path: `${processInstanceApiSearchPath}?${queryParamString}`,
httpMethod: 'POST', httpMethod: 'POST',
@ -1081,6 +1051,7 @@ export default function ProcessInstanceListTable({
reportColumn, reportColumn,
{ filter_field_value: '', filter_operator: '' } { filter_field_value: '', filter_operator: '' }
); );
if (reportColumn.filterable) {
const reportFilter = getFilterByFromReportMetadata( const reportFilter = getFilterByFromReportMetadata(
reportColumnForEditing.accessor reportColumnForEditing.accessor
); );
@ -1090,6 +1061,7 @@ export default function ProcessInstanceListTable({
reportColumnForEditing.filter_operator = reportColumnForEditing.filter_operator =
reportFilter.operator || 'equals'; reportFilter.operator || 'equals';
} }
}
return reportColumnForEditing; return reportColumnForEditing;
}; };
@ -1212,10 +1184,9 @@ export default function ProcessInstanceListTable({
}; };
const columnSelections = () => { const columnSelections = () => {
if (reportColumns()) { if (reportColumns().length > 0) {
const tags: any = []; const tags: any = [];
reportColumns().forEach((reportColumn: ReportColumn) => {
(reportColumns() as any).forEach((reportColumn: ReportColumn) => {
const reportColumnForEditing = const reportColumnForEditing =
reportColumnToReportColumnForEditing(reportColumn); reportColumnToReportColumnForEditing(reportColumn);