public function showSinglestat($statID)
 {
     if (Auth::check()) {
         return Redirect::route('connect.connect');
     } else {
         try {
             $user = User::find(1);
             Auth::login($user);
             $currentMetrics = Calculator::currentMetrics();
             $metricValues = Metric::where('user', Auth::user()->id)->where('date', '<', Carbon::now())->orderBy('date', 'desc')->take(31)->get();
             foreach ($currentMetrics as $metricID => $statClassName) {
                 if ($metricID == $statID) {
                     $metricsArray = array();
                     foreach ($metricValues as $metric) {
                         $metricsArray[$metric->date] = $metric->{$metricID};
                     }
                     ksort($metricsArray);
                     $allMetrics[$metricID] = $metricsArray;
                 }
             }
             if (isset($currentMetrics[$statID])) {
                 return View::make('demo.single_stat', array('data' => $currentMetrics[$statID]['metricClass']::show($allMetrics[$statID], true), 'metricDetails' => $currentMetrics[$statID], Auth::logout()));
             }
             return Redirect::route('demo.dashboard')->with('error', 'Statistic does not exist.');
         } catch (Exception $e) {
             Auth::logout();
             Log::error($e);
             return Redirect::route('demo.dashboard')->with('error', 'Something went wrong, we will return shortly.');
         }
     }
 }
Exemple #2
0
 public function formatMetrics()
 {
     // get currently calculated metrics
     $currentMetrics = Calculator::currentMetrics();
     $this->date = Carbon::createFromFormat('Y-m-d', $this->date)->format('Y-m-d, l');
     // go through them
     foreach ($currentMetrics as $statID => $statName) {
         if (is_null($this->{$statID})) {
             $this->{$statID} = 'Not enough data';
         } else {
             switch ($statID) {
                 // money formats fall through
                 case 'mrr':
                 case 'arr':
                 case 'arpu':
                     // divide the value by 100
                     $this->{$statID} /= 100;
                     // format it into money
                     setlocale(LC_MONETARY, 'en_US');
                     $this->{$statID} = money_format('%n', $this->{$statID});
                     break;
                     // percent formats fall through
                 // percent formats fall through
                 case 'uc':
                     $this->{$statID} = $this->{$statID} . '%';
                     break;
                     // pieces formats fall through
                 // pieces formats fall through
                 case 'cancellations':
                 case 'au':
                     break;
                 default:
                     break;
             }
             // /switch
         }
         // /if
     }
     // /foreach
 }
 public function showSinglestat($statID)
 {
     // check if trial period is ended
     if (Auth::user()->isTrialEnded()) {
         return Redirect::route('auth.plan')->with('error', 'Trial period ended.');
     }
     $currentMetrics = Calculator::currentMetrics();
     $metricValues = Metric::where('user', Auth::user()->id)->orderBy('date', 'desc')->take(31)->get();
     foreach ($currentMetrics as $metricID => $statClassName) {
         $metricsArray = array();
         foreach ($metricValues as $metric) {
             $metricsArray[$metric->date] = $metric->{$metricID};
         }
         ksort($metricsArray);
         $allMetrics[$metricID] = $metricsArray;
     }
     if (isset($currentMetrics[$statID])) {
         return View::make('auth.single_stat', array('data' => $currentMetrics[$statID]['metricClass']::show($allMetrics[$statID], true), 'metricDetails' => $currentMetrics[$statID]));
     } else {
         return Redirect::route('auth.dashboard')->with('error', 'Statistic does not exist.');
     }
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     // get all the users
     $users = User::all();
     $previousDayDate = Carbon::yesterday()->toDateString();
     // currently calculated metrics
     $currentMetrics = Calculator::currentMetrics();
     // for each user send email with their metrics
     $dailyEmailSent = 0;
     $weeklyEmailSent = 0;
     foreach ($users as $user) {
         // check if user finished the connect process
         if ($user->isConnected() && $user->ready == 'connected' && !$user->isTrialEnded()) {
             switch ($user->summaryEmailFrequency) {
                 case 'none':
                     // no summary email
                     break;
                 case 'daily':
                     // default behavior, send yesterday's data
                     // get the user's metrics
                     $metric = Metric::where('user', $user->id)->where('date', $previousDayDate)->first();
                     if ($metric) {
                         $previousMetrics = Metric::where('user', $user->id)->where('date', '<=', Carbon::yesterday()->subDays(30)->toDateString())->orderBy('date', 'desc')->first();
                         $changes = array();
                         foreach ($currentMetrics as $metricID => $metricDetails) {
                             // get the correct color
                             $changes[$metricID]['positiveIsGood'] = $metricDetails['metricClass']::POSITIVE_IS_GOOD;
                             $date = $metric->date;
                             if ($previousMetrics) {
                                 if ($previousMetrics->{$metricID} != 0) {
                                     $value = $metric->{$metricID} / $previousMetrics->{$metricID} * 100 - 100;
                                     $changes[$metricID][$date]['isBigger'] = $value > 0 ? true : false;
                                     $changes[$metricID][$date]['value'] = round($value) . ' %';
                                 } else {
                                     $changes[$metricID][$date]['value'] = null;
                                 }
                             } else {
                                 $changes[$metricID][$date]['value'] = null;
                             }
                         }
                         // format metrics to presentable data
                         $metric->formatMetrics();
                         // this line is for making the daily email the same format as the weekly
                         // so we only need one email template
                         $metrics = array($metric->date => $metric);
                         $data = array('metrics' => $metrics, 'currentMetrics' => $currentMetrics, 'changes' => $changes, 'isDaily' => true, 'index' => 0);
                         $email = Mailman::make('emails.summary')->with($data)->to($user->email)->subject('Daily summary')->send();
                         //File::put(public_path().'/summary_email.html',$email);
                         $dailyEmailSent++;
                     }
                     break;
                 case 'weekly':
                     // send a weekly summary to the user with their numbers
                     // check if today is monday (we send weekly emails on monday)
                     /* improvment idea
                     				change this if to switch-case with days
                     				for user controlled daily send
                     			*/
                     if (Carbon::now()->dayOfWeek == Carbon::WEDNESDAY) {
                         // get the user's metrics
                         $metrics = Metric::where('user', $user->id)->where('date', '<=', $previousDayDate)->orderBy('date', 'desc')->take(7)->get();
                         $previousMetrics = Metric::where('user', $user->id)->where('date', '<=', Carbon::yesterday()->subDays(30)->toDateString())->orderBy('date', 'desc')->take(7)->get();
                         $changes = array();
                         foreach ($currentMetrics as $metricID => $metricDetails) {
                             // get the correct color
                             $changes[$metricID]['positiveIsGood'] = $metricDetails['metricClass']::POSITIVE_IS_GOOD;
                             foreach ($previousMetrics as $id => $prevMetric) {
                                 $date = $metrics[$id]->date;
                                 if ($prevMetric->{$metricID} != 0) {
                                     $value = $metrics[$id]->{$metricID} / $prevMetric->{$metricID} * 100 - 100;
                                     $changes[$metricID][$date]['isBigger'] = $value > 0 ? true : false;
                                     $changes[$metricID][$date]['value'] = round($value) . ' %';
                                 } else {
                                     $changes[$metricID][$date]['value'] = null;
                                 }
                             }
                         }
                         // format metrics to presentable data
                         $weeklyMetrics = array();
                         foreach ($metrics as $metric) {
                             $metric->formatMetrics();
                             $weeklyMetrics[$metric->date] = $metric;
                         }
                         $data = array('metrics' => $weeklyMetrics, 'currentMetrics' => $currentMetrics, 'changes' => $changes, 'isDaily' => false, 'index' => 0);
                         // login the user (necessary to get the email address)
                         // Auth::login($user);
                         // send the email to the user
                         $email = Mailman::make('emails.summary')->with($data)->to($user->email)->subject('Weekly summary')->send();
                         //File::put(public_path().'/summary_email.html',$email);
                         $weeklyEmailSent++;
                     }
                     break;
                 default:
                     Log::error('notifications string has been changed, check the email sending code');
                     break;
             }
             // /switch
         }
         // /isConnected
     }
     // /foreach
     Log::info($dailyEmailSent . ' daily summary emails sent out of ' . count($users) . ' users');
     Log::info($weeklyEmailSent . ' daily summary emails sent out of ' . count($users) . ' users');
     Log::info('Total of ' . $dailyEmailSent + $weeklyEmailSent . ' emails sent');
 }