/** * Get number of requests per minute in last X minutes * @param int $minutesFrom default -60 (last hour) * @return array */ private static function getProblematicCountries($minutesFrom = -60) { $cacheKey = "getProblematicCountries{$minutesFrom}"; return Cache::getOrSet($cacheKey, function () use($minutesFrom) { $datetime = new \DateTime(gmdate('Y-m-d H:i:00')); $datetime->modify("{$minutesFrom} minute"); $query = new \Koldy\Db\Select(); $query->from('crash_submit')->field('country')->field(\Db::expr('COUNT(*)'), 'total')->where('created_at', '>=', $datetime->format('Y-m-d H:i:00'))->orderBy(2, 'desc')->groupBy(1); $tmp = $query->fetchAllObj(); $data = array(); foreach ($tmp as $r) { if ($r->country === null) { $name = 'unknown'; } else { $country = \Country::fetchOne(array('tld' => $r->country)); if ($country !== false) { $name = $country->country; } else { $name = 'unknown'; } } $data[$name] = (int) $r->total; } return $data; }, 65 - date('s')); }
/** * Get the meta value * @param string $name * @return string|null */ public function getMeta($name) { $query = new Select(); $query->from('crash_submit_meta')->field('meta_value')->where('submit_id', $this->id)->where('meta_name', $name); $records = $query->fetchAllObj(); if (sizeof($records) == 1) { return $records[0]['meta_value']; } return null; }
/** * Get number of requests per minute in last X minutes * @param int $minutesFrom default -60 (last hour) * @return array */ private static function getRecords($minutesFrom = -60) { $cacheKey = "ProblematicOsVersions-getRecords{$minutesFrom}"; return Cache::getOrSet($cacheKey, function () use($minutesFrom) { $datetime = new \DateTime(gmdate('Y-m-d H:i:00')); $datetime->modify("{$minutesFrom} minute"); $query = new \Koldy\Db\Select(); $query->from('crash_submit')->field(\Db::expr('CONCAT(os, \' \', android_version)'), 'os_version')->field(\Db::expr('COUNT(*)'), 'total')->where('created_at', '>=', $datetime->format('Y-m-d H:i:00'))->orderBy(2, 'desc')->groupBy(1); $tmp = $query->fetchAllObj(); $data = array(); foreach ($tmp as $r) { $data[$r->os_version] = (int) $r->total; } return $data; }, 65 - date('s')); }
public function __construct($days) { $user = Session::get('user'); $this->title("Crash reports per days (UTC time)")->titleOnYAxis('number of requests')->tooltipShared(); // $offset = \Misc::getUserTimezoneOffsetInMinutes(); $offset = 0; $cacheKey = "DashboardDays-last-{$days}-days-offset-{$offset}min"; $data = Cache::getOrSet($cacheKey, function () use($days, $offset) { $datetime = new \DateTime(\Misc::userDate('Y-m-d 00:00:00')); $datetime->modify("-{$days} day"); $query = new Select(); $query->from('crash_archive', 'a')->field('DATE(a.created_at)', 'time')->field('COUNT(*)', 'total')->where('a.created_at', '>=', $datetime->format('Y-m-d H:i:s'))->orderBy(1, 'asc')->groupBy(1); $records = $query->fetchAllObj(); $data = array(); foreach ($records as $r) { $data[$r->time] = (int) $r->total; } return $data; }, 3720 - date('i') * 60 + date('s')); // get data in last 30 days per package per date /* $query = new Select(); $query ->from('crash_archive', 'a') ->field('DATE(a.created_at)', 'date') ->field('a.package_id') ->field('COUNT(*)', 'total') ->innerJoin('package p', 'p.id', '=', 'a.package_id') ->field('p.name', 'package_name') ->where('a.created_at', '>=', $datetime->format('Y-m-d H:i:s')) ->groupBy(2) ->groupBy(1) ->orderBy(1) ->orderBy(2) ->orderBy(4, 'DESC'); $records = $query->fetchAllObj(); $apps = $appName = array(); foreach ($records as $r) { if (!isset($apps[$r->package_id])) { $apps[$r->package_id] = array(); $appName[$r->package_id] = $r->package_name; } $apps[$r->package_id][$r->date] = (int) $r->total; } */ $start = new \DateTime(gmdate('Y-m-d')); $days--; $start->modify("-{$days} day"); $serieData = $appsData = array(); $today = \Misc::userDate('Y-m-d'); do { $pointer = $start->format('Y-m-d'); $serieData[$pointer] = isset($data[$pointer]) ? $data[$pointer] : 0; // foreach ($apps as $packageId => $dates) { // if (!isset($apps[$packageId][$pointer])) { // $apps[$packageId][$pointer] = 0; // } // } $start->modify('+1 day'); } while ($pointer < $today); $this->addSerie('total requests', array_values($serieData)); /*foreach ($apps as $packageId => $dates) { if (sizeof($this->series) < 6) { ksort($dates); $this->addSerie($appName[$packageId], array_values($dates)); } }*/ foreach (array_keys($serieData) as $key) { $date = new \DateTime($key); $this->addOnXAxis($date->format('jS')); } }
/** * @param int $minutesFrom default -60 (last hour) * @return array */ public static function getProblematicBrandModels($minutesFrom = -60) { $cacheKey = "getProblematicBrandModels{$minutesFrom}"; return Cache::getOrSet($cacheKey, function () use($minutesFrom) { $datetime = new \DateTime(gmdate('Y-m-d H:i:s')); $datetime->modify("{$minutesFrom} minute"); $query = new \Koldy\Db\Select(); $query->from('crash_submit')->field('brand')->field(Db::expr('COUNT(*)'), 'total')->where('created_at', '>=', $datetime->format('Y-m-d H:i:s'))->orderBy(2, 'desc')->groupBy(1); $tmp = $query->fetchAllObj(); $data = array(); foreach ($tmp as $r) { $data[trim($r->brand) == '' ? 'unknown' : $r->brand] = (int) $r->total; } return $data; }, 65 - date('s')); }
/** * Calculate for reports * @return boolean */ public function calculate() { if (Status::isCalculationInProgress()) { Log::info('Trying to start calculation, but calculation is already in progress. Last was started on ' . Status::getLastCalculationProcessStart() . ' UTC'); return false; } ini_set('memory_limit', '512M'); set_time_limit(0); Status::calculationStarted(); Status::setCalculationStatus('Initializing'); Log::info('Calculation started'); /** * metas: 'file_path', 'build', 'environment', 'settings_global', 'settings_system', 'settings_secure', * 'device_features', 'shared_preferences', 'initial_configuration', 'crash_configuration', * 'dumpsys_meminfo', 'display', 'stack_trace', 'logcat', 'tktal_mem_size', '@evice_features', 'installation_id' */ $query = new Select(); $query->from('crash_submit')->field('id')->limit(0, 200000); $maxQuery = CrashArchive::query()->field('MAX(created_at)', 'time'); $max = $maxQuery->fetchFirstObj(); unset($maxQuery); $lastDatetime = new DateTime(gmdate('Y-m-d H:00:00')); $lastDatetime->modify('-1 hour'); $query->where('created_at', '<', $lastDatetime->format('Y-m-d H:i:s')); $ids = $query->fetchAllObj(); $sizeofIds = sizeof($ids); Log::info('Loaded ids: ' . $sizeofIds); $brandTotals = $countryTotals = $packageTotals = $packageVersionTotals = $phoneModelTotals = $productTotals = $providerTotals = $stackTraceTotals = $osVersionTotals = array(); foreach ($ids as $index => $r) { // on every 25 records, we should ask ourself: is that it? if ($index % 25 == 0) { $shouldTerminate = Status::shouldCalcuationTerminate(); if ($shouldTerminate !== false) { Log::info("Noticed request for calculation termination on {$shouldTerminate}. Aborted!"); Status::setCalculationStatus('Terminated after ' . ($index + 1) . ' records'); Status::terminateCalculation(false); return false; } } $id = $r->id; // record by record Log::info("Will now fetch id={$id}"); if ($index % 15 == 0) { $percent = round($index / $sizeofIds * 100, 2); Status::setCalculationStatus("Working; {$index}/{$sizeofIds} {$percent}%"); } $submit = CrashSubmit::fetchOne($id); if ($submit !== false && $submit->package_name !== null && trim($submit->package_name !== null) != '') { $appStartTime = strtotime($submit->user_app_start_date); $appCrashTime = strtotime($submit->user_crash_date); $appLifetime = $appCrashTime - $appStartTime; $metas = $submit->getMetas(); if ($submit->report_id !== null && trim($submit->report_id) !== '') { $metas['report_id'] = $submit->report_id; } if ($submit->file_path !== null && trim($submit->file_path) !== '') { $metas['file_path'] = $submit->file_path; } if ($submit->installation_id !== null && trim($submit->installation_id) !== '') { $metas['installation_id'] = $submit->installation_id; } $stackTrace = $submit->stack_trace; if ($stackTrace === null) { $stackTraceSummary = null; } else { $metas['stack_trace'] = $stackTrace; $stackTraceSummary = StackTrace::getSummary($stackTrace); } $packageId = $this->getPackageId($submit->package_name); $packageVersionId = $this->getPackageVersionId($this->getPackageId($submit->package_name), $submit->app_version_name); $brandId = $this->getBrandId($submit->brand); $phoneModelId = $this->getModelId($this->getBrandId($submit->brand), $submit->phone_model); $productId = $this->getProductId($this->getBrandId($submit->brand), $submit->product); $osVersionId = $this->getOsVersion($submit->os, $submit->android_version); $stackTraceId = $this->getStackTraceId($stackTraceSummary, $submit->created_at); $countryId = $this->getCountryId($submit->country); $providerId = $this->getProviderId($submit->provider); $archive = CrashArchive::create(array('created_at' => $submit->created_at, 'package_id' => $packageId, 'package_version_id' => $packageVersionId, 'brand_id' => $brandId, 'model_id' => $phoneModelId, 'product_id' => $productId, 'os' => $submit->os, 'os_version_id' => $osVersionId, 'total_mem_size' => $submit->total_mem_size, 'available_mem_size' => $submit->available_mem_size, 'user_comment' => trim($submit->user_comment) == '' ? null : trim($submit->user_comment), 'user_email' => trim($submit->user_email) == 'N/A' ? null : trim($submit->user_email), 'user_app_start_date' => $submit->user_app_start_date, 'user_crash_date' => $submit->user_crash_date, 'user_app_lifetime' => $appLifetime, 'stack_trace_id' => $stackTraceId, 'country_id' => $countryId, 'provider_id' => $providerId)); $archive->insertMeta($metas); // prepare increments for totals if ($packageId !== null) { if (!isset($packageTotals[$packageId])) { $packageTotals[$packageId] = 0; } $packageTotals[$packageId]++; } if ($packageVersionTotals !== null) { if (!isset($packageVersionTotals[$packageVersionId])) { $packageVersionTotals[$packageVersionId] = 0; } $packageVersionTotals[$packageVersionId]++; } if ($brandId !== null) { if (!isset($brandTotals[$brandId])) { $brandTotals[$brandId] = 0; } $brandTotals[$brandId]++; } if ($phoneModelId !== null) { if (!isset($phoneModelTotals[$phoneModelId])) { $phoneModelTotals[$phoneModelId] = 0; } $phoneModelTotals[$phoneModelId]++; } if ($productId !== null) { if (!isset($productTotals[$productId])) { $productTotals[$productId] = 0; } $productTotals[$productId]++; } if ($osVersionTotals !== null) { if (!isset($osVersionTotals[$osVersionId])) { $osVersionTotals[$osVersionId] = 0; } $osVersionTotals[$osVersionId]++; } if ($stackTraceId !== null) { if (!isset($stackTraceTotals[$stackTraceId])) { $stackTraceTotals[$stackTraceId] = 0; } $stackTraceTotals[$stackTraceId]++; } if ($countryId !== null) { if (!isset($countryTotals[$countryId])) { $countryTotals[$countryId] = 0; } $countryTotals[$countryId]++; } if ($providerId !== null) { if (!isset($providerTotals[$providerId])) { $providerTotals[$providerId] = 0; } $providerTotals[$providerId]++; } } } if ($sizeofIds > 0) { Log::info('Calculation done'); Status::setCalculationStatus('Starting to delete submit records'); $deleteIds = array(); foreach ($ids as $index => $r) { $deleteIds[] = $r->id; if (sizeof($deleteIds) == 100) { $percent = round($index / $sizeofIds * 100, 2); Status::setCalculationStatus("Deleting submit records {$index}/{$sizeofIds} {$percent}%"); Db::delete('crash_submit_meta')->whereIn('submit_id', $deleteIds)->exec(); Db::delete('crash_submit')->whereIn('id', $deleteIds)->exec(); $deleteIds = array(); } } if (sizeof($deleteIds) > 0) { Status::setCalculationStatus("Deleting submit records {$index}/{$sizeofIds} 100%"); Db::delete('crash_submit_meta')->whereIn('submit_id', $deleteIds)->exec(); Db::delete('crash_submit')->whereIn('id', $deleteIds)->exec(); } Status::setCalculationStatus('Started totals calculations update!'); // update calculated increments foreach ($brandTotals as $id => $total) { Status::setCalculationStatus('Brands: Updating calculated totals'); Db::update('brand')->increment('total', $total)->where('id', $id)->exec(); } foreach ($countryTotals as $id => $total) { Status::setCalculationStatus('Countries: Updating calculated totals'); Db::update('country')->increment('total', $total)->where('id', $id)->exec(); } foreach ($packageTotals as $id => $total) { Status::setCalculationStatus('Packages: Updating calculated totals'); Db::update('package')->increment('total', $total)->where('id', $id)->exec(); } foreach ($packageVersionTotals as $id => $total) { Status::setCalculationStatus('Package version: Updating calculated totals'); Db::update('package_version')->increment('total', $total)->where('id', $id)->exec(); } foreach ($phoneModelTotals as $id => $total) { Status::setCalculationStatus('Phone models: Updating calculated totals'); Db::update('phone_model')->increment('total', $total)->where('id', $id)->exec(); } foreach ($productTotals as $id => $total) { Status::setCalculationStatus('Products: Updating calculated totals'); Db::update('product')->increment('total', $total)->where('id', $id)->exec(); } foreach ($providerTotals as $id => $total) { Status::setCalculationStatus('Providers: Updating calculated totals'); Db::update('provider')->increment('total', $total)->where('id', $id)->exec(); } foreach ($stackTraceTotals as $id => $total) { Status::setCalculationStatus('Stack traces: Updating calculated totals'); Db::update('stack_trace')->increment('total', $total)->where('id', $id)->exec(); } foreach ($osVersionTotals as $id => $total) { Status::setCalculationStatus('OS version: Updating calculated totals'); Db::update('version')->increment('total', $total)->where('id', $id)->exec(); } } else { // Log::info('Calculation done, no records processed'); } Status::calculationFinished($sizeofIds); return true; }