// | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Jesus M. Castagnetto <*****@*****.**> | // +----------------------------------------------------------------------+ // // $Id: ex_stats_simple.php,v 1.3 2003/01/04 11:55:39 mj Exp $ // /** * @package Math_Stats */ require_once "Math/Stats.php"; // making some data sets $data = array(2, 2.3, 4.5, 2, 2, 3.2, 5.3, 3, 4, 5, 1, 6); $dnulls = array(1.165, null, "foo", 0.6268, 0.6268, 0.0751, 0.3516, -0.6965); // instantiating a Math_Stats object $s = new Math_Stats(); echo "*** Original data set\n"; print_r($data); $s->setData($data); echo "Basic statistics\n"; print_r($s->calcBasic()); echo "\n*** A data set with nulls\n"; print_r($dnulls); echo "Let's generate an error\n"; print_r($s->setData($dnulls)); echo "Ignoring nulls and trying again\n"; $s->setNullOption(STATS_IGNORE_NULL); $s->setData($dnulls); echo "---> data after ignoring (removing) nulls\n"; print_r($s->getData()); echo "---> stats\n";
/** * Returns workload information for the specified date range and interval. * * @param string $interval The interval to use in this report. * @param string $type If this report is aggregate or individual * @param string $start The start date of this report. * @param string $end The end date of this report. * @param integer $category_id The category to restrict this report to * @return array An array containing workload data. */ public static function getWorkloadByDateRange($interval, $type, $start, $end, $category_id) { $data = array(); $category_id = (int) $category_id; // figure out the correct format code switch ($interval) { case 'day': $format = '%m/%d/%y'; $order_by = "%1\$s"; break; case 'dow': $format = '%W'; $order_by = "CASE WHEN DATE_FORMAT(%1\$s, '%%w') = 0 THEN 7 ELSE DATE_FORMAT(%1\$s, '%%w') END"; break; case 'week': if ($type == 'aggregate') { $format = '%v'; } else { $format = '%v/%y'; } $order_by = "%1\$s"; break; case 'dom': $format = '%d'; break; case 'month': if ($type == 'aggregate') { $format = '%b'; $order_by = "DATE_FORMAT(%1\$s, '%%m')"; } else { $format = '%b/%y'; $order_by = "%1\$s"; } break; default: throw new LogicException('Invalid interval'); } // get issue counts $stmt = 'SELECT DATE_FORMAT(iss_created_date, ?), count(*) FROM {{%issue}} WHERE iss_prj_id=? AND iss_created_date BETWEEN ? AND ?'; $params = array($format, Auth::getCurrentProject(), $start, $end); if (!empty($category_id)) { $stmt .= ' AND iss_prc_id = ?'; $params[] = $category_id; } $stmt .= ' GROUP BY DATE_FORMAT(iss_created_date, ?)'; $params[] = $format; if (!empty($order_by)) { $stmt .= "\nORDER BY " . sprintf($order_by, 'iss_created_date'); } try { $res = DB_Helper::getInstance()->fetchAssoc($stmt, $params); } catch (DbException $e) { return array(); } $data['issues']['points'] = $res; $data['issues']['stats'] = array('total' => 0, 'avg' => 0, 'median' => 0, 'max' => 0); if ($res) { $stats = new Math_Stats(); $stats->setData($res); $data['issues']['stats'] = array('total' => $stats->sum(), 'avg' => $stats->mean(), 'median' => $stats->median(), 'max' => $stats->max()); } // get email counts $params = array(); $stmt = 'SELECT DATE_FORMAT(sup_date, ?), count(*) FROM {{%support_email}}, {{%email_account}}'; $params[] = $format; if (!empty($category_id)) { $stmt .= ', {{%issue}}'; } $stmt .= ' WHERE sup_ema_id=ema_id AND ema_prj_id=? AND sup_date BETWEEN ? AND ?'; $params[] = Auth::getCurrentProject(); $params[] = $start; $params[] = $end; if (!empty($category_id)) { $stmt .= ' AND sup_iss_id = iss_id AND iss_prc_id = ?'; $params[] = $category_id; } $stmt .= ' GROUP BY DATE_FORMAT(sup_date, ?)'; $params[] = $format; if (!empty($order_by)) { $stmt .= "\nORDER BY " . sprintf($order_by, 'sup_date'); } try { $res = DB_Helper::getInstance()->fetchAssoc($stmt, $params); } catch (DbException $e) { return array(); } $data['emails']['points'] = $res; if (count($res) > 0) { $stats = new Math_Stats(); $stats->setData($res); $data['emails']['stats'] = array('total' => $stats->sum(), 'avg' => $stats->mean(), 'median' => $stats->median(), 'max' => $stats->max()); } else { $data['emails']['stats'] = array('total' => 0, 'avg' => 0, 'median' => 0, 'max' => 0); } return $data; }
/** * Returns information about time to close and time to first response. * * @access private * @return array Array of counts. */ function getTimeStats() { // time to close $stmt = "SELECT\n round(((unix_timestamp(iss_closed_date) - unix_timestamp(iss_created_date)) / 60))\n FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue\n WHERE\n iss_closed_date IS NOT NULL AND \n " . $this->getWhereClause("iss_customer_id", array("iss_created_date", "iss_closed_date")); $res = $GLOBALS["db_api"]->dbh->getCol($stmt); if (PEAR::isError($res)) { Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); return array(); } if (count($res) > 0) { $stats = new Math_Stats(); $stats->setData($res); $time_to_close = array("avg" => $stats->mean(), "avg_formatted" => Misc::getFormattedTime($stats->mean()), "median" => $stats->median(), "median_formatted" => Misc::getFormattedTime($stats->median()), "max" => $stats->max(), "max_formatted" => Misc::getFormattedTime($stats->max()), "min" => $stats->min(), "min_formatted" => Misc::getFormattedTime($stats->min())); } else { $time_to_close = array("avg" => 0, "avg_formatted" => Misc::getFormattedTime(0), "median" => 0, "median_formatted" => Misc::getFormattedTime(0), "max" => 0, "max_formatted" => Misc::getFormattedTime(0), "min" => 0, "min_formatted" => Misc::getFormattedTime(0)); } // time to first response $stmt = "SELECT\n round(((unix_timestamp(iss_first_response_date) - unix_timestamp(iss_created_date)) / 60))\n FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue\n WHERE\n iss_first_response_date IS NOT NULL AND \n " . $this->getWhereClause("iss_customer_id", array("iss_created_date", "iss_closed_date")); $res = $GLOBALS["db_api"]->dbh->getCol($stmt); if (PEAR::isError($res)) { Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); return array(); } if (count($res) > 0) { $stats = new Math_Stats(); $stats->setData($res); $time_to_first_response = array("avg" => $stats->mean(), "avg_formatted" => Misc::getFormattedTime($stats->mean()), "median" => $stats->median(), "median_formatted" => Misc::getFormattedTime($stats->median()), "max" => $stats->max(), "max_formatted" => Misc::getFormattedTime($stats->max()), "min" => $stats->min(), "min_formatted" => Misc::getFormattedTime($stats->min())); } else { $time_to_first_response = array("avg" => 0, "avg_formatted" => Misc::getFormattedTime(0), "median" => 0, "median_formatted" => Misc::getFormattedTime(0), "max" => 0, "max_formatted" => Misc::getFormattedTime(0), "min" => 0, "min_formatted" => Misc::getFormattedTime(0)); } return array("time_to_close" => $time_to_close, "time_to_first_response" => $time_to_first_response); }
/** * Returns workload information for the specified date range and interval. * * @access public * @param string $interval The interval to use in this report. * @param string $type If this report is aggregate or individual * @param string $start The start date of this report. * @param string $end The end date of this report. * @return array An array containing workload data. */ function getWorkloadByDateRange($interval, $type, $start, $end) { $data = array(); $start = Misc::escapeString($start); $end = Misc::escapeString($end); // figure out the correct format code switch ($interval) { case "day": $format = '%m/%d/%y'; $order_by = "%1\$s"; break; case "dow": $format = '%W'; $order_by = "IF(DATE_FORMAT(%1\$s, '%%w') = 0, 7, DATE_FORMAT(%1\$s, '%%w'))"; break; case "week": if ($type == "aggregate") { $format = '%v'; } else { $format = '%v/%y'; } $order_by = "%1\$s"; break; case "dom": $format = '%d'; break; case "month": if ($type == "aggregate") { $format = '%b'; $order_by = "DATE_FORMAT(%1\$s, '%%m')"; } else { $format = '%b/%y'; $order_by = "%1\$s"; } break; } // get issue counts $stmt = "SELECT\n DATE_FORMAT(iss_created_date, '{$format}'),\n count(*)\n FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue\n WHERE\n iss_prj_id=" . Auth::getCurrentProject() . " AND\n iss_created_date BETWEEN '{$start}' AND '{$end}'\n GROUP BY\n DATE_FORMAT(iss_created_date, '{$format}')"; if (!empty($order_by)) { $stmt .= "\nORDER BY " . sprintf($order_by, 'iss_created_date'); } $res = $GLOBALS["db_api"]->dbh->getAssoc($stmt); if (PEAR::isError($res)) { Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); return array(); } $data["issues"]["points"] = $res; if (count($res) > 0) { $stats = new Math_Stats(); $stats->setData($res); $data["issues"]["stats"] = array("total" => $stats->sum(), "avg" => $stats->mean(), "median" => $stats->median(), "max" => $stats->max()); } else { $data["issues"]["stats"] = array("total" => 0, "avg" => 0, "median" => 0, "max" => 0); } // get email counts $stmt = "SELECT\n DATE_FORMAT(sup_date, '{$format}'),\n count(*)\n FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "support_email,\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "email_account\n WHERE\n sup_ema_id=ema_id AND\n ema_prj_id=" . Auth::getCurrentProject() . " AND\n sup_date BETWEEN '{$start}' AND '{$end}'\n GROUP BY\n DATE_FORMAT(sup_date, '{$format}')"; if (!empty($order_by)) { $stmt .= "\nORDER BY " . sprintf($order_by, 'sup_date'); } $res = $GLOBALS["db_api"]->dbh->getAssoc($stmt); if (PEAR::isError($res)) { Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); return array(); } $data["emails"]["points"] = $res; if (count($res) > 0) { $stats = new Math_Stats(); $stats->setData($res); $data["emails"]["stats"] = array("total" => $stats->sum(), "avg" => $stats->mean(), "median" => $stats->median(), "max" => $stats->max()); } else { $data["emails"]["stats"] = array("total" => 0, "avg" => 0, "median" => 0, "max" => 0); } return $data; }
// | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Jesus M. Castagnetto <*****@*****.**> | // +----------------------------------------------------------------------+ // // $Id: ex_stats_cummulative_data.php,v 1.3 2003/01/04 11:55:39 mj Exp $ // /** * @package Math_Stats */ require_once "Math/Stats.php"; // making some cummulative data sets $data = array("3" => 4, "2.333" => 5, "1.22" => 6, "0.5" => 3, "0.9" => 2, "2.4" => 7); $dnulls = array("3" => 4, "caca" => 2, "bar is not foo" => 6, "0.5" => 3, "0.9" => 2, "2.4" => 7); // instantiate a Math_Stats object $s = new Math_Stats(); $s->setData($data, STATS_DATA_CUMMULATIVE); echo "*** Original cummulative data set\n"; print_r($data); // let's print some simple statistics echo "Simple stats from cummulative data, note the count\n"; print_r($s->calcBasic()); // now, lets generate an error by using the $dnulls array echo "\n*** Another cummulative data set\n"; print_r($dnulls); echo "Generating an error by using data with nulls\n"; print_r($s->setData($dnulls, STATS_DATA_CUMMULATIVE)); // let's ignore nulls echo "Ignoring the nulls and trying again\n"; $s->setNullOption(STATS_IGNORE_NULL); $s->setData($dnulls, STATS_DATA_CUMMULATIVE);