BUGFIX: Print an error message if a parameter does not have
[gnucomo.git] / src / gcm_daemon / gcm_daemon.php
index 3be6fe2..edfa844 100755 (executable)
    First       : November 8th 2002
    Gnucomo-0.0.3: December 6th 2002
 
+ $Log: gcm_daemon.php,v $
+ Revision 1.12  2003-08-05 07:46:37  arjen
+ BUGFIX: Print an error message if a parameter does not have
+ any history.
+
+ Revision 1.11  2003/07/09 07:25:02  arjen
+ Gcm_daemon gathers statistics on parameters, notifications, etc. for all objects.
+
+ Revision 1.10  2003/03/29 08:33:58  arjen
+ In phpclasses/db.class.php: Added the database connection string as
+ an argument to the function copy_db_class.
+ Fixed the PHP member function db::db_connect(). The Postgres connection
+ string is now passed as an argument to that function.
+
+ Revision 1.9  2003/02/21 08:37:59  arjen
+ Added new table to the database: log_adv_daemon_email.
+
+
 */
 
-// $Id: gcm_daemon.php,v 1.8 2003-02-16 08:24:06 arjen Exp $
+// $Id: gcm_daemon.php,v 1.12 2003-08-05 07:46:37 arjen Exp $
 
 ini_set('include_path', '.:./classes:../phpclasses');
 
@@ -37,10 +55,10 @@ require_once "gnucomo.process_log.php";
 $project_name  = "gnucomo";    //name of the entire project
 $app_name      = "gcm_daemon"; //name of the application running
 $developrelease = "TRUE";      //Indicates if special debug settings are needed
-$db_version    = 31;           //The db_version indicates what the level of 
+$db_version    = 42;           //The db_version indicates what the level of 
                                //the database should be. If the database is 
                                //old an update will be generated.
-$gcmd_version   = 3;           //This value indicates the active version of the gcm_daemon,
+$gcmd_version   = 5;           //This value indicates the active version of the gcm_daemon,
                                //which is saved in the database. Log records that were not
                                //recognized before will now be recognized. The version doesn't
                                //mean anything in the overall gnucomo project.
@@ -52,7 +70,6 @@ set_time_limit(0);
 // Read the database settings //
 $class_settings = new gnucomo_config();
 $class_settings->read($project_name);
-$class_settings->database();
 
 //Open an connection to the database
 $dbms_type = $class_settings->find_parameter("database", "type");
@@ -67,13 +84,13 @@ $dbms->db_host = $dbms_host;
 $dbms->db_name = $dbms_name;
 $dbms->db_user = $dbms_user;
 $dbms->db_password = $dbms_password;
-$dbms->db_connect();
+$dbms->db_connect($class_settings->database());
 
 if ($dbms->have_db_connection() == "FALSE") {
   exit ("Database connection failed.");
 } else {
   //The database connection has been made.
-  $dbms_working = copy_db_class($dbms);
+  $dbms_working = copy_db_class($dbms, $class_settings->database());
 }
 
 //Verify if the database is up-to-date by checking the versionnumber
@@ -118,9 +135,20 @@ if ($dbms->fetch_row() == "TRUE") {
 
 do {
 
+   //  Gather the statistics for each object
+
+   $obj_result = $dbms->query("SELECT objectid FROM object");
+   for ($obj = 0; $obj < $dbms->num_rows($obj_result); $obj++)
+   {
+      $object = $dbms->fetch_object($obj_result, $obj);
+      echo "Gathering statistics for object " . $object->objectid . "\n";
+      GatherStatistics($object->objectid);
+   }
+
   //At this place we start processing new log-lines 
   process_log ();
-  notificationstats();
+  //notificationstats();  //  This function is obsolete
+  find_notifications();
 
   $keep_running = 'FALSE';
 
@@ -140,6 +168,7 @@ function process_log () {
   */
   global $dbms;
   global $dbms_working;
+  global $class_settings;
 
   //Find records in log that still have to be processed.
 
@@ -155,8 +184,8 @@ function process_log () {
   $dbms->query($local_sql);
 
   //Update the log-statistics in the object-table 
-  $local_statistics_db = copy_db_class($dbms);
-  $local_findobject_db = copy_db_class($dbms);
+  $local_statistics_db = copy_db_class($dbms, $class_settings->database());
+  $local_findobject_db = copy_db_class($dbms, $class_settings->database());
 
   //Make totals 
   $local_upper_row = $dbms->num_rows() + $last_log + 1;
@@ -178,7 +207,7 @@ function process_log () {
   if ($dbms->num_rows() > 0) {
 
     //Create a database connection for changes in the database.
-    $dbms_changes = copy_db_class($dbms);
+    $dbms_changes = copy_db_class($dbms, $class_settings->database());
     if ($dbms_changes->have_db_connection() == 'TRUE') {
 
        $local_sql               = 0 ;     
@@ -245,7 +274,94 @@ function process_log () {
 
 }
 
-function notificationstats () {
+/*
+ *   Update a single statistic for some object.
+ *   If it does not yet exist, it will be created.
+ */
+
+function UpdateStatistic($objectid, $name, $value)
+{
+   global $dbms;
+
+   $result = $dbms->query("SELECT objectid FROM object_statistics WHERE
+             objectid='$objectid' AND statname='$name'");
+   if ($dbms->num_rows() == 0)
+   {
+      $dbms->query("INSERT INTO object_statistics VALUES
+                    ('$objectid', '$name', '$value')");
+   }
+   else
+   {
+      $dbms->query("UPDATE object_statistics SET statvalue='$value' WHERE
+           statname='$name' AND objectid='$objectid'");
+   }
+}
+
+/*
+ *   Gather the statistics for a single object ($objectid).
+ *   We count the number of parameters, removed parameters, notifications
+ *   closed notifications and log entries. The totals of these are
+ *   maintained in a separate table: object_statistics.
+ */
+
+function GatherStatistics($objectid)
+{
+   global $dbms;
+
+   //  Gather statistics on parameters
+
+   $r = $dbms->query("SELECT paramid FROM parameter WHERE objectid=CAST('"
+                        . $objectid . "' AS BIGINT)");
+   $nr_parameters = $dbms->num_rows($r);
+
+   $removed_parameters = 0;
+   for ($p = 0; $p < $nr_parameters; $p++)
+   {
+      $param = pg_fetch_object($r, $p);
+      $qry ="select change_nature from history where paramid= CAST('";
+      $qry .= $param->paramid . "' AS BIGINT) order by modified desc";
+      $rhist = $dbms->query($qry);
+      if ($dbms->num_rows($rhist) == 0)
+      {
+         echo "ERROR: No history for parameter id " . $param->paramid . "\n";
+      }
+      else
+      {
+         $hist = $dbms->fetch_object($rhist, 0);
+         if ($hist->change_nature == "REMOVED")
+         {
+            $removed_parameters++;
+         }
+      }
+   }
+
+   UpdateStatistic($objectid, 'parameters', $nr_parameters);
+   UpdateStatistic($objectid, 'removed_parameters', $removed_parameters);
+
+   //  Gather statistics on notifications
+
+   $r = $dbms->query("SELECT count(notificationid) FROM notification WHERE
+                       objectid = CAST('" . $objectid . "' AS BIGINT)");
+   $cnt = $dbms->fetch_object($r, 0);
+   UpdateStatistic($objectid, 'notifications', $cnt->count);
+
+   $r = $dbms->query("SELECT count(notificationid) FROM notification WHERE
+                       objectid = CAST('" . $objectid . "' AS BIGINT) AND statuscode ='cls'");
+   $cnt = $dbms->fetch_object($r, 0);
+   UpdateStatistic($objectid, 'closed_notifications', $cnt->count);
+
+   //  Gather statistics on log entries
+
+   $r = $dbms->query("SELECT count(logid) FROM log WHERE
+                       objectid = CAST('" . $objectid . "' AS BIGINT)");
+   $cnt = $dbms->fetch_object($r, 0);
+   UpdateStatistic($objectid, 'logs', $cnt->count);
+}
+
+function notificationstats ()
+{
+
+  //    OBSOLETE   //
 
 /* This routine will determine how many new notifications have arrived and will
  * update the statistics in the object-table to keep the performance acceptable
@@ -253,12 +369,12 @@ function notificationstats () {
  * OUTPUT : NONE
  */
            
- global $dbms;
+ global $dbms, $class_settings;
 
  //Find records in log that still have to be processed.
  $local_sql = "SELECT setting_value FROM db_value WHERE setting = 'last_notification'";
  $dbms->query($local_sql);
- $local_dbms = copy_db_class($dbms);
+ $local_dbms = copy_db_class($dbms, $class_settings->database());
 
  //Determine the last notification
  if ($dbms->fetch_row() == "TRUE") {
@@ -297,5 +413,25 @@ function notificationstats () {
  } 
 }
 
+function find_notifications () {
+
+/*
+ *  Do something with notification checks.
+ *
+ * INPUT  : NONE
+ * OUTPUT : NONE
+ */
+    
+ global $dbms;
+
+ //Find checks that have to be executed.
+ $local_sql = "select * from notification_check where age(last_execution) > time_between_executions";
+ $dbms->query($local_sql);
+ for ($i=0; $i<$dbms->num_rows(); $i++) {
+     //A check has been found that has to be executed 
+     $dbms->fetch_row();
+ }
+}
+
 ?>