Switch to unified view

a/bitergiametrics/main.py b/bitergiametrics/main.py
...
...
83
        links = [
83
        links = [
84
            SitemapEntry('Summary', base),
84
            SitemapEntry('Summary', base),
85
            SitemapEntry('Git', base),
85
            SitemapEntry('Git', base),
86
            SitemapEntry(' Commits & committers', self.config.url() + 'commits_commiters'),
86
            SitemapEntry(' Commits & committers', self.config.url() + 'commits_commiters'),
87
            SitemapEntry(' Ratio commits/committer', self.config.url() + 'ratio_commits_commiters'),
87
            SitemapEntry(' Ratio commits/committer', self.config.url() + 'ratio_commits_commiters'),
88
            # SitemapEntry(' Lines added/removed', self.config.url() + 'lines_added_removed', ui_icon=g.icons['stats']),
89
            SitemapEntry(' Lines added/removed', self.config.url() + 'lines_added_removed'),
88
            SitemapEntry(' Lines added/removed', self.config.url() + 'lines_added_removed'),
90
            SitemapEntry('Tickets', base),
89
            SitemapEntry('Tickets', base),
91
            SitemapEntry(' Open & closed', self.config.url() + 'open_closed'),
90
            SitemapEntry(' Open & closed', self.config.url() + 'open_closed'),
92
            SitemapEntry('Tools', self.config.url() + 'open_closed'),
91
            SitemapEntry('Tools', self.config.url() + 'open_closed'),
93
            SitemapEntry(' Bicho', self.config.url() + 'bicho'),
92
            SitemapEntry(' Bicho', self.config.url() + 'bicho'),
...
...
127
        # setattr(self, 'feed.atom', self.feed)
126
        # setattr(self, 'feed.atom', self.feed)
128
        # setattr(self, 'feed.rss', self.feed)
127
        # setattr(self, 'feed.rss', self.feed)
129
        # self._discuss = AppDiscussionController()
128
        # self._discuss = AppDiscussionController()
130
        pass
129
        pass
131
130
131
    # TODO: samples of other kind of graphs with Flotr2
132
    @expose('jinja:bitergiametrics:templates/metrics/bar.html')
132
    @expose('jinja:bitergiametrics:templates/metrics/bar.html')
133
    @with_trailing_slash
133
    @with_trailing_slash
134
    def bar(self, page=0, limit=10, **kw):
134
    def bar(self, page=0, limit=10, **kw):
135
        return dict()
135
        return dict()
136
    
137
    @expose('jinja:bitergiametrics:templates/metrics/tickets_swscopio.html')
138
    @with_trailing_slash
139
    def tickets_swscopio(self, page=0, limit=10, **kw):
140
141
        # FIXME: SQL query to get the results            
142
        tickets_per_month =  [
143
          # ['23989','1999','1','Jan 1999','10','2','2','3'],
144
          # ['23990','1999','2','Feb 1999','15','2','2','3'],
145
          # ['23991','1999','3','Mar 1999','20','4','9','3'],
146
          ['24135','2011','3','Mar 2011','0.56','9','23','12'],
147
          ['24136','2011','4','Apr 2011','0.89','2','30','15'],
148
          ['24137','2011','5','May 2011','0.210','5','12','50'],
149
          ['24138','2011','6','Jun 2011','0.300','6','3','33'],
150
          ['24139','2011','7','Jul 2011','0.215','2','50','45'],
151
          ['24140','2011','8','Aug 2011','0.120','3','88','15'],
152
          ['24141','2011','9','Sep 2011','0.100','8','93','84'],
153
          ['24142','2011','10','Oct 2011','0.20','4','87','29'],
154
          ['24143','2011','11','Nov 2011','0.18','4','90','88'],
155
          ['24144','2011','12','Dec 2011','0.45','2','15','90'],
156
          ['24145','2012','1','Jan 2012','0.40','3','23','56'],
157
          ['24146','2012','2','Feb 2012','0.37','4','80','15'],
158
          ['24147','2012','3','Mar 2012','0.89','7','14','9'],
159
          ['24148','2012','4','Apr 2012','0.121','5','78','33'],
160
        ]
161
            
162
        tickets_per_month_json = {}
163
        tickets_per_month_json['id'] = []
164
        tickets_per_month_json['year'] = []
165
        tickets_per_month_json['month'] = []
166
        tickets_per_month_json['date'] = []
167
        tickets_per_month_json['live'] = []
168
        tickets_per_month_json['people'] = []
169
        tickets_per_month_json['open'] = []
170
        tickets_per_month_json['close'] = []
171
        
172
        for ticket in tickets_per_month:                
173
            tickets_per_month_json['id'].append(ticket[0])
174
            tickets_per_month_json['year'].append(ticket[1])
175
            tickets_per_month_json['month'].append(ticket[2])
176
            tickets_per_month_json['date'].append(ticket[3])
177
            tickets_per_month_json['live'].append(ticket[4])
178
            tickets_per_month_json['people'].append(ticket[5])
179
            tickets_per_month_json['open'].append(ticket[6])
180
            tickets_per_month_json['close'].append(ticket[7])
181
        # FIXME: Find better way to find the path to the file
182
        f = open ("../AlluraBitergiaMetrics/bitergiametrics/nf/metrics/tickets_per_month_swscopio.json", 'w')
183
        # f.write(tickets_per_month)            
184
        f.write(json.dumps(tickets_per_month_json))            
185
        f.close()
186
                
187
        return dict()
188
189
    
136
    
190
    @expose('jinja:bitergiametrics:templates/metrics/pie.html')
137
    @expose('jinja:bitergiametrics:templates/metrics/pie.html')
191
    @with_trailing_slash
138
    @with_trailing_slash
192
    def pie(self, page=0, limit=10, **kw):
139
    def pie(self, page=0, limit=10, **kw):
193
        return dict()
140
        return dict()
...
...
230
177
231
    # FIXME: Clean testing code and direct SQL to MySQL
178
    # FIXME: Clean testing code and direct SQL to MySQL
232
    @expose('jinja:bitergiametrics:templates/metrics/index.html')
179
    @expose('jinja:bitergiametrics:templates/metrics/index.html')
233
    @with_trailing_slash
180
    @with_trailing_slash
234
    def index(self, page=0, limit=10, **kw):
181
    def index(self, page=0, limit=10, **kw):
235
        query_filter = dict(app_config_id=c.app.config._id)
236
        if not has_access(c.app, 'write')():
237
            query_filter['state'] = 'published'
238
        # q = BM.BlogPost.query.find(query_filter)
239
        # post_count = q.count()
240
        # limit, page = h.paging_sanitizer(limit, page, post_count)
241
        # posts = q.sort('timestamp', pymongo.DESCENDING) \
242
        #         .skip(page * limit).limit(limit)
243
        c.form = W.view_metrics_form
244
        # c.pager = W.pager
245
        # return dict(posts=posts, page=page, limit=limit, count=post_count)
246
        
247
        # TODO: error and config management. Share db connection
248
        bichodb = None
249
        tickets_per_month = None
250
        try:
251
            bichodb = MySQLdb.connect(user="root", db="bicho_allura")
252
            cursor = bichodb.cursor()
253
            tickets_month_sql  = "SELECT id, DATE_FORMAT(submitted_on, '%Y') as year, "
254
            tickets_month_sql += "DATE_FORMAT(submitted_on, '%m') as month, "
255
            tickets_month_sql += "DATE_FORMAT(submitted_on, '%Y %m') AS yearmonth, "
256
            tickets_month_sql += "COUNT(*) AS nissues FROM issues "
257
            tickets_month_sql += "GROUP BY yearmonth order by yearmonth" 
258
            # cursor.execute("SELECT DATE_FORMAT(submitted_on, '%Y%V') AS yearweek, COUNT(*) AS nissues FROM issues GROUP BY yearweek")
259
            cursor.execute(tickets_month_sql) 
260
            tickets_per_month = cursor.fetchall()
261
            
262
            tickets_per_month_json = {}
263
            tickets_per_month_json['id'] = []
264
            tickets_per_month_json['year'] = []
265
            tickets_per_month_json['month'] = []
266
            tickets_per_month_json['date'] = []
267
            tickets_per_month_json['tickets'] = []
268
            
269
            for ticket in tickets_per_month:                
270
                tickets_per_month_json['id'].append(ticket[0])
271
                tickets_per_month_json['year'].append(ticket[1])
272
                tickets_per_month_json['month'].append(ticket[2])
273
                tickets_per_month_json['date'].append(ticket[3])
274
                tickets_per_month_json['tickets'].append(ticket[4])
275
            # FIXME: Find better way to find the path to the file
276
            f = open ("../AlluraBitergiaMetrics/bitergiametrics/nf/metrics/tickets_per_month.json", 'w')
277
            # f.write(tickets_per_month)            
278
            f.write(json.dumps(tickets_per_month_json))            
279
            f.close()
280
            
281
            
282
        except MySQLdb.Error, e:
283
            log.error("Error accessing Bicho %d: %s" % (e.args[0], e.args[1]))
284
        finally:
285
            if bichodb:
286
                bichodb.close()
287
                    
288
        
289
        total = TM.Ticket.query.find().count()
290
        tickets = TM.Ticket.query.find()
291
        
292
        now = datetime.utcnow()
293
        week = timedelta(weeks=1)
294
        week_ago = now - week
295
296
        week_tickets = self.tickets_since(week_ago)
297
        
298
           
299
        return dict(week_ago=str(week_ago),week_tickets=week_tickets, 
300
                    total=total, tickets = tickets, tickets_per_month = tickets_per_month) 
301
    
302
    def tickets_since(self, when=None):
303
        count = 0
304
        if when:
305
            count = TM.Ticket.query.find(dict(created_date={'$gte':when})).count()
306
        else:
307
            count = TM.Ticket.query.find(dict(app_config_id=c.app.config._id)).count()
308
        return count
182
        return dict()
309
183
184
# TODO: Use this class to configure Bitergia Metrics Tools
310
class MetricsAdminController(DefaultAdminController):
185
class MetricsAdminController(DefaultAdminController):
311
    def __init__(self, app):
186
    def __init__(self, app):
312
        self.app = app
187
        self.app = app
313
188
314
    @without_trailing_slash
189
    @without_trailing_slash