File: bug_graph_bycategory.php

package info (click to toggle)
mantis 1.1.6+dfsg-2lenny6
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 9,576 kB
  • ctags: 14,851
  • sloc: php: 54,817; sql: 348; sh: 181; makefile: 56
file content (235 lines) | stat: -rw-r--r-- 9,299 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
# Mantis - a php based bugtracking system

# Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
# Copyright (C) 2002 - 2007  Mantis Team   - mantisbt-dev@lists.sourceforge.net

# Mantis is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Mantis is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mantis.  If not, see <http://www.gnu.org/licenses/>.

	# --------------------------------------------------------
	# $Id: bug_graph_bycategory.php,v 1.2.2.1 2007-10-13 22:32:40 giallu Exp $
	# --------------------------------------------------------
?>
<?php
	require_once( 'core.php' );

	$t_core_path = config_get( 'core_path' );

	require_once( $t_core_path.'class.period.php' );
	require_once( $t_core_path.'graph_api.php' );

	access_ensure_project_level( config_get( 'view_summary_threshold' ) );

	$f_width = gpc_get_int( 'width', 600 );
	$t_ar = config_get( 'graph_bar_aspect' );
	$t_interval = new Period();
	$t_interval->set_period_from_selector( 'interval' );
	$f_show_as_table = gpc_get_bool( 'show_table', FALSE );
	$f_summary = gpc_get_bool( 'summary', FALSE );
	
	$t_interval_days = $t_interval->get_elapsed_days();
	if ( $t_interval_days <= 14 ) {
	    $t_incr = 60 * 60; // less than 14 days, use hourly
	} else if ( $t_interval_days <= 92 ) {
	    $t_incr = 24 * 60 * 60; // less than three month, use daily
	} else {
	    $t_incr = 7 * 24 * 60 * 60; // otherwise weekly
	}

	$f_page_number = 1;

	$t_per_page = 0;
	$t_bug_count = null;
	$t_page_count = 0;

	$t_filter = current_user_get_bug_filter();
    $t_filter['_view_type']	= 'advanced';
    $t_filter['show_status'] = array(META_FILTER_ANY);
	$t_filter['sort'] = '';
	$rows = filter_get_bug_rows( $f_page_number, $t_per_page, $t_page_count, $t_bug_count, $t_filter, null, null, true );
	if ( count($rows) == 0 ) {
		// no data to graph
		exit();
	}
	
	$t_bug_table			= config_get( 'mantis_bug_table' );
	$t_bug_hist_table			= config_get( 'mantis_bug_history_table' );

	$t_marker = array();
	$t_data = array();
	$t_ptr = 0;
	$t_end = $t_interval->get_end_timestamp();
	$t_start = $t_interval->get_start_timestamp();
	
    $t_resolved = config_get( 'bug_resolved_status_threshold' );
    $t_closed = CLOSED;
    
    $t_bug = array();
    $t_bug_cat = array(); // save categoties or bugs to look up resolved ones.
    $t_category = array();

	// walk through all issues and grab their category for 'now' 
	$t_marker[$t_ptr] = time();
	$t_data[$t_ptr] = array();
	foreach ($rows as $t_row) {
	    // the following function can treat the resolved parameter as an array to match
        $t_cat = $t_row['category'];
        if ($t_cat == '')
            $t_cat = 'none';
	    if ( !access_compare_level( $t_row['status'], $t_resolved ) ) {
	        if (in_array($t_cat, $t_category)) {
                $t_data[$t_ptr][$t_cat] ++;
            } else {
                $t_data[$t_ptr][$t_cat] = 1;
                $t_category[] = $t_cat;              
            }
        }
        $t_bug[] = $t_row['id'];
        $t_bug_cat[$t_row['id']] = $t_cat;
	}

    // get the history for these bugs over the interval required to offset the data
    // type = 0 and field=status are status changes
    // type = 1 are new bugs
    $t_select = 'SELECT bug_id, type, field_name, old_value, new_value, date_modified FROM '.$t_bug_hist_table.
        ' WHERE bug_id in ('.implode(',', $t_bug).') and '.
            '( (type='.NORMAL_TYPE.' and field_name=\'category\') or '.
                '(type='.NORMAL_TYPE.' and field_name=\'status\') or type='.NEW_BUG.' ) and '.
                'date_modified >= \''.db_date( $t_start ).'\''.
            ' order by date_modified DESC';
    $t_result = db_query( $t_select );
	$row = db_fetch_array( $t_result );
    
	for ($t_now = time() - $t_incr; $t_now >= $t_start; $t_now -= $t_incr) {
	    // walk through the data points and use the data retrieved to update counts
	    while( ( $row !== false ) && ( db_unixtimestamp($row['date_modified']) >= $t_now ) ) {
	        switch ($row['type']) {
    	        case 0: // updated bug
    	            if ($row['field_name'] == 'category') {
	                    $t_cat = $row['new_value'];
            	        if ($t_cat == '')
            	            $t_cat = 'none';
            	        if (in_array($t_cat, $t_category)) {
                            $t_data[$t_ptr][$t_cat] --;
                        } else {
                            $t_data[$t_ptr][$t_cat] = 0;
                            $t_category[] = $t_cat;              
                        }
	                    $t_cat = $row['old_value'];
            	        if ($t_cat == '')
            	            $t_cat = 'none';
            	        if (in_array($t_cat, $t_category)) {
                            $t_data[$t_ptr][$t_cat] ++;
                        } else {
                            $t_data[$t_ptr][$t_cat] = 1;
                            $t_category[] = $t_cat;              
                        }
                        // change the category associated with the bug to match in case the bug was
                        //  created during the scan
                        $t_bug_cat[$row['bug_id']] = $t_cat; 
                    } else { // change of status access_compare_level( $t_row['status'], $t_resolved )
                        if ( access_compare_level( $row['new_value'], $t_resolved ) && 
                                !access_compare_level( $row['old_value'], $t_resolved ) ) {
                            // transition from open to closed
                            $t_cat = $t_bug_cat[$row['bug_id']];
            	            if ($t_cat == '')
            	                $t_cat = 'none';
            	            if (in_array($t_cat, $t_category)) {
                                $t_data[$t_ptr][$t_cat] ++;
                            } else {
                                $t_data[$t_ptr][$t_cat] = 1;
                                $t_category[] = $t_cat;              
                            }
                        }
                    }
                    break;
    	        case 1: // new bug
                    $t_cat = $t_bug_cat[$row['bug_id']];
    	            if ($t_cat == '')
    	                $t_cat = 'none';
    	            if (in_array($t_cat, $t_category)) {
                        $t_data[$t_ptr][$t_cat] --;
                    } else {
                        $t_data[$t_ptr][$t_cat] = 0;
                        $t_category[] = $t_cat;              
                    }
                    break;
            }
        	$row = db_fetch_array( $t_result );
        }

	    if ($t_now <= $t_end) {
    	    $t_marker[$t_ptr] = $t_now;
	        $t_ptr++;
	        foreach ( $t_category as $t_cat ) {
	            $t_data[$t_ptr][$t_cat] = $t_data[$t_ptr-1][$t_cat];
            }
        }
	}
    $t_bin_count = $t_ptr;
// drop any categories that have no counts
//  These arise when bugs are opened and closed within the data intervals
    $t_count_cat = count( $t_category );
    for ( $t=0; $t<$t_count_cat; $t++ ) {
        $t_cat = $t_category[ $t ];
        $t_not_zero = false;
        for ($t_ptr=0; $t_ptr<$t_bin_count; $t_ptr++) {
            if ( isset( $t_data[$t_ptr][$t_cat] ) && ( $t_data[$t_ptr][$t_cat] > 0 ) ) {
                $t_not_zero = true;
                break;
            }
        }  
        if ( !$t_not_zero ) {
            unset( $t_category[ $t ] );
        }
    }
// sort and display the results
    sort($t_category);
    if ($f_show_as_table) {
        html_begin();
        html_head_begin();
        html_css();
        html_content_type();
    	html_head_end();
    	html_body_begin();
	    echo '<table class="width100"><tr><td></td>';
        foreach ( $t_category as $t_cat ) {
            echo '<th>'.$t_cat.'</th>';
        }
        echo '</tr>';
	    for ($t_ptr=0; $t_ptr<$t_bin_count; $t_ptr++) {
            echo '<tr class="row-'.($t_ptr%2+1).'"><td>'.$t_ptr.' ('.db_date( $t_marker[$t_ptr] ).')'.'</td>';
            foreach ( $t_category as $t_cat ) {
                echo '<td>'.(isset($t_data[$t_ptr][$t_cat]) ? $t_data[$t_ptr][$t_cat] : 0).'</td>';
            }
            echo '</tr>';  
        }     
	    echo '</table>';
    	html_body_end();
    	html_end();
	} else {
	    // reverse the array and reorder the data, if necessary
	    $t_metrics = array();
	    for ($t_ptr=0; $t_ptr<$t_bin_count; $t_ptr++) {
	        $t = $t_bin_count - $t_ptr - 1;
	        $t_metrics[0][$t_ptr] = $t_marker[$t];
            $i = 0;
            foreach ( $t_category as $t_cat ) {
        	    $t_metrics[++$i][$t_ptr] = isset($t_data[$t][$t_cat]) ? $t_data[$t][$t_cat] : 0;
            }               
	    }
	    array_unshift( $t_category, '' ); // add placeholder
	    graph_bydate( $t_metrics, $t_category, lang_get( 'by_category' ), $f_width, $f_width * $t_ar );
    }
?>