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: add filter operation to variables filter #4

Merged
merged 1 commit 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
14 changes: 12 additions & 2 deletions data/src/main/kotlin/io/zeebe/zeeqs/data/entity/VariableFilter.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package io.zeebe.zeeqs.data.entity

enum class EqualityOperation {
enum class ComparisonOperation {
EQUALS,
CONTAINS
}

enum class FilterOperation {
AND,
OR
}

class VariableFilter (
val name: String,
val value: String,
val equalityOperation: EqualityOperation = EqualityOperation.EQUALS
val comparisonOperation: ComparisonOperation = ComparisonOperation.EQUALS
)

class VariableFilterGroup (
val variables: List<VariableFilter>,
val filterOperation: FilterOperation = FilterOperation.OR
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,42 @@ class ProcessInstanceService(
private val processInstancesRepository: ProcessInstanceRepository,
private val variableRepository: VariableRepository) {

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

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

}

private fun getVariablesByProcessInstanceKeys(processInstances: List<ProcessInstanceKeyOnly>, variables: List<VariableFilter>): List<Variable> {
val variableNames = variables.map { it.name }
private fun matchesFilter(variable: Variable, filter: VariableFilter): Boolean {
return when (filter.comparisonOperation) {
ComparisonOperation.EQUALS -> variable.name == filter.name && variable.value == filter.value
ComparisonOperation.CONTAINS -> variable.name == filter.name && variable.value.contains(filter.value)
}
}

private fun getVariablesByProcessInstanceKeys(processInstances: List<ProcessInstanceKeyOnly>, variableFilterGroup: VariableFilterGroup): List<Variable> {
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)
}
val variableNames = variableFilterGroup.variables.map { it.name }
val variablesList = variableRepository.findByProcessInstanceKeyInAndNameIn(processInstancesKeys, variableNames)

return variablesList.filter { variable ->
if (variableFilterGroup.filterOperation == FilterOperation.AND) {
variableFilterGroup.variables.all { matchesFilter(variable, it) }
} else {
variableFilterGroup.variables.any { matchesFilter(variable, it) }
}
}
return filteredVariables;
}

fun getProcessInstances(perPage: Int, page: Int, stateIn: List<ProcessInstanceState>, variables: List<VariableFilter>?): List<ProcessInstance> {
if(!variables.isNullOrEmpty()) {
val filteredVariables = getVariables(stateIn, variables);

fun getProcessInstances(perPage: Int, page: Int, stateIn: List<ProcessInstanceState>, variableFilterGroup: VariableFilterGroup?): List<ProcessInstance> {
if (variableFilterGroup?.variables?.isNotEmpty() == true) {
val filteredVariables = getVariables(stateIn, variableFilterGroup);
val filteredProcessInstances = processInstancesRepository.findByStateInAndKeyIn(stateIn, filteredVariables.map { it.processInstanceKey }, PageRequest.of(page, perPage)).toList();
return filteredProcessInstances;
}
Expand All @@ -49,9 +56,9 @@ class ProcessInstanceService(
}
}

fun countProcessInstances(stateIn: List<ProcessInstanceState>, variables: List<VariableFilter>?): Long {
if(!variables.isNullOrEmpty()) {
val filteredVariables = getVariables(stateIn, variables);
fun countProcessInstances(stateIn: List<ProcessInstanceState>, variableFilterGroup: VariableFilterGroup?): Long {
if (variableFilterGroup?.variables?.isNotEmpty() == true) {
val filteredVariables = getVariables(stateIn, variableFilterGroup);
return filteredVariables.count().toLong();
}

Expand All @@ -61,9 +68,9 @@ class ProcessInstanceService(
}


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);
fun getProcessInstances(perPage: Int, page: Int, stateIn: List<ProcessInstanceState>, processDefinitionKey: Long, variableFilterGroup: VariableFilterGroup?): List<ProcessInstance> {
if (variableFilterGroup?.variables?.isNotEmpty() == true) {
val filteredVariables = getVariables(stateIn, processDefinitionKey, variableFilterGroup);
val filteredProcessInstances = processInstancesRepository.findByStateInAndKeyIn(stateIn, filteredVariables.map { it.processInstanceKey }, PageRequest.of(page, perPage)).toList();
return filteredProcessInstances;
}
Expand All @@ -72,9 +79,9 @@ class ProcessInstanceService(
}
}

fun countProcessInstances(stateIn: List<ProcessInstanceState>, processDefinitionKey: Long, variables: List<VariableFilter>?): Long {
if(!variables.isNullOrEmpty()) {
val filteredVariables = getVariables(stateIn, processDefinitionKey, variables);
fun countProcessInstances(stateIn: List<ProcessInstanceState>, processDefinitionKey: Long, variableFilterGroup: VariableFilterGroup?): Long {
if (variableFilterGroup?.variables?.isNotEmpty() == true) {
val filteredVariables = getVariables(stateIn, processDefinitionKey, variableFilterGroup);
return filteredVariables.count().toLong();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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.entity.VariableFilterGroup
import io.zeebe.zeeqs.data.repository.ProcessInstanceRepository
import io.zeebe.zeeqs.data.service.ProcessInstanceService
import io.zeebe.zeeqs.graphql.resolvers.connection.ProcessInstanceConnection
Expand All @@ -23,11 +23,11 @@ class ProcessInstanceQueryResolver(
@Argument perPage: Int,
@Argument page: Int,
@Argument stateIn: List<ProcessInstanceState>,
@Argument variables: List<VariableFilter>?
@Argument variablesFilter: VariableFilterGroup?
): ProcessInstanceConnection {
return ProcessInstanceConnection(
getItems = { processInstanceService.getProcessInstances(perPage, page, stateIn, variables) },
getCount = { processInstanceService.countProcessInstances(stateIn, variables) }
getItems = { processInstanceService.getProcessInstances(perPage, page, stateIn, variablesFilter) },
getCount = { processInstanceService.countProcessInstances(stateIn, variablesFilter) }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ProcessResolver(
@Argument perPage: Int,
@Argument page: Int,
@Argument stateIn: List<ProcessInstanceState>,
@Argument variables: List<VariableFilter>?
@Argument variablesFilter: VariableFilterGroup?
): ProcessInstanceConnection {
return ProcessInstanceConnection(
getItems = {
Expand All @@ -39,14 +39,14 @@ class ProcessResolver(
page,
stateIn,
process.key,
variables
variablesFilter
).toList()
},
getCount = {
processInstanceService.countProcessInstances(
stateIn,
process.key,
variables
variablesFilter
)
}
)
Expand Down
2 changes: 1 addition & 1 deletion graphql-api/src/main/resources/graphql/Process.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Process {
perPage: Int = 10,
page: Int = 0,
stateIn: [ProcessInstanceState!] = [ACTIVATED, COMPLETED, TERMINATED]
variables: [VariableFilter] = null): ProcessInstanceConnection!
variablesFilter: VariableFilterGroup = null): ProcessInstanceConnection!
# the scheduled timers of the timer start events of the process
timers: [Timer!]
# the opened message subscriptions of the message start events of the process
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type Query {
perPage: Int = 10,
page: Int = 0,
stateIn: [ProcessInstanceState!] = [ACTIVATED, COMPLETED, TERMINATED],
variables: [VariableFilter] = null
variablesFilter: VariableFilterGroup = null
): ProcessInstanceConnection!

}
Expand Down
15 changes: 12 additions & 3 deletions graphql-api/src/main/resources/graphql/Variable.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@ type VariableUpdate {
input VariableFilter {
name: String!,
value: String!,
equalityOperation: EqualityOperation = EQUALS
comparisonOperation: ComparisonOperation!
}

# The type of a variable value filter
enum EqualityOperation {
input VariableFilterGroup {
variables: [VariableFilter!]
filterOperation: FilterOperation = OR
}

enum ComparisonOperation {
EQUALS,
CONTAINS
}

enum FilterOperation {
AND,
OR
}