Edgewall Software

Ticket #5572: milestone_groups-r5758.diff

File milestone_groups-r5758.diff, 8.0 KB (added by cboos, 5 years ago)

Make the default ITicketGroupStatsProvider configurable. Default configuration is generic enough to accomodate all configurable workflows having at least a closed status (which they should have anyway).

  • 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 
    102109        if self.done_count and total_percent != 100: 
    103             fudge_int = [i for i in self.intervals if i['countsToProg']][0] 
     110            fudge_int = [i for i in self.intervals 
     111                         if i['overall_completion']][0] 
    104112            fudge_amt = 100 - total_percent 
    105113            fudge_int['percent'] += fudge_amt 
    106114            self.done_percent += fudge_amt 
    107115 
     116 
    108117class DefaultTicketGroupStatsProvider(Component): 
     118    """Configurable ticket group statistics provider. 
     119 
     120    Example configuration (which is also the default): 
     121 
     122    [milestone-groups] 
     123    closed = closed                      # a list of accepted status 
     124    closed.order = 0                     # sequence number in the progress bar 
     125    closed.args = group=resolution       # optional extra param for the query 
     126    closed.overall_completion = true     # count for overall completion 
     127 
     128    active = !closed                     # '!' for a list of rejected status 
     129    active.order = 1 
     130    active.css = open                    # css class for this interval 
     131    """ 
     132     
    109133    implements(ITicketGroupStatsProvider) 
    110134 
     135    def _get_ticket_groups(self): 
     136        if 'milestone-groups' in self.config: 
     137            groups = {} 
     138            order = 0 
     139            for option, value in self.config.options('milestone-groups'): 
     140                if '.' in option: 
     141                    name, qualifier = option.split('.', 1) 
     142                    group = groups.get(name) 
     143                    if group: 
     144                        group[qualifier] = value 
     145                else: 
     146                    groups[option] = {'name': option, 'states': value, 
     147                                      'order': order} 
     148                    order += 1 
     149            return [group for group in sorted(groups.values(), 
     150                                              key=lambda g: g['order'])] 
     151        else: 
     152            return [{'name': 'closed', 'status': 'closed', 
     153                     'args': 'group=resolution', 'overall_completion': 'true'}, 
     154                    {'name': 'active', 'status': '!closed', 'css': 'open'}] 
     155 
    111156    def get_ticket_group_stats(self, ticket_ids): 
    112157        total_cnt = len(ticket_ids) 
     158        status_cnt = {} 
    113159        if total_cnt: 
     160            active_cnt = ticket_cnt = 0 
    114161            cursor = self.env.get_db_cnx().cursor() 
    115             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 
     162            cursor.execute("SELECT count(*) FROM ticket") 
    120163            for cnt, in cursor: 
    121                 active_cnt = cnt 
    122         else: 
    123             active_cnt = 0 
     164                ticket_cnt = cnt 
     165            if ticket_ids > ticket_cnt / 4: 
     166                # then it's probably faster to get the status for all tickets 
     167                ids = set(ticket_ids) 
     168                cursor.execute("SELECT id, status FROM ticket") 
     169                for id, s in cursor: 
     170                    if id in ids: 
     171                        status_cnt[s] = status_cnt.get(s, 0) + 1 
     172            else: 
     173                str_ids = [str(x) for x in sorted(ticket_ids)] 
     174                cursor.execute("SELECT status, count(status) FROM ticket " 
     175                               "WHERE id IN (%s) GROUP BY status" % 
     176                               ",".join(str_ids)) 
     177                for s, cnt in cursor: 
     178                    status_cnt[s] = cnt 
    124179 
    125         closed_cnt = total_cnt - active_cnt 
    126  
    127180        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) 
     181        for group in self._get_ticket_groups(): 
     182            group_cnt = 0 
     183            accepted = [s.strip() for s in group['status'].split(',')] 
     184            rejected = [] 
     185            if '!' in group['status']: 
     186                accepted, rejected = rejected, accepted 
     187            for s, cnt in status_cnt.iteritems(): 
     188                if s in accepted or (rejected and s not in rejected): 
     189                    group_cnt += cnt 
     190            query_args = {} 
     191            for arg in [kv for kv in group.get('args', '').split(',') 
     192                        if '=' in kv]: 
     193                k, v = [a.strip() for a in arg.split('=', 1)] 
     194                query_args[k] = v 
     195            stat.add_interval(group['name'], group_cnt, query_args, 
     196                              group.get('css', group['name']), 
     197                              group.get('overall_completion', False)) 
    134198        stat.refresh_calcs() 
    135199        return stat 
    136200 
     
    631695 
    632696            for idx, gstat in enumerate(group_stats): 
    633697                gs_dict = milestone_groups[idx] 
    634                 gs_dict['percent_of_max_total'] = (float(gstat.count) / 
    635                                                    float(max_count) * 100) 
     698                percent = 1.0 
     699                if max_count: 
     700                    percent = float(gstat.count) / float(max_count) * 100 
     701                gs_dict['percent_of_max_total'] = percent 
    636702 
    637703        return 'milestone_view.html', data, None 
    638704 
  • 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