Line # Revision Author
1 296 ahitrov #!/usr/bin/perl
2
3 use strict;
4 use warnings "all";
5 use locale;
6
7 use FindBin;
8 BEGIN {
9 require "$FindBin::RealBin/../lib/Modules.pm";
10 }
11
12 use Contenido::Globals;
13 use Contenido::Init;
14 use ErrorTee;
15 use PidFile;
16 use JSON::XS;
17 use Data::Dumper;
18
19 # begin
20 Contenido::Init->init();
21
22 #PidFile->new($state); # db-based locking (run only on one host)
23 #PidFile->new($state, per_host=>1); # db-based locking (run on whole cluster)
24 #PidFile->new($state->{run_dir}); # file-based locking (run on whole cluster)
25
26 my $keeper_module = $state->project.'::Keeper';
27 $keeper = $keeper_module->new($state);
28
29 ############################################
30 # please use:
31 # $state->{log_dir} for logging
32 # $state->{tmp_dir} for temporary files
33 ###########################################
34 my @votings = $keeper->get_documents (
35 class => 'promosuite::Voting',
36 status => [1,2],
37 );
38 my $SIZE = 400;
39 foreach my $voting ( @votings ) {
40 my @voices; my $p = 0;
41 my $voting_data = $voting->get_image('voting');
42 while ( my @vs = $keeper->get_documents (
43 class => 'promosuite::Voice', object_id => $voting->id,
44 order_by => 'id', limit => $SIZE, offset => $SIZE * $p++ ) ) {
45 push @voices, @vs;
46 }
47
48 if ( @voices ) {
49 my %voting_data;
50 foreach my $voice ( @voices ) {
51 my $json = JSON::XS->new;
52 my $voice_data = $json->decode($voice->voting);
53 next unless ref $voice_data eq 'HASH';
54 while ( my ($qnum, $answer) = each %$voice_data ) {
55 next unless ref $answer eq 'HASH';
56 while ( my ($anum, $value) = each %$answer ) {
57 $voting_data{$qnum}{$anum}{total} = 0 unless defined $voting_data{$qnum}{$anum}{total};
58 $voting_data{$qnum}{$anum}{total} += $value;
59 $voting_data{$qnum}{$anum}{count} = 0 unless defined $voting_data{$qnum}{$anum}{count};
60 $voting_data{$qnum}{$anum}{count}++;
61 }
62 }
63
64 }
65 for ( 1..$voting_data->{'question_amount'} ) {
66 my $qnum = $_;
67 if ( $voting_data->{questions}->[$qnum-1]->{amount} ) {
68 for ( 1..$voting_data->{questions}->[$qnum-1]->{amount} ) {
69 my $anum = $_ - 1;
70 if ( exists $voting_data{$qnum} && exists $voting_data{$qnum}{$anum} ) {
71 if ( $voting_data->{questions}->[$qnum-1]->{range} ) {
72 $voting_data->{questions}->[$qnum-1]->{choices}->[$anum]->{votes} = $voting_data{$qnum}{$anum}{total}/$voting_data{$qnum}{$anum}{count};
73 } else {
74 $voting_data->{questions}->[$qnum-1]->{choices}->[$anum]->{votes} = $voting_data{$qnum}{$anum}{total};
75 }
76 }
77 }
78 }
79 }
80 $voting_data->{total} = scalar @voices;
81 {
82 local $Data::Dumper::Indent = 0;
83 $voting->voting( Dumper($voting_data) );
84 }
85 }
86 $voting->status(3) if $voting->status == 2;
87 $voting->store;
88
89 }