Edgewall Software

Ticket #5572: milestone_groups-r5765.diff

File milestone_groups-r5765.diff, 8.8 KB (added by ecarter, 17 months ago)

Somewhat re-worked version of the previous patch

  • trac/ticket/roadmap.py

     
    6363        self.done_percent = 0 
    6464        self.done_count = 0 
    6565 
    66     def add_interval(self, title, count, qry_args, css_class, countsToProg=0): 
     66    def add_interval(self, title, count, qry_args, css_class, 
     67                     overall_completion=None, countsToProg=0): 
    6768        """Adds a division to this stats' group's progress bar. 
    6869 
    6970        `title` is the display name (eg 'closed', 'spent effort') of this 
     
    7273        `qry_args` is a dict of extra params that will yield the subset of 
    7374          tickets in this interval on a query. 
    7475        `css_class` is the css class that will be used to display the division. 
    75         `countsToProg` can be set to true to make this interval count towards 
    76           overall completion of this group of tickets. 
     76        `overall_completion` can be set to true to make this interval count 
     77          towards overall completion of this group of tickets. 
     78           
     79        (Warning: `countsToProg` argument will be removed in 0.12, use 
     80        `overall_completion` instead) 
    7781        """ 
     82        if overall_completion is None: 
     83            overall_completion = countsToProg 
    7884        self.intervals.append({ 
    7985            'title': title, 
    8086            'count': count, 
    8187            'qry_args': qry_args, 
    8288            'css_class': css_class, 
    8389            'percent': None, 
    84             'countsToProg': countsToProg 
     90            'countsToProg': overall_completion, 
     91            'overall_completion': overall_completion, 
    8592        }) 
    8693        self.count = self.count + count 
    8794 
     
    95102            interval['percent'] = round(float(interval['count'] /  
    96103                                        float(self.count) * 100)) 
    97104            total_percent = total_percent + interval['percent'] 
    98             if interval['countsToProg']: 
     105            if interval['overall_completion']: 
    99106                self.done_percent += interval['percent'] 
    100107                self.done_count += interval['count'] 
    101108 
     109        # We want the percentages to add up to 100%.  To do that, we fudge the 
     110        # first interval that counts as "completed".  That interval is adjusted 
     111        # by enough to make the intervals sum to 100%. 
    102112        if self.done_count and total_percent != 100: 
    103             fudge_int = [i for i in self.intervals if i['countsToProg']][0] 
     113            fudge_int = [i for i in self.intervals 
     114                         if i['overall_completion']][0] 
    104115            fudge_amt = 100 - total_percent 
    105116            fudge_int['percent'] += fudge_amt 
    106117            self.done_percent += fudge_amt 
    107118 
     119 
    108120class DefaultTicketGroupStatsProvider(Component): 
     121    """Configurable ticket group statistics provider. 
     122 
     123    Example configuration (which is also the default): 
     124 
     125    [milestone-groups] 
     126    closed = closed                      # a list of accepted status 
     127    closed.order = 0                     # sequence number in the progress bar 
     128    closed.args = group=resolution       # optional extra param for the query 
     129    closed.overall_completion = true     # count for overall completion 
     130 
     131    active = !closed                     # '!' for a list of rejected status 
     132    active.order = 1 
     133    active.css = open                    # css class for this interval 
     134    """ 
     135     
    109136    implements(ITicketGroupStatsProvider) 
    110137 
     138    def _get_ticket_groups(self): 
     139        """Returns a dict describing the ticket groups used in milestone 
     140        progress bars. 
     141        """ 
     142        if 'milestone-groups' in self.config: 
     143            groups = {} 
     144            order = 0 
     145            for option, value in self.config.options('milestone-groups'): 
     146                if '.' in option: 
     147                    name, qualifier = option.split('.', 1) 
     148                    group = groups.get(name) 
     149                    if group: 
     150                        group[qualifier] = value 
     151                else: 
     152                    groups[option] = {'name': option, 'status': value, 
     153                                      'order': order} 
     154                    order += 1 
     155            return [group for group in sorted(groups.values(), 
     156                                              key=lambda g: int(g['order']))] 
     157        else: 
     158            return [{'name': 'closed', 'status': 'closed', 
     159                     'args': 'group=resolution', 'overall_completion': 'true'}, 
     160                    {'name': 'active', 'status': '!closed', 'css': 'open'}] 
     161 
    111162    def get_ticket_group_stats(self, ticket_ids): 
    112163        total_cnt = len(ticket_ids) 
     164        status_cnt = {} 
     165        for s in TicketSystem(self.env).get_all_status(): 
     166            status_cnt[s] = 0 
    113167        if total_cnt: 
    114168            cursor = self.env.get_db_cnx().cursor() 
    115169            str_ids = [str(x) for x in sorted(ticket_ids)] 
    116             active_cnt = cursor.execute("SELECT count(1) FROM ticket " 
    117                                         "WHERE status <> 'closed' AND id IN " 
    118                                         "(%s)" % ",".join(str_ids)) 
    119             active_cnt = 0 
    120             for cnt, in cursor: 
    121                 active_cnt = cnt 
    122         else: 
    123             active_cnt = 0 
     170            cursor.execute("SELECT status, count(status) FROM ticket " 
     171                           "WHERE id IN (%s) GROUP BY status" % 
     172                           ",".join(str_ids)) 
     173            for s, cnt in cursor: 
     174                status_cnt[s] = cnt 
    124175 
    125         closed_cnt = total_cnt - active_cnt 
    126  
    127176        stat = TicketGroupStats('ticket status', 'ticket') 
    128         stat.add_interval('closed', closed_cnt, 
    129                           {'status': 'closed', 'group': 'resolution'}, 
    130                           'closed', True) 
    131         stat.add_interval('active', active_cnt, 
    132                           {'status': ['new', 'assigned', 'reopened']}, 
    133                           'open', False) 
     177        for group in self._get_ticket_groups(): 
     178            group_cnt = 0 
     179            accepted = [s.strip() for s in 
     180                        group['status'].replace('!', '').split(',')] 
     181            invert = '!' in group['status'] 
     182            query_args = {} 
     183            for s, cnt in status_cnt.iteritems(): 
     184                if (s in accepted) ^ invert: 
     185                    group_cnt += cnt 
     186                    query_args.setdefault('status', []).append(s) 
     187            for arg in [kv for kv in group.get('args', '').split(',') 
     188                        if '=' in kv]: 
     189                k, v = [a.strip() for a in arg.split('=', 1)] 
     190                query_args[k] = v 
     191            stat.add_interval(group['name'], group_cnt, query_args, 
     192                              group.get('css', group['name']), 
     193                              group.get('overall_completion', False)) 
    134194        stat.refresh_calcs() 
    135195        return stat 
    136196 
     
    631691 
    632692            for idx, gstat in enumerate(group_stats): 
    633693                gs_dict = milestone_groups[idx] 
    634                 gs_dict['percent_of_max_total'] = (float(gstat.count) / 
    635                                                    float(max_count) * 100) 
     694                percent = 1.0 
     695                if max_count: 
     696                    percent = float(gstat.count) / float(max_count) * 100 
     697                gs_dict['percent_of_max_total'] = percent 
    636698 
    637699        return 'milestone_view.html', data, None 
    638700 
  • trac/ticket/workflows/basic-workflow.ini

     
    2121reopen = closed -> reopened 
    2222reopen.permissions = TICKET_CREATE 
    2323reopen.operations = del_resolution 
     24 
     25[milestone-groups] 
     26closed = closed 
     27closed.order = 0 
     28closed.args = group=resolution 
     29closed.overall_completion = true 
     30 
     31active = assigned,accepted 
     32active.order = 1 
     33active.css = open 
     34 
     35new = new,reopened 
     36new.order = 2 
  • trac/htdocs/css/roadmap.css

     
    1919 text-decoration: none 
    2020} 
    2121table.progress td { background: #fff; padding: 0 } 
     22table.progress td.new { background: #f5f5b5 } 
    2223table.progress td.closed { background: #bae0ba } 
    2324table.progress td :hover { background: none } 
    2425p.percent { font-size: 10px; line-height: 2.4em; margin: 0.9em 0 0 } 
  • trac/templates/macros.html

     
    237237        <dd><a href="${interval_hrefs[idx]}">${interval.count}</a></dd> 
    238238      </py:for> 
    239239      <py:if test="stats_href"> 
    240         <dt>Total ${stats.unit}s:</dt> 
     240        <dt>/ Total ${stats.unit}s:</dt> 
    241241        <dd><a href="${stats_href}">${sum([x.count for x in stats.intervals], 0)}</a></dd> 
    242242      </py:if> 
    243243    </dl>