Ejemplo n.º 1
0
 /**
  * Increment one numeric field in table on the row identified by primary key.
  * You can use this only if your primary key is just one field.
  * 
  * @param string $field
  * @param mixed $where the primary key value of the record
  * @param int $howMuch [optional] default 1
  * @return int number of affected rows
  */
 public static function increment($field, $where, $howMuch = 1)
 {
     $update = Db::update(static::getTableName())->increment($field, $howMuch);
     if ($where instanceof Where) {
         $update->where($where);
     } else {
         if (is_array($where)) {
             foreach ($where as $field => $value) {
                 $update->where($field, $value);
             }
         } else {
             if (!is_array(static::$primaryKey) && (is_numeric($where) || is_string($where))) {
                 $update->where(static::$primaryKey, $where);
             } else {
                 throw new Exception('Unhandeled increment case in DB model');
             }
         }
     }
     return $update->exec(static::$connection);
 }
Ejemplo n.º 2
0
 public function indexAjax()
 {
     $where = array();
     $bindings = array();
     $params = Input::requireParams('package', 'package_version', 'brand', 'phone_model', 'product', 'os_version', 'country');
     // package
     if ($params->package !== '') {
         $a = explode(',', $params->package);
         $vals = array();
         foreach ($a as $val) {
             $vals[] = "s.package_name = ?";
             $bindings[] = $val;
         }
         $vals = '(' . implode(' OR ', $vals) . ')';
         $where[] = $vals;
     }
     // package_version
     if ($params->package_version !== '') {
         $a = explode(',', $params->package_version);
         $vals = array();
         foreach ($a as $val) {
             $vals[] = "s.app_version_name = ?";
             $bindings[] = $val;
         }
         $vals = '(' . implode(' OR ', $vals) . ')';
         $where[] = $vals;
     }
     // brand
     if ($params->brand !== '') {
         $a = explode(',', $params->brand);
         $vals = array();
         foreach ($a as $val) {
             $vals[] = "s.brand = ?";
             $bindings[] = $val;
         }
         $vals = '(' . implode(' OR ', $vals) . ')';
         $where[] = $vals;
     }
     // phone_model
     if ($params->phone_model !== '') {
         $a = explode(',', $params->phone_model);
         $vals = array();
         foreach ($a as $val) {
             $vals[] = "s.phone_model = ?";
             $bindings[] = $val;
         }
         $vals = '(' . implode(' OR ', $vals) . ')';
         $where[] = $vals;
     }
     // product
     if ($params->product !== '') {
         $a = explode(',', $params->product);
         $vals = array();
         foreach ($a as $val) {
             $vals[] = "s.product = ?";
             $bindings[] = $val;
         }
         $vals = '(' . implode(' OR ', $vals) . ')';
         $where[] = $vals;
     }
     // os_version
     if ($params->os_version !== '') {
         $a = explode(',', $params->os_version);
         $vals = array();
         foreach ($a as $val) {
             $vals[] = "s.android_version = ?";
             $bindings[] = $val;
         }
         $vals = '(' . implode(' OR ', $vals) . ')';
         $where[] = $vals;
     }
     // country
     if ($params->country !== '') {
         $a = explode(',', $params->country);
         $vals = array();
         foreach ($a as $val) {
             $vals[] = "s.country = ?";
             $bindings[] = $val;
         }
         $vals = '(' . implode(' OR ', $vals) . ')';
         $where[] = $vals;
     }
     if (sizeof($where) == 0) {
         $where = '1';
     } else {
         $where = implode(' AND ', $where);
     }
     $query = "\n\t\t\tSELECT\n\t\t\t\ts.id,\n\t\t\t\ts.created_at,\n\t\t\t\ts.package_name,\n\t\t\t\ts.app_version_name as package_version,\n\t\t\t\ts.brand,\n\t\t\t\ts.phone_model,\n\t\t\t\ts.product,\n\t\t\t\ts.android_version,\n\t\t\t\ts.country,\n\t\t\t\ts.stack_trace\n\t\t\tFROM\n\t\t\t\tcrash_submit s\n\t\t\tWHERE\n\t\t\t\t{$where}\n\t\t\tORDER BY s.created_at DESC\n\t\t\tLIMIT 0, 50\n\t\t";
     $records = Db::getAdapter()->query($query, $bindings);
     $data = array();
     foreach ($records as $r) {
         if ($r->stack_trace !== null) {
             $lines = explode("\n", $r->stack_trace);
             $stackTraceSummary = array();
             $more = 0;
             foreach ($lines as $line) {
                 $ord = ord(substr($line, 0, 1));
                 if ($ord >= 65 && $ord <= 90 || $ord >= 97 && $ord <= 122) {
                     if (sizeof($stackTraceSummary) < 2) {
                         $stackTraceSummary[] = trim($line);
                     } else {
                         $more++;
                     }
                 }
             }
             if ($more > 0) {
                 $stackTraceSummary[] = "... and {$more} more line(s)";
             }
             $stackTraceSummary = implode("\n", $stackTraceSummary);
         } else {
             $stackTraceSummary = null;
         }
         $data[] = array('id' => (int) $r->id, 'created_at' => Misc::userDate('H:i:s', $r->created_at), 'package' => "{$r->package_name} {$r->package_version}", 'device' => "{$r->brand}<br/>{$r->phone_model}", 'product' => $r->phone_model != $r->product ? $r->product : null, 'os' => "Android {$r->android_version}", 'country' => $r->country, 'stack_trace' => $stackTraceSummary);
     }
     return Json::create(array('success' => true, 'time' => Misc::userDate('Y-m-d H:i:s'), 'data' => $data));
 }
Ejemplo n.º 3
0
 /**
  * @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'));
 }
Ejemplo n.º 4
0
 /**
  * Rebuild stack_trace table
  * CAUTION!!!! If you have a lot of records, this could take hours or days do get done!
  */
 public static function rebuild()
 {
     Status::calculationStarted();
     Status::setCalculationStatus('Initializing');
     Status::setCalculationStatus('Reseting stack_trace_ids to NULL');
     Db::query('UPDATE crash_archive SET stack_trace_id = NULL');
     Status::setCalculationStatus('Emptying stack_trace table');
     Db::query('TRUNCATE TABLE stack_trace');
     $stacks = array();
     // 		foreach (Stack\Trace::all() as $r) {
     // 			$stacks[$r['hash']] = (int) $r['id'];
     // 		}
     $total = \Crash\Archive::count();
     $index = 0;
     $start = 0;
     do {
         $query = new Select('crash_archive');
         $query->field('id')->where('id', '>', $start)->where('id', '<=', $start + 100000);
         Status::setCalculationStatus("Taking IDs from {$start} to " . ($start + 100000));
         $records = $query->fetchAll();
         $sizeofIds = count($records);
         foreach ($records as $r) {
             $id = (int) $r['id'];
             $crash = new \Crash\Archive(array('id' => $id));
             $stackTrace = $crash->getMeta('stack_trace');
             if ($stackTrace !== null) {
                 $summary = static::getSummary($stackTrace);
                 $md5 = md5($summary);
                 if (isset($stacks[$md5])) {
                     $stackTraceId = $stacks[$md5];
                 } else {
                     $tmp = \Stack\Trace::create(array('hash' => $md5, 'summary' => $summary, 'created_at' => $crash->created_at));
                     $stackTraceId = (int) $tmp->id;
                     $stacks[$md5] = $stackTraceId;
                 }
                 $crash->stack_trace_id = $stackTraceId;
                 $crash->save();
                 Log::info("Updated #{$id} with stack={$stackTraceId}");
             } else {
                 Log::info("Crash report #{$id} is skipped because stack_trace is missing in meta table");
             }
             if ($index % 25 == 0) {
                 $percent = round($index / $total * 100, 2);
                 Status::setCalculationStatus("Working on {$index}/{$sizeofIds} {$percent}%");
             }
             $index++;
         }
         $start += 100000;
     } while (sizeof($records) > 0);
     Status::setCalculationStatus('Started stack trace recalculation!');
     Log::info('Started recalculation of stack_trace counts');
     static::recalculate();
     Status::calculationFinished('all');
 }
Ejemplo n.º 5
0
 /**
  * (non-PHPdoc)
  * @see \Koldy\Log\Writer\AbstractLogWriter::logMessage()
  */
 protected function logMessage($level, $message)
 {
     if ($this->inserting) {
         return;
     }
     $data = $this->getFieldsData($level, $message);
     if ($data !== false) {
         if (in_array($level, $this->config['log'])) {
             $this->inserting = true;
             $insert = new Insert($this->config['table']);
             $insert->add($data);
             if ($insert->exec($this->config['connection']) === false) {
                 $adapter = KoldyDb::getAdapter($this->config['connection']);
                 // do not process this with Log::exception because it will run into recursion
                 $this->detectEmailAlert('exception');
                 $this->appendMessage(date('Y-m-d H:i:sO') . "\tERROR inserting log message into database: {$adapter->getLastError()}\n\n{$adapter->getLastException()->getTraceAsString()}\n");
             }
         }
         $this->inserting = false;
         $this->detectEmailAlert($level);
         $this->appendMessage(date('Y-m-d H:i:sO') . "\t" . implode("\t", array_values($data)) . "\n");
     }
 }
Ejemplo n.º 6
0
 /**
  * Execute the query
  * 
  * @param string|\Koldy\Db\Adapter $adapter [optional] execute this query on which adapter?
  * @throws \Koldy\Exception
  * @return array|int number of affected rows or array of resultset returned by database
  */
 public function exec($adapter = null)
 {
     $query = $this->getQuery();
     if ($query === null) {
         throw new Exception('SQL not built, can not execute query');
     }
     if ($adapter === null) {
         $adapter = $this->getAdapter();
     } else {
         if (is_string($adapter)) {
             $adapter = Db::getAdapter($adapter);
         } else {
             if ($adapter instanceof Adapter) {
                 // then its fine, don't do anything, just continue
             } else {
                 throw new Exception('Invalid DB adapter');
             }
         }
     }
     return $adapter->query($query, $this->getBindings());
 }
Ejemplo n.º 7
0
 /**
  * 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;
 }