Convert Passive Analytics to use the new ProgressBar with Download code.

This commit is contained in:
Colin Kuskie 2011-10-15 13:54:17 -07:00
parent 63c31404c2
commit 9ef8f098cf
2 changed files with 94 additions and 28 deletions

View file

@ -13,7 +13,7 @@ use WebGUI::Workflow;
use WebGUI::Workflow::Instance;
use WebGUI::User;
use WebGUI::Text;
use WebGUI::ProgressBar;
use WebGUI::Fork;
=head1 NAME
@ -60,7 +60,7 @@ sub canView {
#----------------------------------------------------------------------------
=head2 exportSomething ( session, sth, filename )
=head2 exportSomething ( $process, $data )
Generates CSV data from the supplied statement handle and generates
a temporary WebGUI::Storage object containing that data in the requested
@ -69,42 +69,56 @@ filename.
This subroutine also does a setRedirect to the URL of the file in
the storage object.
=head3 session
=head3 $process
Session variable, to set the http redirect correctly.
A WebGUI::Fork object, to let the user know what's going on.
=head3 sth
=head3 $data
Statement handle for reading data and getting column names
Hash ref of data.
=head3 tableName
The name of the table where data will be pulled and translated into CSV.
=head3 filename
The name of the file to create inside the storage object.
The name of the file to generate
=cut
sub exportSomething {
my ($session, $sth, $filename) = @_;
my $pb = WebGUI::ProgressBar->new($session);
my ($process, $data) = @_;
my $session = $process->session;
my $i18n = WebGUI::International->new($session, 'Asset_Thingy');
$pb->start($i18n->get('export label'), $session->url->extras('adminConsole/passiveAnalytics.png'));
$pb->update($i18n->get('Creating column headers'));
my $storage = WebGUI::Storage->createTemp($session);
open my $CSV, '>', $storage->getPath($data->{filename});
my $sth = $session->db->read('select SQL_CALC_FOUND_ROWS * from '.$data->{tableName});
my %status = (
current => 0,
message => '',
total => $session->db->quickScalar('select found_rows()') + 0,
);
my $update = sub {
$process->update( sub { JSON::to_json(\%status) } );
};
$update->();
my @columns = $sth->getColumnNames;
my $csvData = WebGUI::Text::joinCSV( @columns ). "\n";
$pb->update($i18n->get('Writing data'));
my $rowCounter=0;
print $CSV WebGUI::Text::joinCSV( @columns ). "\n";
my $rowCounter = 0;
$status{message} = $i18n->get('Writing data');
$update->();
while (my $row = $sth->hashRef()) {
my @row = @{ $row }{@columns};
$csvData .= WebGUI::Text::joinCSV(@row) . "\n";
if (! ++$rowCounter % 25) {
$pb->update($i18n->get('Writing data'));
}
print $CSV WebGUI::Text::joinCSV(@row) . "\n";
++$status{current };
$update->();
}
$storage->addFileFromScalar($filename, $csvData);
$session->http->setRedirect($storage->getUrl($filename));
$pb->update(sprintf q|<a href="%s">%s</a>|, $session->url->page('op=passiveAnalytics;func=editRuleflow'), sprintf($i18n->get('Return to %s'), $i18n->get('Passive Analytics','PassiveAnalytics')));
return $pb->finish($storage->getUrl($filename));
close $CSV;
$sth->finish;
$process->setRedirect($storage->getUrl($data->{filename}));
$status{message} = sprintf '<a href="%s">%s</a>', $session->url->page('op=passiveAnalytics;func=editRuleflow'), 'Return to Passive Analytics';
$update->();
}
#-------------------------------------------------------------------
@ -348,8 +362,24 @@ Dump the contents of the bucket log.
sub www_exportBucketData {
my ($session) = @_;
my $bucket = $session->db->read('select * from bucketLog order by userId, Bucket, timeStamp');
exportSomething($session, $bucket, 'bucketData.csv');
my $process = WebGUI::Fork->start(
$session,
__PACKAGE__, 'exportSomething',
{ tableName => 'bucketLog', filename => 'bucketData.csv', },
);
my $i18n = WebGUI::International->new($session, 'PassiveAnalytics');
$session->http->setRedirect(
$session->url->page(
$process->contentPairs(
'ProgressBar', {
icon => 'passiveAnalytics',
title => $i18n->get('Export bucket data'),
proceed => $session->url->page('op=passiveAnalytics;func=editRuleflow'),
},
),
),
);
return "redirect";
}
@ -363,8 +393,23 @@ Dump the contents of the delta log.
sub www_exportDeltaData {
my ($session) = @_;
my $delta = $session->db->read('select * from deltaLog order by userId, timeStamp');
exportSomething($session, $delta, 'deltaData.csv');
my $process = WebGUI::Fork->start(
$session,
__PACKAGE__, 'exportSomething',
{ tableName => 'deltaLog', filename => 'deltaData.csv', },
);
my $i18n = WebGUI::International->new($session, 'PassiveAnalytics');
$session->http->setRedirect(
$session->url->page(
$process->contentPairs(
'ProgressBar', {
icon => 'passiveAnalytics',
title => $i18n->get('Export delta data'),
proceed => $session->url->page('op=passiveAnalytics;func=editRuleflow'),
},
),
),
);
return "redirect";
}
@ -378,8 +423,23 @@ Dump the contents of the raw log.
sub www_exportLogs {
my ($session) = @_;
my $raw = $session->db->read('select * from passiveLog order by userId, timeStamp');
exportSomething($session, $raw, 'passiveData.csv');
my $process = WebGUI::Fork->start(
$session,
__PACKAGE__, 'exportSomething',
{ tableName => 'passiveLog', filename => 'passiveData.csv', },
);
my $i18n = WebGUI::International->new($session, 'PassiveAnalytics');
$session->http->setRedirect(
$session->url->page(
$process->contentPairs(
'ProgressBar', {
icon => 'passiveAnalytics',
title => $i18n->get('Export raw logs'),
proceed => $session->url->page('op=passiveAnalytics;func=editRuleflow'),
},
),
),
);
return "redirect";
}

View file

@ -201,6 +201,12 @@ home\?func=match<br />
context => q|Error message|,
},
'error creating workflow' => {
message => q|Error creating the workflow instance.|,
lastUpdated => 0,
context => q|Error message|,
},
};
1;