Permalink
Browse files

store vote totals and result for divisions

Because we are going to be displaying summary information about votes
next to them calculate the summary details (votes for/against etc) as we
import the vote data rather than have to do it on the fly. At also
avoids having to right some fairly awful SQL in order to calculate
things on the fly.

As vote data only comes in via one script there should be no
problem with consistency.
  • Loading branch information...
struan committed Mar 1, 2017
1 parent 539eb86 commit 39eaddda5799a25b28213b2b322e255c96e16182
Showing with 42 additions and 5 deletions.
  1. +6 −0 db/0012-add-totals-to-policy-divisions.sql
  2. +5 −0 db/schema.sql
  3. +31 −5 scripts/json2db.pl
@@ -0,0 +1,6 @@
ALTER TABLE policydivisions
ADD COLUMN `yes_total` int(3) default 0,
ADD COLUMN `no_total` int(3) default 0,
ADD COLUMN `absent_total` int(3) default 0,
ADD COLUMN `both_total` int(3) default 0,
ADD COLUMN `majority_vote` enum('aye', 'no', '') default '';
View
@@ -144,6 +144,11 @@ CREATE TABLE `policydivisions` (
`division_date` date NOT NULL default '1000-01-01',
`division_number` int(11),
`policy_vote` enum('aye', 'aye3', 'no', 'no3', 'both', 'absent', '') default '',
`yes_total` int(3) default 0,
`no_total` int(3) default 0,
`absent_total` int(3) default 0,
`both_total` int(3) default 0,
`majority_vote` enum('aye', 'no', '') default '',
`lastupdate` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
UNIQUE KEY `policy_division` (`division_id`, `policy_id`),
KEY `division_id` (`division_id`)
View
@@ -35,10 +35,10 @@
my $policycheck = $dbh->prepare("SELECT policy_id from policies where policy_id = ?");
my $policyadd = $dbh->prepare("INSERT INTO policies (policy_id, title, description) VALUES (?, ?, ?)");
my $motioncheck = $dbh->prepare("SELECT division_title, gid, direction, policy_vote, yes_text, no_text FROM policydivisions WHERE division_id = ? AND policy_id = ?");
my $motioncheck = $dbh->prepare("SELECT division_title, gid, direction, policy_vote, yes_text, no_text, yes_total, no_total, absent_total, both_total, majority_vote FROM policydivisions WHERE division_id = ? AND policy_id = ?");
my $motionadd = $dbh->prepare("INSERT INTO policydivisions (division_id, policy_id, house, direction, policy_vote, division_title, yes_text, no_text, division_date, division_number, gid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
my $motionupdate = $dbh->prepare("UPDATE policydivisions SET gid = ?, division_title = ?, yes_text = ?, no_text = ?, direction = ?, policy_vote = ? WHERE division_id = ? AND policy_id = ?");
my $motionadd = $dbh->prepare("INSERT INTO policydivisions (division_id, policy_id, house, direction, policy_vote, division_title, yes_text, no_text, division_date, division_number, gid, yes_total, no_total, absent_total, both_total, majority_vote) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
my $motionupdate = $dbh->prepare("UPDATE policydivisions SET gid = ?, division_title = ?, yes_text = ?, no_text = ?, direction = ?, policy_vote = ?, yes_total = ?, no_total = ?, absent_total = ?, both_total = ?, majority_vote = ? WHERE division_id = ? AND policy_id = ?");
my $votecheck = $dbh->prepare("SELECT person_id, vote FROM persondivisionvotes WHERE division_id = ?");
my $voteadd = $dbh->prepare("INSERT INTO persondivisionvotes (person_id, division_id, vote) VALUES (?, ?, ?)");
@@ -110,8 +110,29 @@
$no_text = $motion->{motion}->{actions}->{no};
}
my $totals = {
yes => 0,
no => 0,
absent => 0,
both => 0,
};
my $majority_vote = '';
if ( $motion->{motion}->{vote_events}->[0]->{counts} ) {
for my $count ( @{ $motion->{motion}->{vote_events}->[0]->{counts} } ) {
$totals->{$count->{option}} = $count->{value};
}
if ($totals->{yes} > $totals->{no}) {
$majority_vote = 'aye';
} else {
$majority_vote = 'no';
}
}
if ( !defined $curr_motion ) {
my $r = $motionadd->execute($motion_id, $dreamid, $house, $motion->{direction}, $motion->{motion}->{policy_vote}, $motion->{motion}->{text}, $yes_text, $no_text, $motion->{motion}->{date}, $motion_num, $gid);
my $r = $motionadd->execute($motion_id, $dreamid, $house, $motion->{direction}, $motion->{motion}->{policy_vote}, $motion->{motion}->{text}, $yes_text, $no_text, $motion->{motion}->{date}, $motion_num, $gid, $totals->{yes}, $totals->{no}, $totals->{absent}, $totals->{both}, $majority_vote);
unless ( $r > 0 ) {
warn "problem creating policymotion for $dreamid, skipping motions\n";
next;
@@ -120,10 +141,15 @@
$curr_motion->{gid} ne $gid ||
$curr_motion->{yes_text} ne $yes_text ||
$curr_motion->{no_text} ne $no_text ||
$curr_motion->{yes_total} ne $totals->{yes} ||
$curr_motion->{no_total} ne $totals->{no} ||
$curr_motion->{absent_total} ne $totals->{absent} ||
$curr_motion->{both_total} ne $totals->{both} ||
$curr_motion->{majority_vote} ne $majority_vote ||
$motion->{direction} ne $curr_motion->{direction} ||
$motion->{motion}->{policy_vote} ne $curr_motion->{policy_vote}
) {
my $r = $motionupdate->execute($gid, $text, $yes_text, $no_text, $motion->{direction}, $motion->{motion}->{policy_vote}, $motion_id, $dreamid);
my $r = $motionupdate->execute($gid, $text, $yes_text, $no_text, $motion->{direction}, $motion->{motion}->{policy_vote}, , $totals->{yes}, $totals->{no}, $totals->{absent}, $totals->{both}, $majority_vote, $motion_id, $dreamid);
unless ( $r > 0 ) {
warn "problem updating division $motion_id from " . $curr_motion->{division_title} . " to $text AND " . $curr_motion->{gid} . " to $gid\n";
}

0 comments on commit 39eaddd

Please sign in to comment.