Location: PHPKode > projects > RepMan > repman/run.php
<?
  require ('inc/functions.php');
  
  ConnectIt();
  
  # CONSTANT
  $day = date ("w");
  $date = date("Ymd");
  $yesterday = date("Y-m-d",strtotime("-1 day"));
  $dom_date = date("j", strtotime("now"));  // poradove cislo dne v mesici (1-31)
  
  echo "<h1>Today will be checked:</h1>\n";
  
  
  /*          ++++++Day of Week++++++         */

  $mask = str_repeat("_", $day*2)."1%";    // vytvoreni masky pro vyber planu a dnesni den 

  echo "<h2>Plan: Day of Week</h2>\n";
  // dotaz na DoW plan podle masky:
  foreach (Query("SELECT scheduler.id,client.name,job.job,scheduler.client_id,scheduler.job_id FROM scheduler LEFT JOIN (client,job) ON (client.id=scheduler.client_id AND job.id=scheduler.job_id) WHERE scheduler.type=1 AND dow LIKE '$mask';") as $sched) {
  	echo "<em>$sched[0]&nbsp;&ndash;&nbsp;$sched[1]&nbsp;&ndash;&nbsp;$sched[2]</em><br>\n"; // vypis nalezeneho klienta
  	
    if ($data=Query("SELECT id,finished,status FROM sess WHERE client_id='$sched[3]' AND job_id='$sched[4]' AND processed=0;")) {
    	foreach ($data as $sess) {
        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$sess[0], $sess[1], $sess[2]";  // vypis nalezene session
        
        // nastaveni priznaku - zpracovano
        if (Query("UPDATE sess SET processed='$date' WHERE id=$sess[0];")) {
        	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Processed</b><br>\n";
        	logdebug("Processed id=$sess[0] group='$date'");
        } else {
          echo "&nbsp;&nbsp;HELP! I cann't process it! Some problem with DB.<br>\n";
          logerr("Can not do Query UPDATE sess SET processed $date WHERE id $sess[0]");
        }
      }
      
      // Pokud neni nalezena nezpracovana session....    
    } else {
      echo "No unprocessed session found with CLID $sched[3] and JOID $sched[4]";
      if (Query("SELECT * FROM sess WHERE client_id='$sched[3]' AND job_id='$sched[4]' AND processed='$date';")) {
        echo ", but today was <b>some session processed</b><br>\n";
      } else {        
        //  vytvori se zpracovany zaznam o tom, ze chybi session
        if (Query("INSERT INTO sess(client_id,job_id,status,finished,processed) VALUES($sched[3],$sched[4],'-1',NOW(),'$date');")) {
          echo ", adding information about missing session.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>missing processed</b><br>\n";
        } else {
          echo "<b>!!!!!</b>I can not save information about missing session<br>\n";
          logerr("Nepodarilo se vlozit radek NEnalezene session pro klienta $sched[0] a job $sched[1]");
        }
        
      }
    }
  }
  
  
  /*        ++++++Day of Month++++++        */

  echo "<h2>Plan: Day of Month</h2>";

  /* dle cisla dne bez hledani masky mesice - spousti se kazdy mesic */
  echo "<h3>&raquo; every month:</h3>";
  
  // vsechny plany s mesicnim cyklem pro dnesni datum
  if ($dom_list=Query("SELECT scheduler.id,client.name,job.job,scheduler.client_id,scheduler.job_id FROM scheduler LEFT JOIN (client,job) ON (client.id=scheduler.client_id AND job.id=scheduler.job_id) WHERE scheduler.type=2 AND scheduler.doM='$dom_date';")) {
    foreach ($dom_list as $cdl) {
    	echo "<em>$cdl[0]&nbsp;&ndash;&nbsp;$cdl[1]&nbsp;&ndash;&nbsp;$cdl[2]</em><br>\n";
    	
      // hledani session odpovidajicich planu
      if ($mdata=Query("SELECT id,finished,status FROM sess WHERE client_id=$cdl[3] AND job_id=$cdl[4] AND processed=0;")) {
        foreach ($mdata as $msess) {
          echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$msess[0], $msess[1], $msess[2]";
          
          // nastaveni priznaku zpracovano
          if (Query("UPDATE sess SET processed='$date' WHERE id=$msess[0];")) {
          	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Processed</b><br>\n";
          	logdebug("Processed id=$msess[0] group='$date'");
          } else {
            echo "&nbsp;&nbsp;HELP! I cann't process it! Some problem with DB.<br>\n";
            logerr("Can not do Query UPDATE sess SET processed $date WHERE id $msess[0]");
          }
        }
      } else {
          echo "No unprocessed session found with CLID $cdl[3] and JOID $cdl[4]";
          if (Query("SELECT * FROM sess WHERE client_id='$cdl[3]' AND job_id='$cdl[4]' AND processed='$date';")) {
            echo ", but today was <b>some session processed</b><br>\n";
          } else {
            // vytvori se zpracovany zaznam o tom, ze chybi session
            if (Query("INSERT INTO sess(client_id,job_id,status,finished,processed) VALUES($cdl[3],$cdl[4],'-1',NOW(),'$date');")) {
              echo ", adding information about missing session.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>missing processed</b><br>\n";
            } else {
              echo "<b>!!!!!</b>I can not save information about missing session<br>\n";
              logerr("Nepodarilo se vlozit radek NEnalezene session pro klienta $cdl[0] a job $cdl[1]");
            }
          }
      }
      
    }
  } else {
    echo "There is no plan in scheduler today.<br>\n";
  }
  
      /*  vybrane mesice + cislo dne  */
  $mmask = str_repeat("_", (date("n")-1)*2)."1%";    // vytvoreni masky pro vyber planu v tomto mesici

  echo "<h3>&raquo; this month (".date("F")." = ".date("n")."):</h3>";
  // podle masky tohoto mesice se budou hledat dny ke spusteni. vylouci se ty, ktere se spousti kazdy mesic - to zarucuje polozka scheduler.type 
  if ($nlist=Query("SELECT scheduler.id,client.name,job.job,scheduler.client_id,scheduler.job_id,scheduler.mon FROM scheduler LEFT JOIN (client,job) ON (client.id=scheduler.client_id AND job.id=scheduler.job_id) WHERE scheduler.type=3 AND scheduler.mon LIKE '$mmask' AND scheduler.doM='$dom_date';")) { 
    foreach ($nlist as $nn) {
    	echo "<em>$nn[0]&nbsp;&ndash;&nbsp;$nn[1]&nbsp;&ndash;&nbsp;$nn[2]&nbsp;&ndash;&nbsp;plan: '$nn[5]'</em><br>\n";

      // hledani session odpovidajicich planu
      if ($ndata=Query("SELECT id,finished,status FROM sess WHERE client_id=$nn[3] AND job_id=$nn[4] AND processed=0;")) {
        foreach ($ndata as $nsess) {
          echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$nsess[0], $nsess[1], $nsess[2]";
          
          // nastaveni priznaku zpracovano
          if (Query("UPDATE sess SET processed='$date' WHERE id=$nsess[0];")) {
          	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Processed</b><br>\n";
          	logdebug("Processed id=$nsess[0] group='$date'");
          } else {
            echo "&nbsp;&nbsp;HELP! I cann't process it! Some problem with DB.<br>\n";
            logerr("Can not do Query UPDATE sess SET processed $date WHERE id $nsess[0]");
          }
        }
      } else {
          echo "No unprocessed session found with CLID $nn[3] and JOID $nn[4]";
          if (Query("SELECT * FROM sess WHERE client_id='$nn[3]' AND job_id='$nn[4]' AND processed='$date';")) {
            echo ", but today was <b>some session processed</b><br>\n";
          } else {
              //  vytvori se zpracovany zaznam o tom, ze chybi session
              if (Query("INSERT INTO sess(client_id,job_id,status,finished,processed) VALUES($nn[3],$nn[4],'-1',NOW(),'$date');")) {
                echo ", adding information about missing session.&nbsp;&nbsp;<b>missing processed</b><br>\n";
              } else {
                echo "<b>!!!!!</b>I can not save information about missing session<b>\n";
                logerr("Nepodarilo se vlozit radek NEnalezene session pro klienta $nn[0] a job $nn[1]");
              }
          }
      }

    }
  } else {
    echo "There is no plan in scheduler for this day.<br>\n";
  }

  
  /*        ++++++DoM "31+" ++++++++        */
  
  if ($dom_date>=1&&$dom_date<=7) {       // ($dom_date>=1&&$dom_date<=7) ------------ F*I*R*S*T DoM
    echo "<h3>&raquo; 1st ".date("l", strtotime("now"))." of month:</h3>";
    $did = 40+$day;        // this "first day" of month ID
 
    if ($lfst=Query("SELECT scheduler.id,client.name,job.job,scheduler.client_id,scheduler.job_id,scheduler.mon FROM scheduler LEFT JOIN (client,job) ON (client.id=scheduler.client_id AND job.id=scheduler.job_id) WHERE scheduler.type<>1 AND scheduler.mon LIKE '$mmask' AND scheduler.doM='$did';")) {
      foreach ($lfst as $dd) {
      	echo "<em>$dd[0]&nbsp;&ndash;&nbsp;$dd[1]&nbsp;&ndash;&nbsp;$dd[2]&nbsp;&ndash;&nbsp;plan: '$dd[5]'</em><br>\n";
 
        if ($fdata=Query("SELECT id,finished,status FROM sess WHERE client_id=$dd[3] AND job_id=$dd[4] AND processed=0;")) {
          foreach ($fdata as $fsess) {
            echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$fsess[0], $fsess[1], $fsess[2]";

            // nastaveni priznaku zpracovano
            if (Query("UPDATE sess SET processed='$date' WHERE id=$fsess[0];")) {
            	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Processed</b><br>\n";
            	logdebug("Processed id=$fsess[0] group='$date'");
            } else {
              echo "&nbsp;&nbsp;HELP! I cann't process it! Some problem with DB.<br>\n";
              logerr("Can not do Query UPDATE sess SET processed $date WHERE id $fsess[0]");
            }
          }
        } else {
          echo "No unprocessed session found with CLID $dd[3] and JOID $dd[4]";
          if (Query("SELECT * FROM sess WHERE client_id='$dd[3]' AND job_id='$dd[4]' AND processed='$date';")) {
            echo ", but today was <b>some session processed</b><br>\n";
          } else {
            // vytvori se zpracovany zaznam o tom, ze chybi session
            if (Query("INSERT INTO sess(client_id,job_id,status,finished,processed) VALUES($dd[3],$dd[4],'-1',NOW(),'$date');")) {
              echo ", adding information about missing session.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>missing processed</b><br>\n";
            } else {
              echo "<b>!!!!!</b>I can not save information about missing session<br>\n";
              logerr("Nepodarilo se vlozit radek NEnalezene session pro klienta $dd[0] a job $dd[1]");
            }
          }
        }
      }
    } else {
      echo "There is no plan today.<br>\n";
    }
  }
  
  if ($dom_date>=2&&$dom_date<=8) {       // ($dom_date>=2&&$dom_date<=8) ------------ NEXT DoM
    echo "<h3>&raquo; next ".date("l", strtotime("now"))." after 1st ".date("l", strtotime("-1 day"))." of month:</h3>";
    $ndid = 50+$day;        // this "next day" of month ID

    if ($lxst=Query("SELECT scheduler.id,client.name,job.job,scheduler.client_id,scheduler.job_id,scheduler.mon FROM scheduler LEFT JOIN (client,job) ON (client.id=scheduler.client_id AND job.id=scheduler.job_id) WHERE scheduler.type<>1 AND scheduler.mon LIKE '$mmask' AND scheduler.doM='$ndid';")) {
      foreach ($lxst as $xx) {
      	echo "<em>$xx[0]&nbsp;&ndash;&nbsp;$xx[1]&nbsp;&ndash;&nbsp;$xx[2]&nbsp;&ndash;&nbsp;plan: '$xx[5]'</em><br>\n";

        if ($xdata=Query("SELECT id,finished,status FROM sess WHERE client_id=$xx[3] AND job_id=$xx[4] AND processed=0;")) {
          foreach ($xdata as $xsess) {
            echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$xsess[0], $xsess[1], $xsess[2]";

            // nastaveni priznaku zpracovano
            if (Query("UPDATE sess SET processed='$date' WHERE id=$xsess[0];")) {
            	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Processed</b><br>\n";
            	logdebug("Processed id=$xsess[0] group='$date'");
            } else {
              echo "&nbsp;&nbsp;HELP! I cann't process it! Some problem with DB.<br>\n";
              logerr("Can not do Query UPDATE sess SET processed $date WHERE id $xsess[0]");
            }
          }
        } else {
          echo "No unprocessed session found with CLID $xx[3] and JOID $xx[4]";
          if (Query("SELECT * FROM sess WHERE client_id='$xx[3]' AND job_id='$xx[4]' AND processed='$date';")) {
            echo ", but today was <b>some session processed</b><br>\n";
          } else {
            // vytvori se zpracovany zaznam o tom, ze chybi session
            if (Query("INSERT INTO sess(client_id,job_id,status,finished,processed) VALUES($xx[3],$xx[4],'-1',NOW(),'$date');")) {
              echo ", adding information about missing session.&nbsp;&nbsp;<b>missing processed</b><br>\n";
            } else {
              echo "<b>!!!!!</b>I can not save information about missing session<br>\n";
              logerr("Nepodarilo se vlozit radek NEnalezene session pro klienta $xx[0] a job $xx[1]");
            }
          }
        }
      }
    } else {
      echo "No plan in scheduler today.<br>\n";
    }
  }


  /*        ++++++neplanovane++++++        */
  
  if ($rmdata=Query("SELECT sess.id,sess.finished,sess.status,client.name,sess.client_id,job.job,sess.job_id FROM sess LEFT JOIN (client,job) ON (client.id=sess.client_id AND job.id=sess.job_id) WHERE sess.client_id<>".$GLOBALS["test_client_id"]." AND sess.job_id<>".$GLOBALS["test_job_id"]." AND sess.processed=0;")) {
    echo "<h2>Remaining unplanned:</h2>";
     foreach ($rmdata as $rmn) {
      echo "$rmn[0]&nbsp;&nbsp;$rmn[3]($rmn[4])&nbsp;&nbsp;$rmn[5]($rmn[6])&nbsp;&nbsp;$rmn[1]&nbsp;&nbsp;$rmn[2]";

      // nastaveni priznaku zpracovano
      if (Query("UPDATE sess SET processed='$date' WHERE id=$rmn[0];")) {
      	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Processed</b><br>\n";
      	logdebug("Processed id=$rmn[0] group='$date'");
      } else {
        echo "&nbsp;&nbsp;HELP! I cann't process it! Some problem with DB.<br>\n";
        logerr("Can not do Query UPDATE sess SET processed $date WHERE id $rmn[0]");
      }
    }
  }


  /*     zobrazeni session, ktere byly dokonceny vcera      */
  
  if ($yest = Query("SELECT * FROM sess WHERE finished LIKE '$yesterday%';")) {
    echo "<hr align=\"left\" size=\"1px\" width=\"33%\" noshade color=\"#C0C0C0\">\n";
    echo "Yesterday finished:<br>\n";
    foreach ($yest as $fin) {
      echo $fin[0]." ".$fin[1]." ".$fin[2]." ".$fin[3]." ".$fin[4]." ".$fin[5]."<br>\n";
    }
  }
  
  
  /*          ++++++++++++++E-MAILY+++++++++++++++          */
  require ('inc/mail.php');

?>

Return current item: RepMan