| <?php |
| |
| require_once("/home/data/httpd/eclipse-php-classes/system/dbconnection_bugs_ro.class.php"); |
| require_once("/home/data/httpd/eclipse-php-classes/system/dbconnection_dashboard_rw.class.php"); |
| require_once($_SERVER['DOCUMENT_ROOT'] . "/projects/stats/config.php"); |
| require_once($_SERVER['DOCUMENT_ROOT'] . "/projects/stats/hostname.php"); |
| require_once($_SERVER['DOCUMENT_ROOT'] . "/projects/common/projects-info.class.php"); |
| |
| // function countbugs($whereclause, $bugzilla_conn) |
| // |
| // Count the number of bugs with the specified clause. |
| function countbugs($whereclause, $bugzilla_conn){ |
| $sql_info = "SELECT count(*) |
| FROM bugs |
| WHERE " . $whereclause; |
| $rs = mysql_query($sql_info, $bugzilla_conn) or die("Error: ".mysql_error()); |
| $myrow = mysql_fetch_row($rs); |
| $value = $myrow[0]; |
| |
| return $value; |
| } |
| |
| // function total_bugs($product_id, $dbh_bugzilla) |
| // |
| // Get the total number of bugs for the last 7, 30 and 180 days for the |
| // specified project. |
| function total_bugs($product_id, $dbh_bugzilla){ |
| # Get the number of bugs today, a week ago, a month ago, six months ago |
| $B0 = countbugs("$product_id", |
| $dbh_bugzilla); |
| $B7 = countbugs("$product_id |
| AND creation_ts < DATE_SUB(NOW(), INTERVAL 7 DAY)", |
| $dbh_bugzilla); |
| $B30 = countbugs("$product_id |
| AND creation_ts < DATE_SUB(NOW(), INTERVAL 1 MONTH)", |
| $dbh_bugzilla); |
| $B180 = countbugs("$product_id |
| AND creation_ts < DATE_SUB(NOW(), INTERVAL 6 MONTH)", |
| $dbh_bugzilla); |
| |
| return array($B0,$B7,$B30,$B180); |
| } |
| |
| // function closed_bugs($product_id, $dbh_bugzilla) |
| // |
| // Get the total number of closed bugs for the last 7, 30 and 180 days for |
| // the specified project. |
| function closed_bugs($product_id, $dbh_bugzilla){ |
| # Get the number of closed bugs today, etc. |
| $C0 = countbugs("(bug_status = 'RESOLVED' OR bug_status = 'VERIFIED' |
| OR bug_status = 'CLOSED') AND $product_id", |
| $dbh_bugzilla); |
| $C7 = countbugs("(bug_status = 'RESOLVED' OR bug_status = 'VERIFIED' |
| OR bug_status = 'CLOSED') AND $product_id |
| AND creation_ts < DATE_SUB(NOW(), INTERVAL 7 DAY)", |
| $dbh_bugzilla); |
| $C30 = countbugs("(bug_status = 'RESOLVED' OR bug_status = 'VERIFIED' |
| OR bug_status = 'CLOSED') AND $product_id |
| AND creation_ts < DATE_SUB(NOW(), INTERVAL 1 MONTH)", |
| $dbh_bugzilla); |
| $C180 = countbugs("(bug_status = 'RESOLVED' OR bug_status = 'VERIFIED' |
| OR bug_status = 'CLOSED') AND $product_id |
| AND creation_ts < DATE_SUB(NOW(), INTERVAL 6 MONTH)", |
| $dbh_bugzilla); |
| |
| return array($C0,$C7,$C30,$C180); |
| } |
| |
| // function open_bugs($product_id, $dbh_bugzilla) |
| // |
| // Get the total number of open bugs for the last 7, 30 and 180 days for |
| // the specified project. |
| function open_bugs($product_id, $dbh_bugzilla){ |
| # Get the number of open bugs (P1, P2, P3 non-enhancements) today, etc. |
| $O0 = countbugs("(bug_status = 'UNCONFIRMED' OR bug_status = 'NEW' |
| OR bug_status = 'ASSIGNED' OR bug_status = 'REOPENED') |
| AND (priority = 'P1' OR priority = 'P2' |
| OR (priority = 'P3' AND bug_severity <> 'enchancement')) |
| AND $product_id", |
| $dbh_bugzilla); |
| $O7 = countbugs("(bug_status = 'UNCONFIRMED' OR bug_status = 'NEW' |
| OR bug_status = 'ASSIGNED' OR bug_status = 'REOPENED') |
| AND (priority = 'P1' OR priority = 'P2' OR |
| (priority = 'P3' AND bug_severity <> 'enchancement')) |
| AND $product_id |
| AND creation_ts < DATE_SUB(NOW(), INTERVAL 7 DAY)", |
| $dbh_bugzilla); |
| $O30 = countbugs("(bug_status = 'UNCONFIRMED' OR bug_status = 'NEW' |
| OR bug_status = 'ASSIGNED' OR bug_status = 'REOPENED') |
| AND (priority = 'P1' OR priority = 'P2' |
| OR (priority = 'P3' AND bug_severity <> 'enchancement')) |
| AND $product_id |
| AND creation_ts < DATE_SUB(NOW(), INTERVAL 1 MONTH)", |
| $dbh_bugzilla); |
| $O180 = countbugs("(bug_status = 'UNCONFIRMED' OR bug_status = 'NEW' |
| OR bug_status = 'ASSIGNED' OR bug_status = 'REOPENED') |
| AND (priority = 'P1' OR priority = 'P2' |
| OR (priority = 'P3' AND bug_severity <> 'enchancement')) |
| AND $product_id |
| AND creation_ts < DATE_SUB(NOW(), INTERVAL 6 MONTH)", |
| $dbh_bugzilla); |
| |
| return array($O0,$O7,$O30,$O180); |
| } |
| |
| // function delta($bugs1,$bugs2,$closed1,$closed2,$open1,$open2) |
| // |
| // Determine the deltas for the number of total bugs, closed bugs and |
| // open bugs. |
| function delta($bugs1,$bugs2,$closed1,$closed2,$open1,$open2){ |
| $delta1 = $bugs1 - $bugs2; |
| $delta2 = $closed1 - $closed2; |
| $delta3 = $open1 - $open2; |
| |
| return array($delta1,$delta2,$delta3); |
| } |
| |
| // function gainloss($cdelta,$odelta) |
| // |
| // Simple gain or loss function. |
| function gainloss($cdelta,$odelta){ |
| return $cdelta - $odelta; |
| } |
| |
| // function percents($delta,$gainloss) |
| // |
| // Change the gain or loss as a percentage. |
| function percents($delta,$gainloss){ |
| return round(($gainloss / $delta) * 100); |
| } |
| |
| // function get_product_from_name($name,$db_handle) |
| // |
| // Get the bugzilla product id from the name. |
| function get_product_from_name($name,$db_handle){ |
| $query = "SELECT id FROM products WHERE name = \"".$name."\""; |
| $result = mysql_query($query,$db_handle) or die("MySQL Error: ".mysql_error()); |
| $row = mysql_fetch_assoc($result); |
| |
| return $row['id']; |
| } |
| |
| // function construct_id_query($project,$db_handle) |
| // |
| // Construct the where clause passed to other functions. This function will |
| // basically join all the products for a project using ORs. |
| function construct_id_query($project,$db_handle){ |
| if ($project->hasBugzillaProducts() == 0) |
| return ""; |
| |
| $tmp_query = "("; |
| |
| for($i=0;$i<$project->hasBugzillaProducts();){ |
| $curr_id = str_replace("%20"," ",$project->getBugzillaProduct($i)); |
| $curr_id = get_product_from_name($curr_id,$db_handle); |
| $i++; |
| if ($curr_id == "") // Bugzilla product not found on the DB |
| continue; |
| $tmp_query .= "product_id = \"".$curr_id."\""; |
| if ($i == $project->hasBugzillaProducts()) |
| $tmp_query .= ")"; |
| else |
| $tmp_query .= " OR "; |
| } |
| |
| if ($tmp_query == "(") |
| return ""; |
| else |
| return $tmp_query; |
| |
| } |
| |
| // function flush_pring($text) |
| // |
| // Will force the browser to do a flush print and not wait for more input. |
| function flush_print($text){ |
| echo $text."<br>\n"; |
| ob_flush(); |
| flush(); |
| } |
| |
| // function updateLog($db_handle,$project,$text) |
| // |
| // Update the DB log for users to retrive how processing was done. |
| // The log will be downloaded by |
| // $_SERVER['DOCUMENT_ROOT'] . "projects/dashboard/index.php" |
| // and |
| // $_SERVER['DOCUMENT_ROOT'] . "projects/dashboard/dashboard_detail.php" |
| function updateLog($db_handle,$project,$text){ |
| $text = str_replace("\"","'",$text); // mysql can't handle |
| $query = "SELECT * FROM ".log_table()." WHERE project_id = \"".$project."\""; |
| $result = mysql_query($query,$db_handle); |
| if ($result && mysql_num_rows($result)){ |
| $query = "UPDATE ".log_table()." SET bugs_text = \"".$text."\""; |
| $query .= " WHERE project_id = \"".$project."\""; |
| }else{ |
| $query = "INSERT INTO ".log_table()." (project_id, bugs_text) "; |
| $query .= "VALUES(\"".$project."\",\"".$text."\")"; |
| } |
| |
| #echo "<pre>".$text."</pre><br>\n"; |
| mysql_query($query,$db_handle) or die("Error: ".mysql_error()); |
| } |
| |
| // function updateStats($db_handle,$day,$project,$mysql_date,$bugs,$delta, |
| // $perc,$gl) |
| // Update the bugs stats in the DB. Perform a query to determine if |
| // there is already information in the table as we only allow one stat per |
| // project per day. Also prevents getting errors if a different stats program |
| // (news, mail, articles, etc) created the row. |
| function updateStats($db_handle,$day,$project,$mysql_date,$bugs,$delta, |
| $perc,$gl){ |
| $query = "SELECT * FROM ".stats_table()." WHERE project_id = \"".$project."\" AND stats_date = \"".$mysql_date."\""; |
| $result = mysql_query($query,$db_handle); |
| if ($result && mysql_num_rows($result)) |
| updateBugs($db_handle,$day,$project,$mysql_date,$bugs,$delta,$perc,$gl); |
| else |
| insertBugs($db_handle,$day,$project,$mysql_date,$bugs,$delta,$perc,$gl); |
| } |
| |
| // function updateBugs($db_handle,$day,$project,$mysql_date,$bugs,$delta, |
| // $perc,$gl) |
| // |
| // This will perform an update on an alreay existing row for a project on |
| // the stats table. |
| function updateBugs($db_handle,$day,$project,$mysql_date,$bugs,$delta, |
| $perc,$gl){ |
| $query = "UPDATE ".stats_table()." SET bugs_total = ".$bugs.", bugs_".$day."_delta = ".$delta.", bugs_".$day."_percentage = ".$perc.", bugs_".$day."_gainloss = ".$gl; |
| $query .= " WHERE project_id = \"".$project."\" AND stats_date = \"".$mysql_date. |
| "\""; |
| |
| echo "<pre>$query</pre>"; |
| mysql_query($query,$db_handle); |
| } |
| |
| // function insertBugs($db_handle,$day,$project,$mysql_date,$bugs,$delta, |
| // $perc,$gl){ |
| // |
| // This will perform a single project/row insert on the stats table. |
| function insertBugs($db_handle,$day,$project,$mysql_date,$bugs,$delta, |
| $perc,$gl){ |
| $query = "INSERT INTO ".stats_table()." (project_id, stats_date, bugs_total, bugs_".$day."_delta, bugs_".$day."_percentage, bugs_".$day."_gainloss) VALUES (\"".$project."\",\"".$mysql_date."\",".$bugs.",".$delta.",".$perc.",".$gl.")"; |
| |
| echo "<pre>$query</pre>"; |
| mysql_query($query,$db_handle); |
| } |
| |
| |
| echo "<html>\n<body>\n"; |
| |
| # Connection to the bugzilla DB |
| $dbc_bugzilla = new DBConnectionBugs(); |
| $dbh_bugzilla = $dbc_bugzilla->connect(); |
| |
| # Connection to the dashboard DB |
| $db_connection = new DBConnectionDashboard(); |
| $db_handle = $db_connection->connect(); |
| |
| $mysql_date = date('Y-m-d'); |
| |
| # List of projects |
| $projects = New ProjectsInfo(); |
| |
| # Process all projects using ProjectInfo objects |
| $project_count = $projects->hasProjects(); |
| echo "Total number of projects to process: ".$project_count."<br>\n"; |
| for($pindex=0;$pindex<$project_count;$pindex++){ |
| $project = $projects->getProject($pindex); |
| if ($project->exclude_from_dashboard()) // Don't process projects with |
| continue; // this XML tag |
| flush_print("<b>Processing project: ".$project->getName()."</b>"); |
| $text = "Bugs information for project ".$project->getName()."\r\n\r\n"; |
| $id = construct_id_query($project,$dbh_bugzilla); |
| |
| // If we couldn't find any product IDs for this project then update log and |
| // continue with the next project. |
| if ($id == ""){ |
| flush_print("No valid bugzilla info found for ".$project->getName()); |
| $text .= "No valid bugzilla info found for: ".$project->getName()."\r\n\r\n"; |
| $days = explode(",",BUGS_DAYS); |
| while(count($days)){ |
| $day = array_pop($days); |
| updateStats($db_handle,$day,$project->getProjectID(),$mysql_date,-1,0,-1,-1); |
| } |
| continue; |
| } |
| |
| $perc = array(); |
| $gl = array(); |
| |
| // Get bugs: total, closed and open |
| $bugs = total_bugs($id, $dbh_bugzilla); |
| $closed = closed_bugs($id, $dbh_bugzilla); |
| $open = open_bugs($id, $dbh_bugzilla); |
| |
| // Write to log (Bugs) |
| $text .= "Bugs,Today,Last week,Last month,Last six months\r\n"; |
| $text .= "Total,".$bugs[0].",".$bugs[1].",".$bugs[2].",".$bugs[3]."\r\n"; |
| $text .= "Closed,".$closed[0].",".$closed[1].",".$closed[2].",".$closed[3]."\r\n"; |
| $text .= "Open,".$open[0].",".$open[1].",".$open[2].",".$open[3]."\r\n\r\n"; |
| |
| // Compute deltas for the last 7, 30 and 180 days |
| $delta7 = delta($bugs[0],$bugs[1],$closed[0],$closed[1],$open[0],$open[1]); |
| $delta30 = delta($bugs[0],$bugs[2],$closed[0],$closed[2],$open[0],$open[2]); |
| $delta180 = delta($bugs[0],$bugs[3],$closed[0],$closed[3],$open[0],$open[3]); |
| $deltas[0] = $delta7[0]; |
| $deltas[1] = $delta30[0]; |
| $deltas[2] = $delta180[0]; |
| |
| // Write to log (Deltas) |
| $text .= "Deltas,Last week,Last month,Last six months\r\n"; |
| $text .= "Total,".$delta7[0].",".$delta30[0].",".$delta180[0]."\r\n"; |
| $text .= "Closed,".$delta7[1].",".$delta30[1].",".$delta180[1]."\r\n"; |
| $text .= "Open,".$delta7[2].",".$delta30[2].",".$delta180[2]."\r\n\r\n"; |
| |
| // Compute gain-loss for the last 7, 30 and 180 days |
| $gl[0] = gainloss($delta7[1],$delta7[2]); |
| $gl[1] = gainloss($delta30[1],$delta30[2]); |
| $gl[2] = gainloss($delta180[1],$delta180[2]); |
| |
| // Write to log (Gain-Loss) |
| $text .= "Gain-Loss,Last week,Last month,Last six months\r\n"; |
| $text .= ",".$gl[0].",".$gl[1].",".$gl[2]."\r\n\r\n"; |
| |
| // Compute gain/loss as percentages |
| $perc[0] = $delta7[0]?percents($delta7[0],$gl[0]):0; |
| $perc[1] = $delta30[0]?percents($delta30[0],$gl[1]):0; |
| $perc[2] = $delta180[0]?percents($delta180[0],$gl[2]):0; |
| |
| // Write to log (Percentages) |
| $text .= "Percentages,Last week,Last month,Last six months\r\n"; |
| $text .= ",".$perc[0].",".$perc[1].",".$perc[2]."\r\n"; |
| |
| // Store the computed stats in the DB |
| $days = explode(",",BUGS_DAYS); |
| $i = 0; |
| while(count($days)){ # For every expected date |
| $day = array_pop($days); |
| updateStats($db_handle,$day,$project->getProjectID(),$mysql_date,$bugs[0],$deltas[$i],$perc[$i],$gl[$i]); |
| $i++; |
| } |
| updateLog($db_handle,$project->getProjectID(),$text); |
| } |
| |
| echo "\n</body>\n</html>\n"; |
| |
| $dbc_bugzilla->disconnect(); |
| //$db_connection->disconnect(); |
| |
| $rs = null; |
| $dbh_bugzilla = null; |
| $dbc_bugzilla = null; |
| $db_handle = null; |
| $db_connection = null; |
| |
| ?> |