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

Expose count_workflow_executions on the temporal client #272

Merged
merged 8 commits into from
Nov 9, 2023
36 changes: 36 additions & 0 deletions examples/spec/integration/count_workflows_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

describe 'Temporal.count_workflow_executions', :integration do
it 'returns the number of workflows matching the provided query' do
workflow_id = SecureRandom.uuid
run_id = Temporal.start_workflow(
HelloWorldWorkflow,
'Test',
options: { workflow_id: workflow_id }
)

Temporal.await_workflow_result(
HelloWorldWorkflow,
workflow_id: workflow_id,
run_id: run_id
)

query = "WorkflowType = \"HelloWorldWorkflow\" AND WorkflowId = \"#{workflow_id}\""

# This is a workaround for the fact that this API hits the visibility store and there's a lag
# before the workflow gets indexed
result = nil

5.times do
result = Temporal.count_workflow_executions(
'ruby-samples', query: query
)

break if result.count.positive?

sleep 1
end

expect(result.count).to eq(1)
end
end
1 change: 1 addition & 0 deletions lib/temporal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module Temporal
:list_open_workflow_executions,
:list_closed_workflow_executions,
:query_workflow_executions,
:count_workflow_executions,
:add_custom_search_attributes,
:list_custom_search_attributes,
:remove_custom_search_attributes,
Expand Down
12 changes: 12 additions & 0 deletions lib/temporal/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'temporal/activity/async_token'
require 'temporal/workflow'
require 'temporal/workflow/context_helpers'
require 'temporal/workflow/count_workflows_result'
require 'temporal/workflow/history'
require 'temporal/workflow/execution_info'
require 'temporal/workflow/executions'
Expand Down Expand Up @@ -425,6 +426,17 @@ def query_workflow_executions(namespace, query, filter: {}, next_page_token: nil
Temporal::Workflow::Executions.new(connection: connection, status: :all, request_options: { namespace: namespace, query: query, next_page_token: next_page_token, max_page_size: max_page_size }.merge(filter))
end

# Count the number of workflows matching the provided query
#
# @param namespace [String]
# @param query [String]
#
# @return [Temporal::Workflow::CountWorkflowsResult] an integer count of workflows matching the query
def count_workflow_executions(namespace, query: nil)
response = connection.count_workflow_executions(namespace: namespace, query: query)
Workflow::CountWorkflowsResult.new(count: response.count)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harsh-stripe Why create a new response object to return here? It'd be simpler to just return the count from this method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made a commit to return the count instead of this wrapped object. I had initially created it thinking it might be useful for a future contributor if they wanted to include aggregation groups based on https://github.com/temporalio/api/blob/master/temporal/api/workflowservice/v1/request_response.proto#L795-L798 in the future, but it isn't required now since it doesn't quite work yet in Temporal.

end

# @param attributes [Hash[String, Symbol]] name to symbol for type, see INDEXED_VALUE_TYPE above
# @param namespace String, required for SQL enhanced visibility, ignored for elastic search
def add_custom_search_attributes(attributes, namespace: nil)
Expand Down
11 changes: 11 additions & 0 deletions lib/temporal/workflow/count_workflows_result.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Temporal
class Workflow
class CountWorkflowsResult
def initialize(count:)
@count = count
end

attr_reader :count
end
end
end
24 changes: 24 additions & 0 deletions spec/unit/lib/temporal/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1102,4 +1102,28 @@ class NamespacedWorkflow < Temporal::Workflow
end
end
end

describe '#count_workflow_executions' do
let(:response) do
Temporalio::Api::WorkflowService::V1::CountWorkflowExecutionsResponse.new(
count: 5
)
end

before do
allow(connection)
.to receive(:count_workflow_executions)
.and_return(response)
end

it 'returns the count' do
resp = subject.count_workflow_executions(namespace, query: 'ExecutionStatus="Running"')

expect(connection)
.to have_received(:count_workflow_executions)
.with(namespace: namespace, query: 'ExecutionStatus="Running"')

expect(resp.count).to eq(5)
end
end
end
Loading