上一篇文章咱們編寫了此例的DTO層,本文將數據訪問層封裝爲邏輯層,提供給界面使用。app
1.獲取TFS Dto實例,而且能夠獲取項目集合,以及單獨獲取某個項目實體spa
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等信息code
/// <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信息的組成方式server
/// <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等信息集合blog
/// <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數據展現。ip