mirror of
https://github.com/cydrobolt/polr.git
synced 2024-09-19 15:11:40 +02:00
Move analytics SQL & logic to StatsHelper, refactor to use left_bound and right_bound
This commit is contained in:
parent
8f8e29ba48
commit
fc22ed0652
71
app/Helpers/StatsHelper.php
Normal file
71
app/Helpers/StatsHelper.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
namespace App\Helpers;
|
||||
use App\Models\Click;
|
||||
use App\Models\Link;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class StatsHelper {
|
||||
function __construct($link_id, $left_bound, $right_bound) {
|
||||
$this->link_id = $link_id;
|
||||
$this->left_bound_parsed = Carbon::parse($left_bound);
|
||||
$this->right_bound_parsed = Carbon::parse($right_bound);
|
||||
|
||||
if (!$this->left_bound_parsed->lte($this->right_bound_parsed)) {
|
||||
// If left bound is not less than or equal to right bound
|
||||
throw new Exception('Invalid bounds.');
|
||||
}
|
||||
}
|
||||
|
||||
public function getBaseRows() {
|
||||
/**
|
||||
* Fetches base rows given left date bound, right date bound, and link id
|
||||
*
|
||||
* @param integer $link_id
|
||||
* @param string $left_bound
|
||||
* @param string $right_bound
|
||||
*
|
||||
* @return DB rows
|
||||
*/
|
||||
|
||||
return DB::table('clicks')
|
||||
->where('link_id', $this->link_id)
|
||||
->where('created_at', '>=', $this->left_bound_parsed)
|
||||
->where('created_at', '<=', $this->right_bound_parsed);
|
||||
}
|
||||
|
||||
public function getDayStats() {
|
||||
// Return stats by day from the last 30 days
|
||||
// date => x
|
||||
// clicks => y
|
||||
$stats = $this->getBaseRows()
|
||||
->select(DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d') AS x, count(*) AS y"))
|
||||
->groupBy(DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d')"))
|
||||
->orderBy('x', 'asc')
|
||||
->get();
|
||||
|
||||
return $stats;
|
||||
}
|
||||
|
||||
public function getCountryStats() {
|
||||
$stats = $this->getBaseRows()
|
||||
->select(DB::raw("country AS label, count(*) AS clicks"))
|
||||
->groupBy('country')
|
||||
->orderBy('clicks', 'desc')
|
||||
->get();
|
||||
|
||||
return $stats;
|
||||
}
|
||||
|
||||
public function getRefererStats() {
|
||||
$stats = $this->getBaseRows()
|
||||
->select(DB::raw("COALESCE(referer_host, 'Direct') as label, count(*) as clicks"))
|
||||
->groupBy('referer_host')
|
||||
->orderBy('clicks', 'desc')
|
||||
->get();
|
||||
|
||||
return $stats;
|
||||
}
|
||||
}
|
@ -57,7 +57,6 @@ class UserHelper {
|
||||
|
||||
public static function resetRecoveryKey($username) {
|
||||
$recovery_key = CryptoHelper::generateRandomHex(50);
|
||||
|
||||
$user = self::getUserByUsername($username);
|
||||
|
||||
if (!$user) {
|
||||
@ -72,7 +71,6 @@ class UserHelper {
|
||||
|
||||
public static function userResetKeyCorrect($username, $recovery_key, $inactive=false) {
|
||||
// Given a username and a recovery key, return true if they match.
|
||||
|
||||
$user = self::getUserByUsername($username, $inactive);
|
||||
|
||||
if ($user) {
|
||||
|
15
app/Http/Controllers/Api/ApiAnalyticsController.php
Normal file
15
app/Http/Controllers/Api/ApiAnalyticsController.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace App\Http\Controllers\Api;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
// use App\Factories\LinkFactory;
|
||||
use App\Helpers\LinkHelper;
|
||||
use App\Helpers\StatsHelper;
|
||||
|
||||
class ApiLinkController extends ApiController {
|
||||
public function lookupLinkAnalytics (Request $request) {
|
||||
$response_type = $request->input('response_type');
|
||||
$user = self::getApiUserInfo($request);
|
||||
|
||||
}
|
||||
}
|
@ -6,53 +6,17 @@ use Carbon\Carbon;
|
||||
|
||||
use App\Models\Link;
|
||||
use App\Models\Clicks;
|
||||
use App\Helpers\StatsHelper;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class StatsController extends Controller {
|
||||
const DAYS_TO_FETCH = 30;
|
||||
|
||||
private function getBaseRows($link_id) {
|
||||
// Get past month rows
|
||||
return DB::table('clicks')
|
||||
->where('link_id', $link_id)
|
||||
->where('created_at', '>=', Carbon::now()->subDays(self::DAYS_TO_FETCH));
|
||||
}
|
||||
|
||||
private function getDayStats($link_id) {
|
||||
// Return stats by day from the last 30 days
|
||||
// date => x
|
||||
// clicks => y
|
||||
$stats = $this->getBaseRows($link_id)
|
||||
->select(DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d') AS x, count(*) AS y"))
|
||||
->groupBy(DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d')"))
|
||||
->orderBy('x', 'asc')
|
||||
->get();
|
||||
|
||||
return $stats;
|
||||
}
|
||||
|
||||
private function getCountryStats($link_id) {
|
||||
$stats = $this->getBaseRows($link_id)
|
||||
->select(DB::raw("country AS label, count(*) AS clicks"))
|
||||
->groupBy('country')
|
||||
->orderBy('clicks', 'desc')
|
||||
->get();
|
||||
|
||||
return $stats;
|
||||
}
|
||||
|
||||
private function getRefererStats($link_id) {
|
||||
$stats = $this->getBaseRows($link_id)
|
||||
->select(DB::raw("COALESCE(referer_host, 'Direct') as label, count(*) as clicks"))
|
||||
->groupBy('referer_host')
|
||||
->orderBy('clicks', 'desc')
|
||||
->get();
|
||||
|
||||
return $stats;
|
||||
}
|
||||
|
||||
|
||||
public function displayStats(Request $request, $short_url) {
|
||||
// Carbon bounds for StatHelper
|
||||
$left_bound = Carbon::now()->subDays(self::DAYS_TO_FETCH);
|
||||
$right_bound = Carbon::now();
|
||||
|
||||
if (!$this->isLoggedIn()) {
|
||||
return redirect(route('login'))->with('error', 'Please login to view link stats.');
|
||||
}
|
||||
@ -74,9 +38,12 @@ class StatsController extends Controller {
|
||||
return redirect(route('admin'))->with('error', 'You do not have permission to view stats for this link.');
|
||||
}
|
||||
|
||||
$day_stats = $this->getDayStats($link_id);
|
||||
$country_stats = $this->getCountryStats($link_id);
|
||||
$referer_stats = $this->getRefererStats($link_id);
|
||||
// Fetch base rows for StatHelper
|
||||
$stats = new StatsHelper($link_id, $left_bound, $right_bound);
|
||||
|
||||
$day_stats = $stats->getDayStats();
|
||||
$country_stats = $stats->getCountryStats();
|
||||
$referer_stats = $stats->getRefererStats();
|
||||
|
||||
return view('link_stats', [
|
||||
'link' => $link,
|
||||
|
Loading…
Reference in New Issue
Block a user