Example #1
0
 function doBulkUpdate($filter, $do, $ids = array())
 {
     @set_time_limit(600);
     // [TODO] Temp!
     $change_fields = array();
     $custom_fields = array();
     if (empty($do)) {
         return;
     }
     if (is_array($do)) {
         foreach ($do as $k => $v) {
             switch ($k) {
                 //				case 'is_disabled':
                 //					$change_fields[DAO_Sensor::IS_DISABLED] = intval($v);
                 //					break;
                 default:
                     // Custom fields
                     if (substr($k, 0, 3) == "cf_") {
                         $custom_fields[substr($k, 3)] = $v;
                     }
                     break;
             }
         }
     }
     $pg = 0;
     if (empty($ids)) {
         do {
             list($objects, $null) = DAO_Alert::search(array(), $this->params, 100, $pg++, SearchFields_Alert::ID, true, false);
             $ids = array_merge($ids, array_keys($objects));
         } while (!empty($objects));
     }
     $batch_total = count($ids);
     for ($x = 0; $x <= $batch_total; $x += 100) {
         $batch_ids = array_slice($ids, $x, 100);
         DAO_Alert::update($batch_ids, $change_fields);
         // Custom Fields
         //			self::_doBulkSetCustomFields(PsCustomFieldSource_Sensor::ID, $custom_fields, $batch_ids);
         unset($batch_ids);
     }
     unset($ids);
 }
Example #2
0
 function run()
 {
     $logger = DevblocksPlatform::getConsoleLog();
     $logger->info("[Alerts] Starting...");
     $alerts = DAO_Alert::getAll();
     $check_sensors = DAO_Sensor::getAll();
     $workers = DAO_Worker::getAll();
     if (is_array($alerts)) {
         foreach ($alerts as $alert) {
             /* @var $alert Model_Alert */
             if (!isset($workers[$alert->worker_id])) {
                 continue;
             }
             $logger->info(sprintf("[Alerts] Checking '%s' for %s...", $alert->name, $workers[$alert->worker_id]->getName()));
             $hit_sensors = $alert->getMatches($check_sensors);
             if (is_array($hit_sensors)) {
                 $alert->run($hit_sensors);
             }
         }
     }
     $logger->info("[Alerts] Finished!");
 }
Example #3
0
 function saveAlertPeekAction()
 {
     $translate = DevblocksPlatform::getTranslationService();
     @($id = DevblocksPlatform::importGPC($_REQUEST['id'], 'integer', 0));
     @($view_id = DevblocksPlatform::importGPC($_POST['view_id'], 'string'));
     @($active_worker = PortSensorApplication::getActiveWorker());
     /*****************************/
     @($name = DevblocksPlatform::importGPC($_POST['name'], 'string', ''));
     @($is_disabled = DevblocksPlatform::importGPC($_POST['is_disabled'], 'integer', 0));
     @($worker_id = DevblocksPlatform::importGPC($_POST['worker_id'], 'integer', 0));
     @($rules = DevblocksPlatform::importGPC($_POST['rules'], 'array', array()));
     @($do = DevblocksPlatform::importGPC($_POST['do'], 'array', array()));
     @($delete = DevblocksPlatform::importGPC($_POST['do_delete'], 'integer', 0));
     if (!empty($id) && !empty($delete)) {
         DAO_Alert::delete($id);
     } else {
         if (empty($name)) {
             $name = $translate->_('Alert');
         }
         $criterion = array();
         $actions = array();
         // Custom fields
         $custom_fields = DAO_CustomField::getAll();
         $alert_criteria_exts = DevblocksPlatform::getExtensions('portsensor.alert.criteria', false);
         // Criteria
         if (is_array($rules)) {
             foreach ($rules as $rule) {
                 $rule = DevblocksPlatform::strAlphaNumDash($rule);
                 @($value = DevblocksPlatform::importGPC($_POST['value_' . $rule], 'string', ''));
                 // [JAS]: Allow empty $value (null/blank checking)
                 $criteria = array('value' => $value);
                 // Any special rule handling
                 switch ($rule) {
                     case 'dayofweek':
                         // days
                         $days = DevblocksPlatform::importGPC($_REQUEST['value_dayofweek'], 'array', array());
                         if (in_array(0, $days)) {
                             $criteria['sun'] = 'Sunday';
                         }
                         if (in_array(1, $days)) {
                             $criteria['mon'] = 'Monday';
                         }
                         if (in_array(2, $days)) {
                             $criteria['tue'] = 'Tuesday';
                         }
                         if (in_array(3, $days)) {
                             $criteria['wed'] = 'Wednesday';
                         }
                         if (in_array(4, $days)) {
                             $criteria['thu'] = 'Thursday';
                         }
                         if (in_array(5, $days)) {
                             $criteria['fri'] = 'Friday';
                         }
                         if (in_array(6, $days)) {
                             $criteria['sat'] = 'Saturday';
                         }
                         unset($criteria['value']);
                         break;
                     case 'timeofday':
                         $from = DevblocksPlatform::importGPC($_REQUEST['timeofday_from'], 'string', '');
                         $to = DevblocksPlatform::importGPC($_REQUEST['timeofday_to'], 'string', '');
                         $criteria['from'] = $from;
                         $criteria['to'] = $to;
                         unset($criteria['value']);
                         break;
                     case 'event':
                         @($events = DevblocksPlatform::importGPC($_REQUEST['value_event'], 'array', array()));
                         if (is_array($events)) {
                             foreach ($events as $event) {
                                 $criteria[$event] = true;
                             }
                         }
                         unset($criteria['value']);
                         break;
                     case 'alert_last_ran':
                         @($from = DevblocksPlatform::importGPC($_REQUEST['value_alert_last_ran_from'], 'string', ''));
                         @($to = DevblocksPlatform::importGPC($_REQUEST['value_alert_last_ran_to'], 'string', ''));
                         $criteria['from'] = $from;
                         $criteria['to'] = $to;
                         unset($criteria['value']);
                         break;
                     case 'sensor_name':
                         break;
                     case 'sensor_fail_count':
                         $oper = DevblocksPlatform::importGPC($_REQUEST['oper_sensor_fail_count'], 'string', '=');
                         $criteria['oper'] = $oper;
                         break;
                     case 'sensor_type':
                         @($types = DevblocksPlatform::importGPC($_REQUEST['value_sensor_types'], 'array', array()));
                         if (is_array($types)) {
                             foreach ($types as $type) {
                                 $criteria[$type] = true;
                             }
                         }
                         unset($criteria['value']);
                         break;
                     default:
                         // ignore invalids // [TODO] Very redundant
                         // Custom fields
                         if ("cf_" == substr($rule, 0, 3)) {
                             $field_id = intval(substr($rule, 3));
                             if (!isset($custom_fields[$field_id])) {
                                 continue;
                             }
                             // [TODO] Operators
                             switch ($custom_fields[$field_id]->type) {
                                 case 'S':
                                     // string
                                 // string
                                 case 'T':
                                     // clob
                                 // clob
                                 case 'U':
                                     // URL
                                     @($oper = DevblocksPlatform::importGPC($_REQUEST['value_cf_' . $field_id . '_oper'], 'string', 'regexp'));
                                     $criteria['oper'] = $oper;
                                     break;
                                 case 'D':
                                     // dropdown
                                 // dropdown
                                 case 'M':
                                     // multi-dropdown
                                 // multi-dropdown
                                 case 'X':
                                     // multi-checkbox
                                 // multi-checkbox
                                 case 'W':
                                     // worker
                                     @($in_array = DevblocksPlatform::importGPC($_REQUEST['value_cf_' . $field_id], 'array', array()));
                                     $out_array = array();
                                     // Hash key on the option for quick lookup later
                                     if (is_array($in_array)) {
                                         foreach ($in_array as $k => $v) {
                                             $out_array[$v] = $v;
                                         }
                                     }
                                     $criteria['value'] = $out_array;
                                     break;
                                 case 'E':
                                     // date
                                     @($from = DevblocksPlatform::importGPC($_REQUEST['value_cf_' . $field_id . '_from'], 'string', '0'));
                                     @($to = DevblocksPlatform::importGPC($_REQUEST['value_cf_' . $field_id . '_to'], 'string', 'now'));
                                     $criteria['from'] = $from;
                                     $criteria['to'] = $to;
                                     unset($criteria['value']);
                                     break;
                                 case 'N':
                                     // number
                                     @($oper = DevblocksPlatform::importGPC($_REQUEST['value_cf_' . $field_id . '_oper'], 'string', '='));
                                     $criteria['oper'] = $oper;
                                     $criteria['value'] = intval($value);
                                     break;
                                 case 'C':
                                     // checkbox
                                     $criteria['value'] = intval($value);
                                     break;
                             }
                         } elseif (isset($alert_criteria_exts[$rule])) {
                             // Extensions
                             // Save custom criteria properties
                             try {
                                 $crit_ext = $alert_criteria_exts[$rule]->createInstance();
                                 /* @var $crit_ext Extension_AlertCriteria */
                                 $criteria = $crit_ext->saveConfig();
                             } catch (Exception $e) {
                                 // print_r($e);
                             }
                         } else {
                             continue;
                         }
                         break;
                 }
                 $criterion[$rule] = $criteria;
             }
         }
         $alert_action_exts = DevblocksPlatform::getExtensions('portsensor.alert.action', false);
         // Actions
         if (is_array($do)) {
             foreach ($do as $act) {
                 $action = array();
                 switch ($act) {
                     // Forward a copy to...
                     case 'email':
                         @($emails = DevblocksPlatform::importGPC($_REQUEST['do_email'], 'array', array()));
                         if (!empty($emails)) {
                             $action = array('to' => $emails);
                         }
                         break;
                         // Watcher notification
                     // Watcher notification
                     case 'notify':
                         //@$emails = DevblocksPlatform::importGPC($_REQUEST['do_email'],'array',array());
                         //if(!empty($emails)) {
                         $action = array();
                         //}
                         break;
                     default:
                         // ignore invalids
                         // Custom fields
                         if ("cf_" == substr($act, 0, 3)) {
                             $field_id = intval(substr($act, 3));
                             if (!isset($custom_fields[$field_id])) {
                                 continue;
                             }
                             $action = array();
                             switch ($custom_fields[$field_id]->type) {
                                 case 'S':
                                     // string
                                 // string
                                 case 'T':
                                     // clob
                                 // clob
                                 case 'U':
                                     // URL
                                 // URL
                                 case 'D':
                                     // dropdown
                                 // dropdown
                                 case 'W':
                                     // worker
                                     $value = DevblocksPlatform::importGPC($_REQUEST['do_cf_' . $field_id], 'string', '');
                                     $action['value'] = $value;
                                     break;
                                 case 'M':
                                     // multi-dropdown
                                 // multi-dropdown
                                 case 'X':
                                     // multi-checkbox
                                     $in_array = DevblocksPlatform::importGPC($_REQUEST['do_cf_' . $field_id], 'array', array());
                                     $out_array = array();
                                     // Hash key on the option for quick lookup later
                                     if (is_array($in_array)) {
                                         foreach ($in_array as $k => $v) {
                                             $out_array[$v] = $v;
                                         }
                                     }
                                     $action['value'] = $out_array;
                                     break;
                                 case 'E':
                                     // date
                                     $value = DevblocksPlatform::importGPC($_REQUEST['do_cf_' . $field_id], 'string', '');
                                     $action['value'] = $value;
                                     break;
                                 case 'N':
                                     // number
                                 // number
                                 case 'C':
                                     // checkbox
                                     $value = DevblocksPlatform::importGPC($_REQUEST['do_cf_' . $field_id], 'string', '');
                                     $action['value'] = intval($value);
                                     break;
                             }
                         } elseif (isset($alert_action_exts[$act])) {
                             // Save custom action properties
                             try {
                                 $action_ext = $alert_action_exts[$act]->createInstance();
                                 $action = $action_ext->saveConfig();
                             } catch (Exception $e) {
                                 // print_r($e);
                             }
                         } else {
                             continue;
                         }
                         break;
                 }
                 $actions[$act] = $action;
             }
         }
         $fields = array(DAO_Alert::NAME => $name, DAO_Alert::IS_DISABLED => $is_disabled, DAO_Alert::WORKER_ID => $worker_id, DAO_Alert::CRITERIA_JSON => json_encode($criterion), DAO_Alert::ACTIONS_JSON => json_encode($actions));
         // Create
         if (empty($id)) {
             $fields[DAO_Alert::POS] = 0;
             $id = DAO_Alert::create($fields);
             // Update
         } else {
             DAO_Alert::update($id, $fields);
         }
     }
     if (!empty($view_id)) {
         $view = Ps_AbstractViewLoader::getView($view_id);
         $view->render();
     }
 }