Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/cydrobolt/polr.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorChaoyi Zha <summermontreal@gmail.com>2017-03-11 18:47:43 +0300
committerChaoyi Zha <summermontreal@gmail.com>2017-03-11 18:47:43 +0300
commitfc22ed06524fb04521922c3cde6b284e322be134 (patch)
treed77ae0af33025ad66d9c7d1e22a5fbfbf50d0041 /app
parent8f8e29ba480c4b45265267a2e9adfd30e485b453 (diff)
Move analytics SQL & logic to StatsHelper, refactor to use left_bound and right_bound
Diffstat (limited to 'app')
-rw-r--r--app/Helpers/StatsHelper.php71
-rw-r--r--app/Helpers/UserHelper.php2
-rw-r--r--app/Http/Controllers/Api/ApiAnalyticsController.php15
-rw-r--r--app/Http/Controllers/StatsController.php55
4 files changed, 97 insertions, 46 deletions
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 @@
+<?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;
+ }
+}
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 @@
+<?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);
+
+ }
+}
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,