function populate_tracker_queries()
 {
     if ($monitor4 = $this->trackerManager->getMonitor('tracker_queries')) {
         $sql = "INSERT INTO FOO VALUES ('Test', '{$monitor4->monitor_id}', 101, 'News Time', 'Our Latest Headlines', 4)";
         $sql = $query = TrackerUtility::getGenericSQL($sql);
         $monitor4->setValue('text', $sql);
         $monitor4->setValue('query_id', $monitor4->monitor_id);
         $monitor4->setValue('query_hash', md5($monitor4->text));
         $monitor4->setValue('run_count', rand(1, 25));
         $monitor4->setValue('sec_total', rand(200, 500));
         $monitor4->setValue('sec_avg', rand(200, 500));
         $monitor4->setValue('date_modified', $this->randomTimestamp());
     }
     //Insert into tracker_tracker_queries here
     if ($monitor5 = $this->trackerManager->getMonitor('tracker_tracker_queries')) {
         $monitor5->setValue('monitor_id', $monitor5->monitor_id);
         $monitor5->setValue('date_modified', $monitor4->date_modified);
         $monitor5->setValue('query_id', $monitor4->query_id);
     }
 }
 /**
  * save
  * This method retrieves the Store instances associated with monitor and calls
  * the flush method passing with the montior ($this) instance.
  * 
  */
 public function save($flush = true)
 {
     if (empty($this->date_modified)) {
         $this->date_modified = $GLOBALS['timedate']->nowDb();
     }
     $this->cached_data[] = $this->toArray();
     if ($flush && empty($GLOBALS['tracker_' . $this->table_name]) && !empty($this->cached_data)) {
         require_once 'modules/Trackers/TrackerUtility.php';
         $write_entries = array();
         foreach ($this->cached_data as $entry) {
             $query = str_replace(array("\r", "\n", "\r\n", "\t"), ' ', $entry['text']);
             $query = preg_replace("/\\s{2,}/", ' ', $query);
             $query = TrackerUtility::getGenericSQL($query);
             $entry['text'] = $query;
             $md5 = md5($query);
             if (!isset($write_entries[$md5])) {
                 $entry['query_hash'] = $md5;
                 $result = $GLOBALS['db']->query("SELECT * FROM tracker_queries WHERE query_hash = '{$md5}'");
                 if ($row = $GLOBALS['db']->fetchByAssoc($result)) {
                     $entry['query_id'] = $row['query_id'];
                     $entry['run_count'] = $row['run_count'] + 1;
                     $entry['sec_total'] = $row['sec_total'] + $entry['sec_total'];
                     $entry['sec_avg'] = $entry['sec_total'] / $entry['run_count'];
                 } else {
                     $entry['query_id'] = create_guid();
                     $entry['run_count'] = 1;
                     $entry['sec_total'] = $entry['sec_total'];
                     $entry['sec_avg'] = $entry['sec_total'];
                 }
                 $write_entries[$md5] = $entry;
             } else {
                 $write_entries[$md5]['run_count'] = $write_entries[$md5]['run_count']++;
                 $write_entries[$md5]['sec_total'] = $write_entries[$md5]['sec_total'] + $entry['sec_total'];
                 $write_entries[$md5]['sec_avg'] = $write_entries[$md5]['sec_total'] / $write_entries[$md5]['run_count'];
             }
             //if-else
         }
         //foreach
         $trackerManager = TrackerManager::getInstance();
         if ($monitor2 = $trackerManager->getMonitor('tracker_tracker_queries')) {
             $trackerManager->pause();
             //Loop through the stored cached data entries
             foreach ($write_entries as $write_e) {
                 //Set the values from the cached data entries
                 foreach ($write_e as $name => $value) {
                     $this->{$name} = $value;
                 }
                 //foreach
                 //Write to the tracker_tracker_monitor monitor
                 $monitor2->setValue('monitor_id', $this->monitor_id);
                 $monitor2->setValue('date_modified', $this->date_modified);
                 $monitor2->setValue('query_id', $this->query_id);
                 $monitor2->save($flush);
                 // <--- save to tracker_tracker_monitor
                 foreach ($this->stores as $s) {
                     $store = $this->getStore($s);
                     //Flush to the store
                     $store->flush($this);
                 }
                 //Clear the monitor
                 $this->clear();
             }
             //foreach
             $trackerManager->unPause();
         }
         unset($this->cached_data);
     }
     //if
 }