| @@ -11,8 +11,9 @@ | ||
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | 13 | trait ReportingHelperTrait |
| 14 | 14 | { |
| 15 | + protected static $hourly = 'PT1H'; | |
| 15 | 16 | protected static $daily = 'P1D'; |
| 16 | 17 | protected static $weekly = 'P1W'; |
| 17 | 18 | protected static $monthly = 'P1M'; |
| 18 | 19 | |
| @@ -52,9 +53,12 @@ | ||
| 52 | 53 | protected function getFrequency($from, $to) |
| 53 | 54 | { |
| 54 | 55 | $numDays = $to->diff($from)->format("%a"); |
| 55 | 56 | //If number of days in the range is greater than 2 month and less than or equal to 3 months |
| 56 | - if ($numDays > 62 && $numDays <= 181) { | |
| 57 | + if($numDays <= 0) { | |
| 58 | + return static::$hourly; | |
| 59 | + } | |
| 60 | + else if ($numDays > 62 && $numDays <= 181) { | |
| 57 | 61 | return static::$weekly; |
| 58 | 62 | } else if ($numDays > 181) { |
| 59 | 63 | //Greater than 3 months |
| 60 | 64 | return static::$monthly; |
| @@ -68,16 +72,30 @@ | ||
| 68 | 72 | * @param $frequency |
| 69 | 73 | * @param string $dateField |
| 70 | 74 | * @return array |
| 71 | 75 | */ |
| 72 | - protected function prepareSelect($frequency, $dateField = 'created_at') | |
| 76 | + protected function prepareSelect($frequency, $dateField = 'created_at', $type = 'count_id') | |
| 73 | 77 | { |
| 78 | + switch ($type) { | |
| 79 | + case 'count_id': | |
| 80 | + $countField = 'COUNT(id) AS count'; | |
| 81 | + break; | |
| 82 | + case 'response_count': | |
| 83 | + $countField = 'SUM(response_count) AS count'; | |
| 84 | + break; | |
| 85 | + default: | |
| 86 | + $countField = 'COUNT(id) AS count'; | |
| 87 | + } | |
| 88 | + | |
| 74 | 89 | $select = [ |
| 75 | - $this->db()->raw('COUNT(id) AS count'), | |
| 76 | - $this->db()->raw('DATE('.$dateField.') AS date') | |
| 90 | + $this->db()->raw($countField), | |
| 91 | + $this->db()->raw('DATE('.$dateField.') AS date'), | |
| 92 | + $this->db()->raw('TIME('.$dateField.') AS time') | |
| 77 | 93 | ]; |
| 78 | 94 | |
| 79 | - if ($frequency == static::$weekly) { | |
| 95 | + if ($frequency == static::$hourly) { | |
| 96 | + $select[] = $this->db()->raw('HOUR(created_at) hourly'); | |
| 97 | + } else if ($frequency == static::$weekly) { | |
| 80 | 98 | $select[] = $this->db()->raw('WEEK(created_at) week'); |
| 81 | 99 | } else if ($frequency == static::$monthly) { |
| 82 | 100 | $select[] = $this->db()->raw('MONTH(created_at) month'); |
| 83 | 101 | } |
| @@ -84,8 +102,22 @@ | ||
| 84 | 102 | |
| 85 | 103 | return $select; |
| 86 | 104 | } |
| 87 | 105 | |
| 106 | + protected function prepareBetween($frequency, $from, $to) | |
| 107 | + { | |
| 108 | + if($frequency == static::$hourly){ | |
| 109 | + return [ | |
| 110 | + $from->format('Y-m-d')." 00:00:00", | |
| 111 | + $to->format('Y-m-d')." 23:59:59" | |
| 112 | + ]; | |
| 113 | + } | |
| 114 | + return [ | |
| 115 | + $from->format('Y-m-d')." 00:00:00", | |
| 116 | + $to->format('Y-m-d')." 23:59:59" | |
| 117 | + ]; | |
| 118 | + } | |
| 119 | + | |
| 88 | 120 | /** |
| 89 | 121 | * getGroupAndOrder method will return order by and group by value based on frequency |
| 90 | 122 | * @param $frequency |
| 91 | 123 | * @return string[] |
| @@ -93,9 +125,11 @@ | ||
| 93 | 125 | protected function getGroupAndOrder($frequency) |
| 94 | 126 | { |
| 95 | 127 | $orderBy = $groupBy = 'date'; |
| 96 | 128 | |
| 97 | - if ($frequency == static::$weekly) { | |
| 129 | + if ($frequency == static::$hourly) { | |
| 130 | + $orderBy = $groupBy = 'hourly'; | |
| 131 | + } elseif ($frequency == static::$weekly) { | |
| 98 | 132 | $orderBy = $groupBy = 'week'; |
| 99 | 133 | } else if ($frequency == static::$monthly) { |
| 100 | 134 | $orderBy = $groupBy = 'month'; |
| 101 | 135 | } |
| @@ -126,15 +160,23 @@ | ||
| 126 | 160 | $range = $this->getDateRangeArray($period); |
| 127 | 161 | |
| 128 | 162 | $formatter = 'basicFormatter'; |
| 129 | 163 | |
| 130 | - if ($this->isMonthly($period)) { | |
| 164 | + if ($this->isHourly($period)) { | |
| 165 | + $formatter = 'hourlyFormatter'; | |
| 166 | + } | |
| 167 | + else if ($this->isMonthly($period)) { | |
| 131 | 168 | $formatter = 'monYearFormatter'; |
| 132 | 169 | } |
| 133 | 170 | |
| 134 | 171 | foreach ($items as $item) { |
| 135 | - $date = $this->{$formatter}($item->date); | |
| 136 | - $range[$date] = (int) $item->count; | |
| 172 | + if($this->isHourly($period)){ | |
| 173 | + $time = $this->{$formatter}($item->time); | |
| 174 | + $range[$time] = (int) $item->count; | |
| 175 | + }else{ | |
| 176 | + $date = $this->{$formatter}($item->date); | |
| 177 | + $range[$date] = (int) $item->count; | |
| 178 | + } | |
| 137 | 179 | } |
| 138 | 180 | |
| 139 | 181 | return $range; |
| 140 | 182 | } |
| @@ -143,8 +185,13 @@ | ||
| 143 | 185 | { |
| 144 | 186 | return !!$period->getDateInterval()->m; |
| 145 | 187 | } |
| 146 | 188 | |
| 189 | + protected function isHourly($period) | |
| 190 | + { | |
| 191 | + return !!$period->getDateInterval()->h; | |
| 192 | + } | |
| 193 | + | |
| 147 | 194 | protected function basicFormatter($date) |
| 148 | 195 | { |
| 149 | 196 | if (is_string($date)) { |
| 150 | 197 | $date = new \DateTime($date); |
| @@ -150,8 +197,17 @@ | ||
| 150 | 197 | $date = new \DateTime($date); |
| 151 | 198 | } |
| 152 | 199 | |
| 153 | 200 | return $date->format('Y-m-d'); |
| 201 | + } | |
| 202 | + | |
| 203 | + protected function hourlyFormatter($date) | |
| 204 | + { | |
| 205 | + if (is_string($date)) { | |
| 206 | + $date = new \DateTime($date); | |
| 207 | + } | |
| 208 | + | |
| 209 | + return $date->format('H').':00' .' - '. $date->format('H').':59'; | |
| 154 | 210 | } |
| 155 | 211 | |
| 156 | 212 | protected function monYearFormatter($date) |
| 157 | 213 | { |