Skip to content
Snippets Groups Projects
Unverified Commit eb3180cc authored by Robb Lewis's avatar Robb Lewis
Browse files

Stats

parent fbda44f1
No related branches found
No related tags found
No related merge requests found
<?php
namespace App\Http\Controllers;
use App\Tweets\TweetRepository;
class AdminController extends Controller
{
/**
* @var TweetRepository
*/
private $tweets;
public function __construct(TweetRepository $tweets)
{
$this->tweets = $tweets;
}
public function stats()
{
list($totals, $clients, $average) = $this->tweets->stats();
return view('admin.stats', compact(
'totals', 'clients', 'average'
));
}
}
Loading
Loading
@@ -98,6 +98,51 @@ class TweetRepository {
->paginate(self::$paginate);
}
 
public function stats()
{
$clients = $this->topClients();
$totals = $this->typeCounts()->mapWithKeys(function($type) {
return [TweetType::getTypeString($type['type']) => $type['count']];
});
$totals['all'] = $totals->sum();
$average = $this->average();
return [$totals, $clients, $average];
}
public function topClients()
{
return Tweet::select(DB::raw('count(*) as count, source'))
->groupBy('source')
->orderBy('count', 'desc')
->limit(10)
->get();
}
public function typeCounts()
{
return Tweet::select(DB::raw('count(*) as count, type'))
->groupBy('type')
->get();
}
public function average()
{
$firstTweet = Tweet::first();
$daysSince = $firstTweet->time->diffInDays(Carbon::now());
return [
'average' => number_format(Tweet::count() / $daysSince, 2),
'daysSince' => $daysSince,
'first' => $firstTweet->time
];
}
/**
* Get tweets for specific date range
*
Loading
Loading
Loading
Loading
@@ -8,4 +8,27 @@ class TweetType {
const TYPE_REPLY = 1;
const TYPE_RETWEET = 2;
 
protected static $types = [
self::TYPE_TWEET => 'tweet',
self::TYPE_REPLY => 'reply',
self::TYPE_RETWEET => 'retweet',
];
public static function getTypeString($type, $plural = false)
{
$typeString = self::$types[$type];
if (! $plural) return $typeString;
if ($type == self::TYPE_REPLY)
{
$typeString = 'replies';
}
else {
$typeString = $typeString . 's';
}
return $typeString;
}
}
\ No newline at end of file
@extends('layouts.main')
@section('content')
<div class="row">
<div class="col-md-9">
<h1>Tweet Stats</h1>
Total of <strong>{{ $totals['all'] }}</strong> tweets, with an average of <strong>{{ $average['average'] }}</strong> tweets per day since {{ $average['first']->format('jS F Y') }}, <strong>{{ $average['daysSince'] }}</strong> days ago.
You've replied <strong>{{ $totals['reply'] }}</strong> times, and retweeted <strong>{{ $totals['retweet'] }}</strong> tweets.
<h2>Types</h2>
<ul>
@forelse ($totals as $key => $type)
<li>{!! $type !!} {!! $key !!}</li>
@empty
NO STATS FOUND
@endforelse
</ul>
<h2>Top Clients</h2>
<ul>
@forelse ($clients as $client)
<li>{!! $client['count'] !!} tweets - {!! $client['source'] !!}</li>
@empty
NO STATS FOUND
@endforelse
</ul>
</div>
<div class="col-md-3"></div>
</div>
@endsection
\ No newline at end of file
Loading
Loading
@@ -23,6 +23,7 @@
@endif
<ul class="nav navbar-nav navbar-right">
@if (Auth::check())
<li><a href="/stats">Stats</a></li>
<li><a href="/logs">Fetch Logs</a></li>
<li><a href="/logout">Logout</a></li>
@else
Loading
Loading
Loading
Loading
@@ -8,6 +8,7 @@ Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
 
Route::group(['middleware' => ['auth']], function() {
Route::get('logs', 'LogController@index');
Route::get('stats', 'AdminController@stats');
});
 
Route::group(['middleware' => ['private']], function() {
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment