westdc-zf1/application/module/Westdc/Visual/Record.php

106 lines
2.1 KiB
PHP
Raw Normal View History

2014-05-12 06:53:21 +00:00
<?php
namespace Westdc\Visual;
class Record
{
public $db;
public $subdataset;
public $timeFiled = "utctime";
private $sql;
function __construct($uuid = NULL,$identifier = "")
2014-05-12 06:53:21 +00:00
{
$this->initDatabase();
if(!empty($identifier))
{
$this->subdataset = $identifier;
}
if(!empty($uuid) && !empty($identifier))
{
$this->initVisual($uuid);
}
}
public function __invoke()
{
return $this->getData();
}
public function initDatabase()
{
$config = \Zend_Registry::get('config');
$dsn = "pgsql:host={$config->visual_db->hostname};"
."port={$config->visual_db->port};"
."dbname={$config->visual_db->database};"
."user={$config->visual_db->username};"
."password={$config->visual_db->password}";
$this->db = new \PDO($dsn);
2014-05-21 09:26:15 +00:00
}
public function initVisual($uuid)
{
$visual = new Visual;
$var_data = $visual->getVisualVars($uuid);
$this->sql = $this->makeSql($var_data['data'],$this->subdataset);
//echo $this->sql;exit();
}
public function getSql()
{
return $this->sql;
}
public function getData()
{
$rs = $this->db->query($this->sql);
$data = [];
while($row = $rs->fetch(\PDO::FETCH_ASSOC))
{
$row['utctime'] = $this->utcMsTime(strtotime($row['utctime']));
//var_dump($row);
$data[] = array(
$row['utctime'],
$row['value']
);
}
return $data;
}
public function makeSql($table,$fieldValue)
2014-05-21 09:26:15 +00:00
{
$sql = "SELECT
{$this->timeFiled} as utctime , \"$fieldValue\" as value
FROM $table
2014-05-21 09:26:15 +00:00
ORDER BY
extract(YEAR from \"{$this->timeFiled}\") ASC,
extract(MONTH from \"{$this->timeFiled}\") ASC,
extract(DAY from \"{$this->timeFiled}\") ASC,
extract(HOUR from \"{$this->timeFiled}\") ASC,
extract(MINUTE from \"{$this->timeFiled}\") ASC,
extract(SECOND from \"{$this->timeFiled}\") ASC
2014-05-21 09:26:15 +00:00
";
return $sql;
2014-05-12 06:53:21 +00:00
}
2014-05-21 09:26:15 +00:00
public function utcMsTime($time=''){
if(empty($time))
return (time()+8*3600)*1000;
else
return $time*1000;
}
2014-05-12 06:53:21 +00:00
}