Cleaned up code, added caching for stats

This commit is contained in:
Josef Citrine 2016-05-04 17:07:00 +01:00
parent 7b4cf4adff
commit a6c9393e3d
5 changed files with 137 additions and 101 deletions

View file

@ -2,7 +2,7 @@
/**
* Pony.fm - A community for pony fan music.
* Copyright (C) 2015 Peter Deltchev
* Copyright (C) 2016 Josef Citrine
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
@ -22,138 +22,118 @@ namespace Poniverse\Ponyfm\Http\Controllers\Api\Web;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Poniverse\Ponyfm\Http\Controllers\ApiControllerBase;
use Poniverse\Ponyfm\Models\ResourceLogItem;
use Poniverse\Ponyfm\Models\Track;
use Auth;
use Cache;
use DB;
use Response;
use Carbon\Carbon;
class StatsController extends ApiControllerBase
{
public function getTrackStatsHourly($id)
{
$query = DB::table('resource_log_items')
->selectRaw('created_at AS time, COUNT(1) AS `plays`')
private function getStatsData($id, $hourly = false) {
$playRange = "1 MONTH";
if ($hourly) {
$playRange = "2 DAY";
}
$statQuery = DB::table('resource_log_items')
->selectRaw('created_at, COUNT(1) AS `plays`')
->where('track_id', '=', $id)
->where('log_type', '=', 3)
->whereRaw('`created_at` > now() - INTERVAL 1 DAY')
->where('log_type', '=', ResourceLogItem::PLAY)
->whereRaw('`created_at` > now() - INTERVAL ' . $playRange)
->groupBy('created_at')
->orderBy('created_at')
->get();
return $statQuery;
}
private function sortTrackStatsArray($query, $hourly = false) {
$now = Carbon::now();
$calcArray = array();
$output = array();
$playsArray = [];
$output = [];
if ($hourly) {
$playsArray = array_fill(0, 24, 0);
} else {
$playsArray = array_fill(0, 30, 0);
}
foreach($query as $item) {
$playDate = new Carbon($item->time);
$playDate = new Carbon($item->created_at);
$key = 0;
if ($hourly) {
$key = $playDate->diffInHours($now);
if (array_key_exists($key, $calcArray)) {
$calcArray[$key] += $item->plays;
} else {
$calcArray[$key] = $item->plays;
}
}
// Get the first key in the array (oldest play)
reset($calcArray);
$lastKey = (int) key($calcArray);
for ($i = 0; $i < $lastKey; $i++) {
if (!isset($calcArray[$i])) {
$calcArray[$i] = 0;
}
}
krsort($calcArray);
// Covert calcArray into output we can understand
foreach($calcArray as $hour => $plays) {
$set = [
'hour' => $hour . ' ' . str_plural('hour', $hour),
'plays' => $plays
];
array_push($output, $set);
}
return Response::json(['playStats' => $output, 'type' => 'Hourly'], 200);
}
public function getTrackStatsDaily($id)
{
$query = DB::table('resource_log_items')
->selectRaw('created_at AS time, COUNT(1) AS `plays`')
->where('track_id', '=', $id)
->where('log_type', '=', 3)
->whereRaw('`created_at` > now() - INTERVAL 1 MONTH')
->groupBy('created_at')
->get();
$now = Carbon::now();
$calcArray = array();
$output = array();
foreach($query as $item) {
$playDate = new Carbon($item->time);
$key = $playDate->diffInDays($now);
if (array_key_exists($key, $calcArray)) {
$calcArray[$key] += $item->plays;
}
if (array_key_exists($key, $playsArray)) {
$playsArray[$key] += $item->plays;
} else {
$calcArray[$key] = $item->plays;
$playsArray[$key] = $item->plays;
}
}
// Get the first key in the array (oldest play)
reset($calcArray);
$lastKey = (int) key($calcArray);
krsort($playsArray);
for ($i = 0; $i < $lastKey; $i++) {
if (!isset($calcArray[$i])) {
$calcArray[$i] = 0;
}
}
krsort($calcArray);
// Covert calcArray into output we can understand
foreach($calcArray as $days => $plays) {
// Covert playsArray into output we can understand
foreach($playsArray as $timeOffet => $plays) {
if ($hourly) {
$set = [
'days' => $days . ' ' . str_plural('day', $days),
'hours' => $timeOffet . ' ' . str_plural('hour', $timeOffet),
'plays' => $plays
];
} else {
$set = [
'days' => $timeOffet . ' ' . str_plural('day', $timeOffet),
'plays' => $plays
];
}
array_push($output, $set);
}
if ($hourly) {
return Response::json(['playStats' => $output, 'type' => 'Hourly'], 200);
} else {
return Response::json(['playStats' => $output, 'type' => 'Daily'], 200);
}
}
public function getTrackStats($id) {
// Get track to check if it exists
// and if we are allowed to view it.
// In the future we could do something
// with this data, not sure.
$cachedOutput = Cache::remember('track_stats'.$id, 5, function() use ($id) {
try {
$track = Track::findOrFail($id);
$track = Track::published()->findOrFail($id);
} catch (ModelNotFoundException $e) {
return $this->notFound('Track not found!');
}
// Do we have permission to view this track?
if (!$track->canView(Auth::user()))
if (!$track->canView(Auth::user())) {
return $this->notFound('Track not found!');
}
// Run one of the functions depending on
// how old the track is
$now = Carbon::now();
$trackDate = $track->published_at;
// Error catching for tracks that don't exist anymore
// or are not published
if ($trackDate == null)
return $this->notFound('Track not found!');
$hourly = true;
if ($trackDate->diffInDays($now) >= 1)
return $this->getTrackStatsDaily($id);
if ($trackDate->diffInDays($now) >= 1) {
$hourly = false;
}
return $this->getTrackStatsHourly($id);
$statsData = $this->getStatsData($id, $hourly);
$output = $this->sortTrackStatsArray($statsData, $hourly);
return $output;
});
return $cachedOutput;
}
}

View file

@ -0,0 +1,31 @@
<?php
/**
* Pony.fm - A community for pony fan music.
* Copyright (C) 2016 Josef Citrine
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Poniverse\Ponyfm\Http\Controllers;
use View;
class StatsController extends Controller
{
public function getIndex()
{
return View::make('tracks.stats');
}
}

View file

@ -36,6 +36,7 @@ Route::get('/tracks/random', 'TracksController@getIndex');
Route::get('tracks/{id}-{slug}', 'TracksController@getTrack');
Route::get('tracks/{id}-{slug}/edit', 'TracksController@getEdit');
Route::get('tracks/{id}-{slug}/stats', 'StatsController@getIndex');
Route::get('t{id}', 'TracksController@getShortlink' )->where('id', '\d+');
Route::get('t{id}/embed', 'TracksController@getEmbed' );
Route::get('t{id}/stream.{extension}', 'TracksController@getStream' );

View file

@ -22,7 +22,7 @@
<li>Plays: <strong>{{::track.stats.plays}}</strong></li>
<li>Downloads: <strong>{{::track.stats.downloads}}</strong></li>
<li>Favourites: <strong>{{::track.stats.favourites}}</strong></li>
<li><strong><a href="{{::track.url}}/stats">View more stats</a></strong></li>
<li><strong><a ui-sref="content.track.stats">View more stats</a></strong></li>
</ul>
</div>
<div class="left">

View file

@ -0,0 +1,24 @@
{{--
Pony.fm - A community for pony fan music.
Copyright (C) 2016 Josef Citrine
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
--}}
@extends('shared._app_layout')
@section('app_content')
<h1>Track Stats!</h1>
<p>This page should be what search engines see</p>
@endsection