提升稳定性和效率

This commit is contained in:
wlx 2014-03-03 13:37:10 +00:00
parent 7a9d11a977
commit 6ffe628c27
1 changed files with 143 additions and 127 deletions

View File

@ -1,5 +1,8 @@
<?php <?php
set_time_limit(0); set_time_limit(0);
error_reporting(-1);
ini_set("display_errors",'On');
ini_set('error_log','/tmp/wsn.log');
//安全检查只接受来自westdc.westgis.ac.cn的访问 //安全检查只接受来自westdc.westgis.ac.cn的访问
if(@$_SERVER['HTTP_CLIENT_IP']){ if(@$_SERVER['HTTP_CLIENT_IP']){
$onlineip=$_SERVER['HTTP_CLIENT_IP']; $onlineip=$_SERVER['HTTP_CLIENT_IP'];
@ -8,169 +11,182 @@
}else{ }else{
$onlineip=@$_SERVER['REMOTE_ADDR']; $onlineip=@$_SERVER['REMOTE_ADDR'];
} }
if ($argc!=2 && $onlineip!='210.77.68.241' && $onlineip!='210.77.68.215' && $onlineip!='2001:cc0:f006::9') if ($argc!=2 && $onlineip!='210.77.68.241' && $onlineip!='210.77.68.215' && $onlineip!='2001:cc0:f006::9' && $onlineip!='2001:cc0:f006::405')
die("error! You are not permitted to visit this page.".$onlineip); die("error! You are not permitted to visit this page.".$onlineip);
$db = new PDO('pgsql:host=210.77.68.40;dbname=observation', "gis", "gispassword"); $db = new PDO('pgsql:host=210.77.68.40;dbname=observation', "gis", "gispassword");
$station = 0; $station = 0;
$site = 0; $site = 0;
$logger = 0; $logger = 0;
$variable = 0; $variable = 0;
$start; $start;
$end; $end;
$uid=0; $uid=0;
$sitename=""; $sitename="";
$mailname = ""; $mailname = "";
$createtime = date("Ymd",time()); $createtime = date("Ymd",time());
$endtime=""; $endtime="";
$html=""; $html="";
$time=""; $time="";
$value=""; $value="";
$var=""; $var="";
$file=array(); $file=array();
$basepath='/disk1/WestDC/wsndata/'; $basepath='/disk1/WestDC/wsndata/';
//接收用户uid只生成这个用户的数据 //接收用户uid只生成这个用户的数据
if($argc==2) if($argc==2)
{ {
$uid = (int)$argv[1]; $uid = (int)$argv[1];
}else{ }else{
$uid = (int)@$_GET['uid']; $uid = (int)@$_GET['uid'];
} }
if (!($uid>0)) if (!($uid>0))
{ {
die("Error in parameter."); die("Error in parameter.");
} }
$action=@$_GET['action']; $action=@$_GET['action'];
if(!empty($action) && $action=='prepare') if(!empty($action) && $action=='prepare')
{ {
$content="php -f /var/www/ftp2.westgis.ac.cn/wsndata.php $uid"; $content="php -f /var/www/ftp2.westgis.ac.cn/wsndata.php $uid";
$filename='wsndata.sh'; $filename='wsndata.sh';
if (!file_put_contents($filename, $content, LOCK_EX)) if (!file_put_contents($filename, $content, LOCK_EX))
{ {
sleep(1); sleep(1);
if (!file_put_contents($filename, $content, LOCK_EX)) if (!file_put_contents($filename, $content, LOCK_EX))
{ {
sleep(1); sleep(1);
if (!file_put_contents($filename, $content, LOCK_EX)) if (!file_put_contents($filename, $content, LOCK_EX))
die("error in write wsndatar.sh"); die("error in write wsndatar.sh");
} }
} }
} else { } else {
$sql_user = "SELECT * from users_pm_down where starttime is not null and result=-1 and type='down' and uid=$uid"; $sql_user = "SELECT distinct uid,site,variable,starttime,endtime from users_pm_down where starttime is not null and result=-1 and type='down' and uid=$uid";
$sth_user = $db->query($sql_user); $sth_user = $db->query($sql_user);
$rows_user = $sth_user->fetchAll(); $rows_user = $sth_user->fetchAll();
//有需要生成的数据 //有需要生成的数据
if(!empty($rows_user)) if($rows_user)
{ {
foreach($rows_user as $k=>$v ) foreach($rows_user as $k=>$v )
{ {
//逐个生成 //逐个生成
$site = $v['site']; $site = $v['site'];
$variable = $v['variable']; $variable = $v['variable'];
$start = $v['starttime']; $start = date('Y-m-d',strtotime($v['starttime']));
$end = $v['endtime']; $end = date('Y-m-d',strtotime($v['endtime']));
$uid = $v['uid']; $uid = $v['uid'];
//计算有多少天
$days=round((strtotime($end)-strtotime($start))/3600/24)+1; //计算有多少天
//获取观测点的名称 $days=round((strtotime($end)-strtotime($start))/3600/24)+1;
$sql_site= "SELECT fld_name from tbl_site_info where fld_site_id=$site";
$sth_site = $db->query($sql_site); //获取观测点的名称
$rows_site = $sth_site->fetch(); $sql_site= "SELECT fld_name from tbl_site_info where fld_site_id=$site";
$sitename= $rows_site['fld_name']; $sth_site = $db->query($sql_site);
//获取用户的邮箱 $rows_site = $sth_site->fetch();
$sql_mail= "SELECT email from users where id=$uid"; $sitename= $rows_site['fld_name'];
$sth_mail = $db->query($sql_mail);
$rows_mail = $sth_mail->fetch(); //先为该用户创建一个文件夹
if(empty($rows_mail)) $aimDir_user=$basepath.'wsn_'.$uid;
{$rows_mail['email']=="wuadan@lzb.ac.cn";}
//先为该用户创建一个文件夹
$aimDir_user=$basepath.'wsn_'.$uid;
if (!file_exists($aimDir_user)) if (!file_exists($aimDir_user))
{ {
$result = mkdir($aimDir_user); $result = mkdir($aimDir_user);
} }
//在该用户文件夹下面再建立一个观测点文件夹
$aimDir_site=$aimDir_user.DIRECTORY_SEPARATOR.$site; //在该用户文件夹下面再建立一个观测点文件夹
$aimDir_site=$aimDir_user.DIRECTORY_SEPARATOR.$sitename;
if (!file_exists($aimDir_site)) if (!file_exists($aimDir_site))
{ {
$result = mkdir($aimDir_site); $result = mkdir($aimDir_site);
} }
//选择全部变量 //选择全部变量
if($variable==-1) if($variable==-1)
{ {
//获取数据 //获取数据
for ($i = 1; $i <= $days; $i++) $sql2= "SELECT v.fld_time as time,v.fld_value as value,va.fld_name as variablename FROM tbl_value v left join tbl_variable_info va on v.fld_variable_id=va.fld_variable_id WHERE v.fld_site_id=$site AND v.fld_time>='$start' AND v.fld_time<'$endtime' ORDER BY time ";
$sth2 = $db->query($sql2);
$rows2 = $sth2->fetchAll();
$endtime=date("Y-m-d",strtotime($start)+24*3600);
$head = "Time,Variable,Value, \n";
$html="";
foreach ($rows2 as $k=>$v)
{ {
$endtime=date("Y-m-d 00:00:00",strtotime($start)+24*3600);
$sql2= "SELECT v.fld_time as time,v.fld_value as value,va.fld_name as variablename FROM tbl_value v left join tbl_variable_info va on v.fld_variable_id=va.fld_variable_id WHERE v.fld_site_id=$site AND v.fld_time>='$start' AND v.fld_time<'$endtime' ORDER BY time ";
$sth2 = $db->query($sql2);
$rows2 = $sth2->fetchAll();
$datamount=count( $rows2);
//组装数据 //组装数据
$html=""; if ($v['time']>=$start && $v['time']<$endtime)
foreach($rows2 as $k=>$v)
{ {
$time[$k] = $v['time']; $html.=$v['time'].",".$v['variablename'].",".$v['value']."\n";
$value[$k] = $v['value']; } elseif ($v['time']>=$endtime) {
$var[$k] = $v['variablename']; //写入文件
$html .=$time[$k].",".$var[$k].",".$value[$k]."\n"; $resulttime=date("Ymd",strtotime($start));
} $fp = fopen($aimDir_site.DIRECTORY_SEPARATOR.$sitename."_all_".$resulttime.".CSV","w+");
fwrite($fp,$head.$html); //写入数据
$head = "Time,Variable,Value, \n"; fclose($fp); //关闭文件句柄
$start=$endtime;
//将数据写入文件 $endtime=date("Y-m-d",strtotime($start)+24*3600);
$resulttime=date("Ymd",strtotime($start)); $html=$v['time'].",".$v['variablename'].",".$v['value']."\n";
}
$fp = fopen($aimDir_site.DIRECTORY_SEPARATOR.$sitename."_all_".$resulttime.".CSV","a"); //打开csv文件如果不存在则创建 if ($k==count($rows2)) //last record
fwrite($fp,$head.$html); //写入数据 {
fclose($fp); //关闭文件句柄 //写入文件
$resulttime=date("Ymd",strtotime($start));
$start = $endtime; $fp = fopen($aimDir_site.DIRECTORY_SEPARATOR.$sitename."_all_".$resulttime.".CSV","w+");
} fwrite($fp,$head.$html); //写入数据
fclose($fp); //关闭文件句柄
}
}
print $sitename."\r\n";
$sql_up= "update users_pm_down set result=0 WHERE site=$site and variable=-1 and uid=$uid and result=-1 and type='down'"; $sql_up= "update users_pm_down set result=0 WHERE site=$site and variable=-1 and uid=$uid and result=-1 and type='down'";
$sth_up = $db->query($sql_up); $db->query($sql_up);
} else { } else {
//获取观测变量的名称 //获取观测变量的名称
$sql3= "SELECT fld_name from tbl_variable_info where fld_variable_id=$variable"; $sql3= "SELECT fld_name from tbl_variable_info where fld_variable_id=$variable";
$sth3 = $db->query($sql3); $sth3 = $db->query($sql3);
$rows3 = $sth3->fetch(); $rows3 = $sth3->fetch();
$varname=$rows3['fld_name']; $varname=$rows3['fld_name'];
//获取数据 //获取数据
$endtime=""; $endtime=date("Y-m-d",strtotime($start)+24*3600);
for ($i = 1; $i <= $days; $i++) $sql2= "SELECT v.fld_time as time,v.fld_value as value,va.fld_name as variablename FROM tbl_value v left join tbl_variable_info va on v.fld_variable_id=va.fld_variable_id WHERE v.fld_variable_id=$variable and v.fld_site_id=$site AND v.fld_time>='$start' AND v.fld_time<='$end' ORDER BY time ";
$sth2 = $db->query($sql2);
$rows2 = $sth2->fetchAll();
$head = "Time,Variable,Value, \n";
$html="";
foreach ($rows2 as $k=>$v)
{ {
$endtime=date("Y-m-d H:m:s",strtotime($start)+24*3600);
$sql2= "SELECT v.fld_time as time,v.fld_value as value,va.fld_name as variablename FROM tbl_value v left join tbl_variable_info va on v.fld_variable_id=va.fld_variable_id WHERE v.fld_variable_id =$variable and v.fld_site_id=$site AND v.fld_time>='$start' AND v.fld_time<='$endtime' ORDER BY time ";
$sth2 = $db->query($sql2);
$rows2 = $sth2->fetchAll();
$datamount=count( $rows2);
//组装数据 //组装数据
$html=""; if ($v['time']>=$start && $v['time']<$endtime)
foreach($rows2 as $k=>$v)
{ {
$time[$k] = $v['time']; $html.=$v['time'].",".$v['variablename'].",".$v['value']."\n";
$value[$k] = $v['value']; } elseif ($v['time']>=$endtime) {
$var[$k] = $v['variablename']; //写入文件
$html .=$time[$k].",".$var[$k].",".$value[$k]."\n"; $resulttime=date("Ymd",strtotime($start));
} $fp = fopen($aimDir_site.DIRECTORY_SEPARATOR.$sitename."_".$varname."_".$resulttime.".CSV","w+");
fwrite($fp,$head.$html); //写入数据
$head = "Time,Variable,Value, \n"; fclose($fp); //关闭文件句柄
//写入文件 $start=$endtime;
$resulttime=date("Ymd",strtotime($start)); $endtime=date("Y-m-d",strtotime($start)+24*3600);
$fp = fopen($aimDir_site.DIRECTORY_SEPARATOR.$sitename."_".$varname."_".$resulttime.".CSV","a+"); $html=$v['time'].",".$v['variablename'].",".$v['value']."\n";
fwrite($fp,$head.$html); //写入数据 }
fclose($fp); //关闭文件句柄 if ($k==count($rows2)) //last record
$start = $endtime; {
//写入文件
$resulttime=date("Ymd",strtotime($start));
$fp = fopen($aimDir_site.DIRECTORY_SEPARATOR.$sitename."_".$varname."_".$resulttime.".CSV","w+");
fwrite($fp,$head.$html); //写入数据
fclose($fp); //关闭文件句柄
}
} }
print $sitename.'--'.$varname."\r\n";
//给处理的数据做个标记result=0 //给处理的数据做个标记result=0
$sql_up= "update users_pm_down set result=0 WHERE site=$site and variable=$variable and uid=$uid and result=-1 and type='down'"; $sql_up= "update users_pm_down set result=0 WHERE site=$site and variable=$variable and uid=$uid and result=-1 and type='down'";
$sth_up = $db->query($sql_up); $db->query($sql_up);
} }
}
} }
}
}
}