禪道數據的統計【源代碼在文末】


項目管理工具很多,我們公司一直用禪道做bug管理,隨便把項目管理也搞到禪道上,從需求建立--立項--分任務--執行任務--完成,用的是開源版本,所以少了很多報表,用過一段時間專業版,統計報表數據也不怎么好用,報表都不是自己想要的。

所以自己用python搞了一套統計:

 

 

 簡單模塊:

1.各種報表分個類,比如bug的,周報,日報,項目統計等

2.common,處理table,發郵件,鏈接數據庫等

3.sql_script,存放數據庫腳本的

貼幾個項目代碼【需要的拿去用】:

## sql內部用的,數據量不大,沒怎么優化,怎么方便怎么寫

#BUG每天分布情況


SELECT
    z.`部門組`,
    z.`被指派`,
    CONCAT(
        z.提交人,
        '(',
        t.realname,
        ')'
    ) 提交人,
    z.`項目名稱`,
    z.`Bug標題`,
    z.Bug狀態,
    z.提交時間
FROM
    (
        SELECT
            d.`name` 部門組,
            CONCAT(
                a.assignedTo,
                '(',
                u.realname,
                ')'
            ) 被指派,
            a.openedBy 提交人,
            a.`name` 項目名稱,
            CONCAT(a.id, '--', a.title) Bug標題,
            CASE
        WHEN a.`status` = 'resolved' THEN
            '已解決'
        WHEN a.`status` = 'active' THEN
            '激活'
        WHEN a.`status` = 'closed' THEN
            '已關閉'
        END Bug狀態,
        a.openedDate 提交時間
    FROM
        (
            SELECT
                b.id,
                p.`name`,
                b.title,
                b.`status`,
                b.openedBy,
                b.openedDate,
                CASE
            WHEN LENGTH(b.resolvedBy) > 0 THEN
                b.resolvedBy
            ELSE
                b.assignedTo
            END assignedTo,
            b.resolvedBy
        FROM
            zt_bug AS b,
            zt_project AS p
        WHERE
            b.openedDate >= CURDATE() # DATE_SUB(CURDATE(), INTERVAL 7 DAY)
        AND b.project NOT IN ('107')
        AND b.project = p.id
        AND b.deleted = '0'
        ) AS a,
        zt_user AS u,
        zt_dept AS d
    WHERE
        a.assignedTo = u.account
    AND u.dept = d.id
    ) AS z,
    zt_user AS t
WHERE
    z.`提交人` = t.account
ORDER BY
    部門組 ASC,
    被指派 ASC,
    Bug狀態 ASC
# -*- coding: UTF-8 -*-
# 表列合並算法

import numpy as np


# 統計相同的列的值及個數
def find_same_column(l, end=0, exc=''):
    """
    :param l: 需要計算的列表
    :param end: 需要合並的列數(前end列合並),默認為0列計算
    :param exc: 列需要排除合並的值,默認為所有值都不合並
    :return al: 返回計算好的列表
    """
    al = []
    for i in range(len(l)):
        # if end == 0:
        #     end = 100000
        stop = end
        if exc == '':
            exc = '!@#$%^&*'
        logo = str(exc)
        last = ''
        num = 1
        part = []
        for x in range(len(l[i])):
            # 只合並列數范圍
            if i < stop:
                # 列值和標識值相同則不合並
                if str(l[i][x]) != str(logo):
                    # 找相同的列值,相等則計數+1
                    if l[i][x] == last:
                        num += 1
                        # 如果列表循環完則append計數
                        if x + 1 == len(l[i]):
                            part.append(str(num))
                    # 如果列值不同則更新last值並重置計數位
                    else:
                        # 上一個列表的num沒有append,這里append
                        # 如果是列表第一個則不更新,如果列表上一個值與標識相同也不更新
                        if x != 0 and str(l[i][x-1]) != str(logo):
                            part.append(str(num))
                        last = l[i][x]
                        part.append(last)
                        num = 1
                        # 如果列表循環完append計數
                        if x+1 == len(l[i]):
                            part.append(str(num))
                # 列表值與標識為相同不合並,直接append
                else:
                    # 還是要判斷是否append num
                    if x != 0 and str(l[i][x-1]) != str(logo):
                        part.append(str(num))
                    part.append(l[i][x])
                    part.append(str(1))
            else:
                part.append(l[i][x])
                part.append(str(1))
        al.append(part)
    return al


# 處理td標簽
def deal_td(l):
    """
    :param l:  需要處理td標簽的列表
    :return al: 返回處理好的列表
    """
    al = []
    for i in range(len(l)):
        part = []
        for x in range(0, len(l[i]), 2):
            if x == len(l[i]):
                break
            t_value = str(l[i][x])
            t_sum = int(l[i][x+1])
            if t_sum >= 2:
                for num in range(t_sum):
                    if num == 0:
                        part.append('<td rowspan="%s">' % str(t_sum) + t_value + '</td>')
                    else:
                        part.append('')
            else:
                part.append('<td>' + t_value + '</td>')

        al.append(part)
    return al


# 處理tr標簽
def deal_tr(l):
    """
    :param l: 需要處理tr的列表
    :return: 返回處理好的tr字符串
    """
    # 倒置列表
    td = np.tile(l, 1).T.tolist()
    tr = ''
    for i in td:
        l_tr = ''
        for x in range(len(i)):
            l_tr += i[x]
            s_tr = '<tr>' + l_tr + '</tr>' + '\n'
        tr += s_tr
    return tr
# coding = utf-8
# 禪道項目度量--每日bug激活統計


import sys
import datetime
from common import common_mail_test as mail
from common import common_read_sql as read_sql
from common import common_mysql_config as mysql


# 數據獲取
def deal_mysql(f):
    """
    數據獲取與處理
    :param f:
    :return:
    """
    cursor = mysql.db.cursor()
    sql = read_sql.read_sql(f)
    cursor.execute(sql)
    results = cursor.fetchall()
    cursor.close()
    mysql.db.close()
    # tuple類型轉換為list類型
    li = list(results)
    """
    把id相同的數據的放入pl列表,再把列表放入al列表
    """
    al = []
    pl = []
    for i in range(len(li)):
        if i == 0:
            pl.append(li[i])
        # 如果和列表里的名字相同,繼續append
        if li[i][0] == li[i - 1][0]:
            pl.append(li[i])
        # 如果不相同,沒有到末尾,把當前列表加入al列表,並清空pl列表/i-1 >0 排除i=0情況
        if li[i][0] != li[i - 1][0] and i - 1 > 0 and len(pl) != 0:
            al.append(pl)
            pl = []

        if i != len(li) - 1:
            # 如果和之前的不同,和之后的也不同,且不是末尾:
            if li[i][0] != li[i - 1][0] and i - 1 > 0 and li[i][0] != li[i + 1][0]:
                pl.append(li[i])
                al.append(pl)
                pl = []
            # 如果和之前的不同,和之后的相同,且不是末尾:
            if li[i][0] != li[i - 1][0] and i - 1 > 0 and li[i][0] == li[i + 1][0]:
                pl.append(li[i])

        # 如果是末尾
        if i == len(li) - 1:
            # pl.append(li[i])  # 后來發現重復插入最后一條數據  先注釋
            al.append(pl)
    # 只取滿足日期的數據
    el = []
    for i in al:
        for x in i:
            if str(x[3]).split(' ')[0] == str(datetime.datetime.now()).split(' ')[0]:
                el.append(i)
                break
    return el


# 處理html
def deal_html(tl):
    """
    生成html
    :param tl:
    :return:
    """
    subject = 'BUG激活統計'
    head = """
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>BUG被激活</title>
        <body>
        <div id="container">
         <center>
        <strong>匯總時間: """ + str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) + """</strong>
        <p><strong>備注:BUG被激活次數大於等於3次</strong></p>
        <div id="content">
         <table border="1" cellpadding="2" style="border-collapse:collapse;margin-top:10px">
         <tr>
           <td colspan="5" align="center" height="35px"><font size="5"><strong>BUG激活情況</strong></font></td> 
         </tr>
    """
    ht = ''
    for i in tl:

        zj = ''
        beg = ''
        a = ''
        b = ''
        c = ''
        title = """
                <tr>
                   <td colspan="5" width="120" align="left"><font size="3" height="30px" ><strong>BUG標題:</strong>
                   <a href="http://chandao.thecover.cn/zentao/bug-view-%s.html">%s</a></font></td>
                </tr>
                """ % (i[0][0], i[0][4])
        for x in range(len(i)):
            ti = """
                  <tr>
                    <td rowspan="%s" width="80" align="center"><font size="3"><strong>ID</strong></font></td>
                    <td width="100" align="center"><font size="3"><strong>操作人</strong></font></td>
                    <td width="100" align="center"><font size="3"><strong>解決類型</strong></font></td>
                    <td width="180" align="center"><font size="3"><strong>操作時間</strong></font></td>
                    <td width="700" align="left"><font size="3"><strong>備注</strong></font></td>
                  </tr> 
                """
            if x == 0:
                beg = """
                       <tr>
                         <td rowspan="%s" width="80" align="center"><font size="3">%s</font></td>
                         <td width="100" align="center"><font size="3">%s</font></td>
                         <td width="100" align="center"><font size="3">%s</font></td>
                         <td width="180" align="center"><font size="3">%s</font></td>
                         <td width="700" align="left"><font size="3">%s</font></td>
                       </tr> 
                    """ % (len(i), i[x][0], i[x][1], i[x][2], i[x][3], i[x][5])
            else:
                a = """<tr><td width="100" align="center"><font size="3">%s</font></td>""" % i[x][1]
                if i[x][2] == 'activated':
                    b = """<td width="100" align="center"><font size="3" color="red">%s</font></td>""" % i[x][2]
                else:
                    b = """<td width="100" align="center"><font size="3">%s</font></td>""" % i[x][2]
                c = """
                    <td width="180" align="center"><font size="3">%s</font></td>
                    <td width="700" align="left"><font size="3">%s</font></td>
                    </tr> 
                    """ % (i[x][3], i[x][5])

            zj += a + b + c

            ho = """
                <tr>
                   <td colspan="5" width="120" align="right" ><font size="3" color="white">.</font></td>
                 </tr>
                """
        ht += title + ti + beg + zj + ho

    end = """
            </table>
            <p><font size="3" ><center>--------------------<strong><a href="http://chandao.thecover.cn/">匯總數據源於禪道系統</a>
            </strong>--------------------</center></font>
            </center>
            </div>
            </div>
            </div>
            </body>
            </html>
        """
    html = head + ht + end
    return subject, html


mysql_l = deal_mysql(sys._getframe())
if len(mysql_l) > 0:
    s, h = deal_html(mysql_l)
    try:
        if mail.cs_mail_send(s, h):
            print('Send success')
        else:
            print('Send failure')
    except Exception as e:
        raise e
else:
    print("NO Data !")

 

統計展示:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

配置jenkins定時跑任務執行

 

源代碼下載地址:

 

https://download.csdn.net/download/tengdakuaijie1/12272565

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM