collectstats.pl 6.29 KB
Newer Older
1 2 3
#!/usr/bonsaitools/bin/perl -w
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
4 5 6 7 8 9 10 11 12 13
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
14
# The Original Code is the Bugzilla Bug Tracking System.
15
#
16
# The Initial Developer of the Original Code is Netscape Communications
17 18 19 20
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
21 22
# Contributor(s): Terry Weissman <terry@mozilla.org>,
#                 Harrison Page <harrison@netscape.com>
23
#         Gervase Markham <gerv@gerv.net>
24 25 26

# Run me out of cron at midnight to collect Bugzilla statistics.

27

28
use AnyDBM_File;
29
use strict;
30
use vars @::legal_product;
31 32 33

require "globals.pl";

34
# tidy up after graphing module
35
if (chdir("graphs")) {
36
    unlink <./*.gif>;
37 38 39
    unlink <./*.png>;
    chdir("..");
}
40

41
ConnectToDatabase(1);
42
GetVersionTable();
43

44 45 46 47
my @myproducts;
push( @myproducts, "-All-", @::legal_product );

foreach (@myproducts) {
48
    my $dir = "data/mining";
49

50 51 52
    &check_data_dir ($dir);
    &collect_stats ($dir, $_);
}
53

54 55
&calculate_dupes();

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
# Generate a static RDF file containing the default view of the duplicates data.
open(CGI, "REQUEST_METHOD=GET QUERY_STRING=ctype=rdf ./duplicates.cgi |")
  || die "can't fork duplicates.cgi: $!";
open(RDF, ">data/duplicates.tmp")
  || die "can't write to data/duplicates.tmp: $!";
my $headers_done = 0;
while (<CGI>) {
  print RDF if $headers_done;
  $headers_done = 1 if $_ eq "\n";
}
close CGI;
close RDF;
if (-s "data/duplicates.tmp") {
    rename("data/duplicates.rdf", "data/duplicates-old.rdf");
    rename("data/duplicates.tmp", "data/duplicates.rdf");
}

73 74
sub check_data_dir {
    my $dir = shift;
75

76
    if (! -d $dir) {
77 78 79 80
        mkdir $dir, 0777;
        chmod 0777, $dir;
    }
}
81

82 83 84 85
sub collect_stats {
    my $dir = shift;
    my $product = shift;
    my $when = localtime (time);
86 87 88
    my $product_id = get_product_id($product) unless $product eq '-All-';

    die "Unknown product $product" unless ($product_id or $product eq '-All-');
89

90 91 92 93 94
    # NB: Need to mangle the product for the filename, but use the real
    # product name in the query
    my $file_product = $product;
    $file_product =~ s/\//-/gs;
    my $file = join '/', $dir, $file_product;
95 96 97 98 99
    my $exists = -f $file;

    if (open DATA, ">>$file") {
        push my @row, &today;

100 101 102 103
        foreach my $status ('NEW', 'ASSIGNED', 'REOPENED', 'UNCONFIRMED', 'RESOLVED', 'VERIFIED', 'CLOSED') {
            if( $product eq "-All-" ) {
                SendSQL("select count(bug_status) from bugs where bug_status='$status'");
            } else {
104
                SendSQL("select count(bug_status) from bugs where bug_status='$status' and product_id=$product_id");
105 106 107 108 109 110 111 112 113
            }

            push @row, FetchOneColumn();
        }

        foreach my $resolution ('FIXED', 'INVALID', 'WONTFIX', 'LATER', 'REMIND', 'DUPLICATE', 'WORKSFORME', 'MOVED') {
            if( $product eq "-All-" ) {
                SendSQL("select count(resolution) from bugs where resolution='$resolution'");
            } else {
114
                SendSQL("select count(resolution) from bugs where resolution='$resolution' and product_id=$product_id");
115 116 117 118 119 120 121
            }

            push @row, FetchOneColumn();
        }

        if (! $exists) {
            print DATA <<FIN;
122
# Bugzilla Daily Bug Stats
123
#
124
# Do not edit me! This file is generated.
125
#
126
# fields: DATE|NEW|ASSIGNED|REOPENED|UNCONFIRMED|RESOLVED|VERIFIED|CLOSED|FIXED|INVALID|WONTFIX|LATER|REMIND|DUPLICATE|WORKSFORME|MOVED
127 128
# Product: $product
# Created: $when
129
FIN
130 131
        }

132 133 134 135 136 137 138
        print DATA (join '|', @row) . "\n";
        close DATA;
    } else {
        print "$0: $file, $!";
    }
}

139 140 141 142 143 144 145 146 147
sub calculate_dupes {
    SendSQL("SELECT * FROM duplicates");

    my %dupes;
    my %count;
    my @row;
    my $key;
    my $changed = 1;

148
    my $today = &today_dash;
149 150 151 152

    # Save % count here in a date-named file
    # so we can read it back in to do changed counters
    # First, delete it if it exists, so we don't add to the contents of an old file
153 154
    if (my @files = <data/duplicates/dupes$today*>) {
        unlink @files;
155 156
    }
   
157
    dbmopen(%count, "data/duplicates/dupes$today", 0644) || die "Can't open DBM dupes file: $!";
158 159 160

    # Create a hash with key "a bug number", value "bug which that bug is a
    # direct dupe of" - straight from the duplicates table.
161 162 163 164
    while (@row = FetchSQLData()) {
        my $dupe_of = shift @row;
        my $dupe = shift @row;
        $dupes{$dupe} = $dupe_of;
165 166 167 168 169 170 171
    }

    # Total up the number of bugs which are dupes of a given bug
    # count will then have key = "bug number", 
    # value = "number of immediate dupes of that bug".
    foreach $key (keys(%dupes)) 
    {
172
        my $dupe_of = $dupes{$key};
173

174 175 176
        if (!defined($count{$dupe_of})) {
            $count{$dupe_of} = 0;
        }
177

178
        $count{$dupe_of}++;
179 180 181 182 183 184
    }   

    # Now we collapse the dupe tree by iterating over %count until
    # there is no further change.
    while ($changed == 1)
    {
185 186 187 188 189 190 191 192 193 194 195 196 197 198
        $changed = 0;
        foreach $key (keys(%count)) {
            # if this bug is actually itself a dupe, and has a count...
            if (defined($dupes{$key}) && $count{$key} > 0) {
                # add that count onto the bug it is a dupe of,
                # and zero the count; the check is to avoid
                # loops
                if ($count{$dupes{$key}} != 0) {
                    $count{$dupes{$key}} += $count{$key};
                    $count{$key} = 0;
                    $changed = 1;
                }
            }
        }
199 200 201 202 203
    }

    # Remove the values for which the count is zero
    foreach $key (keys(%count))
    {
204 205 206
        if ($count{$key} == 0) {
            delete $count{$key};
        }
207 208 209 210 211
    }
   
    dbmclose(%count);
}

212 213 214 215
sub today {
    my ($dom, $mon, $year) = (localtime(time))[3, 4, 5];
    return sprintf "%04d%02d%02d", 1900 + $year, ++$mon, $dom;
}
216

217 218 219 220 221
sub today_dash {
    my ($dom, $mon, $year) = (localtime(time))[3, 4, 5];
    return sprintf "%04d-%02d-%02d", 1900 + $year, ++$mon, $dom;
}