Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ProcessMonitor.php

00001 <?php
00002 include_once(GALAXIA_LIBRARY.'/src/common/Base.php');
00005 
00008 class ProcessMonitor extends Base {
00009 
00010   function monitor_stats() {
00011     $res = Array();
00012     $res['active_processes'] = $this->getOne("select count(*) from `".GALAXIA_TABLE_PREFIX."processes` where `isActive`=?",array('y'));
00013     $res['processes'] = $this->getOne("select count(*) from `".GALAXIA_TABLE_PREFIX."processes`");
00014     $result = $this->query("select distinct(`pId`) from `".GALAXIA_TABLE_PREFIX."instances` where `status`=?",array('active'));
00015     $res['running_processes'] = $result->numRows();
00016     // get the number of instances per status
00017     $query = "select status, count(*) as num_instances from ".GALAXIA_TABLE_PREFIX."instances group by status";
00018     $result = $this->query($query);
00019     $status = array();
00020     while($info = $result->fetchRow()) {
00021       $status[$info['status']] = $info['num_instances'];
00022     }
00023     $res['active_instances'] = isset($status['active']) ? $status['active'] : 0;
00024     $res['completed_instances'] = isset($status['completed']) ? $status['completed'] : 0;
00025     $res['exception_instances'] = isset($status['exception']) ? $status['exception'] : 0;
00026     $res['aborted_instances'] = isset($status['aborted']) ? $status['aborted'] : 0;
00027     return $res;
00028   }
00029 
00030   function update_instance_status($iid,$status) {
00031     $query = "update `".GALAXIA_TABLE_PREFIX."instances` set `status`=? where `instanceId`=?";
00032     $this->query($query,array($status,$iid));
00033   }
00034 
00035   function update_instance_activity_status($iid,$activityId,$status) {
00036     $query = "update `".GALAXIA_TABLE_PREFIX."instance_activities` set `status`=? where `instanceId`=? and `activityId`=?";
00037     $this->query($query,array($status,$iid,$activityId));
00038   }
00039 
00040   function remove_instance($iid) {
00041     $query = "delete from `".GALAXIA_TABLE_PREFIX."workitems` where `instanceId`=?";
00042     $this->query($query,array($iid));
00043     $query = "delete from `".GALAXIA_TABLE_PREFIX."instance_activities` where `instanceId`=?";
00044     $this->query($query,array($iid));
00045     $query = "delete from `".GALAXIA_TABLE_PREFIX."instances` where `instanceId`=?";
00046     $this->query($query,array($iid));
00047   }
00048 
00049   function remove_aborted() {
00050     $query="select `instanceId` from `".GALAXIA_TABLE_PREFIX."instances` where `status`=?";
00051     $result = $this->query($query,array('aborted'));
00052     while($res = $result->fetchRow()) {
00053       $iid = $res['instanceId'];
00054       $query = "delete from `".GALAXIA_TABLE_PREFIX."instance_activities` where `instanceId`=?";
00055       $this->query($query,array($iid));
00056       $query = "delete from `".GALAXIA_TABLE_PREFIX."workitems` where `instanceId`=?";
00057       $this->query($query,array($iid));
00058     }
00059     $query = "delete from `".GALAXIA_TABLE_PREFIX."instances` where `status`=?";
00060     $this->query($query,array('aborted'));
00061   }
00062 
00063   function remove_all($pId) {
00064     $query="select `instanceId` from `".GALAXIA_TABLE_PREFIX."instances` where `pId`=?";
00065     $result = $this->query($query,array($pId));
00066     while($res = $result->fetchRow()) {
00067       $iid = $res['instanceId'];
00068       $query = "delete from `".GALAXIA_TABLE_PREFIX."instance_activities` where `instanceId`=?";
00069       $this->query($query,array($iid));
00070       $query = "delete from `".GALAXIA_TABLE_PREFIX."workitems` where `instanceId`=?";
00071       $this->query($query,array($iid));
00072     }
00073     $query = "delete from `".GALAXIA_TABLE_PREFIX."instances` where `pId`=?";
00074     $this->query($query,array($pId));
00075   }
00076 
00077 
00078   function monitor_list_processes($offset,$maxRecords,$sort_mode,$find,$where='') {
00079     $sort_mode = $this->convert_sortmode($sort_mode);
00080     if($find) {
00081       $findesc = '%'.$find.'%';
00082       $mid=" where ((name like ?) or (description like ?))";
00083       $bindvars = array($findesc,$findesc);
00084     } else {
00085       $mid="";
00086       $bindvars = array();
00087     }
00088     if($where) {
00089       if($mid) {
00090         $mid.= " and ($where) ";
00091       } else {
00092         $mid.= " where ($where) ";
00093       }
00094     }
00095     // get the requested processes
00096     $query = "select * from ".GALAXIA_TABLE_PREFIX."processes $mid order by $sort_mode";
00097     $query_cant = "select count(*) from ".GALAXIA_TABLE_PREFIX."processes $mid";
00098     $result = $this->query($query,$bindvars,$maxRecords,$offset);
00099     $cant = $this->getOne($query_cant,$bindvars);
00100     $ret = Array();
00101     while($res = $result->fetchRow()) {
00102       $pId = $res['pId'];
00103       // Number of active instances
00104       $res['active_instances'] = 0;
00105       // Number of exception instances
00106       $res['exception_instances'] = 0;
00107       // Number of completed instances
00108       $res['completed_instances'] = 0;
00109       // Number of aborted instances
00110       $res['aborted_instances'] = 0;
00111       $res['all_instances'] = 0;
00112       // Number of activities
00113       $res['activities'] = 0;
00114       $ret[$pId] = $res;
00115     }
00116     if (count($ret) < 1) {
00117       $retval = Array();
00118       $retval["data"] = $ret;
00119       $retval["cant"] = $cant;
00120       return $retval;
00121     }
00122     // get number of instances and timing statistics per process and status
00123     $query = "select pId, status, count(*) as num_instances,
00124               min(ended - started) as min_time, avg(ended - started) as avg_time, max(ended - started) as max_time
00125               from ".GALAXIA_TABLE_PREFIX."instances where pId in (" . join(', ', array_keys($ret)) . ") group by pId, status";
00126     $result = $this->query($query);
00127     while($res = $result->fetchRow()) {
00128       $pId = $res['pId'];
00129       if (!isset($ret[$pId])) continue;
00130       switch ($res['status']) {
00131         case 'active':
00132           $ret[$pId]['active_instances'] = $res['num_instances'];
00133           $ret[$pId]['all_instances'] += $res['num_instances'];
00134           break;
00135         case 'completed':
00136           $ret[$pId]['completed_instances'] = $res['num_instances'];
00137           $ret[$pId]['all_instances'] += $res['num_instances'];
00138           $ret[$pId]['duration'] = array('min' => $res['min_time'], 'avg' => $res['avg_time'], 'max' => $res['max_time']);
00139           break;
00140         case 'exception':
00141           $ret[$pId]['exception_instances'] = $res['num_instances'];
00142           $ret[$pId]['all_instances'] += $res['num_instances'];
00143           break;
00144         case 'aborted':
00145           $ret[$pId]['aborted_instances'] = $res['num_instances'];
00146           $ret[$pId]['all_instances'] += $res['num_instances'];
00147           break;
00148       }
00149     }
00150     // get number of activities per process
00151     $query = "select pId, count(*) as num_activities
00152               from ".GALAXIA_TABLE_PREFIX."activities
00153               where pId in (" . join(', ', array_keys($ret)) . ")
00154               group by pId";
00155     $result = $this->query($query);
00156     while($res = $result->fetchRow()) {
00157       $pId = $res['pId'];
00158       if (!isset($ret[$pId])) continue;
00159       $ret[$pId]['activities'] = $res['num_activities'];
00160     }
00161     $retval = Array();
00162     $retval["data"] = $ret;
00163     $retval["cant"] = $cant;
00164     return $retval;
00165   }
00166 
00167   function monitor_list_activities($offset,$maxRecords,$sort_mode,$find,$where='') {
00168     $sort_mode = $this->convert_sortmode($sort_mode);
00169     if($find) {
00170       $findesc = '%'.$find.'%';
00171       $mid=" where ((ga.name like ?) or (ga.description like ?))";
00172       $bindvars = array($findesc,$findesc);
00173     } else {
00174       $mid="";
00175       $bindvars = array();
00176     }
00177     if($where) {
00178       $where = preg_replace('/pId/', 'ga.pId', $where);
00179       if($mid) {
00180         $mid.= " and ($where) ";
00181       } else {
00182         $mid.= " where ($where) ";
00183       }
00184     }
00185     $query = "select gp.`name` as `procname`, gp.`version`, ga.*
00186               from ".GALAXIA_TABLE_PREFIX."activities ga
00187                 left join ".GALAXIA_TABLE_PREFIX."processes gp on gp.pId=ga.pId
00188               $mid order by $sort_mode";
00189     $query_cant = "select count(*) from ".GALAXIA_TABLE_PREFIX."activities ga $mid";
00190     $result = $this->query($query,$bindvars,$maxRecords,$offset);
00191     $cant = $this->getOne($query_cant,$bindvars);
00192     $ret = Array();
00193     while($res = $result->fetchRow()) {
00194       // Number of active instances
00195       $aid = $res['activityId'];
00196       $res['active_instances']=$this->getOne("select count(gi.instanceId) from ".GALAXIA_TABLE_PREFIX."instances gi,".GALAXIA_TABLE_PREFIX."instance_activities gia where gi.instanceId=gia.instanceId and gia.activitYId=$aid and gi.status='active' and pId=".$res['pId']);
00197     // activities of completed instances are all removed from the instance_activities table for some reason, so we need to look at workitems
00198       $res['completed_instances']=$this->getOne("select count(distinct gi.instanceId) from ".GALAXIA_TABLE_PREFIX."instances gi,".GALAXIA_TABLE_PREFIX."workitems gw where gi.instanceId=gw.instanceId and gw.activityId=$aid and gi.status='completed' and pId=".$res['pId']);
00199     // activities of aborted instances are all removed from the instance_activities table for some reason, so we need to look at workitems
00200       $res['aborted_instances']=$this->getOne("select count(distinct gi.instanceId) from ".GALAXIA_TABLE_PREFIX."instances gi,".GALAXIA_TABLE_PREFIX."workitems gw where gi.instanceId=gw.instanceId and gw.activityId=$aid and gi.status='aborted' and pId=".$res['pId']);
00201       $res['exception_instances']=$this->getOne("select count(gi.instanceId) from ".GALAXIA_TABLE_PREFIX."instances gi,".GALAXIA_TABLE_PREFIX."instance_activities gia where gi.instanceId=gia.instanceId and gia.activityId=$aid and gi.status='exception' and pId=".$res['pId']);
00202     $res['act_running_instances']=$this->getOne("select count(gi.instanceId) from ".GALAXIA_TABLE_PREFIX."instances gi,".GALAXIA_TABLE_PREFIX."instance_activities gia where gi.instanceId=gia.instanceId and gia.activityId=$aid and gia.status='running' and pId=".$res['pId']);
00203     // completed activities are removed from the instance_activities table unless they're part of a split for some reason, so this won't work
00204     //  $res['act_completed_instances']=$this->getOne("select count(gi.instanceId) from ".GALAXIA_TABLE_PREFIX."instances gi,".GALAXIA_TABLE_PREFIX."instance_activities gia where gi.instanceId=gia.instanceId and gia.activityId=$aid and gia.status='completed' and pId=".$res['pId']);
00205       $res['act_completed_instances'] = 0;
00206       $ret[$aid] = $res;
00207     }
00208     if (count($ret) < 1) {
00209       $retval = Array();
00210       $retval["data"] = $ret;
00211       $retval["cant"] = $cant;
00212       return $retval;
00213     }
00214     $query = "select activityId, count(distinct instanceId) as num_instances, min(ended - started) as min_time, avg(ended - started) as avg_time, max(ended - started) as max_time
00215               from ".GALAXIA_TABLE_PREFIX."workitems
00216               where activityId in (" . join(', ', array_keys($ret)) . ")
00217               group by activityId";
00218     $result = $this->query($query);
00219     while($res = $result->fetchRow()) {
00220       // Number of active instances
00221       $aid = $res['activityId'];
00222       if (!isset($ret[$aid])) continue;
00223       $ret[$aid]['act_completed_instances'] = $res['num_instances'] - $ret[$aid]['aborted_instances'];
00224       $ret[$aid]['duration'] = array('min' => $res['min_time'], 'avg' => $res['avg_time'], 'max' => $res['max_time']);
00225     }
00226     $retval = Array();
00227     $retval["data"] = $ret;
00228     $retval["cant"] = $cant;
00229     return $retval;
00230   }
00231 
00232   function monitor_list_instances($offset,$maxRecords,$sort_mode,$find,$where='',$wherevars=array()) {
00233     if($find) {
00234       $findesc = $this->qstr('%'.$find.'%');
00235       $mid=" where (`properties` like $findesc)";
00236     } else {
00237       $mid="";
00238     }
00239     if($where) {
00240       if($mid) {
00241         $mid.= " and ($where) ";
00242       } else {
00243         $mid.= " where ($where) ";
00244       }
00245     }
00246     $query = "select gp.`pId`, ga.`isInteractive`, gi.`owner`, gp.`name` as `procname`, gp.`version`, ga.`type`,";
00247     $query.= " ga.`activityId`, ga.`name`, gi.`instanceId`, gi.`status`, gia.`activityId`, gia.`user`, gi.`started`, gi.`ended`, gia.`status` as actstatus ";
00248     $query.=" from `".GALAXIA_TABLE_PREFIX."instances` gi LEFT JOIN `".GALAXIA_TABLE_PREFIX."instance_activities` gia ON gi.`instanceId`=gia.`instanceId` ";
00249     $query.= "LEFT JOIN `".GALAXIA_TABLE_PREFIX."activities` ga ON gia.`activityId` = ga.`activityId` ";
00250     $query.= "LEFT JOIN `".GALAXIA_TABLE_PREFIX."processes` gp ON gp.`pId`=gi.`pId` $mid order by ".$this->convert_sortmode($sort_mode);
00251 
00252     $query_cant = "select count(*) from `".GALAXIA_TABLE_PREFIX."instances` gi LEFT JOIN `".GALAXIA_TABLE_PREFIX."instance_activities` gia ON gi.`instanceId`=gia.`instanceId` ";
00253     $query_cant.= "LEFT JOIN `".GALAXIA_TABLE_PREFIX."activities` ga ON gia.`activityId` = ga.`activityId` LEFT JOIN `".GALAXIA_TABLE_PREFIX."processes` gp ON gp.`pId`=gi.`pId` $mid";
00254     $result = $this->query($query,$wherevars,$maxRecords,$offset);
00255     $cant = $this->getOne($query_cant,$wherevars);
00256     $ret = Array();
00257     while($res = $result->fetchRow()) {
00258       $iid = $res['instanceId'];
00259       $res['workitems']=$this->getOne("select count(*) from `".GALAXIA_TABLE_PREFIX."workitems` where `instanceId`=?",array($iid));
00260       $ret[$iid] = $res;
00261     }
00262     $retval = Array();
00263     $retval["data"] = $ret;
00264     $retval["cant"] = $cant;
00265     return $retval;
00266   }
00267 
00268 
00269   function monitor_list_all_processes($sort_mode = 'name_asc', $where = '') {
00270     if (!empty($where)) {
00271       $where = " where ($where) ";
00272     }
00273     $query = "select `name`,`version`,`pId` from `".GALAXIA_TABLE_PREFIX."processes` $where order by ".$this->convert_sortmode($sort_mode);
00274     $result = $this->query($query);
00275     $ret = Array();
00276     while($res = $result->fetchRow()) {
00277       $pId = $res['pId'];
00278       $ret[$pId] = $res;
00279     }
00280     return $ret;
00281   }
00282 
00283   function monitor_list_all_activities($sort_mode = 'name_asc', $where = '') {
00284     if (!empty($where)) {
00285       $where = " where ($where) ";
00286     }
00287     $query = "select `name`,`activityId` from `".GALAXIA_TABLE_PREFIX."activities` $where order by ".$this->convert_sortmode($sort_mode);
00288     $result = $this->query($query);
00289     $ret = Array();
00290     while($res = $result->fetchRow()) {
00291       $aid = $res['activityId'];
00292       $ret[$aid] = $res;
00293     }
00294     return $ret;
00295   }
00296 
00297   function monitor_list_statuses() {
00298     $query = "select distinct(`status`) from `".GALAXIA_TABLE_PREFIX."instances`";
00299     $result = $this->query($query);
00300     $ret = Array();
00301     while($res = $result->fetchRow()) {
00302       $ret[] = $res['status'];
00303     }
00304     return $ret;
00305   }
00306 
00307   function monitor_list_users() {
00308     $query = "select distinct(`user`) from `".GALAXIA_TABLE_PREFIX."instance_activities`";
00309     $result = $this->query($query);
00310     $ret = Array();
00311     while($res = $result->fetchRow()) {
00312       $ret[] = $res['user'];
00313     }
00314     return $ret;
00315   }
00316 
00317   function monitor_list_wi_users() {
00318     $query = "select distinct(`user`) from `".GALAXIA_TABLE_PREFIX."workitems`";
00319     $result = $this->query($query);
00320     $ret = Array();
00321     while($res = $result->fetchRow()) {
00322       $ret[] = $res['user'];
00323     }
00324     return $ret;
00325   }
00326 
00327 
00328   function monitor_list_owners() {
00329     $query = "select distinct(`owner`) from `".GALAXIA_TABLE_PREFIX."instances`";
00330     $result = $this->query($query);
00331     $ret = Array();
00332     while($res = $result->fetchRow()) {
00333       $ret[] = $res['owner'];
00334     }
00335     return $ret;
00336   }
00337 
00338 
00339   function monitor_list_activity_types() {
00340     $query = "select distinct(`type`) from `".GALAXIA_TABLE_PREFIX."activities`";
00341     $result = $this->query($query);
00342     $ret = Array();
00343     while($res = $result->fetchRow()) {
00344       $ret[] = $res['type'];
00345     }
00346     return $ret;
00347   }
00348 
00349   function monitor_get_workitem($itemId) {
00350     $query = "select gw.`orderId`,ga.`name`,ga.`type`,ga.`isInteractive`,gp.`name` as `procname`,gp.`version`,";
00351     $query.= "gw.`itemId`,gw.`properties`,gw.`user`,`started`,`ended`-`started` as duration ";
00352     $query.= "from `".GALAXIA_TABLE_PREFIX."workitems` gw,`".GALAXIA_TABLE_PREFIX."activities` ga,`".GALAXIA_TABLE_PREFIX."processes` gp where ga.`activityId`=gw.`activityId` and ga.`pId`=gp.`pId` and `itemId`=?";
00353     $result = $this->query($query, array($itemId));
00354     $res = $result->fetchRow();
00355     $res['properties'] = unserialize($res['properties']);
00356     return $res;
00357   }
00358 
00359   // List workitems per instance, remove workitem, update_workitem
00360   function monitor_list_workitems($offset,$maxRecords,$sort_mode,$find,$where='',$wherevars=array()) {
00361     $mid = '';
00362     if ($where) {
00363       $mid.= " and ($where) ";
00364     }
00365     if($find) {
00366       $findesc = $this->qstr('%'.$find.'%');
00367       $mid.=" and (`properties` like $findesc)";
00368     }
00369 // TODO: retrieve instance status as well
00370     $query = "select `itemId`,`ended`-`started` as duration,ga.`isInteractive`, ga.`type`,gp.`name` as procname,gp.`version`,ga.`name` as actname,";
00371     $query.= "ga.`activityId`,`instanceId`,`orderId`,`properties`,`started`,`ended`,`user` from `".GALAXIA_TABLE_PREFIX."workitems` gw,`".GALAXIA_TABLE_PREFIX."activities` ga,`".GALAXIA_TABLE_PREFIX."processes` gp ";
00372     $query.= "where gw.`activityId`=ga.`activityId` and ga.`pId`=gp.`pId` $mid order by gp.`pId` desc,".$this->convert_sortmode($sort_mode);
00373     $query_cant = "select count(*) from `".GALAXIA_TABLE_PREFIX."workitems` gw,`".GALAXIA_TABLE_PREFIX."activities` ga,`".GALAXIA_TABLE_PREFIX."processes` gp where gw.`activityId`=ga.`activityId` and ga.`pId`=gp.`pId` $mid";
00374     $result = $this->query($query,$wherevars,$maxRecords,$offset);
00375     $cant = $this->getOne($query_cant,$wherevars);
00376     $ret = Array();
00377     while($res = $result->fetchRow()) {
00378       $itemId = $res['itemId'];
00379       $ret[$itemId] = $res;
00380     }
00381     $retval = Array();
00382     $retval["data"] = $ret;
00383     $retval["cant"] = $cant;
00384     return $retval;
00385   }
00386 
00387 
00388 }
00389 ?>

Generated on Mon Jun 7 16:37:38 2004 for Galaxia by doxygen 1.3.4