public static void Run() { MeetingElections polls = MeetingElections.GetAll(); foreach (MeetingElection poll in polls) { Console.Write("Looking at poll #" + poll.Identity + "..."); if (DateTime.Now > poll.VotingCloses) { Console.Write(" after closedatetime " + poll.VotingCloses.ToString("yyyy-MM-dd HH:mm") + "..."); // Poll should be CLOSED, definitely CLOSED. if (poll.VotingOpen) { Console.WriteLine(" CLOSING."); poll.VotingOpen = false; // Closes poll poll.Creator.SendNotice("Poll Closed: " + poll.Name, string.Empty, poll.OrganizationId); Person.FromIdentity(1) .SendNotice("Poll Closed: " + poll.Name, string.Empty, poll.OrganizationId); ReportPollResults(poll, false); } else { Console.WriteLine(" no action."); } } else if (DateTime.Now > poll.VotingOpens) { try { // Poll should be OPEN. Console.Write("after opendatetime " + poll.VotingOpens.ToString("yyyy-MM-dd HH:mm") + "..."); if (!poll.VotingOpen) { Console.WriteLine("OPENING for " + poll.Organization.Name + ":"); // Populate voters, then open for voting Organizations orgTree = poll.Organization.ThisAndBelow(); Participations participations = Participations.ForOrganizations(orgTree); Dictionary <int, bool> dupeCheck = new Dictionary <int, bool>(); int count = 0; foreach (Participation membership in participations) { Console.Write("\r - voters: {0:D6}/{1:D6} (person #{2:D6})...", ++count, participations.Count, membership.PersonId); if (!membership.Active) { continue; // paranoid programming } if (!membership.Organization.IsOrInherits(poll.Organization)) { continue; // more paranoid programming } if (!dupeCheck.ContainsKey(membership.PersonId)) { if (membership.Person.GeographyId == 0) { // It's a trap! Console.Write(" GEOGRAPHY ZERO, ignoring\r\n"); } else { Geography geo = membership.Person.Geography; if (geo.Inherits(Geography.FromIdentity(poll.GeographyId)) || geo.Identity == poll.GeographyId) { poll.AddVoter(membership.Person); dupeCheck[membership.PersonId] = true; } } } } Console.WriteLine(" done."); Console.Write(" - opening poll..."); poll.VotingOpen = true; Console.WriteLine(" done."); Console.Write("Sending notices... "); string body = "Electoral roll for poll \"" + poll.Name + "\" primed. " + dupeCheck.Count + " people can vote. The vote is now OPEN."; string subject = "Poll Open: " + poll.Name; poll.Creator.SendNotice(subject, body, poll.OrganizationId); Person.FromIdentity(1).SendNotice(subject, body, poll.OrganizationId); Console.WriteLine(" done."); } else { Console.WriteLine(" already open, no action."); // Report intermediate results to owner ReportPollResults(poll, true); } } catch (Exception exception) { Console.WriteLine(exception.ToString()); throw exception; } } } }
public static GeographyStatistics GeneratePresent(int[] memberCountForOrganizations) { Dictionary <int, int> voterCounts = SwarmDb.GetDatabaseForReading().GetGeographyVoterCounts(); GeographyStatistics result = new GeographyStatistics(); // Step 1 - tally the leaf nodes foreach (int geographyId in voterCounts.Keys) { GeographyDataPoint dataPoint = new GeographyDataPoint { GeographyName = Geography.FromIdentity(geographyId).Name, GeographyId = geographyId, VoterCount = voterCounts[geographyId] }; result[geographyId] = dataPoint; } // Step 2 - add the member counts to the individual requested geo nodes foreach (int orgId in memberCountForOrganizations) { People members = People.FromMemberships( Participations.ForOrganizations(Organization.FromIdentity(orgId).ThisAndBelow())); foreach (Person person in members) { Geography geography = person.Geography; // If we don't have this key, it's because it's too far down. Move up the tree until we're at least // at municipal level. while (geography.Identity != 1 && !result.ContainsKey(geography.Identity)) { geography = geography.Parent; } // Add the data, unless we hit the roof in the last op. if (geography.Identity != 1) { int birthYearBracket = (person.Birthdate.Year - 1900) / 5; if (birthYearBracket >= 0 && birthYearBracket < 30) { result[geography.Identity].OrganizationData[orgId - 1].BirthYearBracketMemberCounts[ birthYearBracket]++; } if (person.IsFemale) { result[geography.Identity].OrganizationData[orgId - 1].FemaleMemberCount++; } else if (person.IsMale) { result[geography.Identity].OrganizationData[orgId - 1].MaleMemberCount++; } } } } // TODO: Activist count as a new step // Step 3 - add up the totals for every intermediate node (expensive!) Geographies allGeographies = Geography.Root.ThisAndBelow(); foreach (Geography geography in allGeographies) { Geographies localTree = geography.ThisAndBelow(); int voterCount = 0; GeographyOrganizationDataPoint[] tempOrgData = new GeographyOrganizationDataPoint[2]; // HACK tempOrgData[0] = new GeographyOrganizationDataPoint(); tempOrgData[1] = new GeographyOrganizationDataPoint(); foreach (Geography localNode in localTree) { // Increment our temp values for every geo node below the one we're currently processing. if (!result.ContainsKey(localNode.Identity)) { continue; } voterCount += result[localNode.Identity].VoterCount; for (int orgIndex = 0; orgIndex < 2; orgIndex++) { for (int ageBracketIndex = 0; ageBracketIndex < 30; ageBracketIndex++) { tempOrgData[orgIndex].BirthYearBracketMemberCounts[ageBracketIndex] += result[localNode.Identity].OrganizationData[orgIndex].BirthYearBracketMemberCounts[ ageBracketIndex]; } tempOrgData[orgIndex].ActivistCount += result[localNode.Identity].OrganizationData[orgIndex].ActivistCount; tempOrgData[orgIndex].FemaleMemberCount += result[localNode.Identity].OrganizationData[orgIndex].FemaleMemberCount; tempOrgData[orgIndex].MaleMemberCount += result[localNode.Identity].OrganizationData[orgIndex].MaleMemberCount; } } if (!result.ContainsKey(geography.Identity)) { result[geography.Identity] = new GeographyDataPoint { GeographyId = geography.Identity, GeographyName = geography.Name }; } // Save our temp values to the processed node. result[geography.Identity].VoterCount = voterCount; for (int orgIndex = 0; orgIndex < 2; orgIndex++) { for (int ageBracketIndex = 0; ageBracketIndex < 30; ageBracketIndex++) { result[geography.Identity].OrganizationData[orgIndex].BirthYearBracketMemberCounts[ ageBracketIndex] = tempOrgData[orgIndex].BirthYearBracketMemberCounts[ageBracketIndex]; } result[geography.Identity].OrganizationData[orgIndex].ActivistCount = tempOrgData[orgIndex].ActivistCount; result[geography.Identity].OrganizationData[orgIndex].FemaleMemberCount = tempOrgData[orgIndex].FemaleMemberCount; result[geography.Identity].OrganizationData[orgIndex].MaleMemberCount = tempOrgData[orgIndex].MaleMemberCount; } } // Step 4 - collect return(result); }