Index: trunk/MagicSoft/Mars/datacenter/scripts/sourcefile
===================================================================
--- trunk/MagicSoft/Mars/datacenter/scripts/sourcefile	(revision 9543)
+++ trunk/MagicSoft/Mars/datacenter/scripts/sourcefile	(revision 9585)
@@ -20,5 +20,5 @@
 #   Author(s): Daniela Dorner  05/2005 <mailto:dorner@astro.uni-wuerzburg.de>
 #
-#   Copyright: MAGIC Software Development, 2000-2007
+#   Copyright: MAGIC Software Development, 2000-2010
 #
 #
@@ -209,59 +209,16 @@
 }
 
-function resetstatusvalues()
-{
-   statustime=NULL
-   starttime=NULL
-   returncode=NULL
-   programid=NULL
-   failedtime=NULL
-}
-
+# print the current status values
 function printstatusvalues()
 {
    echo "the current values are:"
-   echo " statustime=$statustime"
    echo " starttime=$starttime"
+   echo " stoptime=$stoptime"
    echo " returncode=$returncode"
-   echo " programid=$programid"
-   echo " failedtime=$failedtime"
    echo "-- check: -$check-"
    echo ""
 }
 
-# function evaluating the statusvalues
-function evalstatus()
-{
-   case $@ in
-      start)   printprocesslog "INFO setstatus start"
-               starttime="Now()"
-               ;;
-       stop)   case $check in
-                  ok)  printprocesslog "INFO setstatus stop - ok"
-                       statustime="Now()"
-                       ;;
-                  no)  printprocesslog "INFO setstatus stop - nothing new"
-                       check="ok"
-                       ;;
-                   *)  printprocesslog "INFO setstatus stop - failed"
-                       starttime=noreset
-                       if [ "$check" == "" ]
-                       then
-                          returncode=1
-                       else
-                          returncode=$check
-                       fi
-                       programid=$com
-                       failedtime="Now()"
-                       check="ok"
-                       ;;
-               esac
-               ;;
-          *)   printprocesslog "ERROR function evalstatus got wrong variable"
-               finish
-               ;;
-   esac
-}
-
+# get the db-setup from the sql.rc
 function getdbsetup()
 {
@@ -277,53 +234,125 @@
 }
 
+# function to get the needed information from the dependencies-file steps.rc
 function getstepinfo()
 {
    getdbsetup
-   table=`grep "$column:" $steps | sed -e "s/[.]$column://" -e 's/#//' -e 's/ //g'`
-   coltab=`grep "$column:" $steps | sed -e 's/://' -e 's/#//' -e 's/ //g'`
-   needs=`grep "$coltab[.]Needs:" $steps | sed -e "s/$coltab[.]Needs://"`
-   noderestricted=`grep "$coltab[.]NodeRestricted:" $steps | sed -e "s/$coltab[.]NodeRestricted://" -e 's/ //g'`
-   influences=`grep "$coltab[.]Influences:" $steps | sed -e "s/$coltab[.]Influences://"`
-   prims=( `grep "$table[.]Primary:" $steps | sed -e "s/$table[.]Primary://"` )
-#   echo " column $column - table $table - coltab $coltab"
+   needs=`grep "$step[.]Needs:" $steps | sed -e "s/$step[.]Needs://"`
+   noderestricted=`grep "$step[.]NodeRestricted:" $steps | sed -e "s/$step[.]NodeRestricted://" -e 's/ //g'`
+   prims=( `grep "$step[.]Primaries:" $steps | sed -e "s/$step[.]Primaries://"` )
 #   echo " needs: $needs"
-#   echo " influences: $influences"
 #   echo " noderestricted: $noderestricted"
 #   echo " prims: ${prims[@]}"
 }
 
+# function to get the primaries of a step from the dependencies-file steps.rc
+function getprimary()
+{
+   getdbsetup
+   grep $@"[.]Primaries:" $steps | sed -e "s/$@[.]Primaries://"
+}
+
+# function to get the join of a step from the dependencies-file steps.rc
+function getjoin()
+{
+   getdbsetup
+   grep $@"[.]Join:" $steps | sed -e "s/$@[.]Join://"
+}
+
+# function to create the middle part of a query
+#  which is identical for the functions getstatus() and gettodo()
+function middlepartofquery()
+{
+   # add from which table the information is queried
+   query=$query" FROM "$step"Status "
+   # add the joins to the tables in which the status of the preceding steps is stored
+   for need in $needs
+   do
+      needprims=( `getprimary $need` )
+      if [ "`echo ${needprims[@]}`" == "`echo ${prims[@]}`" ]
+      then 
+         query=$query" LEFT JOIN "$need"Status USING (${prims[@]}) "
+      fi
+   done
+   # add condition
+   query=$query" WHERE "
+   # add condition for the status of the peceding steps
+   counter=0
+   for need in $needs
+   do
+      if [ $counter -gt 0 ]
+      then
+         query=$query" AND "
+      fi
+      needprims=( `getprimary $need` )
+      # in case the primaries of the tables agree
+      #   only the condition is given
+      # for tables with differing primaries a special query 
+      #   is needed
+      if [ "`echo ${needprims[@]}`" == "`echo ${prims[@]}`" ]
+      then 
+         query=$query" NOT ISNULL("$need"Status.fStartTime) AND "
+         query=$query" NOT ISNULL("$need"Status.fStopTime) AND "
+         query=$query" ISNULL("$need"Status.fReturnCode) "
+      else 
+         query=$query" (SELECT COUNT(*) FROM "$need"Status "
+         query=$query" "`getjoin $need`" "
+         query=$query" WHERE "$step"Status."`echo ${prims[0]} | sed -e 's/,//g'`"="$step"Status."`echo ${prims[0]} | sed -e 's/,//g'`
+         for (( j=1 ; j < ${#prims[@]} ; j++ ))
+         do
+            query=$query" AND "$step"Status."`echo ${prims[$j]} | sed -e 's/,//g'`"="$step"Status."`echo ${prims[$j]} | sed -e 's/,//g'`
+         done
+         query=$query") = (SELECT COUNT(*) FROM "$need"Status "
+         query=$query" "`getjoin $need`" "
+         query=$query" WHERE "$step"Status."`echo ${prims[0]} | sed -e 's/,//g'`"="$step"Status."`echo ${prims[0]} | sed -e 's/,//g'`
+         for (( j=1 ; j < ${#prims[@]} ; j++ ))
+         do
+            query=$query" AND "$step"Status."`echo ${prims[$j]} | sed -e 's/,//g'`"="$step"Status."`echo ${prims[$j]} | sed -e 's/,//g'`
+         done
+         query=$query" AND NOT ISNULL(fStartTime) "
+         query=$query" AND NOT ISNULL(fStopTime) "
+         query=$query" AND ISNULL(fReturnCode)) "
+      fi
+      counter=`echo $counter + 1 | bc -l`
+   done
+   # add condition for the status of the step itself
+   query=$query" AND ISNULL("$step"Status.fStartTime) "
+   query=$query" AND ISNULL("$step"Status.fStopTime) "
+   query=$query" AND ISNULL("$step"Status.fReturnCode) "
+}
+
 # function to get todolist
+#   returns the next or the list of next steps
 function gettodo()
 {
+   # reset the variable for the number of the next step 
    process=
    printprocesslog "INFO getting todo..."
    getstepinfo
    # get query
-   query=" SELECT "${prims[0]}
-   for (( i=1 ; i < ${#prims[@]} ; i++ ))
+   query=" SELECT "`echo ${prims[0]} | sed -e 's/,//g'`
+   for (( j=1 ; j < ${#prims[@]} ; j++ ))
    do
-      query=$query", ${prims[$i]}"
+      query=$query", "`echo ${prims[$j]} | sed -e 's/,//g'`
    done
-   query=$query" FROM $table WHERE "
-   if ! echo $needs | grep '#' > /dev/null
-   then
-      for need in $needs
-      do
-         query=$query" NOT ISNULL($need) AND"
-      done
-   fi
-   query=$query" ISNULL($column) "
+   # get middle par of query 
+   middlepartofquery
+   # add requirement for production host in case it is needed
    if [ "$2 " != " " ]
    then 
       query=$query" AND fProductionHostKEY=$2 "
    fi
-   query=$query" AND ISNULL(fStartTime) AND ISNULL(fFailedTime) AND ISNULL(fProgramId) AND ISNULL(fReturnCode) "
-   query=$query" ORDER BY fPriority desc "
+   # order by priority to the the number of the next step to be done
+   query=$query" ORDER BY "$step"Status.fPriority desc "
+   # add limitation in case only one or a limited number of 
+   #  processes should be executed
    if [ "$1 " != " " ]
    then 
       query=$query" limit 0, $1 "
    fi
-#   echo " QUERY: "$query
+   # print query 
+   echo " gettodo QUERY: "$query
    printprocesslog "INFO gettodo QUERY: "$query
+   # execute query 
    if ! process=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
    then
@@ -331,5 +360,5 @@
       finish
    fi
-
+   # get numbers of next step from mysql result
    if [ "$process" = "" ]
    then
@@ -342,29 +371,26 @@
 }
 
-
 # function to get the number of processes which still have to be done
 function getstatus()
 {
+   # reset the variable for the number of steps to be done
    numproc=
    getstepinfo
    # get query
-   query=" SELECT COUNT(*) FROM $table WHERE "
-   if ! echo $needs | grep '#' > /dev/null
-   then
-      for need in $needs
-      do
-         query=$query" NOT ISNULL($need) AND"
-      done
-   fi
-   query=$query" ISNULL($column) "
+   query=" SELECT COUNT(*), 1 " # the 1 is just for grouping
+   # get middle part of query 
+   middlepartofquery
+   # add requirement for production host in case it is needed
    if [ "$1 " != " " ]
    then 
       query=$query" AND fProductionHostKEY=$1 "
    fi
-   query=$query" AND ISNULL(fStartTime) AND ISNULL(fFailedTime) AND ISNULL(fProgramId) AND ISNULL(fReturnCode) "
-   query=$query" GROUP BY $column "
-#   echo "QUERY: "$query
+   # group by an 'artifical' column to get the number of lines
+   query=$query" GROUP BY 2 "
+   # printing query
+   echo " getstatus QUERY: "$query
    printprocesslog "INFO getstatus QUERY: "$query
-   if ! numproc=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
+   # execute query
+   if ! numprocs=( `mysql -s -u $us --password=$pw --host=$ho $db -e " $query "` )
    then
       printprocesslog "ERROR could not query number of processes from db (program: $program, function getstatus)"
@@ -372,7 +398,10 @@
       continue
    fi
-   if [ "$numproc" = "" ]
+   # get number of processes from mysql result
+   if [ "${numprocs[1]}" = "" ]
    then
       numproc=0
+   else
+      numproc=${numprocs[0]}
    fi
 }
@@ -381,43 +410,95 @@
 function setstatus()
 {
-   if [ "$column" = "no" ]
+   # remark:
+   # this function does not include the 'Default' flag 
+   # for resetting steps
+
+   # for dowebplots (there are steps which have no entry in the DB)
+   if [ "$step" = "no" ]
    then
       return
    fi
-   resetstatusvalues
-   evalstatus $@
+   
+   # reset status values
+   starttime=NULL
+   stoptime=NULL
+   returncode=NULL
+   # evaluate the status values
+   case $@ in
+      start)   printprocesslog "INFO setstatus start"
+               starttime="Now()"
+               ;;
+       stop)   case $check in
+                  ok)  printprocesslog "INFO setstatus stop - ok"
+                       starttime=noreset
+                       stoptime="Now()"
+                       ;;
+                  no)  printprocesslog "INFO setstatus stop - nothing new"
+                       check="ok"
+                       ;;
+                   *)  printprocesslog "INFO setstatus stop - failed"
+                       starttime=noreset
+                       stoptime="Now()"
+                       if [ "$check" == "" ]
+                       then
+                          returncode=1
+                       else
+                          returncode=$check
+                       fi
+                       check="ok"
+                       ;;
+               esac
+               ;;
+          *)   printprocesslog "ERROR function setstatus got wrong variable"
+               finish
+               ;;
+   esac
+   # get 
    getstepinfo
+   
+   # get the influences from the steps.rc by evaluating the needs of all steps
+   influences=`grep $step $rc | grep "Needs" | grep -v "$step[.]Needs" | cut -d'.' -f1`
+   
    # get query
-   reset=`grep "$coltab[.]Reset:" $steps | sed -e "s/$coltab[.]Reset://" -e 's/ //g'`
-   if [ "$reset" = "no" ]
-   then
-      printprocesslog "ERROR You cannot reset $column for ${primaries[$s+$s]}"
-      finish
-   fi
-   query=" update $table set $column=$statustime"
-   if ! echo $influences | grep '#' > /dev/null
-   then
-      for influence in $influences
-      do
-         query=$query", $influence=NULL"
-      done
-   fi
+   query=" UPDATE "$step"Status "
+   # add joins to the influenced tables
+   for influence in $influences
+   do
+      query=$query" LEFT JOIN $influence USING("`getprimary $influence`") "
+      specialjoin=`getjoin $influence`
+      if ! [ "$specialjoin" = "" ]
+      then 
+         query=$query$specialjoin
+      fi
+   done
+   # set the status values according to the new status of the step
+   query=$query" SET "
    if ! [ "$starttime" = "noreset" ]
    then
-      query=$query", fStartTime=$starttime"
-   fi
-   query=$query", fFailedTime=$failedtime, fProgramId=$programid, fReturnCode=$returncode "
-   query=$query" where "
+      query=$query" "$step"Status.fStartTime=$starttime, "
+   fi
+   query=$query" "$step"Status.fStopTime=$stoptime, "$step"Status.fReturnCode=$returncode "
+   # set also the status values of the influenced steps
+   for influence in $influences
+   do
+      query=$query", "$influence"Status.fStartTime=NULL "
+      query=$query", "$influence"Status.fStopTime=NULL "
+      query=$query", "$influence"Status.fReturnCode=NULL "
+   done
+   # give the condition for which step the status values have to be set
+   query=$query" WHERE "
    if [ "$s" = "" ]
    then
       s=0
    fi
-   query=$query" ${prims[0]}='${primaries[$s*${#prims[@]}]}'"
-   for (( i=1 ; i < ${#prims[@]} ; i++ ))
+   query=$query" "`echo ${prims[0]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}]}'"
+   for (( j=1 ; j < ${#prims[@]} ; j++ ))
    do
-      query=$query" AND ${prims[$i]}='${primaries[$s*${#prims[@]}+$i]}' "
+      query=$query" AND "`echo ${prims[$j]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}+$j]}' "
    done   
-#   echo " QUERY: "$query
+   # print query
+   echo " setstatus QUERY: "$query
    printprocesslog "INFO setstatus QUERY: "$query
+   # execute query
    if ! mysql -s -u $us --password=$pw --host=$ho $db -e " $query "
    then
@@ -425,5 +506,4 @@
       finish
    fi
-
 }
 
