| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Modules\Reporting; |
| 4 |
|
| 5 |
/** |
| 6 |
* Date range, bucketing and formatting helpers for reports. |
| 7 |
*/ |
| 8 |
|
| 9 |
trait ReportingHelperTrait |
| 10 |
{ |
| 11 |
protected static $hourly = 'PT1H'; |
| 12 |
protected static $daily = 'P1D'; |
| 13 |
protected static $weekly = 'P1W'; |
| 14 |
protected static $monthly = 'P1M'; |
| 15 |
|
| 16 |
protected function db() { |
| 17 |
return \FluentSupport\App\App::getInstance('db'); |
| 18 |
} |
| 19 |
|
| 20 |
protected function makeFromDate($from) |
| 21 |
{ |
| 22 |
$from = $from ?: '-30 days'; |
| 23 |
|
| 24 |
return new \DateTime($from); |
| 25 |
} |
| 26 |
|
| 27 |
protected function makeToDate($to = false) |
| 28 |
{ |
| 29 |
$to = $to ?: '+1 days'; |
| 30 |
|
| 31 |
return new \DateTime($to); |
| 32 |
} |
| 33 |
|
| 34 |
protected function makeDatePeriod($from, $to, $interval = null) |
| 35 |
{ |
| 36 |
$interval = $interval ?: static::$daily; |
| 37 |
|
| 38 |
return new \DatePeriod($from, new \DateInterval($interval), $to); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* Bucket size for the range: hourly for one day, daily up to 62 days, |
| 44 |
* weekly up to 181, monthly beyond. |
| 45 |
* |
| 46 |
* @param $from |
| 47 |
* @param $to |
| 48 |
* @return string |
| 49 |
*/ |
| 50 |
protected function getFrequency($from, $to) |
| 51 |
{ |
| 52 |
$numDays = $to->diff($from)->format("%a"); |
| 53 |
if($numDays <= 0) { |
| 54 |
return static::$hourly; |
| 55 |
} |
| 56 |
else if ($numDays > 62 && $numDays <= 181) { |
| 57 |
return static::$weekly; |
| 58 |
} else if ($numDays > 181) { |
| 59 |
return static::$monthly; |
| 60 |
} |
| 61 |
|
| 62 |
return static::$daily; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @param $frequency |
| 67 |
* @param string $dateField |
| 68 |
* @return array |
| 69 |
*/ |
| 70 |
protected function prepareSelect($frequency, $dateField = 'created_at', $type = 'count_id') |
| 71 |
{ |
| 72 |
switch ($type) { |
| 73 |
case 'count_id': |
| 74 |
$countField = 'COUNT(id) AS count'; |
| 75 |
break; |
| 76 |
case 'response_count': |
| 77 |
$countField = 'SUM(response_count) AS count'; |
| 78 |
break; |
| 79 |
default: |
| 80 |
$countField = 'COUNT(id) AS count'; |
| 81 |
} |
| 82 |
|
| 83 |
$select = [ |
| 84 |
$this->db()->raw($countField), |
| 85 |
$this->db()->raw('DATE('.$dateField.') AS date'), |
| 86 |
$this->db()->raw('TIME('.$dateField.') AS time') |
| 87 |
]; |
| 88 |
|
| 89 |
if ($frequency == static::$hourly) { |
| 90 |
$select[] = $this->db()->raw('HOUR(created_at) hourly'); |
| 91 |
} else if ($frequency == static::$weekly) { |
| 92 |
$select[] = $this->db()->raw('WEEK(created_at) week'); |
| 93 |
} else if ($frequency == static::$monthly) { |
| 94 |
$select[] = $this->db()->raw('MONTH(created_at) month'); |
| 95 |
} |
| 96 |
|
| 97 |
return $select; |
| 98 |
} |
| 99 |
|
| 100 |
protected function prepareBetween($frequency, $from, $to) |
| 101 |
{ |
| 102 |
if($frequency == static::$hourly){ |
| 103 |
return [ |
| 104 |
$from->format('Y-m-d')." 00:00:00", |
| 105 |
$to->format('Y-m-d')." 23:59:59" |
| 106 |
]; |
| 107 |
} |
| 108 |
return [ |
| 109 |
$from->format('Y-m-d')." 00:00:00", |
| 110 |
$to->format('Y-m-d')." 23:59:59" |
| 111 |
]; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @param $frequency |
| 116 |
* @return string[] |
| 117 |
*/ |
| 118 |
protected function getGroupAndOrder($frequency) |
| 119 |
{ |
| 120 |
$orderBy = $groupBy = 'date'; |
| 121 |
|
| 122 |
if ($frequency == static::$hourly) { |
| 123 |
$orderBy = $groupBy = 'hourly'; |
| 124 |
} elseif ($frequency == static::$weekly) { |
| 125 |
$orderBy = $groupBy = 'week'; |
| 126 |
} else if ($frequency == static::$monthly) { |
| 127 |
$orderBy = $groupBy = 'month'; |
| 128 |
} |
| 129 |
|
| 130 |
return [$groupBy, $orderBy]; |
| 131 |
} |
| 132 |
|
| 133 |
protected function getDateRangeArray($period) |
| 134 |
{ |
| 135 |
$range = []; |
| 136 |
|
| 137 |
$formatter = 'basicFormatter'; |
| 138 |
|
| 139 |
if ($this->isMonthly($period)) { |
| 140 |
$formatter = 'monYearFormatter'; |
| 141 |
} |
| 142 |
|
| 143 |
foreach ($period as $date) { |
| 144 |
$date = $this->{$formatter}($date); |
| 145 |
$range[$date] = 0; |
| 146 |
} |
| 147 |
|
| 148 |
return $range; |
| 149 |
} |
| 150 |
|
| 151 |
protected function getResult($period, $items) |
| 152 |
{ |
| 153 |
$range = $this->getDateRangeArray($period); |
| 154 |
|
| 155 |
$formatter = 'basicFormatter'; |
| 156 |
|
| 157 |
if ($this->isHourly($period)) { |
| 158 |
$formatter = 'hourlyFormatter'; |
| 159 |
} |
| 160 |
else if ($this->isMonthly($period)) { |
| 161 |
$formatter = 'monYearFormatter'; |
| 162 |
} |
| 163 |
|
| 164 |
foreach ($items as $item) { |
| 165 |
if($this->isHourly($period)){ |
| 166 |
$time = $this->{$formatter}($item->time); |
| 167 |
$range[$time] = (int) $item->count; |
| 168 |
}else{ |
| 169 |
$date = $this->{$formatter}($item->date); |
| 170 |
$range[$date] = (int) $item->count; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return $range; |
| 175 |
} |
| 176 |
|
| 177 |
protected function isMonthly($period) |
| 178 |
{ |
| 179 |
return !!$period->getDateInterval()->m; |
| 180 |
} |
| 181 |
|
| 182 |
protected function isHourly($period) |
| 183 |
{ |
| 184 |
return !!$period->getDateInterval()->h; |
| 185 |
} |
| 186 |
|
| 187 |
protected function basicFormatter($date) |
| 188 |
{ |
| 189 |
if (is_string($date)) { |
| 190 |
$date = new \DateTime($date); |
| 191 |
} |
| 192 |
|
| 193 |
return $date->format('Y-m-d'); |
| 194 |
} |
| 195 |
|
| 196 |
protected function hourlyFormatter($date) |
| 197 |
{ |
| 198 |
if (is_string($date)) { |
| 199 |
$date = new \DateTime($date); |
| 200 |
} |
| 201 |
|
| 202 |
return $date->format('H').':00' .' - '. $date->format('H').':59'; |
| 203 |
} |
| 204 |
|
| 205 |
protected function monYearFormatter($date) |
| 206 |
{ |
| 207 |
if (is_string($date)) { |
| 208 |
$date = new \DateTime($date); |
| 209 |
} |
| 210 |
|
| 211 |
return $date->format('M Y'); |
| 212 |
} |
| 213 |
} |
| 214 |
|