Skip to content

Instantly share code, notes, and snippets.

@tarasn
Created January 27, 2015 11:02
Show Gist options
  • Save tarasn/c3224ba0af677fa98511 to your computer and use it in GitHub Desktop.
Save tarasn/c3224ba0af677fa98511 to your computer and use it in GitHub Desktop.
Used to monitor status for builds (TFS) .The received results are cached with predefined interval
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Mime;
using System.Web;
using System.Web.Mvc;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using TeamFoundationMonitor.Domain;
using MicrosoftBuildStatus = Microsoft.TeamFoundation.Build.Client.BuildStatus;
using BuildStatus = TeamFoundationMonitor.Domain.BuildStatus;
namespace TeamFoundationMonitor.Service.Controllers
{
public class TFSMonitorController : Controller
{
private const string CacheIntervalInMinutesKey = "CacheIntervalInMinutes";
private const string BuidResultsKey = "BuidResultsKey";
//private DateTime _lastCacheAccessTime = DateTime.MinValue;
// GET: /TFSMonitor/
public ActionResult Index()
{
return Json(new { foo = "bar", baz = "Blech" },JsonRequestBehavior.AllowGet);
}
// GET: /TFSMonitor/Info
[HttpPost]
public ActionResult Info(BuildDefenitionCriteria buildDefenitionCriteria)
{
BuildResultsList results;
var cacheInterval = ReadCacheInterval();
if (cacheInterval == 0 || IsCacheItemExpired(TimeSpan.FromMinutes(cacheInterval)))
results = FetchBuildResults(buildDefenitionCriteria);
else
results = CachedBuildResults;
return Json(results, JsonRequestBehavior.AllowGet);
}
private DateTime LastCacheAccessTime
{
get
{
if (HttpContext.Application.AllKeys.Contains(CacheIntervalInMinutesKey))
return (DateTime) HttpContext.Application[CacheIntervalInMinutesKey];
return DateTime.MinValue;
}
set { HttpContext.Application[CacheIntervalInMinutesKey] = value; }
}
private BuildResultsList CachedBuildResults
{
get
{
if (HttpContext.Application.AllKeys.Contains(BuidResultsKey))
return (BuildResultsList)HttpContext.Application[BuidResultsKey];
return null;
}
set { HttpContext.Application[BuidResultsKey] = value; }
}
private bool IsCacheItemExpired(TimeSpan cacheIntervalInMinutes)
{
return DateTime.Now.Subtract(cacheIntervalInMinutes) > LastCacheAccessTime;
}
private BuildResultsList FetchBuildResults(BuildDefenitionCriteria buildDefenitionCriteria)
{
var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(buildDefenitionCriteria.ServerAddress));
tfs.EnsureAuthenticated();
var buildServer = tfs.GetService<IBuildServer>();
var buildSpec = buildServer.CreateBuildDetailSpec(buildDefenitionCriteria.TeamProjectName,
buildDefenitionCriteria.BuildDefenitionName);
buildSpec.InformationTypes = null;
buildSpec.MinFinishTime = DateTime.Now.AddHours(-100);
var buildDetails = buildServer.QueryBuilds(buildSpec).Builds.OrderByDescending(bd => bd.FinishTime);
var results = Convert(buildDetails);
UpdateCache(results);
return results;
}
private void UpdateCache(BuildResultsList buildResults)
{
CachedBuildResults = buildResults;
LastCacheAccessTime = DateTime.Now;
}
private BuildResultsList Convert(IEnumerable<IBuildDetail> buildDetails)
{
var buildResultsList = new BuildResultsList
{
LastUpdated = DateTime.Now,
};
buildResultsList.BuildResults.AddRange(ConvertToBuildResultsList(buildDetails));
return buildResultsList;
}
private IEnumerable<BuildResult> ConvertToBuildResultsList(IEnumerable<IBuildDetail> buildDetails)
{
var buildResults = new List<BuildResult>();
foreach (var buildDetail in buildDetails)
{
var buidResult = new BuildResult
{
BuildNumber = buildDetail.BuildNumber,
FinishTime = buildDetail.FinishTime,
LastChangedOn = buildDetail.LastChangedOn,
BuildStatus = ConvertToBuildStatus(buildDetail.Status),
StartTime = buildDetail.StartTime
};
AddCghangesetDetails(buildDetail, buidResult);
buildResults.Add(buidResult);
}
return buildResults;
}
private void AddCghangesetDetails(IBuildDetail buildDetail, BuildResult buidResult)
{
buildDetail.RefreshAllDetails();
var associatedChangeset = InformationNodeConverters.GetAssociatedChangesets(buildDetail);
foreach (var chagesetSummary in associatedChangeset)
{
buidResult.Changesets.Add( new Changeset
{
Id = chagesetSummary.Id,
Uri = chagesetSummary.ChangesetUri.ToString(),
CheckedInBy = chagesetSummary.CheckedInBy,
LastModifiedDate = chagesetSummary.Node.LastModifiedDate,
Description = chagesetSummary.Comment,
});
}
}
private BuildStatus ConvertToBuildStatus(MicrosoftBuildStatus status)
{
BuildStatus buildStatus;
if(!Enum.TryParse(status.ToString(),false, out buildStatus))
{
return BuildStatus.Other;
}
return buildStatus;
}
private int ReadCacheInterval()
{
var cacheIntervalInMinutes = ConfigurationManager.AppSettings[CacheIntervalInMinutesKey];
if (cacheIntervalInMinutes != null)
{
return int.Parse(cacheIntervalInMinutes);
}
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment