From cb6451bfcfab228012fb6fc15d7c6ccc0669cd4c Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Tue, 4 Dec 2018 16:51:13 +0100 Subject: [PATCH] Make reporting work on PostgreSQL too --- views/default/report.php | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/views/default/report.php b/views/default/report.php index 74f7c07..8c5c9fe 100644 --- a/views/default/report.php +++ b/views/default/report.php @@ -23,28 +23,45 @@ // status +$db = Yii::$app->db; +switch ($db->driverName) { + case 'pgsql': + $selectExpression = "to_char(to_timestamp(created_at), 'YYYY-MM-DD')"; + break; + default: + case 'mysql': + $selectExpression = "DATE_FORMAT(FROM_UNIXTIME(created_at), '%Y-%m-%d')"; + break; +} +$subQuery = (new \yii\db\Query()) + ->select([ + 'dd' => new \yii\db\Expression($selectExpression), + 'status', + ])->from('queue_manager'); $data = (new \yii\db\Query()) - ->select(new \yii\db\Expression('DATE_FORMAT(FROM_UNIXTIME(created_at), \'%d-%m-%Y\') AS dd, - status, - count(*)as cnt')) - ->from('queue_manager') - ->groupBy('status,dd') - ->orderBy('created_at') + ->select([ + 'dd', + 'status', + 'cnt' => 'COUNT(*)', + ]) + ->from(['q' => $subQuery]) + ->groupBy(['status', 'dd']) + ->orderBy('dd') ->all(); $labels = []; foreach ($data as $item) { - $dd = $item['dd']; + $dd = Yii::$app->formatter->asDate($item['dd']); if (!in_array($dd, $labels)) { $labels[] = $dd; } } $status1 = array_fill_keys($labels, '0'); -$status1 = array_fill_keys($labels, '0'); $status2 = array_fill_keys($labels, '0'); $status3 = array_fill_keys($labels, '0'); +$status4 = array_fill_keys($labels, '0'); foreach ($data as $item) { - $dd = $item['dd']; + $dd = Yii::$app->formatter->asDate($item['dd']); if ($item['status'] == QueueManager::STATUS_WAITING) { $status1[$dd] = $item['cnt']; }