[Fusionforge-commits] r8398 - trunk/gforge/cronjobs

Roland Mas lolando at libremir.placard.fr.eu.org
Wed Nov 18 21:23:13 CET 2009


Author: lolando
Date: 2009-11-18 21:23:13 +0100 (Wed, 18 Nov 2009)
New Revision: 8398

Modified:
   trunk/gforge/cronjobs/calculate_user_metric.php
Log:
db_query_params() transition

Modified: trunk/gforge/cronjobs/calculate_user_metric.php
===================================================================
--- trunk/gforge/cronjobs/calculate_user_metric.php	2009-11-18 17:21:43 UTC (rev 8397)
+++ trunk/gforge/cronjobs/calculate_user_metric.php	2009-11-18 20:23:13 UTC (rev 8398)
@@ -2,27 +2,26 @@
 <?php
 /**
  * Copyright 1999-2001 (c) VA Linux Systems
- * Copyright 2003 (c) GForge, LLC
+ * Copyright 2003 (c) FusionForge, LLC
+ * Copyright 2009 (c) Roland Mas
  *
- * This file is part of GForge.
+ * This file is part of FusionForge.
  *
- * GForge is free software; you can redistribute it and/or modify
+ * FusionForge is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
  *
- * GForge is distributed in the hope that it will be useful,
+ * FusionForge is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with GForge; if not, write to the Free Software
+ * along with FusionForge; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
  */
 
-//exit;
-
 /*
 Before running the first time, execute initializing SQL:
 
@@ -86,9 +85,9 @@
 FROM user_group
 WHERE
 user_group.group_id=$1
-AND user_group.admin_flags=$2;',
-			array($sys_peer_rating_group,
-			'A')) ;
+AND user_group.admin_flags=$2',
+			array ($sys_peer_rating_group,
+			       'A')) ;
 
 
 $err .= db_error();
@@ -109,44 +108,45 @@
 	db_next_result();
 	$err .= db_error();
 } else {
-	$sql="UPDATE user_metric0 SET
-	metric=(log(times_ranked::float)*avg_rating::float)::float,
-	percentile=(100-(100*((ranking::float-1)/(select count(*) from user_metric0))))::float;";
+	$sql=";";
 
-	db_query($sql);
+	db_query_params ('UPDATE user_metric0 SET metric=(log(times_ranked::float)*avg_rating::float)::float, percentile=(100-(100*((ranking::float-1)/(select count(*) from user_metric0))))::float',
+			 array());
 	$err .= db_error();
 }
 
 if ($sys_database_type == 'mysql') {
 	$sql="UPDATE user_metric0 SET importance_factor=(1+((percentile/100.0)*.5));";
+	db_query_mysql($sql);
 } else {
-	$sql="UPDATE user_metric0 SET importance_factor=(1+((percentile::float/100)*.5))::float;";
+	db_query_params ('UPDATE user_metric0 SET importance_factor=(1+((percentile::float/100)*.5))::float',
+			 array()) ;
 }
 
-db_query($sql);
 $err .= db_error();
 
-for ($i=1; $i<9; $i++) {
-	// $err .= '<br />Starting round: '.$i;
+db_query_params ('SELECT * INTO TEMPORARY TABLE user_metric_cur FROM user_metric0',
+		 array()) ;
 
+
+for ($i=1; $i<9; $i++) {
 	$j=($i-1);
 
 	/*
 		Set up an interim table to grab and average all trusted result
 	*/
-	db_drop_table_if_exists ("user_metric_tmp1_".$i);
+	db_drop_table_if_exists ("user_metric_tmp_next");
+	db_drop_table_if_exists ("user_metric_next");
 
-	$sql="CREATE TABLE user_metric_tmp1_$i (
+	$res = db_query_params ('CREATE TEMPORARY TABLE user_metric_tmp_next (
 		user_id int not null default 0, 
 		times_ranked float(8) null default 0,
 		avg_raters_importance float(8) not null default 0,
 		avg_rating float(8) not null default 0,
-		metric float(8) not null default 0);";
-	$res=db_query($sql);
+		metric float(8) not null default 0)',
+				array()) ;
         if (!$res) {
-                $err .= "Error in round $i inserting final data: ";
-                $err .= '<p>'.$sql.'<p>';
-                $err .= db_error();
+                $err .= "Error in round $i creating table: " . db_error();
                 exit;
         }
 
@@ -154,19 +154,16 @@
 		Now grab/average trusted ratings into this table
 	*/
 
-    $sql="INSERT INTO user_metric_tmp1_$i
+	$res = db_query_params ('INSERT INTO user_metric_tmp_next
 	   	SELECT user_ratings.user_id,count(*) AS count,
-		avg(user_metric$j.importance_factor),
+		avg(user_metric_cur.importance_factor),
 		avg(user_ratings.rating),0
-		FROM user_ratings,user_metric$j
-		WHERE user_ratings.rated_by=user_metric$j.user_id
-		GROUP BY user_ratings.user_id";
-
-	$res=db_query($sql);
+		FROM user_ratings,user_metric_cur
+		WHERE user_ratings.rated_by=user_metric_cur.user_id
+		GROUP BY user_ratings.user_id',
+				array()) ;
 	if (!$res) {
-		$err .= "Error in round $i inserting average ratings: ";
-		$err .= '<p>'.$sql.'<p>';
-		$err .= db_error();
+		$err .= "Error in round $i inserting average ratings: " . db_error();
 		exit;
 		
 	}
@@ -177,22 +174,18 @@
 		This metric will be used in the next step to calculate ranking and importance
 	*/
 
-	$sql="UPDATE user_metric_tmp1_$i SET metric=(log(times_ranked)*avg_raters_importance*avg_rating);";
-	$res=db_query($sql);
+	$res = db_query_params ('UPDATE user_metric_tmp_next SET metric=(log(times_ranked)*avg_raters_importance*avg_rating)',
+				array()) ;
 	if (!$res) {
-		$err .= "Error in round $i calculating metric: ";
-		$err .= '<p>'.$sql.'<p>';
-		$err .= db_error();
+		$err .= "Error in round $i calculating metric: " . db_error();
 		exit;
 		
 	}
 
-	$sql="DELETE FROM user_metric_tmp1_$i WHERE metric < $threshhold";
-	$res=db_query($sql);
+	$res = db_query_params ('DELETE FROM user_metric_tmp_next WHERE metric < $1',
+				array ($threshhold)) ;
 	if (!$res) {
-                $err .= "Error in round $i deleting < threshhold ids: ";
-		$err .= '<p>'.$sql.'<p>';
-                $err .= db_error();
+                $err .= "Error in round $i deleting < threshhold ids: " . db_error();
                 exit;
                 
         }
@@ -203,18 +196,15 @@
 		new people in this round to stay in
 	*/
 
-	$sql="INSERT INTO user_metric_tmp1_$i 
+	$res = db_query_params ('INSERT INTO user_metric_tmp_next
 		SELECT user_id,times_ranked,avg_raters_importance,avg_rating,metric
-		FROM user_metric$j 
+		FROM user_metric_cur
 		WHERE NOT EXISTS 
-		(SELECT user_id FROM user_metric_tmp1_$i 
-		WHERE user_metric_tmp1_$i.user_id=user_metric$j.user_id);";
-
-	$res=db_query($sql);
+		(SELECT user_id FROM user_metric_tmp_next
+		WHERE user_metric_tmp_next.user_id=user_metric_cur.user_id)',
+				array ()) ;
         if (!$res) {
-                $err .= "Error in round $i inserting final data: ";
-                $err .= '<p>'.$sql.'<p>';
-                $err .= db_error();
+                $err .= "Error in round $i inserting final data: " . db_error();
                 exit;
         }
 
@@ -224,12 +214,10 @@
 		Create the final table, then insert the data
 	*/
 
-	// $err .= '<br />Starting Final Metric';
+	db_drop_table_if_exists ("user_metric_next");
+	db_drop_sequence_if_exists ("user_metric_next_ranking_seq");
 
-	db_drop_table_if_exists ("user_metric".$i);
-	db_drop_sequence_if_exists ("user_metric".$i."_ranking_seq");
-
-	$sql="CREATE TABLE user_metric$i (
+	$res = db_query_params ('CREATE TEMPORARY TABLE user_metric_next (
 		ranking serial,
 		user_id int not null default 0,
 		times_ranked int not null default 0,
@@ -237,13 +225,11 @@
 		avg_rating float(8) not null default 0,
 		metric float(8) not null default 0,
 		percentile float(8) not null default 0,
-		importance_factor float(8) not null default 0);";
+		importance_factor float(8) not null default 0)',
+				array ()) ;
 
-	$res=db_query($sql);
 	if (!$res) {
-                $err .= "Error in round $i inserting final data: ";
-                $err .= '<p>'.$sql.'<p>';
-                $err .= db_error();
+                $err .= "Error in round $i creating table: " . db_error();
                 exit;
         }
 
@@ -251,30 +237,26 @@
 		Insert the data in ranked order
 	*/
 
-	$sql="INSERT INTO user_metric$i (user_id,times_ranked,avg_raters_importance,avg_rating,metric)
+	$res = db_query_params ('INSERT INTO user_metric_next (user_id,times_ranked,avg_raters_importance,avg_rating,metric)
 		SELECT user_id,times_ranked,avg_raters_importance,avg_rating,metric
-		FROM user_metric_tmp1_$i
-		ORDER BY metric DESC;";
-	$res=db_query($sql);
+		FROM user_metric_tmp_next
+		ORDER BY metric DESC',
+				array ()) ;
 	if (!$res) {
-		$err .= "Error in round $i inserting final data: ";
-		$err .= '<p>'.$sql.'<p>';
-		$err .= db_error();
+		$err .= "Error in round $i inserting final data: " . db_error();
 		exit;
 	}
 
 	/*
 		Get the row count so we can calc the percentile below
 	*/
-	$res=db_query("SELECT COUNT(*) FROM user_metric$i");
+	$res = db_query_params ('SELECT COUNT(*) FROM user_metric_next',
+				array ());
 	if (!$res) {
-		$err .= "Error in round $i getting row count: ";
-		$err .= '<p>'.$sql.'<p>';
-		$err .= db_error();
+		$err .= "Error in round $i getting row count: " . db_error();
 		exit;
 	}
 
-	//$err .= '<br />Issuing Final Update';
 	// Only do final percentile if row count is not zero
 	if (db_result($res,0,0)) {
 
@@ -282,49 +264,47 @@
 	    	Update with final percentile and importance
 	    */
 		if ($sys_database_type == 'mysql') {
-			$sql="UPDATE user_metric$i SET
+			$sql="UPDATE user_metric_next SET
 			percentile=(100-(100*((ranking-1.0)/". db_result($res,0,0) .")))";
+			$res = db_query_mysql ($sql);
 		} else {
-			$sql="UPDATE user_metric$i SET
-			percentile=(100-(100*((ranking::float-1)/". db_result($res,0,0) .")))";
+			$res = db_query_params ('UPDATE user_metric_next SET
+			percentile=(100-(100*((ranking::float-1)/$1)))',
+						array (db_result($res,0,0))) ;
 		}
-	    $res=db_query($sql);
 	    if (!$res || db_affected_rows($res) < 1) {
-		$err .= "Error in round $i setting percentile: ";
-		$err .= '<p>'.$sql.'<p>';
-		$err .= db_error();
+		$err .= "Error in round $i setting percentile: " . db_error();
 		exit;
 	    }
-	    $sql="UPDATE user_metric$i SET
-		importance_factor=(1+((percentile/100)*.5));";
-	    $res=db_query($sql);
+	    $res = db_query_params ('UPDATE user_metric_next SET
+		importance_factor=(1+((percentile/100)*.5))',
+				    array ()) ;
 	    if (!$res || db_affected_rows($res) < 1) {
-		$err .= "Error in round $i setting importance factor: ";
-		$err .= '<p>'.$sql.'<p>';
-		$err .= db_error();
+		$err .= "Error in round $i setting importance factor: " . db_error();
 		exit;
 	    }
 	}
+
+	db_drop_table_if_exists ("user_metric_tmp_cur");
+	db_drop_table_if_exists ("user_metric_cur");
+	db_query_params ('SELECT * INTO user_metric_cur FROM user_metric_next',
+			 array()) ;
+	db_query_params ('SELECT * INTO user_metric_tmp_cur FROM user_metric_tmp_next',
+			 array()) ;
+	db_drop_table_if_exists ("user_metric_tmp_next");
+	db_drop_table_if_exists ("user_metric_next");
 }
 
-db_commit();
-db_query("DELETE FROM user_metric;");
-db_query("INSERT INTO user_metric SELECT * FROM user_metric".($i-1).";");
-//$err .= '<p>'.db_error().'<p>';
+db_query_params ('DELETE FROM user_metric',
+		 array()) ;
+db_query_params ('INSERT INTO user_metric SELECT * FROM user_metric_cur',
+		 array()) ;
 
-/*
-	Now run through and drop the tmp tables
-*/
-// $err .= "<p>Cleaning up tables<p>";
+db_drop_table_if_exists ("user_metric_tmp_cur");
+db_drop_table_if_exists ("user_metric_cur");
 
-for ($i=1; $i<9; $i++) {
-	db_drop_table_if_exists ("user_metric_tmp1_".$i);
-	db_drop_sequence_if_exists ("user_metric_tmp1_".$i."_ranking_seq");
-	db_drop_table_if_exists ("user_metric".$i);
-};
+db_commit();
 
-$err .= db_error();
-
 $t = time();
 $ts_month = date('Ym', $t);
 $ts_day = date('d', $t);




More information about the Fusionforge-commits mailing list