Skip to content

Commit

Permalink
Merge pull request #1 from HMubaireek/feat/filter-instances-by-variables
Browse files Browse the repository at this point in the history
feat: get process instances by variable name and value
  • Loading branch information
HMubaireek authored May 6, 2024
2 parents bf47b32 + 38d3ac6 commit 530ecb4
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@ import org.springframework.data.domain.Pageable
import org.springframework.data.repository.PagingAndSortingRepository
import org.springframework.stereotype.Repository

interface ProcessInstanceKeyOnly {
fun getKey(): Long
}

@Repository
interface ProcessInstanceRepository : PagingAndSortingRepository<ProcessInstance, Long> {

fun findByParentProcessInstanceKey(parentProcessInstanceKey: Long): List<ProcessInstance>

fun findByProcessDefinitionKeyAndStateIn(processDefinitionKey: Long, stateIn: List<ProcessInstanceState>): List<ProcessInstanceKeyOnly>

fun findByProcessDefinitionKeyAndStateIn(processDefinitionKey: Long, stateIn: List<ProcessInstanceState>, pageable: Pageable): List<ProcessInstance>

fun countByProcessDefinitionKeyAndStateIn(processDefinitionKey: Long, stateIn: List<ProcessInstanceState>): Long

fun findByStateIn(stateIn: List<ProcessInstanceState>, pageable: Pageable): List<ProcessInstance>

fun findByStateIn(stateIn: List<ProcessInstanceState>): List<ProcessInstanceKeyOnly>

fun countByStateIn(stateIn: List<ProcessInstanceState>): Long

fun findByStateInAndKeyIn(stateIn: List<ProcessInstanceState>, key: List<Long>, pageable: Pageable): List<ProcessInstance>
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ interface VariableRepository : PagingAndSortingRepository<Variable, Long> {

@Transactional(readOnly = true)
fun findByScopeKey(scopeKey: Long): List<Variable>

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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.repository.ProcessInstanceKeyOnly
import io.zeebe.zeeqs.data.repository.ProcessInstanceRepository
import io.zeebe.zeeqs.data.repository.VariableRepository
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Component

@Component
class ProcessInstanceService(
private val processInstancesRepository: ProcessInstanceRepository,
private val variableRepository: VariableRepository) {

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

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

}

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 };
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);
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();
}
}

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

else {
return processInstancesRepository.countByStateIn(stateIn);
}
}


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);
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();
}
}

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

else {
return processInstancesRepository.countByStateIn(stateIn);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,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.repository.ProcessInstanceRepository
import io.zeebe.zeeqs.data.service.ProcessInstanceService
import io.zeebe.zeeqs.graphql.resolvers.connection.ProcessInstanceConnection
import org.springframework.data.domain.PageRequest
import org.springframework.data.repository.findByIdOrNull
Expand All @@ -12,18 +13,21 @@ import org.springframework.stereotype.Controller

@Controller
class ProcessInstanceQueryResolver(
val processInstanceRepository: ProcessInstanceRepository
val processInstanceRepository: ProcessInstanceRepository,
val processInstanceService: ProcessInstanceService
) {

@QueryMapping
fun processInstances(
@Argument perPage: Int,
@Argument page: Int,
@Argument stateIn: List<ProcessInstanceState>
@Argument stateIn: List<ProcessInstanceState>,
@Argument variableName: String?,
@Argument variableValue: String?
): ProcessInstanceConnection {
return ProcessInstanceConnection(
getItems = { processInstanceRepository.findByStateIn(stateIn, PageRequest.of(page, perPage)).toList() },
getCount = { processInstanceRepository.countByStateIn(stateIn) }
getItems = { processInstanceService.getProcessInstances(perPage, page, stateIn, variableName, variableValue) },
getCount = { processInstanceService.countProcessInstances(stateIn, variableName, variableValue) }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.zeebe.zeeqs.data.repository.ProcessInstanceRepository
import io.zeebe.zeeqs.data.repository.SignalSubscriptionRepository
import io.zeebe.zeeqs.data.repository.TimerRepository
import io.zeebe.zeeqs.data.service.BpmnElementInfo
import io.zeebe.zeeqs.data.service.ProcessInstanceService
import io.zeebe.zeeqs.data.service.ProcessService
import io.zeebe.zeeqs.graphql.resolvers.connection.ProcessInstanceConnection
import org.springframework.data.domain.PageRequest
Expand All @@ -19,6 +20,7 @@ class ProcessResolver(
val timerRepository: TimerRepository,
val messageSubscriptionRepository: MessageSubscriptionRepository,
val processService: ProcessService,
val processInstanceService: ProcessInstanceService,
private val signalSubscriptionRepository: SignalSubscriptionRepository
) {

Expand All @@ -27,20 +29,27 @@ class ProcessResolver(
process: Process,
@Argument perPage: Int,
@Argument page: Int,
@Argument stateIn: List<ProcessInstanceState>
@Argument stateIn: List<ProcessInstanceState>,
@Argument variableName: String?,
@Argument variableValue: String?
): ProcessInstanceConnection {
return ProcessInstanceConnection(
getItems = {
processInstanceRepository.findByProcessDefinitionKeyAndStateIn(
process.key,
stateIn,
PageRequest.of(page, perPage)
processInstanceService.getProcessInstances(
perPage,
page,
stateIn,
process.key,
variableName,
variableValue
).toList()
},
getCount = {
processInstanceRepository.countByProcessDefinitionKeyAndStateIn(
process.key,
stateIn
processInstanceService.countProcessInstances(
stateIn,
process.key,
variableName,
variableValue
)
}
)
Expand Down
4 changes: 3 additions & 1 deletion graphql-api/src/main/resources/graphql/Process.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ type Process {
processInstances(
perPage: Int = 10,
page: Int = 0,
stateIn: [ProcessInstanceState!] = [ACTIVATED, COMPLETED, TERMINATED]): ProcessInstanceConnection!
stateIn: [ProcessInstanceState!] = [ACTIVATED, COMPLETED, TERMINATED]
variableName: String = null,
variableValue: String = 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 @@ -73,7 +73,9 @@ type Query {
processInstances(
perPage: Int = 10,
page: Int = 0,
stateIn: [ProcessInstanceState!] = [ACTIVATED, COMPLETED, TERMINATED]
stateIn: [ProcessInstanceState!] = [ACTIVATED, COMPLETED, TERMINATED],
variableName: String = null,
variableValue: String = null
): ProcessInstanceConnection!

}
Expand Down

0 comments on commit 530ecb4

Please sign in to comment.