TFS二次开发系列:八、TFS二次开发的数据统计以PBI、Bug、Sprint等为例(二)

TFS二次开发系列:八、TFS二次开发的数据统计以PBI、Bug、Sprint等为例(二)

上一篇文章我们编写了此例的DTO层,本文将数据访问层封装为逻辑层,提供给界面使用。

  1.获取TFS Dto实例,并且可以获取项目集合,以及单独获取某个项目实体

复制代码
        public static TFSServerBll Instance = new TFSServerBll();

        public TFSServerDto dto;

        public TFSServerBll()

        {

            dto = new TFSServerDto("http://server:8080/tfs/Project/");

        }

        public TFSServerBll(string TfsUri)

        {

            dto = new TFSServerDto(TfsUri);

        }

        /// <summary>

        /// 获取项目集合

        /// </summary>

        /// <returns></returns>

        public ProjectCollection GetProjectList()

        {

            return dto.GetProjectList();

        }

        //根据projectId获取Project实体

        public Project GetProject(int projectId)

        {

            return dto.GetProject(projectId);

        }
复制代码

  2.根据规则获取项目的PBI/Bug等信息

复制代码
        /// <summary>

        /// 获取项目的所有数据

        /// </summary>

        /// <param name="project"></param>

        public void GetProjectInfo(Project project)

        {

            TfsSprint projectSprint = GetSprintInfo(project.Uri.ToString());

            GetProjectSprintPBIandBUG(projectSprint, project);

        }



        /// <summary>

        /// 获取某项目所有Sprint的PBI和BUG

        /// </summary>

        /// <param name="projectSprint"></param>

        /// <param name="project"></param>

        public void GetProjectSprintPBIandBUG(TfsSprint projectSprint, Project project)

        {

            IEnumerable<ScheduleInfo> list = GetFinalBugInfo(project);



            foreach (Sprint sprint in projectSprint.SprintList)

            {

                sprint.PBIInfo = GetSimplePbi(project.Name, sprint.SprintPath);

                if (list.Count() > 0)

                {

                    foreach (ScheduleInfo info in list)

                    {

                        if (info.Path == sprint.SprintPath)

                        {

                            sprint.BugInfo = new TfsBug() { New = info.NewBug, Done = info.Closed, opening = info.OpenBug };

                            break;

                        }

                    }

                }

                else

                {

                    sprint.BugInfo = new TfsBug() { New = 0, Done = 0, opening =0 };

                }

            }

            string s = "";

        }



        private TfsPBI GetSimplePbi(string projectName, string IterationSprint)

        {

            WorkItemCollection total = dto.GetWorkItemCollection("Product Backlog Item", projectName, "[Iteration Path]='" + IterationSprint + "'");

            WorkItemCollection doneCollection = dto.GetWorkItemCollection("Product Backlog Item", projectName, "[State]='Done' and [Iteration Path]='" + IterationSprint + "'");

            double totaleffort = GetPBIEffort(total);

            double doneeffort = GetPBIEffort(doneCollection);

            double effortPercent = doneeffort / totaleffort;

            TfsPBI pbiinfo = new TfsPBI()

            {

                Total = total.Count,

                Done = doneCollection.Count,

                EffoctPercent = effortPercent,

                EffoctCurrent = (int)doneeffort,

                EffoctTotal = (int)totaleffort

            };

            return pbiinfo;

        }



        private TfsBug GetSimpleBug(string projectName, string IterationSprint)

        {

            WorkItemCollection total = dto.GetWorkItemCollection("Bug", projectName, "[Iteration Path]='" + IterationSprint + "'");

            WorkItemCollection NewCollection = dto.GetWorkItemCollection("Bug", projectName, "[State]='New' and [Iteration Path]='" + IterationSprint + "'");

            WorkItemCollection doneCollection = dto.GetWorkItemCollection("Bug", projectName, "[State]='Done' and [Iteration Path]='" + IterationSprint + "'");

            WorkItemCollection RemovedCollection = dto.GetWorkItemCollection("Bug", projectName, "[State]='Removed' and [Iteration Path]='" + IterationSprint + "'");

            TfsBug buginfo = new TfsBug()

            {

                Total = total.Count,

                New = NewCollection.Count,

                Done = doneCollection.Count,

                Removed=RemovedCollection.Count

            };

            return buginfo;

        }
复制代码

  3.另外一些获取Bug/PBI信息的组成方式

复制代码
        /// <summary>

        /// 获得某项目的BUG数量信息

        /// </summary>

        /// <param name="projectName"></param>

        /// <returns></returns>

        public TfsBug GetBugInfo(string projectName, string IterationSprint)

        {

            WorkItemCollection bugCollection = dto.GetWorkItemCollection("Bug", projectName, "[Iteration Path]='" + IterationSprint + "'");

            WorkItemCollection bugNewCollection = dto.GetWorkItemCollection("Bug", projectName, "[State]='New' and [Iteration Path]='" + IterationSprint + "'");

            WorkItemCollection bugApprovedCollection = dto.GetWorkItemCollection("Bug", projectName, "[State]='Approved' and [Iteration Path]='" + IterationSprint + "'");

            WorkItemCollection bugCommittedCollection = dto.GetWorkItemCollection("Bug", projectName, "[State]='Committed' and [Iteration Path]='" + IterationSprint + "'");

            WorkItemCollection bugDoneCollection = dto.GetWorkItemCollection("Bug", projectName, "[State]='Done' and [Iteration Path]='" + IterationSprint + "'");

            WorkItemCollection bugRemovedCollection = dto.GetWorkItemCollection("Bug", projectName, "[State]='Removed' and [Iteration Path]='" + IterationSprint + "'");

            TfsBug buginfo = new TfsBug()

            {

                Total = bugCollection.Count,

                New = bugNewCollection.Count,

                Approved = bugApprovedCollection.Count,

                Committed = bugCommittedCollection.Count,

                Done = bugDoneCollection.Count,

                Removed = bugRemovedCollection.Count

            };

            return buginfo;

        }



        /// <summary>

        /// 获取整个项目的PBI信息

        /// </summary>

        /// <param name="projectName"></param>

        /// <returns></returns>

        public ProjectView GetAllInfo(String projectName)

        {

            WorkItemCollection total = dto.GetWorkItemCollection("Product Backlog Item", projectName, string.Empty);

            WorkItemCollection doneCollection = dto.GetWorkItemCollection("Product Backlog Item", projectName, "[State]='Done'");

            WorkItemCollection RemovedCollection = dto.GetWorkItemCollection("Product Backlog Item", projectName, "[State]='Removed'");

            double totaleffort = GetPBIEffort(total);

            double doneeffort = GetPBIEffort(doneCollection);

            double removedeffort = GetPBIEffort(RemovedCollection);

            double effortPercent = 0;

            if(totaleffort!=0)

                effortPercent = doneeffort / totaleffort;





            WorkItemCollection RiskOpenCollection = dto.GetWorkItemCollection("Impediment", projectName, "[State]='Open'");

            int riskopenCount = RiskOpenCollection.Count;





            WorkItemCollection totalBug = dto.GetWorkItemCollection("Bug", projectName, string.Empty);

            WorkItemCollection doneCollectionBug = dto.GetWorkItemCollection("Bug", projectName, "[State]='Done'");

            WorkItemCollection RemovedCollectionBug = dto.GetWorkItemCollection("Bug", projectName, "[State]='Removed'");

            int openbugCount = totalBug.Count - doneCollectionBug.Count - RemovedCollectionBug.Count;



            ProjectView view = new ProjectView() { PbiPercent = effortPercent, OpenBugCount = openbugCount, OpenRiskCount = riskopenCount, TotalPbiEffort = totaleffort};

            return view;

        }

        /// <summary>

        /// 获得某项目的PBI数量信息

        /// </summary>

        /// <param name="projectName"></param>

        /// <returns></returns>

        public TfsPBI GetPBIInfo(string projectName, string IterationSprint)

        {

            WorkItemCollection total = dto.GetWorkItemCollection("Product Backlog Item", projectName, "[Iteration Path]='" + IterationSprint + "'");

            WorkItemCollection newcollection = dto.GetWorkItemCollection("Product Backlog Item", projectName, "[State]='New' and [Iteration Path]='" + IterationSprint + "'");

            WorkItemCollection approvedCollection = dto.GetWorkItemCollection("Product Backlog Item", projectName, "[State]='Approved' and [Iteration Path]='" + IterationSprint + "'");

            WorkItemCollection committedCollection = dto.GetWorkItemCollection("Product Backlog Item", projectName, "[State]='Committed' and [Iteration Path]='" + IterationSprint + "'");

            WorkItemCollection doneCollection = dto.GetWorkItemCollection("Product Backlog Item", projectName, "[State]='Done' and [Iteration Path]='" + IterationSprint + "'");

            WorkItemCollection removedCollection = dto.GetWorkItemCollection("Product Backlog Item", projectName, "[State]='Removed' and [Iteration Path]='" + IterationSprint + "'");



            double totaleffort = GetPBIEffort(total);

            double doneeffort=GetPBIEffort(doneCollection);

            double effortPercent = doneeffort / totaleffort;

            TfsPBI pbiinfo = new TfsPBI()

            {

                Total = total.Count,

                New = newcollection.Count,

                Approved = approvedCollection.Count,

                Committed = committedCollection.Count,

                Done = doneCollection.Count,

                Removed = removedCollection.Count,

                EffoctPercent = effortPercent,

                EffoctCurrent=(int)doneeffort,

                EffoctTotal=(int)totaleffort

            };

            return pbiinfo;

        }

        public double GetPBIEffort(WorkItemCollection collection)

        {

            double totalEff=0;

            foreach (WorkItem item in collection)

            {

                object o=item.Fields.GetById(10009).Value;

                if (o != null)

                    totalEff += (double)o;

                

            }

            return totalEff;

        }
复制代码

  4.获取Sprint,Risk等信息集合

复制代码
        /// <summary>

        /// 获得某项目的Risk数量信息

        /// </summary>

        /// <param name="projectName"></param>

        /// <returns></returns>

        public List<TfsRiskInfo> GetRiskInfo(string projectName)

        {

           WorkItemCollection RiskOpenCollection = dto.GetWorkItemCollection("Impediment", projectName, "[State]='Open'");

           

            List<TfsRiskInfo> list = new List<TfsRiskInfo>();

            foreach (WorkItem item in RiskOpenCollection)

            {

                list.Add(new TfsRiskInfo() { RiskInfo=item.Description, RiskStatus="Open",RiskId=item.Id.ToString()});

            }

            return list;

        }



        /// <summary>

        /// 获取Sprint信息

        /// </summary>

        /// <param name="projectUri"></param>

        /// <returns></returns>

        public TfsSprint GetSprintInfo(String projectUri)

        {

            TeamSettings setting= dto.GetSprintInfo(projectUri);

            TfsSprint tfssprint = new TfsSprint();

            tfssprint.CurrentIterationPath=setting.CurrentIterationPath;

            tfssprint.SprintCount=setting.IterationPaths.Count();



            IEnumerable<string> ea_items =

                 from name in setting.IterationPaths.ToList()

                 where name.Contains("Sprint")

                 select name;

            List<Sprint> list = new List<Sprint>();

            foreach (string path in ea_items)

            {

                string sprintnum = path.Substring(path.LastIndexOf("Sprint") + 6).Trim();

                string sprintname ="Sprint "+sprintnum;

                if(!string.IsNullOrEmpty(sprintnum))

                    list.Add(new Sprint() { SprintName = sprintname, SprintNum = int.Parse(sprintnum), SprintPath = path });

            }

            list.Sort((x, y) => x.SprintNum - y.SprintNum);

            tfssprint.SprintList = list;

            return tfssprint;

        }

        public IEnumerable<ScheduleInfo> GetSprintDate(string projectUri)

        { 

          return dto.GetIterationDates(projectUri);

        }

        /// <summary>

        /// 获取团队成员信息

        /// </summary>

        /// <param name="projectUri"></param>

        /// <returns></returns>

        public List<TfsMember> GetMemberInfo(String projectUri)

        {

            var list=new List<TfsMember>();

            var members=dto.GetMemberInfo(projectUri);

            foreach (TeamFoundationIdentity member in members)

            {

                var m = new TfsMember() { UserName=member.DisplayName,UserSimpleName=member.UniqueName.Substring(member.UniqueName.IndexOf('\\')+1)};

                list.Add(m);

            }

            return list;

        }



        public IEnumerable<ScheduleInfo> GetFinalBugInfo(Project project)

        {

            IEnumerable<ScheduleInfo> sprintlist = GetSprintDate(project.Uri.ToString());

            int newbug = 0;

            int openbug = 0;

            int closed = 0;

            int Totalbug = 0;

            foreach (ScheduleInfo info in sprintlist)

            {



                TfsBug bug = GetSingleBug(project.Name, info.StartDate,info.EndDate);

                info.NewBug = bug.New;

                info.Closed = bug.Done;

                Totalbug += bug.New;

                openbug = Totalbug - info.Closed;

                info.OpenBug = openbug;

            }

            return sprintlist;

        }





        private TfsBug GetSingleBug(string projectName,DateTime? createdate,DateTime? enddate)

        {

            WorkItemCollection total = dto.GetWorkItemCollection("Bug", projectName, "[Created Date]>'" + createdate + "' and [Closed Date]<'"+enddate+"'");

            WorkItemCollection NewCollection = dto.GetWorkItemCollection("Bug", projectName, "[State]='New' and [Created Date]>'" + createdate + "' and [Closed Date]<'" + enddate + "'");

            WorkItemCollection doneCollection = dto.GetWorkItemCollection("Bug", projectName, "[State]='Done' and [Created Date]>'" + createdate + "' and [Closed Date]<'" + enddate + "'");

            TfsBug buginfo = new TfsBug()

            {

                Total = total.Count,

                New = NewCollection.Count,

                Done = doneCollection.Count

            };

            return buginfo;

        }
复制代码

  5.通过以上代码的封装,我们可以得到知己展示于前台的TFS数据展示。

你可能感兴趣的:(print)