add process_instance_report api method

This commit is contained in:
burnettk 2022-06-19 19:19:42 -04:00
parent 83586eae91
commit a38ef72c26
5 changed files with 120 additions and 5 deletions

View File

@ -15,7 +15,7 @@ from flask import current_app
from spiffworkflow_backend.services.process_model_service import ProcessModelService
def print_process_instance_count(process_model_identifier_ticket: str):
def print_process_instance_count(process_model_identifier_ticket: str) -> None:
"""Print process instance count."""
process_instances = ProcessInstanceModel.query.filter_by(process_model_identifier=process_model_identifier_ticket).all()
process_instance_count = len(process_instances)
@ -27,9 +27,9 @@ def main():
os.environ["FLASK_ENV"] = "development"
os.environ["FLASK_SESSION_SECRET_KEY"] = "whatevs"
app = create_app()
process_model_identifier_ticket = "ticket"
with app.app_context():
process_model_identifier_ticket = "ticket"
bpmn_spec_dir = current_app.config["BPMN_SPEC_ABSOLUTE_DIR"]
print(f"bpmn_spec_dir: {bpmn_spec_dir}")
db.session.query(ProcessInstanceModel).filter(ProcessInstanceModel.process_model_identifier == process_model_identifier_ticket).delete()
@ -41,7 +41,7 @@ def main():
user = UserModel.query.filter_by(username='test_user1').first()
with open("tickets.csv") as infile:
with open("tests/files/tickets.csv") as infile:
reader = csv.reader(infile, delimiter=",")
header = next(reader)
for column_name in columns_to_data_key_mappings:

View File

@ -298,7 +298,6 @@ paths:
description: The page number to return. Defaults to page 1.
schema:
type: integer
# process_instance_list
get:
operationId: spiffworkflow_backend.routes.process_api_blueprint.process_instance_list
summary: Returns a list of process instances for a given process model
@ -314,6 +313,41 @@ paths:
items:
$ref: "#/components/schemas/Workflow"
/process-models/{process_model_id}/process-instances/report:
parameters:
- name: process_model_id
in: path
required: true
description: The unique id of an existing workflow specification.
schema:
type: string
- name: page
in: query
required: false
description: The page number to return. Defaults to page 1.
schema:
type: integer
- name: per_page
in: query
required: false
description: The page number to return. Defaults to page 1.
schema:
type: integer
get:
operationId: spiffworkflow_backend.routes.process_api_blueprint.process_instance_report
summary: Returns a report of process instances for a given process model
tags:
- Process Instances
responses:
"200":
description: Workflow.
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Workflow"
/process-models/{process_model_id}/file/{file_name}:
parameters:
- name: process_model_id

View File

@ -274,6 +274,46 @@ def process_instance_list(process_model_id, page=1, per_page=100):
return Response(json.dumps(response_json), status=200, mimetype="application/json")
def process_instance_report(process_model_id, page=1, per_page=100):
"""Process_instance_list."""
process_model = ProcessModelService().get_process_model(process_model_id)
if process_model is None:
raise (
ApiError(
code="process_mode_cannot_be_found",
message=f"Process model cannot be found: {process_model_id}",
status_code=400,
)
)
process_instances = (
ProcessInstanceModel.query.filter_by(process_model_identifier=process_model.id)
.order_by(
ProcessInstanceModel.start_in_seconds.desc(), ProcessInstanceModel.id.desc()
)
.paginate(page, per_page, False)
)
serialized_results = []
for process_instance in process_instances.items:
process_instance_serialized = process_instance.serialized
process_instance_serialized["process_group_id"] = process_model.process_group_id
processor = ProcessInstanceProcessor(process_instance)
process_instance_data = processor.get_data()
process_instance_serialized["data"] = process_instance_data
serialized_results.append(process_instance_serialized)
response_json = {
"results": serialized_results,
"pagination": {
"count": len(process_instances.items),
"total": process_instances.total,
"pages": process_instances.pages,
},
}
return Response(json.dumps(response_json), status=200, mimetype="application/json")
def get_file_from_request():
"""Get_file_from_request."""
request_file = connexion.request.files.get("file")

View File

@ -564,7 +564,7 @@ class ProcessInstanceProcessor:
"""Complete_task."""
self.bpmn_process_instance.complete_task_from_id(task.id)
def get_data(self):
def get_data(self) -> dict[str, str]:
"""Get_data."""
return self.bpmn_process_instance.data

View File

@ -651,6 +651,47 @@ def test_process_instance_list_with_paginated_items(
assert response.json["pagination"]["total"] == 5
def test_process_instance_report_with_default_list(
app: Flask, client: FlaskClient, with_bpmn_file_cleanup: None
) -> None:
"""Test_process_instance_report_with_default_list."""
db.session.query(ProcessInstanceModel).delete()
db.session.commit()
test_process_group_id = "runs_without_input"
process_model_dir_name = "sample"
user = find_or_create_user()
headers = logged_in_headers(user)
create_process_instance(
app, client, test_process_group_id, process_model_dir_name, headers
)
response = client.get(
f"/v1.0/process-models/{process_model_dir_name}/process-instances/report",
headers=logged_in_headers(user),
)
assert response.status_code == 200
assert len(response.json["results"]) == 1
assert response.json["pagination"]["count"] == 1
assert response.json["pagination"]["pages"] == 1
assert response.json["pagination"]["total"] == 1
process_instance_dict = response.json["results"][0]
assert type(process_instance_dict["id"]) is int
assert process_instance_dict["process_model_identifier"] == process_model_dir_name
assert process_instance_dict["process_group_id"] == test_process_group_id
assert type(process_instance_dict["start_in_seconds"]) is int
assert process_instance_dict["start_in_seconds"] > 0
assert type(process_instance_dict["end_in_seconds"]) is int
assert process_instance_dict["end_in_seconds"] > 0
assert (
process_instance_dict["start_in_seconds"]
<= process_instance_dict["end_in_seconds"]
)
assert process_instance_dict["status"] == "complete"
assert process_instance_dict["data"]['Mike'] == "Awesome"
def create_process_instance(
app: Flask,
client: FlaskClient,