using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
namespace pulse
{
///
/// Dashboard.
///
public class Dashboard
{
///
/// Gets or sets the date refresh.
///
///
/// The date refresh.
///
public DateTime? DateRefresh { get; set; }
///
/// Gets or sets the selected item.
///
///
/// The selected item.
///
public int SelectedItem { get; set; }
///
/// Gets or sets the server.
///
///
/// The server.
///
public Uri Server { get; set; }
///
/// Gets the cache file.
///
///
/// The cache file.
///
private string CacheFile
{
get
{
if (this.Server == null)
{
return "$cache";
}
return string.Format(
"{0}_{1}_{2}.cache",
this.Server.Scheme,
this.Server.Host,
this.Server.Port);
}
}
///
/// Gets or sets the commits.
///
///
/// The commits.
///
private int Commits { get; set; }
///
/// Gets the height.
///
///
/// The height.
///
private int Height
{
get
{
return Console.WindowHeight - 2;
}
}
///
/// Gets or sets the highlighted scores.
///
///
/// The highlighted scores.
///
private IList HighlightedScores { get; set; }
///
/// Gets or sets the points.
///
///
/// The points.
///
private IDictionary Points { get; set; }
///
/// Gets or sets the projects.
///
///
/// The projects.
///
private int Projects { get; set; }
///
/// Gets or sets the scores.
///
///
/// The scores.
///
private IDictionary Scores { get; set; }
///
/// Gets the width.
///
///
/// The width.
///
private int Width
{
get
{
return Console.WindowWidth;
}
}
///
/// Gets or sets the work items.
///
///
/// The work items.
///
private int WorkItems { get; set; }
///
/// Initializes a new instance of the class.
///
/// The server.
public Dashboard(Uri server)
{
this.Commits = 0;
this.Projects = 0;
this.WorkItems = 0;
this.DateRefresh = null;
this.Server = server;
this.SelectedItem = 1;
this.Scores = new Dictionary();
this.HighlightedScores = new List();
this.Points = new Dictionary()
{
{ Penalties.LackingVerbosity, -0.2 },
{ Penalties.LackOfPurpose, -0.2 },
{ Rewards.BugReport, 0.7 },
{ Rewards.Collaboration, 0.7 },
{ Rewards.CreatedWork, 0.02 },
{ Rewards.DueDiligence, 0.4 },
{ Rewards.FinishWork, 0.5 },
{ Rewards.HouseCleaning, 0.1 },
{ Rewards.Participation, 0.3 },
{ Rewards.TakingOwnership, 0.1 },
{ Rewards.Verbosity, 0.8 }
};
var cacheFile = new FileInfo(this.CacheFile);
if (cacheFile.Exists)
{
using (var reader = new StreamReader(cacheFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read)))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var fields = line.Split(new char[] { ';' });
switch (fields[0].ToLower())
{
case "refreshed":
if (fields.Count() > 1 && !string.IsNullOrWhiteSpace(fields[1]))
{
this.DateRefresh = Convert.ToDateTime(fields[1]);
}
break;
case "projects":
this.Projects = Convert.ToInt32(fields[1]);
break;
case "work":
this.WorkItems = Convert.ToInt32(fields[1]);
break;
case "commits":
this.Commits = Convert.ToInt32(fields[1]);
break;
case "score":
var points = Convert.ToDouble(fields[2]);
if (!this.Scores.ContainsKey(fields[1]))
{
this.Scores.Add(fields[1], points);
}
else
{
this.Scores[fields[1]] = points;
}
break;
case "point":
var pointValue = Convert.ToDouble(fields[2]);
if (!this.Points.ContainsKey(fields[1]))
{
this.Points.Add(fields[1], pointValue);
}
else
{
this.Points[fields[1]] = pointValue;
}
break;
}
}
}
}
}
///
/// Adds the commits.
///
/// The commits.
public void AddCommits(IEnumerable commits)
{
foreach (var commit in commits)
{
// Add to the overall tally.
this.Commits++;
var personOfInterest = commit.CommitterDisplayName;
this.AddHighlightedScore(personOfInterest);
if (!this.Scores.Any(x => x.Key.Equals(personOfInterest, StringComparison.InvariantCultureIgnoreCase)))
{
this.Scores.Add(personOfInterest, 0);
}
// Get a point for participation.
this.Scores[personOfInterest] += this.GetPoints(Rewards.Participation);
// Handle points for commits with comments.
if (string.IsNullOrWhiteSpace(commit.Comment))
{
this.Scores[personOfInterest] += this.GetPoints(Penalties.LackingVerbosity);
}
else
{
this.Scores[personOfInterest] += this.GetPoints(Rewards.Verbosity);
}
// Handle points with associating commits with actual work.
if (commit.AssociatedWorkItems.Count() == 0)
{
this.Scores[personOfInterest] += this.GetPoints(Penalties.LackOfPurpose);
}
else
{
this.Scores[personOfInterest] += this.GetPoints(Rewards.Collaboration);
}
}
}
///
/// Adds the projects.
///
/// The projects.
public void AddProjects(IEnumerable