Exemple #1
0
        public void PrepareForAnalysis()
        {
            ClearInMemoryCachedInfo();

            _hub.StatusUpdate("Starting Analysis from computer " + UserSession.CurrentComputerCode);
            var electionGuid = TargetElection.ElectionGuid;
            //if (!IsFaked)
            //{
            var resultSummaryCacher = new ResultSummaryCacher(Db);
            var summaries           = resultSummaryCacher.AllForThisElection;

            resultSummaryCacher.RemoveItemsAndSaveCache(summaries.Where(rs => rs.ResultType != ResultType.Manual));

            Db.ResultSummary.RemoveRange(
                Db.ResultSummary.Where(r => r.ElectionGuid == electionGuid && r.ResultType != ResultType.Manual)
                );

            RefreshBallotStatuses();

            // attach results, but don't save yet
            Results.ForEach(delegate(Result result)
            {
                Savers.ResultSaver(DbAction.Attach, result);
                InitializeSomeProperties(result);
            });

            PrepareResultSummaries();

            FillResultSummaryCalc();
        }
Exemple #2
0
        //public ElectionAnalyzerSingleName(Election election, ResultSummary resultSummary, List<Result> results,
        //                                  List<VoteInfo> voteinfos, List<Ballot> ballots, Func<Result, Result> deleteResult,
        //                                  Func<Result, Result> addResult
        //                                  , Func<int> saveChanges, List<Person> people)
        //  : base(election, resultSummary, results, people, ballots, voteinfos, deleteResult, addResult, saveChanges)
        //{
        //}

        public override void AnalyzeEverything()
        {
            PrepareForAnalysis();

            // for single name elections, # votes = # ballots
            ResultSummaryCalc.BallotsReceived
                    = ResultSummaryCalc.NumVoters
                    = ResultSummaryCalc.TotalVotes
                    = VoteInfos.Sum(vi => vi.SingleNameElectionCount).AsInt();

            var invalidBallotGuids =
                Ballots.Where(bi => bi.StatusCode != BallotStatusEnum.Ok).Select(ib => ib.BallotGuid).ToList();

            //VoteInfos.Where(vi => vi.BallotStatusCode != "Ok").Select(ib => ib.BallotGuid).Distinct().ToList();

            ResultSummaryCalc.SpoiledBallots = invalidBallotGuids.Count();

            ResultSummaryCalc.SpoiledVotes =
                VoteInfos.Where(vi => !invalidBallotGuids.Contains(vi.BallotGuid) && vi.VoteStatusCode != VoteStatusCode.Ok).Sum(
                    vi => vi.SingleNameElectionCount).AsInt();

            // vote == ballot for this election
            ResultSummaryCalc.BallotsNeedingReview =
                //Ballots.Count(BallotAnalyzer.BallotNeedsReview);
                VoteInfos.Count(VoteAnalyzer.VoteNeedReview);

            // clear any existing results
            Results.ForEach(InitializeSomeProperties);

            var electionGuid = TargetElection.ElectionGuid;

            _hub.StatusUpdate("Processing votes", true);
            var numDone = 0;

            // collect only valid votes
            foreach (var voteInfo in VoteInfos.Where(vi => vi.VoteStatusCode == VoteStatusCode.Ok))
            {
                numDone++;
//        if (numDone % 10 == 0)
//        {
//          _hub.StatusUpdate("Processed {0} vote{1}".FilledWith(numDone, numDone.Plural()), true);
//        }
//
                // get existing result record for this person, if available
                var result =
                    Results.SingleOrDefault(r => r.ElectionGuid == electionGuid && r.PersonGuid == voteInfo.PersonGuid);
                if (result == null)
                {
                    result = new Result
                    {
                        ElectionGuid = electionGuid,
                        PersonGuid   = voteInfo.PersonGuid.AsGuid()
                    };
                    InitializeSomeProperties(result);
                    Savers.ResultSaver(DbAction.Add, result);
                    Results.Add(result);
                }

                var voteCount = result.VoteCount.AsInt() + voteInfo.SingleNameElectionCount;
                result.VoteCount = voteCount;
            }
            _hub.StatusUpdate("Processed {0} unspoiled vote{1}".FilledWith(numDone, numDone.Plural()));

            FinalizeResultsAndTies();
            FinalizeSummaries();

            var readyForReports = ResultSummaryFinal.UseOnReports.AsBoolean();

            if (UserSession.CurrentElectionStatus == ElectionTallyStatusEnum.Finalized && !readyForReports)
            {
                new ElectionModel().SetTallyStatus(ElectionTallyStatusEnum.Tallying);
            }

            _hub.StatusUpdate("Saving");

            Db.SaveChanges();

            new ResultSummaryCacher(Db).DropThisCache();
            new ResultTieCacher(Db).DropThisCache();
        }
        public override void AnalyzeEverything()
        {
            PrepareForAnalysis();

            ResultSummaryCalc.BallotsNeedingReview = Ballots.Count(BallotAnalyzer.BallotNeedsReview);

            ResultSummaryCalc.TotalVotes = Ballots.Count * TargetElection.NumberToElect;

            var invalidBallotGuids =
                Ballots.Where(b => b.StatusCode != BallotStatusEnum.Ok).Select(b => b.BallotGuid).ToList();

            ResultSummaryCalc.SpoiledBallots = invalidBallotGuids.Count();
            ResultSummaryCalc.SpoiledVotes   =
                VoteInfos.Count(vi => !invalidBallotGuids.Contains(vi.BallotGuid) && vi.VoteStatusCode != VoteHelper.VoteStatusCode.Ok);

            ResultSummaryCalc.BallotsReceived = Ballots.Count - ResultSummaryCalc.SpoiledBallots;

            var electionGuid = TargetElection.ElectionGuid;


            // collect only valid ballots
            _hub.StatusUpdate("Processing ballots", true);
            var numDone       = 0;
            var numVotesTotal = 0;

            foreach (var ballot in Ballots.Where(bi => bi.StatusCode == BallotStatusEnum.Ok))
            {
                numDone++;
                if (numDone % 10 == 0)
                {
                    _hub.StatusUpdate("Processed {0} ballot{1} ({2} votes)".FilledWith(numDone, numDone.Plural(), numVotesTotal), true);
                }

                var ballotGuid = ballot.BallotGuid;
                // collect only valid votes
                foreach (
                    var voteInfoRaw in
                    VoteInfos.Where(vi => vi.BallotGuid == ballotGuid && vi.VoteStatusCode == VoteHelper.VoteStatusCode.Ok))
                {
                    var voteInfo = voteInfoRaw;
                    numVotesTotal++;
                    // get existing result record for this person, if available
                    var result = Results.FirstOrDefault(r => r.ElectionGuid == electionGuid && r.PersonGuid == voteInfo.PersonGuid);
                    //Result result = null;
                    //if (results.Count == 1)
                    //{
                    //  result = results[0];
                    //}
                    //else if (results.Count > 1) {
                    //  // old/bad data!
                    //  foreach (var r in results)
                    //  {
                    //    Savers.ResultSaver(DbAction.AttachAndRemove, r);
                    //    Results.Remove(r);
                    //  }
                    //}
                    if (result == null)
                    {
                        result = new Result
                        {
                            ElectionGuid = electionGuid,
                            PersonGuid   = voteInfo.PersonGuid.AsGuid()
                        };
                        InitializeSomeProperties(result);

                        Savers.ResultSaver(DbAction.Add, result);
                        Results.Add(result);
                    }

                    var voteCount = result.VoteCount.AsInt() + 1;
                    result.VoteCount = voteCount;
                }
            }
            _hub.StatusUpdate("Processed {0} unspoiled ballot{1} ({2} votes)".FilledWith(numDone, numDone.Plural(), numVotesTotal));

            FinalizeResultsAndTies();
            FinalizeSummaries();

            var readyForReports = ResultSummaryFinal.UseOnReports.AsBoolean();

            if (UserSession.CurrentElectionStatus == ElectionTallyStatusEnum.Finalized && !readyForReports)
            {
                new ElectionModel().SetTallyStatus(ElectionTallyStatusEnum.Tallying);
            }

            _hub.StatusUpdate("Saving");

            Db.SaveChanges();

            new ResultSummaryCacher(Db).DropThisCache();
            new ResultTieCacher(Db).DropThisCache();
        }