TFS 2013 leaderboard to provide friendly competition amongst co-workers

Dashboard.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using Microsoft.TeamFoundation.VersionControl.Client;
  7. using Microsoft.TeamFoundation.WorkItemTracking.Client;
  8. namespace pulse
  9. {
  10. /// <summary>
  11. /// Dashboard.
  12. /// </summary>
  13. public class Dashboard
  14. {
  15. /// <summary>
  16. /// Gets or sets the server.
  17. /// </summary>
  18. /// <value>
  19. /// The server.
  20. /// </value>
  21. public Uri Server { get; set; }
  22. /// <summary>
  23. /// Gets or sets the selected item.
  24. /// </summary>
  25. /// <value>
  26. /// The selected item.
  27. /// </value>
  28. public int SelectedItem { get; set; }
  29. /// <summary>
  30. /// Gets or sets the date refresh.
  31. /// </summary>
  32. /// <value>
  33. /// The date refresh.
  34. /// </value>
  35. public DateTime? DateRefresh { get; set; }
  36. /// <summary>
  37. /// Gets or sets the scores.
  38. /// </summary>
  39. /// <value>
  40. /// The scores.
  41. /// </value>
  42. private IDictionary<string, double> Scores { get; set; }
  43. /// <summary>
  44. /// Gets or sets the highlighted scores.
  45. /// </summary>
  46. /// <value>
  47. /// The highlighted scores.
  48. /// </value>
  49. private IList<string> HighlightedScores { get; set; }
  50. /// <summary>
  51. /// Gets the cache file.
  52. /// </summary>
  53. /// <value>
  54. /// The cache file.
  55. /// </value>
  56. private string CacheFile
  57. {
  58. get
  59. {
  60. if (this.Server == null)
  61. {
  62. return "$cache";
  63. }
  64. return string.Format(
  65. "{0}_{1}_{2}.cache",
  66. this.Server.Scheme,
  67. this.Server.Host,
  68. this.Server.Port);
  69. }
  70. }
  71. /// <summary>
  72. /// Gets or sets the commits.
  73. /// </summary>
  74. /// <value>
  75. /// The commits.
  76. /// </value>
  77. private int Commits { get; set; }
  78. /// <summary>
  79. /// Gets or sets the projects.
  80. /// </summary>
  81. /// <value>
  82. /// The projects.
  83. /// </value>
  84. private int Projects { get; set; }
  85. /// <summary>
  86. /// Gets or sets the work items.
  87. /// </summary>
  88. /// <value>
  89. /// The work items.
  90. /// </value>
  91. private int WorkItems { get; set; }
  92. /// <summary>
  93. /// Gets the height.
  94. /// </summary>
  95. /// <value>
  96. /// The height.
  97. /// </value>
  98. private int Height
  99. {
  100. get
  101. {
  102. return Console.WindowHeight - 2;
  103. }
  104. }
  105. /// <summary>
  106. /// Gets the width.
  107. /// </summary>
  108. /// <value>
  109. /// The width.
  110. /// </value>
  111. private int Width
  112. {
  113. get
  114. {
  115. return Console.WindowWidth;
  116. }
  117. }
  118. /// <summary>
  119. /// Initializes a new instance of the <see cref="Dashboard" /> class.
  120. /// </summary>
  121. /// <param name="server">The server.</param>
  122. public Dashboard(Uri server)
  123. {
  124. this.Commits = 0;
  125. this.Projects = 0;
  126. this.WorkItems = 0;
  127. this.DateRefresh = null;
  128. this.Server = server;
  129. this.SelectedItem = 1;
  130. this.Scores = new Dictionary<string, double>();
  131. this.HighlightedScores = new List<string>();
  132. var cacheFile = new FileInfo(this.CacheFile);
  133. if (cacheFile.Exists)
  134. {
  135. using (var reader = new StreamReader(cacheFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read)))
  136. {
  137. while (!reader.EndOfStream)
  138. {
  139. var line = reader.ReadLine();
  140. var fields = line.Split(new char[] { ';' });
  141. switch (fields[0].ToLower())
  142. {
  143. case "refreshed":
  144. if (fields.Count() > 1 && !string.IsNullOrWhiteSpace(fields[1]))
  145. {
  146. this.DateRefresh = Convert.ToDateTime(fields[1]);
  147. }
  148. break;
  149. case "projects":
  150. this.Projects = Convert.ToInt32(fields[1]);
  151. break;
  152. case "work":
  153. this.WorkItems = Convert.ToInt32(fields[1]);
  154. break;
  155. case "commits":
  156. this.Commits = Convert.ToInt32(fields[1]);
  157. break;
  158. case "score":
  159. var points = Convert.ToDouble(fields[2]);
  160. if (!this.Scores.ContainsKey(fields[1]))
  161. {
  162. this.Scores.Add(fields[1], points);
  163. }
  164. else
  165. {
  166. this.Scores[fields[1]] = points;
  167. }
  168. break;
  169. }
  170. }
  171. }
  172. }
  173. }
  174. /// <summary>
  175. /// Adds the commits.
  176. /// </summary>
  177. /// <param name="commits">The commits.</param>
  178. public void AddCommits(IEnumerable<Changeset> commits)
  179. {
  180. foreach (var commit in commits)
  181. {
  182. // Add to the overall tally.
  183. this.Commits++;
  184. var personOfInterest = commit.CommitterDisplayName;
  185. this.AddHighlightedScore(personOfInterest);
  186. if (!this.Scores.Any(x => x.Key.Equals(personOfInterest, StringComparison.InvariantCultureIgnoreCase)))
  187. {
  188. this.Scores.Add(personOfInterest, 0);
  189. }
  190. // Get a point for participation.
  191. this.Scores[personOfInterest] += Rewards.Participation;
  192. // Handle points for commits with comments.
  193. if (string.IsNullOrWhiteSpace(commit.Comment))
  194. {
  195. this.Scores[personOfInterest] += Penalties.LackingVerbosity;
  196. }
  197. else
  198. {
  199. this.Scores[personOfInterest] += Rewards.Verbosity;
  200. }
  201. // Handle points with associating commits with actual work.
  202. if (commit.AssociatedWorkItems.Count() == 0)
  203. {
  204. this.Scores[personOfInterest] += Penalties.LackOfPurpose;
  205. }
  206. else
  207. {
  208. this.Scores[personOfInterest] += Rewards.Collaboration;
  209. }
  210. }
  211. }
  212. /// <summary>
  213. /// Adds the projects.
  214. /// </summary>
  215. /// <param name="projects">The projects.</param>
  216. public void AddProjects(IEnumerable<object> projects)
  217. {
  218. this.Projects = projects.Count();
  219. }
  220. /// <summary>
  221. /// Adds the work items.
  222. /// </summary>
  223. /// <param name="workItems">The work items.</param>
  224. public void AddWorkItems(IEnumerable<WorkItem> workItems)
  225. {
  226. foreach (var item in workItems)
  227. {
  228. // Add to the overall tally.
  229. this.WorkItems++;
  230. if (!string.IsNullOrWhiteSpace(item.CreatedBy))
  231. {
  232. if (!this.Scores.Any(x => x.Key.Equals(item.CreatedBy, StringComparison.InvariantCultureIgnoreCase)))
  233. {
  234. this.Scores.Add(item.CreatedBy, 0);
  235. }
  236. }
  237. if (item.CreatedBy != item.ChangedBy)
  238. {
  239. if (!string.IsNullOrWhiteSpace(item.ChangedBy))
  240. {
  241. if (!this.Scores.Any(x => x.Key.Equals(item.ChangedBy, StringComparison.InvariantCultureIgnoreCase)))
  242. {
  243. this.Scores.Add(item.ChangedBy, 0);
  244. }
  245. // Add points for collaboration.
  246. this.Scores[item.ChangedBy] += Rewards.Collaboration;
  247. }
  248. }
  249. else
  250. {
  251. if (!string.IsNullOrWhiteSpace(item.CreatedBy))
  252. {
  253. // They must have updated something right?
  254. this.Scores[item.CreatedBy] += Rewards.Participation;
  255. }
  256. }
  257. var personOfInterest = item.CreatedDate < item.ChangedDate
  258. ? item.ChangedBy
  259. : item.CreatedBy;
  260. this.AddHighlightedScore(personOfInterest);
  261. if (!string.IsNullOrWhiteSpace(personOfInterest))
  262. {
  263. switch (item.Type.Name.ToLower())
  264. {
  265. case "product backlog item":
  266. this.Scores[personOfInterest] += Rewards.Participation;
  267. break;
  268. case "user story":
  269. this.Scores[personOfInterest] += Rewards.Participation;
  270. break;
  271. case "feature":
  272. this.Scores[personOfInterest] += Rewards.Participation;
  273. break;
  274. case "task":
  275. this.Scores[personOfInterest] += Rewards.CreatedWork;
  276. break;
  277. case "impediment":
  278. case "issue":
  279. case "bug":
  280. this.Scores[personOfInterest] += Rewards.BugReport;
  281. break;
  282. case "test case":
  283. case "shared steps":
  284. this.Scores[personOfInterest] += Rewards.CreatedWork;
  285. break;
  286. default:
  287. Debug.WriteLine("Did not handle work item type [" + item.Type.Name + "]");
  288. break;
  289. }
  290. switch (item.State.ToLower())
  291. {
  292. case "new":
  293. case "design":
  294. case "to do":
  295. case "open":
  296. case "approved":
  297. case "active":
  298. case "in progress":
  299. this.Scores[personOfInterest] += Rewards.TakingOwnership;
  300. break;
  301. case "done":
  302. case "committed":
  303. case "resolved":
  304. case "closed":
  305. this.Scores[personOfInterest] += Rewards.FinishWork;
  306. break;
  307. case "removed":
  308. this.Scores[personOfInterest] += Rewards.HouseCleaning;
  309. break;
  310. default:
  311. Debug.WriteLine("Did not handle work item state [" + item.State + "]");
  312. break;
  313. }
  314. }
  315. }
  316. }
  317. /// <summary>
  318. /// Caches this instance.
  319. /// </summary>
  320. public void Cache()
  321. {
  322. var cacheFile = new FileInfo(this.CacheFile);
  323. using (var writer = new StreamWriter(cacheFile.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)))
  324. {
  325. writer.WriteLine(string.Format("{0};{1}", "refreshed", this.DateRefresh.HasValue ? this.DateRefresh.Value.ToString() : string.Empty));
  326. writer.WriteLine(string.Format("{0};{1}", "projects", this.Projects));
  327. writer.WriteLine(string.Format("{0};{1}", "work", this.WorkItems));
  328. writer.WriteLine(string.Format("{0};{1}", "commits", this.Commits));
  329. foreach (var score in this.Scores)
  330. {
  331. writer.WriteLine(string.Format("{0};{1};{2}", "score", score.Key, score.Value));
  332. }
  333. }
  334. }
  335. /// <summary>
  336. /// Clears the highlights.
  337. /// </summary>
  338. public void ClearHighlights()
  339. {
  340. this.HighlightedScores.Clear();
  341. }
  342. /// <summary>
  343. /// Updates the specified refreshing.
  344. /// </summary>
  345. public void Update()
  346. {
  347. // Header
  348. var buffer = new List<string>();
  349. buffer.AddRange(this.Statistics());
  350. // Divider
  351. buffer.Add(Padding(this.Width, "-"));
  352. // Leaderboard
  353. var leaderBoard = this.Leadboard();
  354. var pageSize = this.Height - buffer.Count();
  355. // Reset the selected item if it exceeds the leader board limitations.
  356. if (this.SelectedItem > leaderBoard.Count())
  357. {
  358. this.SelectedItem = leaderBoard.Count();
  359. }
  360. // Figure out what to put in the page.
  361. if (this.SelectedItem > pageSize)
  362. {
  363. buffer.AddRange(leaderBoard.Skip(this.SelectedItem % pageSize).Take(pageSize));
  364. }
  365. else
  366. {
  367. buffer.AddRange(leaderBoard.Take(pageSize));
  368. }
  369. // Filler
  370. var heightRemaining = this.Height - buffer.Count();
  371. for (var i = 0; i < heightRemaining; i++)
  372. {
  373. buffer.Add(string.Empty);
  374. }
  375. Console.CursorVisible = false;
  376. Console.SetCursorPosition(0, 0);
  377. foreach (var line in buffer)
  378. {
  379. foreach (var highlight in this.HighlightedScores)
  380. {
  381. if (line.Contains(highlight))
  382. {
  383. Console.ForegroundColor = ConsoleColor.Green;
  384. break;
  385. }
  386. }
  387. if (line.StartsWith(string.Format(" {0,3:###}.", this.SelectedItem)))
  388. {
  389. Console.BackgroundColor = ConsoleColor.White;
  390. if (Console.ForegroundColor != ConsoleColor.Green)
  391. {
  392. Console.ForegroundColor = ConsoleColor.Black;
  393. }
  394. }
  395. if (line.Length >= this.Width)
  396. {
  397. Console.Write(line);
  398. }
  399. else
  400. {
  401. Console.WriteLine(line);
  402. }
  403. Console.ResetColor();
  404. }
  405. // Notify with a sound
  406. if (this.HighlightedScores.Count() > 0)
  407. {
  408. Sound.Queue.Enqueue(Sound.Notify);
  409. }
  410. }
  411. /// <summary>
  412. /// Adds the highlighted score.
  413. /// </summary>
  414. /// <param name="person">The person.</param>
  415. private void AddHighlightedScore(string person)
  416. {
  417. if (this.Scores.ContainsKey(person) && !this.HighlightedScores.Contains(person))
  418. {
  419. this.HighlightedScores.Add(person);
  420. }
  421. }
  422. /// <summary>
  423. /// Statisticses this instance.
  424. /// </summary>
  425. /// <returns></returns>
  426. private IEnumerable<string> Statistics()
  427. {
  428. var lines = new List<string>();
  429. var title = "pulse";
  430. var titlePadding = Padding((this.Width - title.Length) / 2);
  431. lines.Add(string.Format("{0}{1}{0}", titlePadding, title));
  432. var stats = string.Format(
  433. "p: {0} | w: {1} | c: {2} | r: {3:MM/dd/yyyy HH:mm}",
  434. this.Projects,
  435. this.WorkItems,
  436. this.Commits,
  437. this.DateRefresh.HasValue ? this.DateRefresh.Value : DateTime.Now);
  438. var statsPadding = Padding((this.Width - stats.Length) / 2);
  439. lines.Add(string.Format("{0}{1}{0}", statsPadding, stats));
  440. return lines;
  441. }
  442. /// <summary>
  443. /// Leadboards this instance.
  444. /// </summary>
  445. /// <returns></returns>
  446. private IEnumerable<string> Leadboard()
  447. {
  448. if (this.Scores == null || this.Scores.Count() == 0)
  449. {
  450. return new List<string>();
  451. }
  452. var rank = 0;
  453. return this.Scores
  454. .OrderByDescending(x => x.Value)
  455. .Select(x =>
  456. {
  457. var player = x.Key;
  458. var points = (int)x.Value;
  459. if (points < 0)
  460. {
  461. points = 0;
  462. }
  463. var basic = string.Format(" {0,3:###}. {1}{{0}}{2}", ++rank, player, points);
  464. return basic.Replace("{0}", Padding(this.Width - basic.Length + 2, " "));
  465. })
  466. .ToList();
  467. }
  468. /// <summary>
  469. /// Paddings the specified length.
  470. /// </summary>
  471. /// <param name="length">The length.</param>
  472. /// <param name="text">The text.</param>
  473. /// <returns></returns>
  474. private static string Padding(int length, string text = " ")
  475. {
  476. var padding = string.Empty;
  477. for (var i = 0; i < length; i++)
  478. {
  479. padding += text;
  480. }
  481. return padding;
  482. }
  483. }
  484. }