diff --git a/mailpoet/lib/Automation/Engine/Storage/AutomationRunStorage.php b/mailpoet/lib/Automation/Engine/Storage/AutomationRunStorage.php index 1f8be07845..4bded8ca09 100644 --- a/mailpoet/lib/Automation/Engine/Storage/AutomationRunStorage.php +++ b/mailpoet/lib/Automation/Engine/Storage/AutomationRunStorage.php @@ -176,6 +176,32 @@ class AutomationRunStorage { } } + public function getAutomationStepStatisticForTimeFrame(int $automationId, string $status, \DateTimeImmutable $after, \DateTimeImmutable $before, int $versionId = null): array { + $table = esc_sql($this->table); + + $where = "automation_id = %d + AND `status` = %s + AND created_at BETWEEN %s AND %s"; + if ($versionId) { + $where .= " AND version_id = %d"; + } + $sql = " + SELECT + COUNT(id) AS `count`, + next_step_id + FROM $table as log + WHERE $where + GROUP BY next_step_id + "; + + $sql = $versionId ? + $this->wpdb->prepare($sql, $automationId, $status, $after->format('Y-m-d H:i:s'), $before->format('Y-m-d H:i:s'), $versionId) : + $this->wpdb->prepare($sql, $automationId, $status, $after->format('Y-m-d H:i:s'), $before->format('Y-m-d H:i:s')); + $sql = is_string($sql) ? $sql : ''; + $result = $this->wpdb->get_results($sql, ARRAY_A); + return is_array($result) ? $result : []; + } + public function truncate(): void { $table = esc_sql($this->table); $this->wpdb->query("TRUNCATE $table"); diff --git a/mailpoet/tests/integration/Automation/Engine/Storage/AutomationRunStorageTest.php b/mailpoet/tests/integration/Automation/Engine/Storage/AutomationRunStorageTest.php new file mode 100644 index 0000000000..1141094591 --- /dev/null +++ b/mailpoet/tests/integration/Automation/Engine/Storage/AutomationRunStorageTest.php @@ -0,0 +1,67 @@ +testee = $this->diContainer->get(AutomationRunStorage::class); + } + + public function testAutomationStepStatisticForTimeFrame() { + global $wpdb; + $expected = [ + [ + 'count' => '2', + 'next_step_id' => 'step-1', + ], + [ + 'count' => '1', + 'next_step_id' => 'step-2', + ], + ]; + $timeFrame = [ + 'after' => new \DateTimeImmutable('2020-01-01 00:00:00'), + 'before' => new \DateTimeImmutable('2020-01-02 00:00:00'), + ]; + $status = AutomationRun::STATUS_RUNNING; + $automationId = 1; + + $this->testee->truncate(); + $sql = "insert into " . $wpdb->prefix . + "mailpoet_automation_runs" . + "(automation_id, version_id, created_at, `status`, trigger_key, next_step_id) values" . + "($automationId, 1, '2019-12-31 23:59:59', '$status', 'trigger_key', 'step-1')," . // Outside of timeframe + "($automationId, 1, '2020-01-01 00:00:00', '$status', 'trigger_key', 'step-1')," . // Should match + "($automationId, 1, '2020-01-01 00:00:00', '$status', 'trigger_key', 'step-2')," . // Should match + "($automationId, 2, '2020-01-02 00:00:00', '$status', 'trigger_key', 'step-1')," . // Should match when version not 1 + "($automationId, 2, '2020-01-02 00:00:01', '$status', 'trigger_key', 'step-1')," . // Outside of timeframe + "($automationId, 1, '2020-01-01 00:00:00', 'complete', 'trigger_key', 'step-1')," . // Wrong status + "(2, 1, '2020-01-01 00:00:00', '$status', 'trigger_key', 'step-1')"; // Wrong automation id + $this->assertNotFalse($wpdb->query($sql)); + + $result = $this->testee->getAutomationStepStatisticForTimeFrame($automationId, $status, $timeFrame['after'], $timeFrame['before']); + $this->assertEquals($expected, $result); + + $versionId = 1; + $result = $this->testee->getAutomationStepStatisticForTimeFrame($automationId, $status, $timeFrame['after'], $timeFrame['before'], $versionId); + + $expected = [ + [ + 'count' => '1', + 'next_step_id' => 'step-1', + ], + [ + 'count' => '1', + 'next_step_id' => 'step-2', + ], + ]; + $this->assertEquals($expected, $result); + } +}