public static Task UpdateAverage(AverageResult a) { return Task.Factory.StartNew(() => { VotingState.CurrentAverage = a.Average; VotingState.LastUpdated = a.DateStamp; //Connect to the signalR hub var context = GlobalHost.ConnectionManager.GetHubContext<VoteHub>(); //Update the client of their vote context.Clients.All.UpdateAverage(a); }); }
public AverageResult GetAverageResult() { AverageResult ret = new AverageResult (); if (_Votes.Count != 0) { int total = _Votes.Where(v => !v.VoteLost) .Sum(v => v.VoteValue); ret.TotalCount = _Votes.Where(v => !v.VoteLost) .Count(); var aggregate = _Votes.Where(v => !v.VoteLost) .GroupBy(v => v.VoteValue) .Select(g => new { Key = g.Key, Num = g.Count() }); ret.CountBigSmile = aggregate.Where (a => a.Key == -2) .Select (a => a.Num) .FirstOrDefault(); ret.CountSmallSmile = aggregate.Where (a => a.Key == -1) .Select (a => a.Num) .FirstOrDefault(); ret.CountNoSmile = aggregate.Where (a => a.Key == 0) .Select (a => a.Num) .FirstOrDefault(); ret.CountSmallFrown = aggregate.Where (a => a.Key == 1) .Select (a => a.Num) .FirstOrDefault(); ret.CountBigFrown = aggregate.Where (a => a.Key == 2) .Select (a => a.Num) .FirstOrDefault(); ret.CountLost = _Votes.Where(v => v.VoteLost) .Count(); var lostAggregate = _Votes.Where(v => v.VoteLost) .GroupBy(v => v.VoteValue) .Select(g => new { Key = g.Key, Num = g.Count() }); ret.LostNoSmile = lostAggregate.Where(a => a.Key == 0) .Select(a => a.Num) .FirstOrDefault(); ret.LostSmallFrown = lostAggregate.Where(a => a.Key == 1) .Select(a => a.Num) .FirstOrDefault(); ret.LostBigFrown = lostAggregate.Where(a => a.Key == 2) .Select(a => a.Num) .FirstOrDefault(); decimal avgDec = (decimal)total / (decimal)ret.TotalCount; _Log.Debug("Total = " + total + ". Votes = " + ret.TotalCount + ". Avg = " + avgDec); ret.Average = (int) Math.Round(avgDec); ret.DateStamp = DateTime.Now; } return ret; }