diff --git a/app/Helpers/StatsHelper.php b/app/Helpers/StatsHelper.php new file mode 100644 index 0000000..93d5bbc --- /dev/null +++ b/app/Helpers/StatsHelper.php @@ -0,0 +1,71 @@ +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; + } +} diff --git a/app/Helpers/UserHelper.php b/app/Helpers/UserHelper.php index 70efe1b..9b85fca 100644 --- a/app/Helpers/UserHelper.php +++ b/app/Helpers/UserHelper.php @@ -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) { diff --git a/app/Http/Controllers/Api/ApiAnalyticsController.php b/app/Http/Controllers/Api/ApiAnalyticsController.php new file mode 100644 index 0000000..478dddd --- /dev/null +++ b/app/Http/Controllers/Api/ApiAnalyticsController.php @@ -0,0 +1,15 @@ +input('response_type'); + $user = self::getApiUserInfo($request); + + } +} diff --git a/app/Http/Controllers/StatsController.php b/app/Http/Controllers/StatsController.php index 58d5ed6..7657962 100644 --- a/app/Http/Controllers/StatsController.php +++ b/app/Http/Controllers/StatsController.php @@ -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,