static void GenerateSummaryDa1(String outputFile) { String logPayload = null; String stringFormatNation = "{{\n \"PrabowoHattaVotes\":\"{0}\",\n \"PrabowoHattaPercentage\":\"{1:N4}\",\n \"JokowiKallaVotes\":\"{2}\",\n \"JokowiKallaPercentage\":\"{3:N4}\",\n \"Total\":\"{4}\"\n}}"; String stringFormatProvince = "{{\n \"Province\":\"{0}\",\n \"PrabowoHattaVotes\":\"{1}\",\n \"PrabowoHattaPercentage\":\"{2:N4}\",\n \"JokowiKallaVotes\":\"{3}\",\n \"JokowiKallaPercentage\":\"{4:N4}\",\n \"Total\":\"{5}\"\n}}"; String stringFormatKabupaten = "{{\n \"ProvinceCode\":\"{0}\",\n \"ProvinceName\":\"{1}\",\n \"KabupatenCode\":\"{2}\",\n \"KabupatenName\":\"{3}\",\n \"KecamatanCode\":\"{4}\",\n \"KecamatanName\":\"{5}\",\n \"PrabowoHattaVotes\":\"{6}\",\n \"PrabowoHattaPercentage\":\"{7:N4}\",\n \"JokowiKallaVotes\":\"{8}\",\n \"JokowiKallaPercentage\":\"{9:N4}\",\n \"Total\":\"{10}\"\n}}"; bool[] firstItems = new bool[2]; for (int i = 0; i < 2; ++i) { firstItems[i] = true; } String totalOutputFile = Path.Combine(Path.GetDirectoryName(outputFile), Path.GetFileNameWithoutExtension(outputFile) + "-total.json"); String totalProvinceOutputFile = Path.Combine(Path.GetDirectoryName(outputFile), Path.GetFileNameWithoutExtension(outputFile) + "-province.json"); String totalKabupatenOutputFile = Path.Combine(Path.GetDirectoryName(outputFile), Path.GetFileNameWithoutExtension(outputFile) + "-kabupaten.json"); using (StreamWriter swCSVTotal = new StreamWriter(Path.Combine(Path.GetDirectoryName(outputFile), Path.GetFileNameWithoutExtension(outputFile) + ".csv"))) { swCSVTotal.WriteLine("#HEADER:ProvinceCode,ProvinceName,KabupatenCode,KabupatenName,KecamatanCode,KecamatanName,PrabowoHattaVotes,JokowiKallaVotes,TotalVotes"); using (StreamWriter swtotal = new StreamWriter(totalOutputFile)) { BinaryTally nationTally = new BinaryTally(); swtotal.WriteLine("["); using (StreamWriter swProvince = new StreamWriter(totalProvinceOutputFile)) { swProvince.WriteLine("["); ThreeLevelDictionary.Enumerator provinceEnumeerator = s_tallyTableDa1.VotingTable.GetEnumerator(); while (provinceEnumeerator.MoveNext()) { BinaryTally provinceTally = new BinaryTally(); String provinceName = ""; using (StreamWriter swKabupaten = new StreamWriter(totalKabupatenOutputFile)) { swKabupaten.WriteLine("["); TwoLevelDictionary.Enumerator kabupatenEnumerator = provinceEnumeerator.Current.Value.GetEnumerator(); while (kabupatenEnumerator.MoveNext()) { BinaryTally kabupatenTally = new BinaryTally(); Dictionary<String, BinaryTally>.Enumerator kecamatanEnumerator = kabupatenEnumerator.Current.Value.GetEnumerator(); while (kecamatanEnumerator.MoveNext()) { provinceName = kecamatanEnumerator.Current.Value.ProvinceName; // Dump complete raw data to CSV swCSVTotal.WriteLine("{0},{1},{2},{3},{4},{5},{6},{7},{8}", kecamatanEnumerator.Current.Value.ProvinceCode, kecamatanEnumerator.Current.Value.ProvinceName, kecamatanEnumerator.Current.Value.KabupatenCode, kecamatanEnumerator.Current.Value.KabupatenName, kecamatanEnumerator.Current.Value.KecamatanCode, kecamatanEnumerator.Current.Value.KecamatanName, kecamatanEnumerator.Current.Value.Counter1, kecamatanEnumerator.Current.Value.Counter2, kecamatanEnumerator.Current.Value.Total); logPayload = String.Format(stringFormatKabupaten, kecamatanEnumerator.Current.Value.ProvinceCode, kecamatanEnumerator.Current.Value.ProvinceName, kecamatanEnumerator.Current.Value.KabupatenCode, kecamatanEnumerator.Current.Value.KabupatenName, kecamatanEnumerator.Current.Value.KecamatanCode, kecamatanEnumerator.Current.Value.KecamatanName, kecamatanEnumerator.Current.Value.Counter1, kecamatanEnumerator.Current.Value.Total == 0 ? 0 : ((float)kecamatanEnumerator.Current.Value.Counter1 / kecamatanEnumerator.Current.Value.Total) * 100, kecamatanEnumerator.Current.Value.Counter2, kecamatanEnumerator.Current.Value.Total == 0 ? 0 : ((float)kecamatanEnumerator.Current.Value.Counter2 / kecamatanEnumerator.Current.Value.Total) * 100, kecamatanEnumerator.Current.Value.Total); if (firstItems[1] == true) { firstItems[1] = false; } else { Console.WriteLine(","); swKabupaten.WriteLine(","); } Console.Write(logPayload); swKabupaten.Write(logPayload); if (!kecamatanEnumerator.Current.Value.CheckVoteIntegrity()) { // Moves on but logs these data for reporting String log = String.Format("Bad data integrity for Kecamatan {0}:{1}, vote1 {2} plus vote2 {3} does not equate to total {4}", kecamatanEnumerator.Current.Value.KecamatanCode, kecamatanEnumerator.Current.Value.KecamatanName, kecamatanEnumerator.Current.Value.Counter1, kecamatanEnumerator.Current.Value.Counter2, kecamatanEnumerator.Current.Value.Total); Console.WriteLine(log); s_badDataList.Add(log); } // Sum each kabupaten to this province kabupatenTally.Counter1 += kecamatanEnumerator.Current.Value.Counter1; kabupatenTally.Counter2 += kecamatanEnumerator.Current.Value.Counter2; kabupatenTally.Total += kecamatanEnumerator.Current.Value.Total; } if (!kabupatenTally.CheckVoteIntegrity()) { // Moves on but logs these data for reporting String log = String.Format("Bad data integrity for Kabupaten {0}:{1}, vote1 {2} plus vote2 {3} does not equate to total {4}", kecamatanEnumerator.Current.Value.KabupatenCode, kecamatanEnumerator.Current.Value.KabupatenName, kabupatenTally.Counter1, kabupatenTally.Counter2, kabupatenTally.Total); Console.WriteLine(log); s_badDataList.Add(log); } provinceTally.Counter1 += kabupatenTally.Counter1; provinceTally.Counter2 += kabupatenTally.Counter2; provinceTally.Total += kabupatenTally.Total; } swKabupaten.WriteLine("]"); } logPayload = String.Format(stringFormatProvince, provinceName, provinceTally.Counter1, provinceTally.Total == 0 ? 0 : ((float)provinceTally.Counter1 / provinceTally.Total) * 100, provinceTally.Counter2, provinceTally.Total == 0 ? 0 : ((float)provinceTally.Counter2 / provinceTally.Total) * 100, provinceTally.Total); if (firstItems[0] == true) { firstItems[0] = false; } else { Console.WriteLine(","); swProvince.WriteLine(","); } Console.Write(logPayload); swProvince.Write(logPayload); firstItems[0] = false; if (!provinceTally.CheckVoteIntegrity()) { // Moves on but log for reporting String log = String.Format("Data for Province {0} is invalid the sum of the vote1 {1} and vote2 {2} does not equate to {3}", provinceName, provinceTally.Counter1, provinceTally.Counter2, provinceTally.Total); Console.WriteLine(log); s_badDataList.Add(log); } // Sum each province to total nationTally.Counter1 += provinceTally.Counter1; nationTally.Counter2 += provinceTally.Counter2; nationTally.Total += provinceTally.Total; } swProvince.WriteLine("]"); } logPayload = String.Format(stringFormatNation, nationTally.Counter1, nationTally.Total == 0 ? 0 : ((float)nationTally.Counter1 / nationTally.Total) * 100, nationTally.Counter2, nationTally.Total == 0 ? 0 : ((float)nationTally.Counter2 / nationTally.Total) * 100, nationTally.Total); if (!nationTally.CheckVoteIntegrity()) { // Moves on but log for reporting String log = String.Format("Data for Nation level is invalid the sum of the vote1 {0} and vote2 {1} does not equate to {2}", nationTally.Counter1, nationTally.Counter2, nationTally.Total); Console.WriteLine(log); s_badDataList.Add(log); } Console.WriteLine(logPayload); swtotal.WriteLine(logPayload); swtotal.WriteLine("]"); } } // Do bad data reportings Console.WriteLine("========================== DA1 BAD DATA REPORTING ============================"); foreach (String badData in s_badDataList) { Console.WriteLine("> BAD DATA: {0}", badData); s_badDataList.Add(badData); } Console.WriteLine("======================== END OF BAD DATA REPORTING ============================"); }
public void Add(BinaryTally tally, String provinceCode) { lock (this.votingTable) { base.Counter1 += tally.Counter1; base.Counter2 += tally.Counter2; base.Total += tally.Counter1 + tally.Counter2; if (!this.votingTable.ContainsKey(provinceCode)) { this.votingTable.Add(provinceCode, tally); } else { throw new InvalidDataException(String.Format("Duplicate key {0} found", provinceCode)); } } }
static BinaryTally CountVotesDc1( String provinceCode, String provinceName) { String baseUrl = "http://pilpres2014.kpu.go.id/dc1.php?cmd=select&grandparent=0&parent={0}"; StringBuilder sb = new StringBuilder(); String url = String.Format(baseUrl, provinceCode); for (int i = 0; i < s_retryCount; ++i) { try { // NOTES: this can be better performance if we use the WebClient pool // but given that we do not need a super high throughput we can backtrack // to a conventional method of instantiating WebClient each time WebClient client = new WebClient(); sb.Append(client.DownloadString(url)); BinaryTally tally = new BinaryTally(); ParseVoteCount(sb.ToString(), tally); tally.ProvinceCode = provinceCode; tally.ProvinceName = provinceName; return tally; } catch (WebException ex) { Console.WriteLine("> Failed to fetch a page from {0}", url); Console.WriteLine(ex); Console.WriteLine("> Retry {0}", i); Thread.Sleep(10000); } } return null; }
public void Add(BinaryTally tally, String provinceCode, String kabupatenCode) { lock (this.votingTable) { base.Counter1 += tally.Counter1; base.Counter2 += tally.Counter2; base.Total += tally.Counter1 + tally.Counter2; Dictionary<String, BinaryTally> kabupatenDictionary; if (!this.votingTable.TryGetValue(provinceCode, out kabupatenDictionary)) { kabupatenDictionary = new Dictionary<String, BinaryTally>(); this.votingTable.Add(provinceCode, kabupatenDictionary); } kabupatenDictionary.Add(kabupatenCode, tally); } }