/** * 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'); }
/** * Prepare MRR for statistics * * @param boolean * * @return array */ public static function show($metrics, $fullDataNeeded = false) { // defaults self::$statName = 'Monthly Recurring Revenue'; self::$statID = 'mrr'; $data = array(); // full MRR data if ($fullDataNeeded) { $data = self::showFullStat($metrics); // correction of the money to dollars from cents foreach ($data['fullHistory'] as $date => $value) { if ($value) { $data['fullHistory'][$date] = $value / 100; } } } else { $data = self::showSimpleStat($metrics); } if (isset($data['history'])) { foreach ($data['history'] as $date => $value) { if ($value) { $data['history'][$date] = $value / 100; } } } else { $data['history'] = array(); } // converting to money format $data = self::toMoneyFormat($data, $fullDataNeeded); return $data; }