Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.

Update ProjectGrid.cs : QueuePosition #238

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
246 changes: 178 additions & 68 deletions project/WebDashboard/Dashboard/ProjectGrid.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using ThoughtWorks.CruiseControl.Core.Reporting.Dashboard.Navigation;
using ThoughtWorks.CruiseControl.Remote;
using ThoughtWorks.CruiseControl.WebDashboard.Plugins.ProjectReport;
Expand All @@ -9,76 +12,183 @@
namespace ThoughtWorks.CruiseControl.WebDashboard.Dashboard
{
public class ProjectGrid : IProjectGrid
{
public ProjectGridRow[] GenerateProjectGridRows(ProjectStatusOnServer[] statusList, string forceBuildActionName,
ProjectGridSortColumn sortColumn, bool sortIsAscending, string categoryFilter,
ICruiseUrlBuilder urlBuilder, Translations translations)
{
var rows = new List<ProjectGridRow>();
foreach (ProjectStatusOnServer statusOnServer in statusList)
{
ProjectStatus status = statusOnServer.ProjectStatus;
IServerSpecifier serverSpecifier = statusOnServer.ServerSpecifier;
DefaultProjectSpecifier projectSpecifier = new DefaultProjectSpecifier(serverSpecifier, status.Name);

if ((categoryFilter != string.Empty) && (categoryFilter != status.Category))
continue;

rows.Add(
new ProjectGridRow(status,
serverSpecifier,
urlBuilder.BuildProjectUrl(ProjectReportProjectPlugin.ACTION_NAME, projectSpecifier),
urlBuilder.BuildProjectUrl(ProjectParametersAction.ActionName, projectSpecifier),
translations));
}

return rows.OrderBy(a => a, GetComparer(sortColumn, sortIsAscending)).ToArray();
}

private IComparer<ProjectGridRow> GetComparer(ProjectGridSortColumn column, bool ascending)
{
return new ProjectGridRowComparer(column, ascending);
}

private class ProjectGridRowComparer
{
public ProjectGridRow[] GenerateProjectGridRows(ProjectGridParameters parameters)
{
var rows = new List<ProjectGridRow>();
var farmService = parametes.FarmService;
var appSettings = ConfigurationManager.AppSettings;
string disk = appSettings["disk"];
string server = appSettings["server"];
foreach (ProjectStatusOnServer statusOnServer in parameters.StatusList)
{
ProjectStatus status = statusOnServer.ProjectStatus;
IServerSpecifier serverSpecifier = statusOnServer.ServerSpecifier;
DefaultProjectSpecifier projectSpecifier = new DefaultProjectSpecifier(serverSpecifier, status.Name);

if ((parametrs.CategoryFilter != string.Empty) && (parametrs.CategoryFilter != status.Category))
continue;

string dir = disk + server;
string statistics = "<!-- UNABLE TO FIND HEARTBEAT FOLDER -->";

if (Directory.Exists(dir))
{
var file = String.Format(dir + @"{0}\ccnet\{1}\TestResults.html", serverSpecifier.ServerName, projectSpecifier.ProjectName);
try
{
statistics = File.ReadAllText(file);
}
catch (Exception e)
{
statistics = String.Format("<!-- UNABLE TO GET STATISTICS: {0} -->", e.Message.Replace("-->", "- - >"));
}
}

var lastFiveDataGridRows = getLastFiveDataGridrows(serverSpecifier, projectSpecifier, dir, farmService);
var queuePosition = getQueuePosition(status);

rows.Add(
new ProjectGridRow(status,
serverSpecifier,
parameters.UrlBuilder.BuildProjectUrl(ProjectReportProjectPlugin.ACTION_NAME, projectSpecifier),
parameters.UrlBuilder.BuildProjectUrl(ProjectParametersAction.ActionName, projectSpecifier),
statistics,
lastFiveDataGridRows,
queuePosition,
parameters.Translation));
}

rows.Sort(GetComparer(parameters.SortColumn, parameters.SortIsAscending));
return rows.ToArray();
}

private IComparer<ProjectGridRow> GetComparer(ProjectGridSortColumn column, bool ascending)
{
return new ProjectGridRowComparer(column, ascending);
}

private class ProjectGridRowComparer
: IComparer<ProjectGridRow>
{
private readonly ProjectGridSortColumn column;
private readonly bool ascending;
{
private readonly ProjectGridSortColumn column;
private readonly bool ascending;

public ProjectGridRowComparer(ProjectGridSortColumn column, bool ascending)
{
this.column = column;
this.ascending = ascending;
}
public ProjectGridRowComparer(ProjectGridSortColumn column, bool ascending)
{
this.column = column;
this.ascending = ascending;
}

public int Compare(ProjectGridRow x, ProjectGridRow y)
{
if (column == ProjectGridSortColumn.Name)
{
return x.Name.CompareTo(y.Name)*(ascending ? 1 : -1);
}
else if (column == ProjectGridSortColumn.LastBuildDate)
{
return x.LastBuildDate.CompareTo(y.LastBuildDate)*(ascending ? 1 : -1);
}
else if (column == ProjectGridSortColumn.BuildStatus)
{
return x.BuildStatus.CompareTo(y.BuildStatus)*(ascending ? 1 : -1);
}
else if (column == ProjectGridSortColumn.ServerName)
{
return x.ServerName.CompareTo(y.ServerName)*(ascending ? 1 : -1);
}
else if (column == ProjectGridSortColumn.Category)
{
if (column == ProjectGridSortColumn.Name)
{
return x.Name.CompareTo(y.Name) * (ascending ? 1 : -1);
}
else if (column == ProjectGridSortColumn.LastBuildDate)
{
return x.LastBuildDate.CompareTo(y.LastBuildDate) * (ascending ? 1 : -1);
}
else if (column == ProjectGridSortColumn.BuildStatus)
{
return x.BuildStatus.CompareTo(y.BuildStatus) * (ascending ? 1 : -1);
}
else if (column == ProjectGridSortColumn.ServerName)
{
return x.ServerName.CompareTo(y.ServerName) * (ascending ? 1 : -1);
}
else if (column == ProjectGridSortColumn.Category)
{
return x.Category.CompareTo(y.Category) * (ascending ? 1 : -1);
}
else
{
return 0;
}
}
}

private List<DataGridRow> getLastFiveDataGridRows(IServerSpecifier serverSpecifier, DefaultProjectSpecifier projectSpecifier, string dir, IFarmService farmService)
{
var lastFiveDataList = new List<DataGridRow>();
IBuildSpecifier[] mostRecentBuildSpecifiers = farmService.GetMostRecentBuildSpecifiers(projectSpecifier, 5, BuildReportBuildPlugin.ACTION_NAME);

if (Directory.Exists(dir))
{
foreach (IBuildSpecifier buildSpecifier in mostRecentBuildSpecifiers)
{
lastFiveDataList = getBuildData(serverSpecifier, projectSpecifier, dir);
}
}
return lastFiveDataList;
}

private List<string> getBuildData(IServerSpecifier serverSpecifier, DefaultProjectSpecifier projectSpecifier, string dir)
{
var dataToReturn = new List<string>();
var file = String.Format(dir + @"{0}\ccnet\{1}\Artifacts\buildlogs\{2}", serverSpecifier.ServerName, projectSpecifier.ProjectName, buildSpecifier.BuildName);
try
{
var doc = XDocument.Load(file);
string lastStatus;
string lastDate;
string lastRunningTime;
string lastLink;

lastStatus = doc.Root.Elements().Select(x => x.Element("CCNetIntegrationStatus"));
XmlNodeList elemList = doc.GetElementsByTagName("build");

foreach (XmlNode node in elemList)
{
lastDate = node.Attributes["date"].Value;
lastRunningTime = node.Attributes["buildtime"].Value;
if (state.Equals("Unknown"))
{
lastLink = "";
}
else
{
var lastlink = String.Format("http://{0}/ccnet/server/{0}/project/{1}/build/{2}/ViewBuildReport.aspx", serverSpecifier.ServerName, projectSpecifier.ProjectName, buildSpecifier.BuildName);
}
}

dataToReturn.Add(new DataGridRow(lastStatus, lastDate, lastRunningTime, lastLink));
return dataToReturn;
}
catch (Exception e)
{
throw new System.ArgumentException("File not found or corrupted. Error: " + e, "Argument");
}
}

private int getQueuePosition(ProjectStatus status)
{
if(status.Activity.ToString().equals("Pending"))
{
return getPositionInQueueList();
}
else
{
return -1;
}
}

private int getPositionInQueueList()
{
int positionInQueue;
QueueSnapshot queueSnapshot = new QueueSnapshot(status.Queue);
var queueList = queueSnapshot.Request;
int count = 1; //Position 0 would be the building project
foreach (QueuedRequestSnapshot queueRequestSnapshot in queueList)
{
if (projectSpecifier.ProjectName == queueRequestSnapshot.ProjectName)
{
return x.Category.CompareTo(y.Category)*(ascending ? 1 : -1);
}
else
{
return 0;
}
}
}
}
positionInQueue = count;
}
count++;
}
return positionInQueue;
}
}
}