From 3393e36eaf3ed4d8ec719ee8de86722064b43450 Mon Sep 17 00:00:00 2001 From: harsh-stripe <109252877+harsh-stripe@users.noreply.github.com> Date: Thu, 9 Nov 2023 11:03:03 -0800 Subject: [PATCH] Expose count_workflow_executions on the temporal client (#272) * Expose count_workflow_executions on the temporal client * Return a wrapped type for count_workflows response * Add integration tests * Remove debug log lines * Update lib/temporal/client.rb Fix a typo with the new return type. Co-authored-by: jazev-stripe <128553781+jazev-stripe@users.noreply.github.com> * Return the count value of count_workflow_executions instead of wrapping it * Remove the example integration spec for count_workflows. ES isn't setup in github actions and while this test is nice to have, it isn't critical to have an integration spec for it because it relies on timing due to the async visibility store --------- Co-authored-by: jazev-stripe <128553781+jazev-stripe@users.noreply.github.com> --- lib/temporal.rb | 1 + lib/temporal/client.rb | 11 +++++++++++ spec/unit/lib/temporal/client_spec.rb | 24 ++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/lib/temporal.rb b/lib/temporal.rb index ec1ca412..617c63b3 100644 --- a/lib/temporal.rb +++ b/lib/temporal.rb @@ -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, diff --git a/lib/temporal/client.rb b/lib/temporal/client.rb index af6ae2bf..02473432 100644 --- a/lib/temporal/client.rb +++ b/lib/temporal/client.rb @@ -425,6 +425,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 [Integer] an integer count of workflows matching the query + def count_workflow_executions(namespace, query: nil) + response = connection.count_workflow_executions(namespace: namespace, query: query) + response.count + 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) diff --git a/spec/unit/lib/temporal/client_spec.rb b/spec/unit/lib/temporal/client_spec.rb index 315f6c8d..1dd4995d 100644 --- a/spec/unit/lib/temporal/client_spec.rb +++ b/spec/unit/lib/temporal/client_spec.rb @@ -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).to eq(5) + end + end end