Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / bin / bugsbydate
1 #!/dev/c/Progra~1/Rational/ClearQuest/CQPerl
2 use strict;
3 use CQPerlExt;
4
5 my $cquser   = "reporter";
6 my $cqpasswd = "news";
7 my $cqdb     = "BUGS2";
8 my ($session, $query, $results, $status);
9 my ($junk, $mday, $mon, $year, $from_date, $to_date);
10
11 my @months = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
12
13 sub date2jul {
14   my $date = shift;
15
16   my ($year, $month, $day);
17   my ($juldate, $juldays);
18
19   ($year, $month, $day) = split /-/, $date;
20
21   $juldays = @months [$month-1] + $day;
22   $juldate = $year . "-" . $juldays;
23   return $juldate;
24 } # date2jul
25
26 sub jul2date {
27   my $juldate = shift;
28
29   my ($year, $juldays);
30   my $month = 0;
31   my $day = 0;
32
33   ($year, $juldays)  = split /-/, $juldate;
34
35   foreach my $nbr_of_days (@months) {
36     last if ($juldays < $nbr_of_days);
37     $month++;
38   } # foreach
39
40   $day = $juldays - @months [$month - 1];
41
42   if ($month < 10) {
43     $month = "0" . $month;
44   } # if
45
46   if ($day < 10) {
47     $day = "0" . $day;
48   } # if
49   return $year . "-" . $month . "-" . $day;
50 } # jul2date
51
52 sub subtract_days {
53   my $date = shift;
54   my $days = shift;
55
56   my $juldate = date2jul ($date);
57   my ($year, $juldays);
58
59   ($year, $juldays) = split /-/, $juldate;
60
61   if ($juldays > $days) {
62     $juldays -= $days;
63   } else {
64     $year -= 1;
65     $juldays = 365 - ($days - $juldays);
66   } # if
67
68   $juldate = $year . "-" . $juldays;
69   return jul2date ($juldate);
70 } # subtract_days
71
72 sub OpenDB {
73   # Create ClearQuest Session
74   $session = CQPerlExt::CQSession_Build ();
75
76   # Login as admin
77   $session->UserLogon ($cquser, $cqpasswd, $cqdb, "");
78 } # OpenDB
79
80 sub ProcessRecords {
81   my $from_date = shift;
82   my $to_date   = shift;
83
84   # Build query for all defects
85   $query = $session->BuildQuery ("defect");
86
87   # Include fields into query
88   $query->BuildField ("id");            my $bugid;
89   $query->BuildField ("State");         my $state;
90   $query->BuildField ("Found_in");      my $found_in;
91   $query->BuildField ("submit_date");   my $submit_date;
92
93   # Generate result set
94   $results = $session->BuildResultSet ($query);
95
96   # Run the query
97   $results->Execute ();
98
99   # Counters
100   my (@assigned, @closed, @duplicate, @info, @junked);
101   my (@opened, @pending, @resolved, @verified, @total);
102
103   # Process results
104   $status = $results->MoveNext;
105
106   while ($status == 1) {
107     # Get column value
108     $bugid       = $results->GetColumnValue (1);
109     $state       = $results->GetColumnValue (2);
110     $found_in    = $results->GetColumnValue (3);
111     $submit_date = $results->GetColumnValue (4);
112
113     $submit_date = substr $submit_date, 0, 10;
114
115     if ($found_in =~ /2\.0/) {
116       @total       [0]++;
117       @assigned    [0]++ if ($state eq "Assigned");
118       @closed      [0]++ if ($state eq "Closed");
119       @duplicate   [0]++ if ($state eq "Duplicate");
120       @info        [0]++ if ($state eq "Info");
121       @junked      [0]++ if ($state eq "Junked");
122       @opened      [0]++ if ($state eq "Opened");
123       @pending     [0]++ if ($state eq "Pending");
124       @resolved    [0]++ if ($state eq "Resolved");
125       @verified    [0]++ if ($state eq "Verified");
126       if ($submit_date ge $from_date && $submit_date le $to_date) {
127         @total       [1]++;
128         @assigned    [1]++ if ($state eq "Assigned");
129         @closed      [1]++ if ($state eq "Closed");
130         @duplicate   [1]++ if ($state eq "Duplicate");
131         @info        [1]++ if ($state eq "Info");
132         @junked      [1]++ if ($state eq "Junked");
133         @opened      [1]++ if ($state eq "Opened");
134         @pending     [1]++ if ($state eq "Pending");
135         @resolved    [1]++ if ($state eq "Resolved");
136         @verified    [1]++ if ($state eq "Verified");
137       } # if
138     } # if
139
140     $status = $results->MoveNext;
141   } # while
142
143   # Print results
144   print "2.0 Bugs by date      Totals  $from_date\n";
145   print "                           to $to_date\n";
146   print "----------------  ----------  ----------\n";
147
148   print "Assigned:\t";
149   if ($assigned[0] eq "") {
150     printf "%12d", "0";
151   } else {
152     printf "%12d", $assigned[0];
153   } # if
154   if ($assigned [1] eq "") {
155     printf "%12d", "0";
156   } else {
157     printf "%12d", $assigned[1];
158   } # if
159   print "\n";
160
161   print "Closed:\t\t";
162   if ($closed[0] eq "") {
163     printf "%12d", "0";
164   } else {
165     printf "%12d", $closed[0];
166   }
167   if ($closed[1] eq "") {
168     printf "%12d", "0";
169   } else {
170     printf "%12d", $closed[1];
171   } # if
172   print "\n";
173
174   print "Duplicate:\t";
175   if ($duplicate[0] eq "") {
176     printf "%12d", "0";
177   } else {
178     printf "%12d", $duplicate[0];
179   } # if
180   if ($duplicate[1] eq "") {
181     printf "%12d", "0";
182   } else {
183     printf "%12d", $duplicate[1];
184   } # if
185   print "\n";
186
187   print "Info:\t\t";
188   if ($info[0] eq "") {
189     printf "%12d", "0";
190   } else {
191     printf "%12d", $info[0];
192   } # if
193   if ($info[1] eq "") {
194     printf "%12d", "0";
195   } else {
196     printf "%12d", $info[1];
197   } # if 
198   print "\n";
199
200   print "Junked:\t\t";
201   if ($junked[0] eq "") {
202     printf "%12d", "0";
203   } else {
204     printf "%12d", $junked[0];
205   } # if
206   if ($junked[1] eq "") {
207     printf "%12d", "0";
208   } else {
209     printf "%12d", $junked[1];
210   } # if
211   print "\n";
212
213   print "Opened:\t\t";
214   if ($opened[0] eq "") {
215     printf "%12d", "0";
216   } else {
217     printf "%12d", $opened[0];
218   } # if 
219   if ($opened[1] eq "") {
220     printf "%12d", "0";
221   } else {
222     printf "%12d", $opened[1];
223   } # if 
224   print "\n";
225
226   print "Pending:\t";
227   if ($pending[0] eq "") {
228     printf "%12d", "0";
229   } else {
230     printf "%12d", $pending[0];
231   } # if 
232   if ($pending[1] eq "") {
233     printf "%12d", "0";
234   } else {
235     printf "%12d", $pending[1];
236   } # if
237   print "\n";
238
239   print "Resolved:\t";
240   if ($resolved[0] eq "") {
241     printf "%12d", "0";
242   } else {
243     printf "%12d", $resolved[0];
244   } # if
245   if ($resolved[1] eq "") {
246     printf "%12d", "0";
247   } else {
248     printf "%12d", $resolved[1];
249   } # if
250   print "\n";
251
252   print "Verified:\t";
253   if ($verified[0] eq "") {
254     printf "%12d", "0";
255   } else {
256     printf "%12d", $verified[0];
257   } # if
258   if ($verified[1] eq "") {
259     printf "%12d", "0";
260   } else {
261     printf "%12d", $verified[1];
262   } # if
263   print "\n";
264   print "----------------  ----------  ----------\n";
265   print "Totals:\t\t";
266   if ($total[0] eq "") {
267     printf "%12d", "0";
268   } else {
269     printf "%12d", $total[0];
270   } # if 
271   if ($total[1] eq "") {
272     printf "%12d", "0";
273   } else {
274     printf "%12d", $total[1];
275   } # if
276   print "\n";
277 } # ProcessRecords
278
279 sub CloseDB {
280   # Destroy ClearQuest Session
281   CQSession::Unbuild ($session);
282 } # CloseDB
283
284 if ($#ARGV == 1) {
285   $from_date = $ARGV[0];
286   $to_date   = $ARGV[1];
287 } else {
288   ($junk,$junk,$junk, $mday, $mon, $year) = localtime (time);
289   $year += 1900;
290   $mon++;
291   if ($mon < 10) {
292     $to_date = $year . "-0" . $mon . "-" . $mday;
293   } else {
294     $to_date = $year . "-" . $mon . "-" . $mday;
295   } # if
296
297   $from_date = subtract_days ($to_date, 7);
298 } # if
299
300 OpenDB;
301 ProcessRecords ($from_date, $to_date);
302 CloseDB;