Merge pull request #7936 from JakubOnderka/app-model-cleanup

chg: [internal] App model cleanup
pull/7938/head
Jakub Onderka 2021-11-07 12:03:24 +01:00 committed by GitHub
commit d355f0f6d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 118 deletions

View File

@ -23,11 +23,11 @@
App::uses('Model', 'Model'); App::uses('Model', 'Model');
App::uses('LogableBehavior', 'Assets.models/behaviors'); App::uses('LogableBehavior', 'Assets.models/behaviors');
App::uses('RandomTool', 'Tools'); App::uses('RandomTool', 'Tools');
App::uses('FileAccessTool', 'Tools');
App::uses('JsonTool', 'Tools');
class AppModel extends Model class AppModel extends Model
{ {
public $name;
/** @var PubSubTool */ /** @var PubSubTool */
private static $loadedPubSubTool; private static $loadedPubSubTool;
@ -39,7 +39,7 @@ class AppModel extends Model
private $__profiler = array(); private $__profiler = array();
public $elasticSearchClient = false; public $elasticSearchClient;
/** @var AttachmentTool|null */ /** @var AttachmentTool|null */
private $attachmentTool; private $attachmentTool;
@ -47,8 +47,6 @@ class AppModel extends Model
public function __construct($id = false, $table = null, $ds = null) public function __construct($id = false, $table = null, $ds = null)
{ {
parent::__construct($id, $table, $ds); parent::__construct($id, $table, $ds);
$this->name = get_class($this);
$this->findMethods['column'] = true; $this->findMethods['column'] = true;
} }
@ -247,13 +245,13 @@ class AppModel extends Model
$result = $this->Feed->addDefaultFeeds($feeds); $result = $this->Feed->addDefaultFeeds($feeds);
$this->Log->create(); $this->Log->create();
$entry = array( $entry = array(
'org' => 'SYSTEM', 'org' => 'SYSTEM',
'model' => 'Server', 'model' => 'Server',
'model_id' => 0, 'model_id' => 0,
'email' => 'SYSTEM', 'email' => 'SYSTEM',
'action' => 'update_database', 'action' => 'update_database',
'user_id' => 0, 'user_id' => 0,
'title' => 'Added new default feeds.' 'title' => 'Added new default feeds.'
); );
if ($result) { if ($result) {
$entry['change'] = 'Feeds added: ' . $feedNames; $entry['change'] = 'Feeds added: ' . $feedNames;
@ -1789,14 +1787,14 @@ class AppModel extends Model
if ($flagStop && $errorCount > 0) { if ($flagStop && $errorCount > 0) {
$this->Log->create(); $this->Log->create();
$this->Log->save(array( $this->Log->save(array(
'org' => 'SYSTEM', 'org' => 'SYSTEM',
'model' => 'Server', 'model' => 'Server',
'model_id' => 0, 'model_id' => 0,
'email' => 'SYSTEM', 'email' => 'SYSTEM',
'action' => 'update_database', 'action' => 'update_database',
'user_id' => 0, 'user_id' => 0,
'title' => __('Issues executing the SQL query for %s', $command), 'title' => __('Issues executing the SQL query for %s', $command),
'change' => __('Database updates stopped as some errors occurred and the stop flag is enabled.') 'change' => __('Database updates stopped as some errors occurred and the stop flag is enabled.')
)); ));
return false; return false;
} }
@ -1862,14 +1860,14 @@ class AppModel extends Model
} }
$this->Log->create(); $this->Log->create();
$this->Log->save(array( $this->Log->save(array(
'org' => 'SYSTEM', 'org' => 'SYSTEM',
'model' => 'Server', 'model' => 'Server',
'model_id' => 0, 'model_id' => 0,
'email' => 'SYSTEM', 'email' => 'SYSTEM',
'action' => 'update_database', 'action' => 'update_database',
'user_id' => 0, 'user_id' => 0,
'title' => ($result ? 'Removed index ' : 'Failed to remove index ') . $icr['STATISTICS']['INDEX_NAME'] . ' from ' . $table, 'title' => ($result ? 'Removed index ' : 'Failed to remove index ') . $icr['STATISTICS']['INDEX_NAME'] . ' from ' . $table,
'change' => ($result ? 'Removed index ' : 'Failed to remove index ') . $icr['STATISTICS']['INDEX_NAME'] . ' from ' . $table, 'change' => ($result ? 'Removed index ' : 'Failed to remove index ') . $icr['STATISTICS']['INDEX_NAME'] . ' from ' . $table,
)); ));
} }
} }
@ -1901,14 +1899,14 @@ class AppModel extends Model
} }
$this->Log->create(); $this->Log->create();
$this->Log->save(array( $this->Log->save(array(
'org' => 'SYSTEM', 'org' => 'SYSTEM',
'model' => 'Server', 'model' => 'Server',
'model_id' => 0, 'model_id' => 0,
'email' => 'SYSTEM', 'email' => 'SYSTEM',
'action' => 'update_database', 'action' => 'update_database',
'user_id' => 0, 'user_id' => 0,
'title' => ($result ? 'Added index ' : 'Failed to add index ') . $field . ' to ' . $table . ($duplicate ? ' (index already set)' : $errorMessage), 'title' => ($result ? 'Added index ' : 'Failed to add index ') . $field . ' to ' . $table . ($duplicate ? ' (index already set)' : $errorMessage),
'change' => ($result ? 'Added index ' : 'Failed to add index ') . $field . ' to ' . $table . ($duplicate ? ' (index already set)' : $errorMessage), 'change' => ($result ? 'Added index ' : 'Failed to add index ') . $field . ' to ' . $table . ($duplicate ? ' (index already set)' : $errorMessage),
)); ));
$additionResult = array('success' => $result || $duplicate); $additionResult = array('success' => $result || $duplicate);
if (!$result) { if (!$result) {
@ -1991,7 +1989,8 @@ class AppModel extends Model
} }
// Try to create a table with a BIGINT(20) // Try to create a table with a BIGINT(20)
public function seenOnAttributeAndObjectPreUpdate() { public function seenOnAttributeAndObjectPreUpdate()
{
$sqlArray[] = "CREATE TABLE IF NOT EXISTS testtable ( $sqlArray[] = "CREATE TABLE IF NOT EXISTS testtable (
`testfield` BIGINT(6) NULL DEFAULT NULL `testfield` BIGINT(6) NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
@ -2009,10 +2008,6 @@ class AppModel extends Model
} }
} }
public function failingPreUpdate() {
throw new Exception('Yolo fail');
}
public function runUpdates($verbose = false, $useWorker = true, $processId = false) public function runUpdates($verbose = false, $useWorker = true, $processId = false)
{ {
$this->AdminSetting = ClassRegistry::init('AdminSetting'); $this->AdminSetting = ClassRegistry::init('AdminSetting');
@ -2059,14 +2054,14 @@ class AppModel extends Model
if ($this->isUpdateLocked()) { // prevent creation of useless workers if ($this->isUpdateLocked()) { // prevent creation of useless workers
$this->Log->create(); $this->Log->create();
$this->Log->save(array( $this->Log->save(array(
'org' => 'SYSTEM', 'org' => 'SYSTEM',
'model' => 'Server', 'model' => 'Server',
'model_id' => 0, 'model_id' => 0,
'email' => 'SYSTEM', 'email' => 'SYSTEM',
'action' => 'update_db_worker', 'action' => 'update_db_worker',
'user_id' => 0, 'user_id' => 0,
'title' => __('Issues executing run_updates'), 'title' => __('Issues executing run_updates'),
'change' => __('Database updates are locked. Worker not spawned') 'change' => __('Database updates are locked. Worker not spawned')
)); ));
if (!empty($job)) { // if multiple prio worker is enabled, want to mark them as done if (!empty($job)) { // if multiple prio worker is enabled, want to mark them as done
$job['Job']['progress'] = 100; $job['Job']['progress'] = 100;
@ -2114,14 +2109,14 @@ class AppModel extends Model
if ($this->isUpdateLocked()) { if ($this->isUpdateLocked()) {
$this->Log->create(); $this->Log->create();
$this->Log->save(array( $this->Log->save(array(
'org' => 'SYSTEM', 'org' => 'SYSTEM',
'model' => 'Server', 'model' => 'Server',
'model_id' => 0, 'model_id' => 0,
'email' => 'SYSTEM', 'email' => 'SYSTEM',
'action' => 'update_db_worker', 'action' => 'update_db_worker',
'user_id' => 0, 'user_id' => 0,
'title' => __('Issues executing run_updates'), 'title' => __('Issues executing run_updates'),
'change' => __('Updates are locked. Stopping worker gracefully') 'change' => __('Updates are locked. Stopping worker gracefully')
)); ));
if (!empty($job)) { if (!empty($job)) {
$job['Job']['progress'] = 100; $job['Job']['progress'] = 100;
@ -2379,6 +2374,10 @@ class AppModel extends Model
} }
} }
/**
* @param string $db_version
* @return array
*/
protected function findUpgrades($db_version) protected function findUpgrades($db_version)
{ {
$updates = array(); $updates = array();
@ -2421,21 +2420,21 @@ class AppModel extends Model
$Job = ClassRegistry::init('Job'); $Job = ClassRegistry::init('Job');
$Job->create(); $Job->create();
$data = array( $data = array(
'worker' => 'default', 'worker' => 'default',
'job_type' => 'generate correlation', 'job_type' => 'generate correlation',
'job_input' => 'All attributes', 'job_input' => 'All attributes',
'status' => 0, 'status' => 0,
'retries' => 0, 'retries' => 0,
'org' => 'ADMIN', 'org' => 'ADMIN',
'message' => 'Job created.', 'message' => 'Job created.',
); );
$Job->save($data); $Job->save($data);
$jobId = $Job->id; $jobId = $Job->id;
$process_id = CakeResque::enqueue( $process_id = CakeResque::enqueue(
'default', 'default',
'AdminShell', 'AdminShell',
array('jobGenerateCorrelation', $jobId), array('jobGenerateCorrelation', $jobId),
true true
); );
$Job->saveField('process_id', $process_id); $Job->saveField('process_id', $process_id);
} }
@ -2502,26 +2501,20 @@ class AppModel extends Model
public function getKafkaPubTool() public function getKafkaPubTool()
{ {
if (!$this->loadedKafkaPubTool) { if (!$this->loadedKafkaPubTool) {
$this->loadKafkaPubTool(); App::uses('KafkaPubTool', 'Tools');
$kafkaPubTool = new KafkaPubTool();
$rdkafkaIni = Configure::read('Plugin.Kafka_rdkafka_config');
$kafkaConf = array();
if (!empty($rdkafkaIni)) {
$kafkaConf = parse_ini_file($rdkafkaIni);
}
$brokers = Configure::read('Plugin.Kafka_brokers');
$kafkaPubTool->initTool($brokers, $kafkaConf);
$this->loadedKafkaPubTool = $kafkaPubTool;
} }
return $this->loadedKafkaPubTool; return $this->loadedKafkaPubTool;
} }
public function loadKafkaPubTool()
{
App::uses('KafkaPubTool', 'Tools');
$kafkaPubTool = new KafkaPubTool();
$rdkafkaIni = Configure::read('Plugin.Kafka_rdkafka_config');
$kafkaConf = array();
if (!empty($rdkafkaIni)) {
$kafkaConf = parse_ini_file($rdkafkaIni);
}
$brokers = Configure::read('Plugin.Kafka_brokers');
$kafkaPubTool->initTool($brokers, $kafkaConf);
$this->loadedKafkaPubTool = $kafkaPubTool;
return true;
}
public function publishKafkaNotification($topicName, $data, $action = false) public function publishKafkaNotification($topicName, $data, $action = false)
{ {
$kafkaTopic = $this->kafkaTopic($topicName); $kafkaTopic = $this->kafkaTopic($topicName);
@ -2649,15 +2642,6 @@ class AppModel extends Model
} }
} }
public function getRowCount($table = false)
{
if (empty($table)) {
$table = $this->table;
}
$table_data = $this->query("show table status like '" . $table . "'");
return $table_data[0]['TABLES']['Rows'];
}
public function benchmarkCustomAdd($valueToAdd = 0, $name = 'default', $customName = 'custom') public function benchmarkCustomAdd($valueToAdd = 0, $name = 'default', $customName = 'custom')
{ {
if (empty($this->__profiler[$name]['custom'][$customName])) { if (empty($this->__profiler[$name]['custom'][$customName])) {
@ -2720,9 +2704,8 @@ class AppModel extends Model
{ {
static $versionArray; static $versionArray;
if ($versionArray === null) { if ($versionArray === null) {
$file = new File(ROOT . DS . 'VERSION.json'); $content = FileAccessTool::readFromFile(ROOT . DS . 'VERSION.json');
$versionArray = $this->jsonDecode($file->read()); $versionArray = JsonTool::decode($content);
$file->close();
} }
return $versionArray; return $versionArray;
} }
@ -2923,14 +2906,14 @@ class AppModel extends Model
$this->Log = ClassRegistry::init('Log'); $this->Log = ClassRegistry::init('Log');
$this->Log->create(); $this->Log->create();
$entry = array( $entry = array(
'org' => 'SYSTEM', 'org' => 'SYSTEM',
'model' => 'Server', 'model' => 'Server',
'model_id' => 0, 'model_id' => 0,
'email' => 'SYSTEM', 'email' => 'SYSTEM',
'action' => 'update_database', 'action' => 'update_database',
'user_id' => 0, 'user_id' => 0,
'title' => 'Bumped the timestamps of locked events containing object references.', 'title' => 'Bumped the timestamps of locked events containing object references.',
'change' => sprintf('Event timestamps updated: %s; Object timestamps updated: %s', count($event_ids), count($object_ids)) 'change' => sprintf('Event timestamps updated: %s; Object timestamps updated: %s', count($event_ids), count($object_ids))
); );
$this->Log->save($entry); $this->Log->save($entry);
} }
@ -3088,10 +3071,7 @@ class AppModel extends Model
$message .= "\n"; $message .= "\n";
do { do {
$message .= sprintf("[%s] %s", $message .= sprintf("[%s] %s", get_class($exception), $exception->getMessage());
get_class($exception),
$exception->getMessage()
);
$message .= "\nStack Trace:\n" . $exception->getTraceAsString(); $message .= "\nStack Trace:\n" . $exception->getTraceAsString();
$exception = $exception->getPrevious(); $exception = $exception->getPrevious();
} while ($exception !== null); } while ($exception !== null);

View File

@ -1,9 +1,7 @@
<?php <?php
App::uses('AppModel', 'Model'); App::uses('AppModel', 'Model');
App::uses('CakeEmail', 'Network/Email'); App::uses('CakeEmail', 'Network/Email');
App::uses('FileAccessTool', 'Tools');
App::uses('AttachmentTool', 'Tools'); App::uses('AttachmentTool', 'Tools');
App::uses('JsonTool', 'Tools');
App::uses('TmpFileTool', 'Tools'); App::uses('TmpFileTool', 'Tools');
App::uses('SendEmailTemplate', 'Tools'); App::uses('SendEmailTemplate', 'Tools');

View File

@ -2,7 +2,6 @@
App::uses('AppModel', 'Model'); App::uses('AppModel', 'Model');
App::uses('RandomTool', 'Tools'); App::uses('RandomTool', 'Tools');
App::uses('TmpFileTool', 'Tools'); App::uses('TmpFileTool', 'Tools');
App::uses('FileAccessTool', 'Tools');
App::uses('AttributeValidationTool', 'Tools'); App::uses('AttributeValidationTool', 'Tools');
class Feed extends AppModel class Feed extends AppModel

View File

@ -2,8 +2,6 @@
App::uses('AppModel', 'Model'); App::uses('AppModel', 'Model');
App::uses('GpgTool', 'Tools'); App::uses('GpgTool', 'Tools');
App::uses('ServerSyncTool', 'Tools'); App::uses('ServerSyncTool', 'Tools');
App::uses('FileAccessTool', 'Tools');
App::uses('JsonTool', 'Tools');
App::uses('SystemSetting', 'Model'); App::uses('SystemSetting', 'Model');
/** /**

View File

@ -72,9 +72,4 @@ class Template extends AppModel
return false; return false;
} }
} }
public function generateRandomFileName()
{
return (new RandomTool())->random_str(false, 12);
}
} }