Query statistic how many runs in a timeframe are at which step

[MAILPOET-5091]
This commit is contained in:
David Remer
2023-06-28 12:51:40 +03:00
committed by Aschepikov
parent ae57f81c14
commit d2fce2014a
2 changed files with 93 additions and 0 deletions

View File

@@ -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");