Пример #1
0
 /**
  * Prepare Active Users for statistics
  *
  * @param boolean
  *
  * @return array
  */
 public static function show($metrics, $fullDataNeeded = false)
 {
     // defaults
     self::$statName = 'Active Users';
     self::$statID = 'au';
     // return array
     $AUData = array();
     // full AU data
     if ($fullDataNeeded) {
         $AUData = self::showFullStat($metrics);
     } else {
         $AUData = self::showSimpleStat($metrics);
     }
     return $AUData;
 }
Пример #2
0
 /** 
  * first time metric calculator - called on connect
  * calculates metrics in the past
  * can be a long running method
  * @param $user
  * 
  * @return null
  */
 public static function calculateMetricsOnConnect($user)
 {
     $timestamp = time();
     $todayDate = date('Y-m-d', $timestamp);
     // request and save events
     Log::info('Connecting user: '******'Saving events');
     self::saveEvents($user);
     Log::info('Events saved, calculating metrics');
     // get first event date
     $firstDate = Event::where('user', $user->id)->orderBy('date', 'asc')->first();
     if ($firstDate) {
         $firstDate = $firstDate->date;
     } else {
         $firstDate = date('Y-m-d', time());
     }
     // request plans and subscription infos (alternativly, customers)
     $customers = TailoredData::getCustomers($user);
     // calculate starter mrr and au
     $starterAU = count($customers);
     $starterMRR = MrrStat::calculateFirstTime($customers);
     // reverse calculate from events
     $historyMRR = MrrStat::calculateHistory($timestamp, $user, $firstDate, $starterMRR);
     $historyAU = AUStat::calculateHistory($timestamp, $user, $firstDate, $starterAU);
     $historyCancellation = CancellationStat::calculateHistory($timestamp, $user);
     // calculate arr, arpu, uc
     $historyUC = UserChurnStat::calculateHistory($historyCancellation['monthly'], $historyAU);
     $historyARR = ArrStat::calculateHistory($historyMRR);
     $historyARPU = ArpuStat::calculateHistory($historyMRR, $historyAU);
     // save all the data
     foreach ($historyAU as $date => $au) {
         $metrics = Metric::firstOrNew(array('date' => $date, 'user' => $user->id));
         $metrics->user = $user->id;
         $metrics->date = $date;
         $metrics->mrr = $historyMRR[$date];
         $metrics->au = $au;
         $metrics->arr = $historyARR[$date];
         $metrics->arpu = $historyARPU[$date];
         $metrics->uc = array_key_exists($date, $historyUC) ? $historyUC[$date] : null;
         $metrics->cancellations = array_key_exists($date, $historyCancellation['daily']) ? $historyCancellation['daily'][$date] : 0;
         $metrics->monthlyCancellations = array_key_exists($date, $historyCancellation['monthly']) ? $historyCancellation['monthly'][$date] : 0;
         $metrics->save();
     }
     Log::info('Calculations done');
 }