From 979fcceb99e555800fdfbc2cecf4ba585e780b73 Mon Sep 17 00:00:00 2001 From: osmiy Date: Wed, 19 Apr 2006 14:47:21 +0000 Subject: - code optimization (Eugene) git-svn-id: svn://svn.zabbix.com/trunk@2744 97f52cf1-0a1b-0410-bd0e-c28be96e8082 --- frontends/php/include/classes/ccombobox.inc.php | 2 +- frontends/php/include/classes/ctable.inc.php | 7 + frontends/php/include/classes/ctag.inc.php | 20 +-- frontends/php/include/classes/ctextarea.inc.php | 2 +- frontends/php/include/classes/ctextbox.inc.php | 2 +- frontends/php/include/config.inc.php | 175 +----------------------- frontends/php/include/copt.lib.php | 124 +++++++++++++++++ frontends/php/include/db.inc.php | 1 + frontends/php/include/html.inc.php | 9 +- frontends/php/include/items.inc.php | 12 +- frontends/php/include/triggers.inc.php | 61 ++++++++- frontends/php/include/validate.inc.php | 6 +- 12 files changed, 217 insertions(+), 204 deletions(-) create mode 100644 frontends/php/include/copt.lib.php (limited to 'frontends/php/include') diff --git a/frontends/php/include/classes/ccombobox.inc.php b/frontends/php/include/classes/ccombobox.inc.php index 69a17636..ea579803 100644 --- a/frontends/php/include/classes/ccombobox.inc.php +++ b/frontends/php/include/classes/ccombobox.inc.php @@ -141,7 +141,7 @@ function Show() { if(isset($this->caption)) - print ($this->caption." "); + echo $this->caption." "; parent::Show(); } } diff --git a/frontends/php/include/classes/ctable.inc.php b/frontends/php/include/classes/ctable.inc.php index e82e5429..b0d5485a 100644 --- a/frontends/php/include/classes/ctable.inc.php +++ b/frontends/php/include/classes/ctable.inc.php @@ -242,5 +242,12 @@ return parent::AddItem($value); } +/* function Show() + { + ob_start(); + parent::Show(); + ob_end_flush(); + } +*/ } ?> diff --git a/frontends/php/include/classes/ctag.inc.php b/frontends/php/include/classes/ctag.inc.php index 9ddc0b20..82caa5b8 100644 --- a/frontends/php/include/classes/ctag.inc.php +++ b/frontends/php/include/classes/ctag.inc.php @@ -176,25 +176,25 @@ /* protected */ function ShowTagStart() { - print ($this->tag_start); - print("<".$this->name); + echo $this->tag_start; + echo "<".$this->name; foreach($this->options as $key => $value) { - print (" $key=\"$value\""); + echo " $key=\"$value\""; } if($this->paired=='yes') - print (">"); + echo ">"; else - print ("/>"); + echo "/>"; - print ($this->tag_body_start); + echo $this->tag_body_start; } function ShowTagItem(&$item) { if(is_null($item)) return; elseif(is_object($item))$item->Show(); - else print (strval($item)); + else echo strval($item); } function ShowTagBody() { @@ -203,12 +203,12 @@ } function ShowTagEnd() { - print ($this->tag_body_end); + echo $this->tag_body_end; if($this->paired=='yes') { - print ("name.">"); - print ($this->tag_end); + echo "name.">"; + echo $this->tag_end; } } function SetEnabled($value='yes') diff --git a/frontends/php/include/classes/ctextarea.inc.php b/frontends/php/include/classes/ctextarea.inc.php index e9e75530..5ea51f70 100644 --- a/frontends/php/include/classes/ctextarea.inc.php +++ b/frontends/php/include/classes/ctextarea.inc.php @@ -39,7 +39,7 @@ function Show() { if(isset($this->caption)) - print ($this->caption." "); + echo $this->caption." "; parent::Show(); } function SetName($value='textarea') diff --git a/frontends/php/include/classes/ctextbox.inc.php b/frontends/php/include/classes/ctextbox.inc.php index 5629f2b6..6345b342 100644 --- a/frontends/php/include/classes/ctextbox.inc.php +++ b/frontends/php/include/classes/ctextbox.inc.php @@ -37,7 +37,7 @@ function Show() { if(isset($this->caption)) - print ($this->caption." "); + echo $this->caption." "; parent::Show(); } function SetReadonly($value='yes') diff --git a/frontends/php/include/config.inc.php b/frontends/php/include/config.inc.php index 3c8924f0..ac694a90 100644 --- a/frontends/php/include/config.inc.php +++ b/frontends/php/include/config.inc.php @@ -22,6 +22,8 @@ function SDI($msg="SDI") { echo "DEBUG INFO: $msg ".BR; } // DEBUG INFO!!! ?> 10 to something like localhost:procload.last(0)>10 - - function explode_exp ($expression, $html) - { -# echo "EXPRESSION:",$expression,"
"; - - $functionid=''; - $exp=''; - $state=''; - for($i=0;$i".$row1["host"].":".$row1["key_"].".".$row1["function"]."(".$row1["parameter"].")}"; - } - else - { - $exp=$exp."{".$row1["host"].":".$row1["key_"].".".$row1["function"]."(".$row1["parameter"].")}"; - } - } - continue; - } - if($state == "FUNCTIONID") - { - $functionid=$functionid.$expression[$i]; - continue; - } - $exp=$exp.$expression[$i]; - } -# echo "EXP:",$exp,"
"; - return $exp; - } - - # Translate localhost:procload.last(0)>10 to {12}>10 - - /*function implode_exp ($expression, $triggerid) - { -// echo "Expression:$expression
"; - $exp=''; - $state=""; - for($i=0;$i"; -// echo "KEY:$key
"; -// echo "FUNCTION:$function
"; -// echo "PARAMETER:$parameter
"; - $state=''; - - $sql="select i.itemid from items i,hosts h where i.key_=".zbx_dbstr($key). - " and h.host=".zbx_dbstr($host)." and h.hostid=i.hostid"; -# echo $sql,"
"; - $res=DBselect($sql); - $row=DBfetch($res); - - $itemid=$row["itemid"]; -# echo "ITEMID:$itemid
"; - - $sql="insert into functions (itemid,triggerid,function,parameter)". - " values ($itemid,$triggerid,".zbx_dbstr($function).",". - zbx_dbstr($parameter).")"; -# echo $sql,"
"; - $res=DBexecute($sql); - if(!$res) - { -# echo "ERROR
"; - return $res; - } - $functionid=DBinsert_id($res,"functions","functionid"); - - $exp=$exp.'{'.$functionid.'}'; - - continue; - } - if($expression[$i] == '(') - { - if($state == "FUNCTION") - { - $state='PARAMETER'; - continue; - } - } - if($expression[$i] == ')') - { - if($state == "PARAMETER") - { - $state=''; - continue; - } - } - if(($expression[$i] == ':') && ($state == "HOST")) - { - $state="KEY"; - continue; - } - if($expression[$i] == '.') - { - if($state == "KEY") - { - $state="FUNCTION"; - continue; - } - // Support for '.' in KEY - if($state == "FUNCTION") - { - $state="FUNCTION"; - $key=$key.".".$function; - $function=""; - continue; - } - } - if($state == "HOST") - { - $host=$host.$expression[$i]; - continue; - } - if($state == "KEY") - { - $key=$key.$expression[$i]; - continue; - } - if($state == "FUNCTION") - { - $function=$function.$expression[$i]; - continue; - } - if($state == "PARAMETER") - { - $parameter=$parameter.$expression[$i]; - continue; - } - $exp=$exp.$expression[$i]; - } - return $exp; - }*/ - function add_image($name,$imagetype,$file) { if(!is_null($file)) @@ -1975,6 +1811,9 @@ function SDI($msg="SDI") { echo "DEBUG INFO: $msg ".BR; } // DEBUG INFO!!! "page_footer_r") )); $table->Show(); + + COpt::profiling_stop("page"); + echo "\n"; echo "\n"; } diff --git a/frontends/php/include/copt.lib.php b/frontends/php/include/copt.lib.php new file mode 100644 index 00000000..aaebaab5 --- /dev/null +++ b/frontends/php/include/copt.lib.php @@ -0,0 +1,124 @@ + 1, "oranges" => 1, "mangoes" => 1, "tomatoes" => 1, "pickles" => 1); + if (isset($keys['mangoes'])) { ... } + + 6)key_exist + if(array_key_exists('mangoes', $keys)) + vs + if (isset($keys['mangoes'])) { ... } + + 7)regexps + POSIX-regexps + vs + Perl-regexps + + 8)constants + UPPER case constans (TRUE, FALSE) + vs + lower case constans (true, false) + + 9)for + for ($i = 0; $i < FUNCTION($j); $i++) {...} + vs + for ($i = 0, $k = FUNCTION($j); $i < $k; $i = $i + 1) {...} + + 10)strings + "var=$var" + vs + 'var='.$var + + */ + + + /* + ** Description: + ** Optimization class. Provide functions for + ** PHP code optimization. + ** + ** Author: + ** Eugene Grigorjev (eugene.grigorjev@zabbix.com) + **/ + + //define("USE_PROFILING",1); + + $starttime[]=array(); + + class COpt + { + /* protected static $starttime[]=array(); */ + + /* protected static */ function getmicrotime() + { + list($usec, $sec) = explode(" ",microtime()); + return ((float)$usec + (float)$sec); + } + /* public static */ function profiling_start($type=NULL) + { + if(!defined('USE_PROFILING')) return; + + global $starttime; + if(is_null($type)) $type="global"; + + $starttime[$type] = COpt::getmicrotime(); + } + /* public static */ function profiling_stop($type=NULL) + { + if(!defined('USE_PROFILING')) return; + + global $starttime; + + $endtime = COpt::getmicrotime(); + + if(is_null($type)) $type="global"; + echo "
\nTime to execute (".$type."): ". bcsub($endtime,$starttime[$type],6)." seconds!\n
"; + } + } + +?> diff --git a/frontends/php/include/db.inc.php b/frontends/php/include/db.inc.php index 6b6df889..ae1a5c42 100644 --- a/frontends/php/include/db.inc.php +++ b/frontends/php/include/db.inc.php @@ -27,6 +27,7 @@ $DB_SERVER ="localhost"; // $DB_DATABASE ="zabbix"; $DB_DATABASE ="osmiy"; +// $DB_DATABASE ="demo"; $DB_USER ="root"; $DB_PASSWORD =""; // END OF DATABASE CONFIGURATION diff --git a/frontends/php/include/html.inc.php b/frontends/php/include/html.inc.php index 277668b7..61ec6dca 100644 --- a/frontends/php/include/html.inc.php +++ b/frontends/php/include/html.inc.php @@ -23,13 +23,6 @@ define("SPACE"," "); define("RARR","⇒"); - function do_vertival_text($str) - { - for($i=0,$out = ""; $iSetHeader($header,"vertical_header"); - $db_items = DBselect("select distinct i.description,h.hostid from hosts h,items i $group_where". + $db_items = DBselect("select distinct i.description from hosts h,items i $group_where". " h.status=".HOST_STATUS_MONITORED." and h.hostid=i.hostid and i.status=".ITEM_STATUS_ACTIVE. " order by 1"); while($item = DBfetch($db_items)) { - if(!check_right("Host","R",$row["hostid"])) continue; //TODO optimize duplication check !!!! see top - $table_row = array(nbsp($item["description"])); + $host_added = 0; foreach($hosts as $hostid) { $db_host_items = DBselect("select itemid,value_type,lastvalue,units from items where". @@ -502,6 +501,7 @@ if(!check_right("Item","R",$host_item["itemid"])) continue; + ++$host_added; // added corect host item; if(!isset($host_item["lastvalue"])) { array_push($table_row,"-"); @@ -521,7 +521,7 @@ array_push($table_row,new CCol(nbsp($value),$style)); } - $table->AddRow($table_row); + if($host_added > 0) $table->AddRow($table_row); } return $table; } diff --git a/frontends/php/include/triggers.inc.php b/frontends/php/include/triggers.inc.php index 393fc77e..ec7e7873 100644 --- a/frontends/php/include/triggers.inc.php +++ b/frontends/php/include/triggers.inc.php @@ -97,7 +97,7 @@ $state=""; $host=""; $hosts=array(); - for($i=0;$i10 to something like localhost:procload.last(0)>10 + + function explode_exp ($expression, $html) + { +# echo "EXPRESSION:",$expression,"
"; + + $functionid=''; + $exp=''; + $state=''; + for($i=0,$max=strlen($expression); $i<$max; $i++) + { + if($expression[$i] == '{') + { + $functionid=''; + $state='FUNCTIONID'; + continue; + } + if($expression[$i] == '}') + { + $state=''; + $sql="select h.host,i.key_,f.function,f.parameter,i.itemid from items i,functions f,hosts h where functionid=$functionid and i.itemid=f.itemid and h.hostid=i.hostid"; + $res1=DBselect($sql); + $row1=DBfetch($res1); + if($html == 0) + { + $exp=$exp."{".$row1["host"].":".$row1["key_"].".".$row1["function"]."(".$row1["parameter"].")}"; + } + else + { + $item=get_item_by_itemid($row1["itemid"]); + if($item["value_type"] ==0) + { + $exp=$exp."{".$row1["host"].":".$row1["key_"].".".$row1["function"]."(".$row1["parameter"].")}"; + } + else + { + $exp=$exp."{".$row1["host"].":".$row1["key_"].".".$row1["function"]."(".$row1["parameter"].")}"; + } + } + continue; + } + if($state == "FUNCTIONID") + { + $functionid=$functionid.$expression[$i]; + continue; + } + $exp=$exp.$expression[$i]; + } +# echo "EXP:",$exp,"
"; + return $exp; + } + function implode_exp ($expression, $triggerid) # Translate localhost:procload.last(0)>10 to {12}>10 { // echo "Expression:$expression
"; $exp=''; $state=""; - for($i=0;$iSetHeader($header,"vertical_header"); - $db_triggers = DBselect("select distinct t.description,h.hostid from hosts h,items i,triggers t,functions f $group_where". + $db_triggers = DBselect("select distinct t.description from hosts h,items i,triggers t,functions f $group_where". " h.status=".HOST_STATUS_MONITORED." and h.hostid=i.hostid and i.itemid=f.itemid and f.triggerid=t.triggerid". " and t.status=".TRIGGER_STATUS_ENABLED. " group by 1"); while($triggers = DBfetch($db_triggers)) { - if(!check_right("Host","R",$row["hostid"])) continue; //TODO optimize duplication check !!!! see top $table_row = array(nbsp($triggers["description"])); foreach($hosts as $hostid) { diff --git a/frontends/php/include/validate.inc.php b/frontends/php/include/validate.inc.php index 0e8ba889..4517c221 100644 --- a/frontends/php/include/validate.inc.php +++ b/frontends/php/include/validate.inc.php @@ -67,9 +67,7 @@ $expression = str_replace('{'.$f.'}','$_REQUEST["'.$f.'"]',$expression); //$debug .= $f." = ".$_REQUEST[$f].BR; } - $expression=rtrim($expression,"&"); - if($expression[strlen($expression)-1]=='&') $expression[strlen($expression)-1]=0; - if($expression[strlen($expression)-1]=='&') $expression[strlen($expression)-1]=0; + $expression = trim($expression,"& "); $exec = "return (".$expression.") ? 1 : 0;"; $ret = eval($exec); @@ -269,7 +267,7 @@ { if($flags&P_SYS) { - info("Critical error. Incorrect value for [".$field."]"); + info("Critical error. Incorrect value for [".$field."] = '".$_REQUEST[$field]."'"); return ZBX_VALID_ERROR; } else -- cgit