Merge updated objects from sa. Implements caching of objects and fixes logged errors when using the default storage group. Fixes #2209

pull/2215/head
Isaac Connor 2018-09-15 09:42:59 -04:00
parent fa55cec12c
commit b0c555599c
5 changed files with 65 additions and 26 deletions

View File

@ -194,12 +194,12 @@ private $defaults = array(
}
if ( isset($options['limit']) ) {
if ( is_integer($options['limit']) or ctype_digit($options['limit']) ) {
$sql .= ' LIMIT ' . $limit;
$sql .= ' LIMIT ' . $options['limit'];
} else {
$backTrace = debug_backtrace();
$file = $backTrace[1]['file'];
$line = $backTrace[1]['line'];
Error("Invalid value for limit($limit) passed to Control::find from $file:$line");
Error("Invalid value for limit(".$options['limit'].") passed to Control::find from $file:$line");
return;
}
}

View File

@ -84,7 +84,10 @@ class Event {
}
public function Monitor() {
return new Monitor( isset($this->{'MonitorId'}) ? $this->{'MonitorId'} : NULL );
if ( isset($this->{'MonitorId'}) ) {
return Monitor::find_one(array('Id'=>$this->{MonitorId}));
}
return new Monitor();
}
public function __call( $fn, array $args){
@ -94,10 +97,14 @@ class Event {
if ( array_key_exists( $fn, $this ) ) {
return $this->{$fn};
$backTrace = debug_backtrace();
$file = $backTrace[1]['file'];
$line = $backTrace[1]['line'];
Warning("Unknown function call Event->$fn from $file:$line");
$backTrace = debug_backtrace();
$file = $backTrace[0]['file'];
$line = $backTrace[0]['line'];
Warning("Unknown function call Event->$fn from $file:$line");
$file = $backTrace[1]['file'];
$line = $backTrace[1]['line'];
Warning("Unknown function call Event->$fn from $file:$line");
Warning(print_r( $this, true ));
}
}
@ -246,7 +253,7 @@ class Event {
if ( is_null($new) or ( $new != '' ) ) {
$this->{'DiskSpace'} = $new;
}
if ( null === $this->{'DiskSpace'} ) {
if ( (!array_key_exists('DiskSpace',$this)) or (null === $this->{'DiskSpace'}) ) {
$this->{'DiskSpace'} = folder_size($this->Path());
dbQuery('UPDATE Events SET DiskSpace=? WHERE Id=?', array($this->{'DiskSpace'}, $this->{'Id'}));
}
@ -498,7 +505,6 @@ class Event {
}
public static function find( $parameters = null, $options = null ) {
$filters = array();
$sql = 'SELECT * FROM Events ';
$values = array();
@ -526,20 +532,21 @@ class Event {
}
if ( isset($options['limit']) ) {
if ( is_integer($options['limit']) or ctype_digit($options['limit']) ) {
$sql .= ' LIMIT ' . $limit;
$sql .= ' LIMIT ' . $options['limit'];
} else {
$backTrace = debug_backtrace();
$file = $backTrace[1]['file'];
$line = $backTrace[1]['line'];
Error("Invalid value for limit($limit) passed to Event::find from $file:$line");
Error("Invalid value for limit(".$options['limit'].") passed to Event::find from $file:$line");
return array();
}
}
}
$filters = array();
$result = dbQuery($sql, $values);
$results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Event');
foreach ( $results as $row => $obj ) {
$filters[] = $obj;
$results = $result->fetchALL();
foreach ( $results as $row ) {
$filters[] = new Event($row);
}
return $filters;
}

View File

@ -85,12 +85,12 @@ class Group {
}
if ( isset($options['limit']) ) {
if ( is_integer($options['limit']) or ctype_digit($options['limit']) ) {
$sql .= ' LIMIT ' . $limit;
$sql .= ' LIMIT ' . $options['limit'];
} else {
$backTrace = debug_backtrace();
$file = $backTrace[1]['file'];
$line = $backTrace[1]['line'];
Error("Invalid value for limit($limit) passed to Group::find from $file:$line");
Error("Invalid value for limit(".$options['limit'].") passed to Group::find from $file:$line");
return array();
}
}

View File

@ -1,6 +1,8 @@
<?php
require_once('database.php');
$server_cache = array();
class Server {
private $defaults = array(
'Id' => null,
@ -10,7 +12,10 @@ class Server {
'zmstats' => 1,
'zmtrigger' => 0,
);
public function __construct( $IdOrRow = NULL ) {
global $server_cache;
$row = NULL;
if ( $IdOrRow ) {
if ( is_integer($IdOrRow) or ctype_digit($IdOrRow) ) {
@ -26,6 +31,7 @@ class Server {
foreach ($row as $k => $v) {
$this->{$k} = $v;
}
$server_cache[$row['Id']] = $this;
} else {
$this->{'Name'} = '';
$this->{'Hostname'} = '';
@ -92,12 +98,12 @@ class Server {
}
if ( isset($options['limit']) ) {
if ( is_integer($options['limit']) or ctype_digit($options['limit']) ) {
$sql .= ' LIMIT ' . $limit;
$sql .= ' LIMIT ' . $options['limit'];
} else {
$backTrace = debug_backtrace();
$file = $backTrace[1]['file'];
$line = $backTrace[1]['line'];
Error("Invalid value for limit($limit) passed to Server::find from $file:$line");
Error("Invalid value for limit(".$options['limit'].") passed to Server::find from $file:$line");
return array();
}
}
@ -110,6 +116,13 @@ class Server {
}
public static function find_one( $parameters = array() ) {
global $server_cache;
if (
( count($parameters) == 1 ) and
isset($parameters['Id']) and
isset($server_cache[$parameters['Id']]) ) {
return $server_cache[$parameters['Id']];
}
$results = Server::find( $parameters, array('limit'=>1) );
if ( ! sizeof($results) ) {
return;

View File

@ -3,15 +3,27 @@ require_once('database.php');
$storage_cache = array();
class Storage {
private $defaults = array(
'Id' => null,
'Path' => '',
'Name' => '',
'Type' => 'local',
'Url' => '',
'DiskSpace' => null,
'Scheme' => 'Medium',
'ServerId' => 0,
'DoDelete' => 1,
);
public function __construct( $IdOrRow = NULL ) {
global $storage_cache;
$row = NULL;
if ( $IdOrRow ) {
if ( is_integer( $IdOrRow ) or is_numeric( $IdOrRow ) ) {
$row = dbFetchOne( 'SELECT * FROM Storage WHERE Id=?', NULL, array( $IdOrRow ) );
if ( is_integer($IdOrRow) or is_numeric($IdOrRow) ) {
$row = dbFetchOne('SELECT * FROM Storage WHERE Id=?', NULL, array($IdOrRow));
if ( ! $row ) {
Error("Unable to load Storage record for Id=" . $IdOrRow );
Error('Unable to load Storage record for Id=' . $IdOrRow);
}
} else if ( is_array($IdOrRow) ) {
$row = $IdOrRow;
@ -59,7 +71,13 @@ class Storage {
if ( array_key_exists($fn, $this) )
return $this->{$fn};
if ( array_key_exists( $fn, $this->defaults ) )
return $this->defaults{$fn};
$backTrace = debug_backtrace();
$file = $backTrace[0]['file'];
$line = $backTrace[0]['line'];
Warning("Unknown function call Storage->$fn from $file:$line");
$file = $backTrace[1]['file'];
$line = $backTrace[1]['line'];
Warning("Unknown function call Storage->$fn from $file:$line");
@ -73,6 +91,7 @@ class Storage {
isset($storage_cache[$parameters['Id']]) ) {
return $storage_cache[$parameters['Id']];
}
$results = Storage::find($parameters, $options);
if ( count($results) > 1 ) {
Error("Storage Returned more than 1");
@ -111,12 +130,12 @@ class Storage {
} # end if options
if ( isset($options['limit']) ) {
if ( is_integer($options['limit']) or ctype_digit($options['limit']) ) {
$sql .= ' LIMIT ' . $limit;
$sql .= ' LIMIT ' . $option['limit'];
} else {
$backTrace = debug_backtrace();
$file = $backTrace[1]['file'];
$line = $backTrace[1]['line'];
Error("Invalid value for limit($limit) passed to Control::find from $file:$line");
Error("Invalid value for limit(".$options['limit'].") passed to Control::find from $file:$line");
return array();
}
} # end if limit
@ -124,9 +143,9 @@ class Storage {
$storage_areas = array();
$result = dbQuery($sql, $values);
if ( $result ) {
$results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Storage');
foreach ( $results as $row => $obj ) {
$storage_areas[] = $obj;
$results = $result->fetchALL();
foreach ( $results as $row ) {
$storage_areas[] = new Storage($row);
}
}
return $storage_areas;