From da595da89e91bbf1392be6748fe288d3235ebbd0 Mon Sep 17 00:00:00 2001 From: Patrick Donelan Date: Wed, 3 Jun 2009 02:11:31 +0000 Subject: [PATCH] Remove bloat from Survey response record to reduce json serialisation time --- .../Asset/Wobject/Survey/ResponseJSON.pm | 28 +++++++++++++------ t/Asset/Wobject/Survey/ResponseJSON.t | 11 ++------ 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm b/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm index 93e7c9653..8cfffc6dd 100644 --- a/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm +++ b/lib/WebGUI/Asset/Wobject/Survey/ResponseJSON.pm @@ -217,11 +217,18 @@ sub session { Serializes the internal perl hash representing the Response to a JSON string +To reduce json serialization time and db bloat, we only serialize the bare essentials + =cut sub freeze { my $self = shift; - return to_json($self->response); + + # These are the only properties of the response hash that we serialize: + my @props = qw(responses lastResponse questionsAnswered startTime tags); + my %serialize; + @serialize{@props} = @{$self->response}{@props}; + return to_json(\%serialize); } #------------------------------------------------------------------- @@ -560,7 +567,10 @@ sub recordResponses { my $aValid = 0; my $qId = $question->{id}; - $newResponse{ $qId }->{comment} = $responses->{ "${qId}comment" }; + my $comment = $responses->{ "${qId}comment" }; + if (defined $comment && length $comment) { + $newResponse{ $qId }->{comment} = $comment; + } for my $answer ( @{ $question->{answers} } ) { my $aId = $answer->{id}; @@ -577,13 +587,13 @@ sub recordResponses { next; } } - elsif ( $questionType eq 'Date' ) { - # Must be a valid date (until we get date i18n this is limited to YYYY/MM/DD) - if ($recordedAnswer !~ m|^\d{4}/\d{1,2}/\d{1,2}$|) { - $self->session->log->debug("Invalid $questionType: $recordedAnswer"); - next; - } - } +# elsif ( $questionType eq 'Date' ) { +# # Accept any date input until we get per-question validation options +# if ($recordedAnswer !~ m|^\d{4}/\d{1,2}/\d{1,2}$|) { +# $self->session->log->debug("Invalid $questionType: $recordedAnswer"); +# next; +# } +# } elsif ( $questionType eq 'Number' || $questionType eq 'Slider' ) { if ( $answer->{max} =~ /\d/ and $recordedAnswer > $answer->{max} ) { $self->session->log->debug("Invalid $questionType: $recordedAnswer"); diff --git a/t/Asset/Wobject/Survey/ResponseJSON.t b/t/Asset/Wobject/Survey/ResponseJSON.t index 01c834300..5a16f0d32 100644 --- a/t/Asset/Wobject/Survey/ResponseJSON.t +++ b/t/Asset/Wobject/Survey/ResponseJSON.t @@ -22,7 +22,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 114; +my $tests = 113; plan tests => $tests + 1; #---------------------------------------------------------------------------- @@ -564,9 +564,6 @@ cmp_deeply( value => 1, # 'recordedAnswer' value used because question is multi-choice verbatim => 'Section 1, question 0, answer 0 verbatim', }, - '1-1' => { - comment => undef, - } }, 'recordResponses: verbatim answer recorded responses correctly' ); @@ -594,9 +591,6 @@ cmp_deeply( 'time' => num(time(), 3), value => 'First answer', # submitted answer value used this time because non-mc }, - '1-1' => { - comment => undef, - } }, 'recordResponses: recorded responses correctly, two questions, one answer, comments, values and time' ); @@ -783,7 +777,8 @@ cmp_deeply( }, 'Valid value recorded correctly' ); -is($rJSON->responses->{'1-1-0'}, undef, 'Invalid date ignored'); +# All date input accepted until validation options supported +#is($rJSON->responses->{'1-1-0'}, undef, 'Invalid date ignored'); ######## # Number