aboutsummaryrefslogtreecommitdiff
blob: 752c78d6bb18e92b9a89d9528c4a1dbcda196969 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/perl -w
# $Id$
#
# Copyright 2015 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# Author: Robin H Johnson <robbat2@gentoo.org>
#
# statify: Produce early election statistics
#

BEGIN {
    my $dirname;
    if(-f '/etc/elections/Votify.pm') {
        $dirname = '/etc/elections';
    } else {
        use Cwd qw(abs_path);
        use File::Basename qw(dirname);
        $dirname = dirname(abs_path(__FILE__));
    }
    push @INC, $dirname;
}

use POSIX;
use Getopt::Long;
use List::Util;
use Cwd qw(abs_path);
use File::Spec::Functions;
use Votify 'official';
use strict;

######################################################################
# Global vars
######################################################################

(my $zero = $0) =~ s,.*/,,;
(my $version = $Votify::version) =~ s/.*?(\d.*\d).*/$zero version $1\n/;
my %opt;

# Collect the open elections
my (%open_elections, $usage_elections);
%open_elections = Votify::get_open_elections_hash();
if (scalar keys %open_elections) {
    $usage_elections = join("\n        ", keys %open_elections);
} else {
    $usage_elections = "(no elections currently open)";
}

# rest of usage
my $usage = <<EOT;

usage: 
    $zero [OPTS] <election>
    $zero [OPTS] --all

where options are:

    --all       Run on all open elections	
	--write-to-officials	Write statistics to officials homedirs

and <election> is one of the elections currently in-progress.  The following
elections are currently open:

    $usage_elections

EOT

######################################################################
# Main
######################################################################
for my $election_name (keys %open_elections) {
	my (@officials, @voters);
	open(F, '<', $open_elections{$election_name}{'officialsfile'})
        or die("failed to open officials file");
    chomp(@officials = <F>);
	close(F);

	$open_elections{$election_name}{'officials'} = @officials;

	open(F, '<', $open_elections{$election_name}{'votersfile'})
        or die("failed to open voters file");
    chomp(@voters = <F>);
	close(F);

	$open_elections{$election_name}{'voters'} = @voters;

	my ($count_voters, $count_submit, $count_pending) = (0, 0,0);
	for my $votername (@voters) {
		$count_voters++;
		if(-f catfile('/home', $votername, ".ballot-${election_name}-submitted")) {
			$count_submit++;
		} elsif (-f catfile('/home', $votername, ".ballot-${election_name}")) {
			$count_pending++;
		}
	}
	die "Mismatched voter count" unless $count_voters == scalar(@voters);
	$open_elections{$election_name}{'count'} = {
		voters => $count_voters,
		submitted => $count_submit,
		pending => $count_pending,
	};
	my $results_buffer =  ''
	.sprintf("Election: %s\n", $election_name)
	.sprintf("Timestamp: %s\n", POSIX::strftime($Votify::datefmt, gmtime))
	.sprintf("Eligable voters: %d\n", $count_voters)
	.sprintf("Submitted votes: %d\n", $count_submit)
	.sprintf("Pending votes: %d\n", $count_pending)
	.sprintf("Turnout: %02.3f%%\n", $count_submit*100.0/$count_voters) 
	.sprintf("Pending Turnout: %02.3f%%\n", ($count_submit+$count_pending)*100.0/$count_voters);
	print "$results_buffer\n";

	umask 057;
	for my $officialname (@officials) {
		my $fn = catfile('/home', $officialname, 'voter-turnout-'.$election_name);
		open(F, '>', $fn) or
			die("failed to open voter turnout file ($fn): $!");
		print F $results_buffer;
		close F;
		chown ((getpwnam($officialname))[2]), 0, ($fn) or
			die("failed to chown voter turnout file ($fn): $!");
	}
}


__END__
# vim:sw=4 et