#!/usr/bin/perl #this script will pregenerate the graph page for all the platforms in the given db instance #note this script was developed from processGraphs.pl and shares most of the same code use strict; use DBI; use XML::LibXML; use LWP::UserAgent; #load database and path info my ($env,$unit_conversion) = @ARGV; 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"; my $unit_suffix; if ($unit_conversion eq 'en') { $unit_suffix = '_en'; } my $file_to; #php_graph my $php_graph = "graph$unit_suffix.php"; $file_to = $dir_base."platform/$platform_handle/html/$php_graph"; open (PHP_GRAPH,">$file_to"); my $html_content .= <<"END_OF_FILE"; $platform_handle - Realtime Graphs $file_to"); my $html_latest .= <<"END_OF_FILE"; $platform_handle - Latest Data END_OF_FILE print PHP_LATEST $html_latest; close (PHP_LATEST); #get sensor_id's for platforms $sql = qq{ select sensor.row_id,sensor.m_type_id,m_type_display_order.row_id from sensor left join m_type_display_order on m_type_display_order.m_type_id=sensor.m_type_id where platform_id = $platform_id order by m_type_display_order.row_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 $title = $xp_graph->findvalue('//observation_list/observation[@m_type_id="'.$m_type_id.'"]/title'); my $standard_uom = $xp_graph->findvalue('//observation_list/observation[@m_type_id="'.$m_type_id.'"]/standard_uom'); my $y_title = $xp_graph->findvalue('//unit_conversion_list/unit_conversion[@id="'.$standard_uom.'_to_'.$uom_type.'"]/y_title'); $html_content .= <<"END_OF_FILE"; print "

$title ($y_title)

Graph latest 3 days | Week | Month | 3 months | Year
"; print "Data latest 3 days | Archive
"; print "\n\n"; END_OF_FILE } #while $sensor_id $html_content .= <<"END_OF_FILE"; ?> END_OF_FILE print PHP_GRAPH $html_content; close (PHP_GRAPH); } #while $platform_id exit 0; sub exit_no_data { $sth->finish; $dbh->disconnect(); exit 0; }