Exemplo n.º 1
0
        public object GetCurrentResults()
        {
            try
            {
                var resultSummaryFinal =
                    _analyzer.ResultSummaryFinal; // resultSummaries.SingleOrDefault(rs => rs.ResultType == ResultType.Final);

                //TODO build online checks into base analysis

                // don't show any details if review is needed or online ballots need to be processed
                var issues = new List <string>();
                if (_election.OnlineCurrentlyOpen)
                {
                    issues.Add("Online voting is still open. It must be Closed before analyzing ballots.");
                }

                var unprocessedOnlineBallots = _election.OnlineWhenOpen.HasValue
          ? Db.OnlineVotingInfo
                                               .Join(Db.Person.Where(p => p.VotingMethod == VotingMethodEnum.Online.Value), ovi => ovi.PersonGuid, p => p.PersonGuid, (ovi, p) => ovi)
                                               .Count(ovi => ovi.ElectionGuid == UserSession.CurrentElectionGuid && ovi.Status == OnlineBallotStatusEnum.Submitted)
          : 0;
                if (unprocessedOnlineBallots > 0)
                {
                    issues.Add($"Online ballots waiting to be accepted: {unprocessedOnlineBallots}");
                }

                if (resultSummaryFinal.BallotsNeedingReview != 0 || issues.Any())
                {
                    var locations         = new LocationCacher(Db).AllForThisElection;
                    var multipleLocations = locations.Count() > 1;

                    var needReview = _analyzer.VoteInfos.Where(VoteAnalyzer.VoteNeedReview)
                                     .Join(locations, vi => vi.LocationId, l => l.C_RowId,
                                           (vi, location) => new { vi, location })
                                     .Select(x => new
                    {
                        x.vi.LocationId,
                        x.vi.BallotId,
                        Status = BallotStatusEnum.TextFor(x.vi.BallotStatusCode),
                        Ballot = multipleLocations ? $"{x.vi.C_BallotCode} ({x.location.Name})" : x.vi.C_BallotCode,
                    })
                                     .Distinct()
                                     .OrderBy(x => x.Ballot);

                    var needReview2 = _analyzer.Ballots.Where(BallotAnalyzer.BallotNeedsReview)
                                      .Join(locations, b => b.LocationGuid, l => l.LocationGuid,
                                            (b, location) => new { b, location })
                                      .OrderBy(x => x.b.ComputerCode)
                                      .ThenBy(x => x.b.BallotNumAtComputer)
                                      .Select(x => new
                    {
                        LocationId = x.location.C_RowId,
                        BallotId   = x.b.C_RowId,
                        Status     = BallotStatusEnum.TextFor(x.b.StatusCode),
                        Ballot     = multipleLocations ? $"{x.b.C_BallotCode} ({x.location.Name})" : x.b.C_BallotCode
                    });

                    return(new
                    {
                        NeedReview = needReview.Concat(needReview2).Distinct(),
                        _election.VotingMethods,
                        ResultsFinal = _analyzer.ResultSummaryFinal
                                       .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                        ResultsCalc =
                            _analyzer.ResultSummaries.First(rs => rs.ResultType == ResultType.Calculated)
                            .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                        ResultsManual = (_analyzer.ResultSummaries.FirstOrDefault(rs => rs.ResultType == ResultType.Manual) ??
                                         new ResultSummary()).GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                        OnlineIssues = issues,
                        _election.OnlineCurrentlyOpen
                        //ResultsFinal =
                        //  resultSummaries.First(rs => rs.ResultType == ResultType.Final)
                        //    .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                    });
                }

                // show vote totals

                var persons = new PersonCacher(Db).AllForThisElection;

                var vResultInfos =
                    // TODO 2012-01-21 Glen Little: Could return fewer columns for non-tied results
                    _analyzer.Results // new ResultCacher(Db).AllForThisElection
                    .OrderBy(r => r.Rank)
                    .ToList()
                    .Select(r => new
                {
                    rid = r.C_RowId,
                    r.CloseToNext,
                    r.CloseToPrev,
                    r.ForceShowInOther,
                    r.IsTied,
                    r.IsTieResolved,
                    PersonName = PersonNameFor(persons, r),
                    r.Rank,
                    //ri.RankInExtra,
                    r.Section,
                    r.TieBreakCount,
                    r.TieBreakGroup,
                    r.TieBreakRequired,
                    r.VoteCount
                }).ToList();

                var ties = _analyzer.ResultTies //  new ResultTieCacher(Db).AllForThisElection
                           .OrderBy(rt => rt.TieBreakGroup)
                           .Select(rt => new
                {
                    rt.TieBreakGroup,
                    rt.NumInTie,
                    rt.NumToElect,
                    rt.TieBreakRequired,
                    rt.IsResolved
                }).ToList();

                //var spoiledVotesSummary = Db.vVoteInfoes.where
                return(new
                {
                    Votes = vResultInfos,
                    UserSession.CurrentElectionStatus,
                    Ties = ties,
                    NumToElect = _election.NumberToElect,
                    NumExtra = _election.NumberExtra,
                    // ShowCalledIn = _election.UseCallInButton,
                    // _election.CustomMethods,
                    _election.VotingMethods,
                    ShowOnline = _election.OnlineWhenOpen.HasValue,
                    ResultsManual =
                        (_analyzer.ResultSummaries.FirstOrDefault(rs => rs.ResultType == ResultType.Manual) ?? new ResultSummary())
                        .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                    ResultsCalc =
                        _analyzer.ResultSummaries.First(rs => rs.ResultType == ResultType.Calculated)
                        .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                    ResultsFinal =
                        _analyzer.ResultSummaries.First(rs => rs.ResultType == ResultType.Final)
                        .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                });
            }
            catch (Exception ex)
            {
                return(new
                {
                    Interrupted = true,
                    Msg = ex.GetAllMsgs("; ") + "\n" + ex.StackTrace
                });
            }
        }
Exemplo n.º 2
0
        public object GetCurrentResults()
        {
            //var ready = _analyzer.IsResultAvailable;
            var dbContext = GetNewDbContext();

            try
            {
                var resultSummaryFinal = _analyzer.ResultSummaryFinal; // resultSummaries.SingleOrDefault(rs => rs.ResultType == ResultType.Final);


                // don't show any details if review is needed
                if (resultSummaryFinal.BallotsNeedingReview != 0)
                {
                    var locations         = new LocationCacher(dbContext).AllForThisElection;
                    var multipleLocations = locations.Count() > 1;

                    //TODO
                    var needReview = _analyzer.VoteInfos // .Where(VoteAnalyzer.VoteNeedReview)
                                     .Join(locations, vi => vi.LocationId, l => l.Id,
                                           (vi, location) => new { vi, location })
                                     .Select(x => new
                    {
                        x.vi.LocationId,
                        x.vi.BallotId,
                        Status =
                            x.vi.BallotStatusCode == "Review"
                            ? BallotStatusEnum.Review.DisplayText
                            : "Verification Needed",
                        Ballot = multipleLocations ?
                                 string.Format("{0} ({1})", x.vi.C_BallotCode, x.location.Name) : x.vi.C_BallotCode
                    })
                                     .Distinct()
                                     .OrderBy(x => x.Ballot);

                    var needReview2 = _analyzer.Ballots.Where(b => b.StatusCode == BallotStatusEnum.Review)
                                      .Join(locations, b => b.LocationGuid, l => l.LocationGuid,
                                            (b, location) => new { b, location })
                                      .Select(x => new
                    {
                        LocationId = x.location.Id,
                        BallotId   = x.b.Id,
                        Status     =
                            x.b.StatusCode == "Review"
                            ? BallotStatusEnum.Review.DisplayText
                            : "Verification Needed",
                        Ballot = multipleLocations ?
                                 string.Format("{0} ({1})", x.b.C_BallotCode, x.location.Name) : x.b.C_BallotCode
                    });

                    return(new
                    {
                        NeedReview = needReview.Concat(needReview2).Distinct(),
                        ResultsFinal = _analyzer.ResultSummaryFinal
                                       .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                        ResultsCalc =
                            _analyzer.ResultSummaries.First(rs => rs.ResultType == ResultType.Calculated)
                            .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                        ResultsManual = (_analyzer.ResultSummaries.FirstOrDefault(rs => rs.ResultType == ResultType.Manual) ??
                                         new ResultSummary()).GetPropertiesExcept(null, new[] { "ElectionGuid" }),

                        //ResultsFinal =
                        //  resultSummaries.First(rs => rs.ResultType == ResultType.Final)
                        //    .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                    });
                }

                // show vote totals

                var persons = new PersonCacher(dbContext).AllForThisElection;

                var vResultInfos =
                    // TODO 2012-01-21 Glen Little: Could return fewer columns for non-tied results
                    _analyzer.Results // new ResultCacher(Db).AllForThisElection
                    .OrderBy(r => r.Rank)
                    .ToList()
                    .Select(r => new
                {
                    rid = r.Id,
                    r.CloseToNext,
                    r.CloseToPrev,
                    r.ForceShowInOther,
                    r.IsTied,
                    r.IsTieResolved,
                    PersonName = PersonNameFor(persons, r),
                    r.Rank,
                    //ri.RankInExtra,
                    r.Section,
                    r.TieBreakCount,
                    r.TieBreakGroup,
                    r.TieBreakRequired,
                    r.VoteCount
                }).ToList();

                var ties = _analyzer.ResultTies //  new ResultTieCacher(Db).AllForThisElection
                           .OrderBy(rt => rt.TieBreakGroup)
                           .Select(rt => new
                {
                    rt.TieBreakGroup,
                    rt.NumInTie,
                    rt.NumToElect,
                    rt.TieBreakRequired,
                    rt.IsResolved
                }).ToList();

                //var spoiledVotesSummary = Db.vVoteInfoes.where



                return(new
                {
                    Votes = vResultInfos,
                    UserSession.CurrentElectionStatus,
                    Ties = ties,
                    NumToElect = _election.NumberToElect,
                    NumExtra = _election.NumberExtra,
                    ShowCalledIn = _election.UseCallInButton,
                    ResultsManual =
                        (_analyzer.ResultSummaries.FirstOrDefault(rs => rs.ResultType == ResultType.Manual) ?? new ResultSummary())
                        .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                    ResultsCalc =
                        _analyzer.ResultSummaries.First(rs => rs.ResultType == ResultType.Calculated)
                        .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                    ResultsFinal =
                        _analyzer.ResultSummaries.First(rs => rs.ResultType == ResultType.Final)
                        .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                });
            }
            catch (Exception ex)
            {
                return(new
                {
                    Interrupted = true,
                    Msg = ex.GetAllMsgs("; ") + "\n" + ex.StackTrace
                });
            }
        }