Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: filter process instances by multiple variables #3

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ enum class BpmnElementType {
BUSINESS_RULE_TASK,
SCRIPT_TASK,
SEND_TASK,
INCLUSIVE_GATEWAY
INCLUSIVE_GATEWAY,
GROUP
}
12 changes: 12 additions & 0 deletions data/src/main/kotlin/io/zeebe/zeeqs/data/entity/VariableFilter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.zeebe.zeeqs.data.entity

enum class EqualityOperation {
EQUALS,
CONTAINS
}

class VariableFilter (
val name: String,
val value: String,
val equalityOperation: EqualityOperation = EqualityOperation.EQUALS
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ interface VariableRepository : PagingAndSortingRepository<Variable, Long> {
@Transactional(readOnly = true)
fun findByProcessInstanceKeyInAndName(processInstanceKey: List<Long>, name: String): List<Variable>


@Transactional(readOnly = true)
fun findByProcessInstanceKeyInAndNameIn(processInstanceKey: List<Long>, name: List<String>): List<Variable>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.zeebe.zeeqs.data.service

data class BpmnElementExtensionProperties (
val name: String? = null,
val value: String? = null,
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package io.zeebe.zeeqs.data.service

import io.camunda.zeebe.model.bpmn.instance.Documentation
import io.camunda.zeebe.model.bpmn.instance.zeebe.ZeebeProperty
import io.zeebe.zeeqs.data.entity.BpmnElementType

data class BpmnElementInfo(
val elementId: String,
val elementName: String?,
val elementType: BpmnElementType,
val metadata: BpmnElementMetadata
val metadata: BpmnElementMetadata,
val extensionProperties: Collection<BpmnElementExtensionProperties>?,
val documentation: String?
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.zeebe.zeeqs.data.service

import io.zeebe.zeeqs.data.entity.ProcessInstance
import io.zeebe.zeeqs.data.entity.ProcessInstanceState
import io.zeebe.zeeqs.data.entity.Variable
import io.zeebe.zeeqs.data.entity.*
import io.zeebe.zeeqs.data.repository.ProcessInstanceKeyOnly
import io.zeebe.zeeqs.data.repository.ProcessInstanceRepository
import io.zeebe.zeeqs.data.repository.VariableRepository
Expand All @@ -14,26 +12,35 @@ class ProcessInstanceService(
private val processInstancesRepository: ProcessInstanceRepository,
private val variableRepository: VariableRepository) {

private fun getVariables(stateIn: List<ProcessInstanceState>, variableName: String, variableValue: String): List<Variable> {
private fun getVariables(stateIn: List<ProcessInstanceState>, variables: List<VariableFilter>): List<Variable> {
val processInstances = processInstancesRepository.findByStateIn(stateIn).toList();
return getVariablesByProcessInstanceKeys(processInstances, variableName, variableValue);
return getVariablesByProcessInstanceKeys(processInstances, variables);
}

private fun getVariables(stateIn: List<ProcessInstanceState>, processDefinitionKey: Long, variableName: String, variableValue: String): List<Variable> {
private fun getVariables(stateIn: List<ProcessInstanceState>, processDefinitionKey: Long, variables: List<VariableFilter>): List<Variable> {
val processInstances = processInstancesRepository.findByProcessDefinitionKeyAndStateIn(processDefinitionKey, stateIn).toList();
return getVariablesByProcessInstanceKeys(processInstances, variableName, variableValue);
return getVariablesByProcessInstanceKeys(processInstances, variables);

}

private fun getVariablesByProcessInstanceKeys(processInstances: List<ProcessInstanceKeyOnly>, variableName: String, variableValue: String): List<Variable> {
val variables = variableRepository.findByProcessInstanceKeyInAndName(processInstances.map { it.getKey() }, variableName);
val filteredVariables = variables.filter { it.value == variableValue };
private fun getVariablesByProcessInstanceKeys(processInstances: List<ProcessInstanceKeyOnly>, variables: List<VariableFilter>): List<Variable> {
val variableNames = variables.map { it.name }
val processInstancesKeys = processInstances.map { it.getKey() }
val variablesList = variableRepository.findByProcessInstanceKeyInAndNameIn(processInstancesKeys, variableNames);
val filteredVariables = variablesList.filter { variable ->
variables.any { filter ->
when (filter.equalityOperation) {
EqualityOperation.EQUALS -> variable.name == filter.name && variable.value == filter.value
EqualityOperation.CONTAINS -> variable.name == filter.name && variable.value.contains(filter.value)
}
}
}
return filteredVariables;
}

fun getProcessInstances(perPage: Int, page: Int, stateIn: List<ProcessInstanceState>, variableName: String?, variableValue: String?): List<ProcessInstance> {
if(variableName != null && variableValue != null) {
val filteredVariables = getVariables(stateIn, variableName, variableValue);
fun getProcessInstances(perPage: Int, page: Int, stateIn: List<ProcessInstanceState>, variables: List<VariableFilter>?): List<ProcessInstance> {
if(!variables.isNullOrEmpty()) {
val filteredVariables = getVariables(stateIn, variables);
val filteredProcessInstances = processInstancesRepository.findByStateInAndKeyIn(stateIn, filteredVariables.map { it.processInstanceKey }, PageRequest.of(page, perPage)).toList();
return filteredProcessInstances;
}
Expand All @@ -42,9 +49,9 @@ class ProcessInstanceService(
}
}

fun countProcessInstances(stateIn: List<ProcessInstanceState>, variableName: String?, variableValue: String?): Long {
if(variableName != null && variableValue != null) {
val filteredVariables = getVariables(stateIn, variableName, variableValue);
fun countProcessInstances(stateIn: List<ProcessInstanceState>, variables: List<VariableFilter>?): Long {
if(!variables.isNullOrEmpty()) {
val filteredVariables = getVariables(stateIn, variables);
return filteredVariables.count().toLong();
}

Expand All @@ -54,25 +61,25 @@ class ProcessInstanceService(
}


fun getProcessInstances(perPage: Int, page: Int, stateIn: List<ProcessInstanceState>, processDefinitionKey: Long, variableName: String?, variableValue: String?): List<ProcessInstance> {
if(variableName != null && variableValue != null) {
val filteredVariables = getVariables(stateIn, processDefinitionKey, variableName, variableValue);
fun getProcessInstances(perPage: Int, page: Int, stateIn: List<ProcessInstanceState>, processDefinitionKey: Long, variables: List<VariableFilter>?): List<ProcessInstance> {
if(!variables.isNullOrEmpty()) {
val filteredVariables = getVariables(stateIn, processDefinitionKey, variables);
val filteredProcessInstances = processInstancesRepository.findByStateInAndKeyIn(stateIn, filteredVariables.map { it.processInstanceKey }, PageRequest.of(page, perPage)).toList();
return filteredProcessInstances;
}
else {
return processInstancesRepository.findByStateIn(stateIn, PageRequest.of(page, perPage)).toList();
return processInstancesRepository.findByProcessDefinitionKeyAndStateIn(processDefinitionKey, stateIn, PageRequest.of(page, perPage)).toList();
}
}

fun countProcessInstances(stateIn: List<ProcessInstanceState>, processDefinitionKey: Long, variableName: String?, variableValue: String?): Long {
if(variableName != null && variableValue != null) {
val filteredVariables = getVariables(stateIn, processDefinitionKey, variableName, variableValue);
fun countProcessInstances(stateIn: List<ProcessInstanceState>, processDefinitionKey: Long, variables: List<VariableFilter>?): Long {
if(!variables.isNullOrEmpty()) {
val filteredVariables = getVariables(stateIn, processDefinitionKey, variables);
return filteredVariables.count().toLong();
}

else {
return processInstancesRepository.countByStateIn(stateIn);
return processInstancesRepository.countByProcessDefinitionKeyAndStateIn(processDefinitionKey, stateIn);
}
}

Expand Down
58 changes: 43 additions & 15 deletions data/src/main/kotlin/io/zeebe/zeeqs/data/service/ProcessService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,28 @@ class ProcessService(val processRepository: ProcessRepository) {
@Cacheable(cacheNames = ["bpmnElementInfo"])
fun getBpmnElementInfo(processDefinitionKey: Long): Map<String, BpmnElementInfo>? {
return getBpmnModel(processDefinitionKey)
?.let { it.getModelElementsByType(FlowElement::class.java) }
?.map { flowElement ->
Pair(
flowElement.id, BpmnElementInfo(
elementId = flowElement.id,
elementName = flowElement.name,
elementType = getBpmnElementType(flowElement),
metadata = getMetadata(flowElement)
)
)
}
?.toMap()
?.let { model ->
(model.getModelElementsByType(FlowElement::class.java)?.associate { flowElement ->
flowElement.id to BpmnElementInfo(
elementId = flowElement.id,
elementName = flowElement.name,
elementType = getBpmnElementType(flowElement),
metadata = getMetadata(flowElement),
extensionProperties = getExtensionProperties(flowElement),
documentation = getDocumentation(flowElement)
)
}.orEmpty() +
model.getModelElementsByType(Group::class.java)?.associate { groupElement ->
groupElement.id to BpmnElementInfo(
elementId = groupElement.id,
elementName = groupElement.category?.value ?: "",
elementType = getBpmnElementType(groupElement),
metadata = getMetadata(groupElement),
extensionProperties = getExtensionProperties(groupElement),
documentation = getDocumentation(groupElement)
)
}.orEmpty())
}
}

private fun getBpmnModel(processDefinitionKey: Long): BpmnModelInstance? {
Expand All @@ -41,7 +51,7 @@ class ProcessService(val processRepository: ProcessRepository) {
?.let { Bpmn.readModelFromStream(it) }
}

private fun getBpmnElementType(element: FlowElement): BpmnElementType {
private fun getBpmnElementType(element: BaseElement): BpmnElementType {
return when (element.elementType.typeName) {
BpmnModelConstants.BPMN_ELEMENT_PROCESS -> BpmnElementType.PROCESS
BpmnModelConstants.BPMN_ELEMENT_SUB_PROCESS -> getBpmnSubprocessType(element)
Expand All @@ -63,11 +73,12 @@ class ProcessService(val processRepository: ProcessRepository) {
BpmnModelConstants.BPMN_ELEMENT_BUSINESS_RULE_TASK -> BpmnElementType.BUSINESS_RULE_TASK
BpmnModelConstants.BPMN_ELEMENT_SCRIPT_TASK -> BpmnElementType.SCRIPT_TASK
BpmnModelConstants.BPMN_ELEMENT_INCLUSIVE_GATEWAY -> BpmnElementType.INCLUSIVE_GATEWAY
BpmnModelConstants.BPMN_ELEMENT_GROUP -> BpmnElementType.GROUP
else -> BpmnElementType.UNKNOWN
}
}

private fun getBpmnSubprocessType(element: FlowElement) =
private fun getBpmnSubprocessType(element: BaseElement) =
if (element is SubProcess) {
if (element.triggeredByEvent()) {
BpmnElementType.EVENT_SUB_PROCESS
Expand All @@ -78,7 +89,7 @@ class ProcessService(val processRepository: ProcessRepository) {
BpmnElementType.UNKNOWN
}

private fun getMetadata(element: FlowElement): BpmnElementMetadata {
private fun getMetadata(element: BaseElement): BpmnElementMetadata {
return BpmnElementMetadata(
jobType = element
.getSingleExtensionElement(ZeebeTaskDefinition::class.java)
Expand Down Expand Up @@ -156,6 +167,23 @@ class ProcessService(val processRepository: ProcessRepository) {
)
}

private fun getExtensionProperties(element: BaseElement): Collection<BpmnElementExtensionProperties>? {
return element.extensionElements?.elementsQuery
?.filterByType(ZeebeProperties::class.java)
?.findSingleResult()
?.map { properties ->
properties.properties?.map { property ->
BpmnElementExtensionProperties(name = property.name, value = property.value)
}
}
?.orElse(null)
}


private fun getDocumentation(element: BaseElement): String {
return element.documentations.joinToString(separator = "") { it.textContent }
}

@Cacheable(cacheNames = ["userTaskForm"])
fun getForm(processDefinitionKey: Long, formKey: String): String? {
return getBpmnModel(processDefinitionKey)
Expand Down
11 changes: 6 additions & 5 deletions data/src/test/kotlin/io/zeebe/zeeqs/ProcessServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ class ProcessServiceTest(
// then
assertThat(info)
.isNotNull()
.contains(entry("s", BpmnElementInfo("s", "start", BpmnElementType.START_EVENT, BpmnElementMetadata())))
.contains(entry("t", BpmnElementInfo("t", "task", BpmnElementType.SERVICE_TASK, BpmnElementMetadata(jobType = "test"))))
.contains(entry("s", BpmnElementInfo("s", "start", BpmnElementType.START_EVENT, BpmnElementMetadata(), null, "")))
.contains(entry("t", BpmnElementInfo("t", "task", BpmnElementType.SERVICE_TASK, BpmnElementMetadata(jobType = "test"), null, "")))
.contains(entry("u", BpmnElementInfo("u", "userTask", BpmnElementType.USER_TASK, BpmnElementMetadata(
userTaskAssignmentDefinition = UserTaskAssignmentDefinition(assignee = "user1", candidateGroups = "group1"))))
userTaskAssignmentDefinition = UserTaskAssignmentDefinition(assignee = "user1", candidateGroups = "group1")), null, ""))
)
.contains(entry("e", BpmnElementInfo("e", null, BpmnElementType.END_EVENT, BpmnElementMetadata())))
.contains(entry("e", BpmnElementInfo("e", null, BpmnElementType.END_EVENT, BpmnElementMetadata(), null, "")))
}

@Test
Expand Down Expand Up @@ -90,7 +90,8 @@ class ProcessServiceTest(
key = "camunda-forms:bpmn:form_A",
resource = """{"x":1}"""
)
)
),
null, ""
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.zeebe.zeeqs.graphql.resolvers.query

import io.zeebe.zeeqs.data.entity.ProcessInstance
import io.zeebe.zeeqs.data.entity.ProcessInstanceState
import io.zeebe.zeeqs.data.entity.VariableFilter
import io.zeebe.zeeqs.data.repository.ProcessInstanceRepository
import io.zeebe.zeeqs.data.service.ProcessInstanceService
import io.zeebe.zeeqs.graphql.resolvers.connection.ProcessInstanceConnection
Expand All @@ -22,12 +23,11 @@ class ProcessInstanceQueryResolver(
@Argument perPage: Int,
@Argument page: Int,
@Argument stateIn: List<ProcessInstanceState>,
@Argument variableName: String?,
@Argument variableValue: String?
@Argument variables: List<VariableFilter>?
): ProcessInstanceConnection {
return ProcessInstanceConnection(
getItems = { processInstanceService.getProcessInstances(perPage, page, stateIn, variableName, variableValue) },
getCount = { processInstanceService.countProcessInstances(stateIn, variableName, variableValue) }
getItems = { processInstanceService.getProcessInstances(perPage, page, stateIn, variables) },
getCount = { processInstanceService.countProcessInstances(stateIn, variables) }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.zeebe.zeeqs.data.entity.ElementInstanceState
import io.zeebe.zeeqs.data.entity.Process
import io.zeebe.zeeqs.data.repository.ElementInstanceRepository
import io.zeebe.zeeqs.data.repository.ProcessRepository
import io.zeebe.zeeqs.data.service.BpmnElementExtensionProperties
import io.zeebe.zeeqs.data.service.BpmnElementInfo
import io.zeebe.zeeqs.data.service.BpmnElementMetadata
import io.zeebe.zeeqs.data.service.ProcessService
Expand Down Expand Up @@ -41,6 +42,21 @@ class BpmnElementResolver(
?: BpmnElementMetadata()
}


@SchemaMapping(typeName = "BpmnElement", field = "extensionProperties")
fun extensionProperties(element: BpmnElement): Collection<BpmnElementExtensionProperties>? {
return findElementInfo(element)
?.extensionProperties
}


@SchemaMapping(typeName = "BpmnElement", field = "documentation")
fun documentation(element: BpmnElement): String? {
return findElementInfo(element)
?.documentation

}

@SchemaMapping(typeName = "BpmnElement", field = "process")
fun process(element: BpmnElement): Process? {
return processRepository.findByIdOrNull(element.processDefinitionKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class ProcessResolver(
@Argument perPage: Int,
@Argument page: Int,
@Argument stateIn: List<ProcessInstanceState>,
@Argument variableName: String?,
@Argument variableValue: String?
@Argument variables: List<VariableFilter>?
): ProcessInstanceConnection {
return ProcessInstanceConnection(
getItems = {
Expand All @@ -40,16 +39,14 @@ class ProcessResolver(
page,
stateIn,
process.key,
variableName,
variableValue
variables
).toList()
},
getCount = {
processInstanceService.countProcessInstances(
stateIn,
process.key,
variableName,
variableValue
variables
)
}
)
Expand Down
Loading