在bugzilla中,由于要用到一些自定义报告的方式,如缺陷生命周期、缺陷趋势数据 ...,,这两种报告bugzilla工具没有提供此报告,所以想办法写了写,实现统计数据方法,并用网页形式出
1.自定义了一张表,来记录缺陷趋势数据,写了一个程序,来实现数据统计功能;
2.修改bugzilla的页面程序,使之每当新建BUG,或bug属性变化时,就自己动更新一次统计数据;
3.缺陷报告,实现了数据的统计,缺陷趋势数据、缺陷生命周期、缺陷原因、按模块统计、按严重级别等几个功能;
the last ,用perl实现图形报表时,一直没有做出来,知道是不是模块的与操作系统的原因,我已经安装过图形包,而bugzilla自己的图形报告也出不来, so,就只能是数据报表输出,再放到excle表中,就出图了:)
此程序的完成得感谢谢杨明同志的辛苦,哈哈,杨明是好同志啊 ~!~
下面是两个程序的代码:
==================================== 报表程序 ======================================
#!/usr/bin/perl
###############################################################
# Author : liulin
#
# Last Edit date : 2010-11-18
#
# change comment : 统计方法有BUG,BUG的生命周期方法修正
################################################################
use strict;
use warnings;
use Switch;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use DBI;
my $bugzillaDBaddr="localhost";
my $bugzillaDBdatabase="bugs";
my $bugzillaDBusr="bugs";
my $bugzillaDBpwd="123123";
# Connect to the database.
my $dbh = DBI->connect("DBI:mysql:".
"database=".$bugzillaDBdatabase
.";host=".$bugzillaDBaddr,
$bugzillaDBusr ,
$bugzillaDBpwd ,
{'RaiseError' => 1});
# test mysql conn
eval { $dbh->do("SET NAMES gbk;") };
print "Set chareset failed: $@/n" if $@;
############################################################
# function : 主执行函数
# parameter:
# return :
############################################################
sub main{
print header(-charset => 'gb18030');
print start_html('statistic bugs data ....');
printHead();
do_work();
print end_html;
}
############################################################
# function : Statistic bugs total trend
# parameter:
# return :
############################################################
sub statisticTrend{
#sql statement handle
my $sth = $dbh->prepare("select substr(bug_time,1,10) as date,bug_sum as bugs_total, bug_close_sum as bugs_closed_total,bug_open_sum from bug_sum_data");
$sth->execute();
print "
日期 | BUG总数 | 关闭总数 | 打开总数 |
$ref->{'date'} | $ref->{'bugs_total'} | $ref->{'bugs_closed_total'} | $ref->{'bug_open_sum'} |
############################################################
# function : Statistic bugs reason
# parameter:
# return :
############################################################
sub statisticReason{
# sql statement handle
my $sth = $dbh->prepare('select cf_bugwhen,count(*) as bugNum from bugs group by cf_bugwhen;');
$sth->execute();
print "
bugcause | bugnum |
$ref->{'cf_bugwhen'} | $ref->{'bugNum'} |
############################################################
# function : 按模块统计 每个模块的bug数
# parameter:
# return :
############################################################
sub statisticModel{
# sql statement handle
my $sth = $dbh->prepare(' select comp.name as compname,count(*) as bugnumbycomp from components comp inner join bugs on comp.id = bugs.component_id group by comp.id order by comp.id;');
$sth->execute();
print "
compname | bugnumbycomp |
$ref->{'compname'} | $ref->{'bugnumbycomp'} |
############################################################
# function : 按严重级别统计
# parameter:
# return :
############################################################
sub statisticSerious{
# sql statement handle
my $sth = $dbh->prepare('select bug_severity,count(bug_severity) as bugNum from bugs group by bug_severity;');
$sth->execute();
print "
bugseverity | bugnum |
$ref->{'bug_severity'} | $ref->{'bugNum'} |
############################################################
# function : 执行统计
# parameter:
# return :
############################################################
sub do_work {
my(@values,$key);
print "
############################################################
# function : 输出选择信息
# parameter:
# return :
############################################################
sub printHead{
print start_form;
print "
请选择你要统计的类型:
";
print popup_menu(-name=>'counType',
#-values=>['trend','Lifetime','Reason','model', 'Serious'],
-values=>['缺陷趋势数据','缺陷生命周期','缺陷原因','按模块统计', '按严重级别'],
-default=>'缺陷趋势数据'), '
';
print submit('Action','提交');
print "
回首页";
print end_form;
}
############################################################
## function : Get Current Time
## parameter:-
## return : $date
## -- sameple: 2008-10-10
#############################################################
sub GetCurrentDate{
my ($sec, $min, $hour, $day, $month, $year) = localtime;
my $date = sprintf("%04u-%02u-%02u", $year + 1900, $month + 1, $day);
return $date;
}
############################################################
# function : 统计 bug 生命周期
# parameter:
# return :
############################################################
sub statisticLifetime
{
# 取当前日期 style 2010-08-19
my $currentDate=DateAddAndSub(GetCurrentDate(), 1); #取当前系统时间
# 取第一条记录时间
my $sqlstm="select date_add(date(min(creation_ts)), interval -1 day) as firstDate from bugs";
my $sth = $dbh->prepare($sqlstm);
$sth->execute();
my $ref = $sth->fetchrow_hashref();
my $fstRecordDate = $ref->{'firstDate'};#取第一条记录时间
my $beginDate=$currentDate; #统计开始的日期
my $endDat=$currentDate; #统计结束的日期
# 输出表格开头样式
print "
生存周期 | 新建 | 打开 | 已修复 | 重新打开 | 拒绝 | 挂起 |
############################################################
# function : 执行 bug生命周期 统计
# parameter: $beginDate -- 统计起始的日期
# $endDat -- 统计结束的日期
# $showDateStyle -- 输出的日期样式
# return :
############################################################
sub ExcuteStasticBugLifeTime{
my $beginDate = "/"".shift."/"";
my $endDat = "/"".shift."/"";
my $showDateStyle = shift;
#print "beginDate:$beginDate.endDat$endDat
" ;
# 查询具体数据;
my $sqlstm = "select TIMESTAMPDIFF(day,$beginDate,date_add(current_date, interval 1 day) ) as daynum, "
."(select count(*) from bugs where bug_status=(select value from bug_status where id = 6) "
."and creation_ts between $beginDate and $endDat ) as new , "
."(select count(*) from bugs where bug_status=(select value from bug_status where id = 7) "
."and creation_ts between $beginDate and $endDat) as open , "
."(select count(*) from bugs where bug_status=(select value from bug_status where id = 8) "
."and creation_ts between $beginDate and $endDat ) as repaire , "
."(select count(*) from bugs where bug_status=(select value from bug_status where id = 9) "
."and creation_ts between $beginDate and $endDat ) as reopen , "
."(select count(*) from bugs where bug_status=(select value from bug_status where id = 11) "
."and creation_ts between $beginDate and $endDat ) as reject , "
."(select count(*) from bugs where bug_status=(select value from bug_status where id = 13) "
."and creation_ts between $beginDate and $endDat ) as hang ";
my $sth = $dbh->prepare($sqlstm);
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
my $temp = $ref->{'daynum'};
if($ref->{'new'}!= 0 || ($ref->{'open'} != 0)|| ($ref->{'repaire'}!=0)# 如果如果不为空,才输出数据
|| ($ref->{'reopen'}!=0) || ($ref->{'reject'} !=0) || ($ref->{'hang'} !=0) )
{
print "
############################################################
# function : 日期加减 只能是日期的;利用mysqlDB实现(用perl实现太麻烦)
#
# parameter: $date -- 要加减的日期
# $days -- 要加减的天数 用(+ - 数值 表示加减)
#
# return : $date -- 加减后的日期 style 2010-10-09
############################################################
sub DateAddAndSub
{
# 把参数取出来
my $date=shift;
my $days=shift;
# 从DB中取日期
my $sqlstm="select date_add(/"".$date."/", interval ".$days." day) as firstDate";
my $sth = $dbh->prepare($sqlstm);
$sth->execute();
my $ref = $sth->fetchrow_hashref();
$date = $ref->{'firstDate'}; #取出加减后的日期
return $date;
}
main();
====================================end ===========================================
==================================== 统计数据程序 ====================================
#!/usr/bin/perl -w
###############################################################
# author : liulin
# Last Edit date : 2010-11-12
# change comment : 数据插入有bug,总是
###############################################################
use strict;
use DBI();
my $bugzillaDBaddr="localhost";
my $bugzillaDBdatabase="bugs";
my $bugzillaDBusr="bugs";
my $bugzillaDBpwd="123123";
# Connect to the database.
my $dbh = DBI->connect("DBI:mysql:".
"database=".$bugzillaDBdatabase
.";host=".$bugzillaDBaddr,
$bugzillaDBusr ,
$bugzillaDBpwd ,
{'RaiseError' => 1});
# test mysql conn
eval { $dbh->do("SET NAMES utf8;") };
print "Set chareset failed: $@/n" if $@;
sub main
{
initBootSystem();
createDatatable();
insertData();
}
###############################################################
# function : init the start insert date ready!
# parameter:
# return :
###############################################################
sub initBootSystem
{
my $fileExist = `grep insertCountDataTobug_sum_data.cgi /etc/rc.d/rc.local`;
if ($fileExist eq '')
{
system ('echo "/var/www/html/bugzilla/insertCountDataTobug_sum_data.cgi &" >> /etc/rc.d/rc.local');
# system ('./insertCountDataTobug_sum_data.cgi &'); # It at the ./checksetup.pl file;
}
}
###############################################################
# function : create table bug_sum_data
# parameter:
# return :
###############################################################
sub createDatatable{
my $sth1 = $dbh->prepare("create table IF NOT EXISTS bug_sum_data(bug_date date,bug_time datetime,bug_open_sum int,bug_close_sum int,bug_sum int);");
$sth1->execute();
}
###############################################################
# function : insert count data of current date into bug_sum_data
# parameter:
# return :
###############################################################
sub insertData {
#print dateCompare()."..../n";
if(dateCompare() == 1){ # 如果日期相等,执行更新数据;
my $sqlsmt = 'update bug_sum_data set '
# .'bug_open_sum=(select count(*)+1 from bugs where bug_status in (select value from bug_status where is_open =1)), '
.'bug_time=(select now()), '
.'bug_open_sum=(select count(*) from bugs where bug_status in (select value from bug_status where is_open =1)),'
.'bug_close_sum=(select count(*) from bugs where bug_status in (select value from bug_status where is_open =0)),'
.'bug_sum=(select count(*) from bugs) where bug_date=/''.GetCurrentTime().'/';';
#print $sqlsmt;
my $sth = $dbh->prepare($sqlsmt);
$sth->execute();
#print " date is eq , insertdat date is eq , update/n";
}elsif (dateCompare() == -2){ #-2 -- bugs 表中没有数据, 什么事也不做;
# do nothing;
#print "bugs data is null, do nothing/n";
}else{ # 0 or -1 , bug_sum_data is null,或 日期相不等, 向表中插放数据
my $sth2 = $dbh->prepare('insert into bug_sum_data values '
.'((select substr((select now()),1,10)),'
.'(select now()),'
.'(select count(*) as openCount from bugs where bug_status in (select value from bug_status where is_open =1)),'
.'(select count(*) as openCount from bugs where bug_status in (select value from bug_status where is_open =0)),'
.'(select count(*) from bugs))');
$sth2->execute();
#print "日期不等,or bug_sum_data is null, insertData! /n";
}
} # end insertData
############################################################
# function : current date compare with last date from
# bug_time of bug_sum_data
# parameter:
# return : bool
# 1 -- eq
# 0 -- not eq
# -1 -- bug_sum_data 表中没有数据
# -2 -- bugs 表中没有数据
############################################################
sub dateCompare
{
#query bugs
my $sth_bugisnull = $dbh->prepare('select count(*) as bugisnull from bugs;');
$sth_bugisnull->execute();
# 看源数据是否为空,如果不是,则比较
my $resultbugisnull = $sth_bugisnull->fetchrow_hashref();
if( $resultbugisnull->{'bugisnull'}!=0){
#print "$resultbugisnull->{'bugisnull'}/n";
#query bug_sum_data
my $sth_selectisnull = $dbh->prepare('select count(*) as isnull from bug_sum_data;');
$sth_selectisnull->execute();
my $resultisnull = $sth_selectisnull->fetchrow_hashref();
if($resultisnull->{'isnull'}==0){
return -1; # 如果统计表为空,则返回-1
}
else{ # 统计表中有数据
my $sth_select = $dbh->prepare('select substr(max(bug_time),1,10) as bugCreatDate from bug_sum_data');
$sth_select->execute();
my $result = $sth_select->fetchrow_hashref();
if ($result->{'bugCreatDate'} eq GetCurrentTime() ) # 程序还有一个问题,当是大于时,数据还是要插入
{ return 1 ;} # eq
else
{ return 0 ;} # not eq
}
}
else{
return -2; # bugs 表中没有数据
}
}
############################################################
# function : Get Current Time
# parameter:
# return : $date
# -- sameple: 2008-10-10
############################################################
sub GetCurrentTime{
my ($sec, $min, $hour, $day, $month, $year) = localtime;
my $date = sprintf("%04u-%02u-%02u",
$year + 1900, $month + 1, $day);
}
main();
====================================end ===========================================
================================== 新增加的表结构 =====================================
--
-- Table structure for table `bug_sum_data`
--
DROP TABLE IF EXISTS `bug_sum_data`;
SET @saved_cs_client = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `bug_sum_data` (
`bug_date` date default NULL,
`bug_time` datetime default NULL,
`bug_open_sum` int(11) default NULL,
`bug_close_sum` int(11) default NULL,
`bug_sum` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SET character_set_client = @saved_cs_client;
====================================end ===========================================