Laravel-ised queries

This commit is contained in:
Josef Citrine 2016-05-04 00:50:59 +01:00
parent c90a8d744f
commit 88bee8c7ab
2 changed files with 21 additions and 31 deletions

View file

@ -30,23 +30,15 @@ use Carbon\Carbon;
class StatsController extends ApiControllerBase class StatsController extends ApiControllerBase
{ {
private function pluralise($count, $singular, $plural = false)
{
if (!$plural) $plural = $singular . 's';
return ($count == 1 ? $singular : $plural) ;
}
public function getTrackStatsHourly($id) public function getTrackStatsHourly($id)
{ {
// I know, raw SQL ugh. I'm not used to laravel's query system $query = DB::table('resource_log_items')
$query = DB::select(DB::raw(' ->selectRaw('created_at AS time, COUNT(1) AS `plays`')
SELECT TIMESTAMP(created_at) AS `time`, COUNT(1) AS `plays` ->where('track_id', '=', $id)
FROM `resource_log_items` ->where('log_type', '=', 3)
WHERE `track_id` = :id AND `log_type` = 3 AND `created_at` > now() - INTERVAL 1 DAY ->whereRaw('`created_at` > now() - INTERVAL 1 DAY')
GROUP BY TIMESTAMP(created_at);'), array( ->groupBy('created_at')
'id' => $id ->get();
));
$now = Carbon::now(); $now = Carbon::now();
$calcArray = array(); $calcArray = array();
@ -76,7 +68,7 @@ class StatsController extends ApiControllerBase
// Covert calcArray into output we can understand // Covert calcArray into output we can understand
foreach($calcArray as $hour => $plays) { foreach($calcArray as $hour => $plays) {
$set = array('hour' => $hour . ' ' . $this->pluralise($hour, 'hour', 'hours'), 'plays' => $plays); $set = array('hour' => $hour . ' ' . str_plural('hour', $hour), 'plays' => $plays);
array_push($output, $set); array_push($output, $set);
} }
@ -85,15 +77,13 @@ class StatsController extends ApiControllerBase
public function getTrackStatsDaily($id) public function getTrackStatsDaily($id)
{ {
// I know, raw SQL ugh. I'm not used to laravel's query system $query = DB::table('resource_log_items')
// Only go back 1 month for daily stuff, may change in the future ->selectRaw('created_at AS time, COUNT(1) AS `plays`')
$query = DB::select(DB::raw(' ->where('track_id', '=', $id)
SELECT TIMESTAMP(created_at) AS `time`, COUNT(1) AS `plays` ->where('log_type', '=', 3)
FROM `resource_log_items` ->whereRaw('`created_at` > now() - INTERVAL 1 MONTH')
WHERE `track_id` = :id AND `log_type` = 3 AND `created_at` > now() - INTERVAL 1 MONTH ->groupBy('created_at')
GROUP BY TIMESTAMP(created_at);'), array( ->get();
'id' => $id
));
$now = Carbon::now(); $now = Carbon::now();
$calcArray = array(); $calcArray = array();
@ -123,7 +113,7 @@ class StatsController extends ApiControllerBase
// Covert calcArray into output we can understand // Covert calcArray into output we can understand
foreach($calcArray as $days => $plays) { foreach($calcArray as $days => $plays) {
$set = array('days' => $days . ' ' . $this->pluralise($days, 'day', 'days'), 'plays' => $plays); $set = array('days' => $days . ' ' . str_plural('day', $days), 'plays' => $plays);
array_push($output, $set); array_push($output, $set);
} }

View file

@ -22,7 +22,7 @@ module.exports = angular.module('ponyfm').controller 'track-stats', [
labelArray = [] labelArray = []
dailyArray = [] dailyArray = []
cumArray = [] cumulativeArray = []
statsLoaded = (stats) -> statsLoaded = (stats) ->
console.log(stats) console.log(stats)
@ -34,13 +34,13 @@ module.exports = angular.module('ponyfm').controller 'track-stats', [
i = 0 i = 0
while i < dailyArray.length while i < dailyArray.length
if i == 0 if i == 0
cumArray[i] = dailyArray[0] cumulativeArray[i] = dailyArray[0]
else else
cumArray[i] = cumArray[i - 1] + dailyArray[i] cumulativeArray[i] = cumulativeArray[i - 1] + dailyArray[i]
i++ i++
$scope.playsLabels = labelArray $scope.playsLabels = labelArray
$scope.playsData = cumArray $scope.playsData = cumulativeArray
$scope.colours = ['#B885BD'] $scope.colours = ['#B885BD']
$scope.series = ['Plays'] $scope.series = ['Plays']
$scope.totalSelected = true $scope.totalSelected = true
@ -48,7 +48,7 @@ module.exports = angular.module('ponyfm').controller 'track-stats', [
$scope.dailyText = stats.type $scope.dailyText = stats.type
$scope.totalClick = () -> $scope.totalClick = () ->
$scope.playsData = cumArray $scope.playsData = cumulativeArray
$scope.totalSelected = true $scope.totalSelected = true
$scope.dailyClick = () -> $scope.dailyClick = () ->