#!/usr/bin/perl #this script will pregenerate graphs for all the platforms in the given db instance for the given latest time_interval use strict; use DBI; use XML::LibXML; use LWP::UserAgent; #load database and path info my ($env,$time_interval,$unit_conversion) = @ARGV; my $time_interval_label = $time_interval; $time_interval_label =~ s/ /_/g; my $xp_env = XML::LibXML->new->parse_file("../environment_xenia_$env.xml"); my $xp_graph = XML::LibXML->new->parse_file("environment_xenia_graph.xml"); my $db_host = $xp_env->findvalue('//db/host'); my $db_name = $xp_env->findvalue('//db/name'); my $db_user = $xp_env->findvalue('//db/user'); my $db_passwd = $xp_env->findvalue('//db/passwd'); my $dir_tmp = $xp_env->findvalue('//path/dir_tmp'); my $dir_base = $xp_env->findvalue('//path/dir_base'); my $http_base = $xp_env->findvalue('//path/http_base'); my $http_xenia_graph = $xp_env->findvalue('//path/http_xenia_graph'); #establish database connection my ($dbh,$sth,$sql,$sth_2,$sth_3); if ($db_host eq '') { $dbh = DBI->connect ("dbi:Pg:dbname=$db_name", "$db_user", "$db_passwd"); } #remove host reference if local else { $dbh = DBI->connect ("dbi:Pg:dbname=$db_name;host=$db_host", "$db_user", "$db_passwd"); } if ( !defined $dbh ) {die "Cannot connect to database!\n";} #get platforms for this xenia instance $sql = qq{ select row_id,platform_handle from platform }; #print "sql:".$sql."\n"; $sth = $dbh->prepare( $sql ); $sth->execute(); if ($sth->rows == 0) { print "no data\n"; &exit_no_data; } ##################################################################### while ( my ($platform_id,$platform_handle) = $sth->fetchrow() ) { print "platform_id:$platform_id\n"; #get sensor_id's for platforms $sql = qq{ select row_id,m_type_id from sensor where platform_id = $platform_id }; #print "sql:".$sql."\n"; $sth_2 = $dbh->prepare( $sql ); $sth_2->execute(); ##################################################################### while ( my ($sensor_id,$m_type_id) = $sth_2->fetchrow() ) { #print " sensor_id:$sensor_id\n"; #get obs_type and uom for associate sensor_id.m_type_id $sql = qq{ select t1.standard_name,t2.standard_name from obs_type t1,uom_type t2 where t1.row_id = (select obs_type_id from m_type where row_id = $m_type_id) and t2.row_id = (select uom_type_id from m_type where row_id = $m_type_id) }; #print "sql:".$sql."\n"; $sth_3 = $dbh->prepare( $sql ); $sth_3->execute(); my ($obs_type,$uom_type) = $sth_3->fetchrow(); if ($unit_conversion eq 'en') { #en is for the default english unit conversion $uom_type = $xp_graph->findvalue('//observation_list/observation[@m_type_id="'.$m_type_id.'"]/standard_uom_en'); } print " $obs_type:$uom_type\n"; my $url = $http_xenia_graph."environment=$env&sensor_id=$sensor_id&output=graph&time_interval=$time_interval&unit_conversion=$unit_conversion"; print "$url\n"; my $url_table = $http_xenia_graph."environment=$env&sensor_id=$sensor_id&output=table&time_interval=$time_interval&unit_conversion=$unit_conversion"; my $file_to = $dir_base."platform/$platform_handle/graph/$obs_type:$uom_type:$time_interval_label.png"; print "$file_to\n"; my $file_to_table = $dir_base."platform/$platform_handle/graph/$obs_type:$uom_type:$time_interval_label.html"; my $url_test = $http_base."platform/$platform_handle/graph/$obs_type:$uom_type:$time_interval_label.png"; print "$url_test\n"; my $ua = new LWP::UserAgent; $ua->timeout(300); #number of seconds before timeout, default is 3 minutes(180 seconds) #determine age of existing files my $head = $ua->head($url_test); #the below returns the file last modified like: Sat, 18 Nov 2006 14:17:21 GMT my $time_file = $head->{_headers}{'last-modified'}; ## not 'date' my ($weekday,$day,$month,$year,$hour_minute_second) = split(/\s+/,$time_file); my $time_file_sec = `date --date='$day $month $year $hour_minute_second +0000' +%s`; #print "time_file:$time_file_sec"; chomp($time_file_sec); my $time_now_sec = `date -u +%s`; #print "time_now:$time_now_sec"; chomp($time_now_sec); my $time_diff = $time_now_sec - $time_file_sec; #print "time_diff:$time_diff\n"; if ($time_diff > 3600) { #existing files are older than an hour so go ahead and generate new my $response = $ua->mirror($url,$file_to) ; if ($response->is_error) { print "couldn't retrieve file\n"; exit 0; } print "ok graph\n"; my $response = $ua->mirror($url_table,$file_to_table) ; if ($response->is_error) { print "couldn't retrieve file\n"; exit 0; } print "ok table\n"; } } #while $sensor_id } #while $platform_id exit 0; ##################################################################### sub exit_no_data { $sth->finish; $dbh->disconnect(); exit 0; }