frontend: caching added
git-svn-id: file:///home/jan/tmp/wetterstation/trunk@247 dd492736-c11a-0410-ad51-8c26713eaf7f
This commit is contained in:
parent
3534adf03a
commit
25960c71aa
|
|
@ -9,6 +9,13 @@
|
|||
$pg_user = "weatherstation";
|
||||
$pg_pass = "";
|
||||
|
||||
/* Caching */
|
||||
$performCaching = true;
|
||||
$cachePath = "/tmp/weatherstation-frontend/cache/";
|
||||
|
||||
/* Debug */
|
||||
$isDebug = false;
|
||||
|
||||
/* Default-Werte */
|
||||
$default_set = "small"; // Default Set welches angezeigt wird
|
||||
$default_chart_dir = "images/chart"; // Verzeichnis in dem die Bilder liegen
|
||||
|
|
|
|||
|
|
@ -0,0 +1,109 @@
|
|||
<?
|
||||
|
||||
/* copyright: Jan Losinski, 2006
|
||||
|
||||
at the Moment this is no free software, look at the
|
||||
COPYING-File in the Main-Directory for License-Terms
|
||||
*/
|
||||
|
||||
include_once($path."php_inc/config.inc.php");
|
||||
|
||||
/* Connection-Klasse..ist fuer die Datenbankverbindung zustaendig */
|
||||
class Cacher{
|
||||
|
||||
function _cacheFileName($identifier){
|
||||
$tmp_dir = Config::getCachePath();
|
||||
if(!file_exists($tmp_dir)){
|
||||
mkdir(dirname($tmp_dir), 0700);
|
||||
mkdir($tmp_dir, 0700);
|
||||
}
|
||||
return $tmp_dir.md5($identifier);
|
||||
}
|
||||
function _lockFileName($identifier){
|
||||
return Cacher::_cacheFileName($identifier).".lock";
|
||||
}
|
||||
|
||||
function _checkCache($identifier, $expire){
|
||||
$file = Cacher::_cacheFileName($identifier);
|
||||
if (file_exists($file)){
|
||||
if (filemtime($file) > (time() - ($expire *60))){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function _lockRead($identifier){
|
||||
$handle = fopen(Cacher::_lockFileName($identifier), "w");
|
||||
$i = 0;
|
||||
while (flock($handle, LOCK_SH) != true){
|
||||
usleep(10);
|
||||
if ($i > 10){
|
||||
fclose($handle);
|
||||
echo "bla1";
|
||||
return false;
|
||||
}
|
||||
$i ++;
|
||||
}
|
||||
return $handle;
|
||||
}
|
||||
|
||||
|
||||
function _unlockRead($handle){
|
||||
flock($handle, LOCK_UN);
|
||||
fclose($handle);
|
||||
}
|
||||
|
||||
|
||||
function _lockWrite($identifier){
|
||||
$handle = fopen(Cacher::_lockFileName($identifier), "a+");
|
||||
$i = 0;
|
||||
while (flock($handle, LOCK_EX) != true){
|
||||
usleep(10);
|
||||
if ($i > 10){
|
||||
fclose($handle);
|
||||
echo "bla";
|
||||
return false;
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
return $handle;
|
||||
}
|
||||
|
||||
|
||||
function _unlockWrite($handle){
|
||||
flock($handle, LOCK_UN);
|
||||
fclose($handle);
|
||||
}
|
||||
|
||||
function getCache($identifier, $expire){
|
||||
if(!Config::performCaching()){
|
||||
return false;
|
||||
}
|
||||
$lock = null;
|
||||
if (Cacher::_checkCache($identifier, $expire) && (($lock = Cacher::_lockRead($identifier)) != false)){
|
||||
$file = Cacher::_cacheFileName($identifier);
|
||||
$handle = fopen($file, "r");
|
||||
$serialized = fread ($handle, filesize ($file));
|
||||
Cacher::_unlockRead($lock);
|
||||
return unserialize($serialized);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
function setCache($identifier, $var){
|
||||
if(!Config::performCaching()){
|
||||
return false;
|
||||
}
|
||||
$lock = null;
|
||||
if (($lock = Cacher::_lockWrite($identifier)) != false){
|
||||
$file = Cacher::_cacheFileName($identifier);
|
||||
$handle = fopen($file, "w");
|
||||
fwrite($handle, serialize($var));
|
||||
fclose($handle);
|
||||
Cacher::_unlockWrite($lock);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -58,5 +58,20 @@ class Config{
|
|||
global $notStandard;
|
||||
return $notStandard;
|
||||
}
|
||||
|
||||
function getCachePath(){
|
||||
global $cachePath;
|
||||
return $cachePath;
|
||||
}
|
||||
|
||||
function performCaching(){
|
||||
global $performCaching;
|
||||
return $performCaching;
|
||||
}
|
||||
|
||||
function isDebug(){
|
||||
global $isDebug;
|
||||
return $isDebug;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ class Connection{
|
|||
|
||||
/* Eine Zeile holen */
|
||||
function fetchQueryResultLine($query){
|
||||
if (Config::isDebug()) echo $query."\n";
|
||||
$this->_createConn();
|
||||
$result = pg_query($this->conn, $query)
|
||||
or die('Abfrage fehlgeschlagen: ' . pg_last_error(). "\n<br>\nquery: '".$query."'");
|
||||
|
|
@ -47,6 +48,7 @@ class Connection{
|
|||
|
||||
/* mehrere Zeilen holen */
|
||||
function fetchQueryResultSet($query){
|
||||
if (Config::isDebug()) echo $query."\n";
|
||||
$returnArray = array();
|
||||
$this->_createConn();
|
||||
$result = pg_query($this->conn, $query)
|
||||
|
|
@ -58,6 +60,7 @@ class Connection{
|
|||
|
||||
/* Result roh zurueckgeben */
|
||||
function &getRawResult($query){
|
||||
if (Config::isDebug()) echo $query."\n";
|
||||
$this->_createConn();
|
||||
$result = pg_query($this->conn, $query)
|
||||
or die('Abfrage fehlgeschlagen: ' . pg_last_error(). "\n<br>\nquery: '".$query."'");
|
||||
|
|
|
|||
|
|
@ -33,9 +33,13 @@ class Hum{
|
|||
/* Funktion, die die Klasse mit den Weten initialisiert */
|
||||
function _fetchHumData(){
|
||||
|
||||
$nowData = null;
|
||||
if (($nowData = Cacher::getCache("HumNow_ID_".$this->sensId, 3)) == false){
|
||||
/* Aktuelle Luftfeuchtigkeit bestimmen */
|
||||
$nowQuery = "SELECT hum, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1";
|
||||
$nowData = $this->connection->fetchQueryResultLine($nowQuery);
|
||||
Cacher::setCache("HumNow_ID_".$this->sensId, $nowData);
|
||||
}
|
||||
|
||||
/* Bestimmte Werte den Klassenvariablen zuordnen */
|
||||
$this->nowHum = $nowData['hum'];
|
||||
|
|
@ -52,8 +56,12 @@ class Hum{
|
|||
if($this->maxHum == "nc" || $this->minHum == "nc"){
|
||||
$this->_fetchMinMax();
|
||||
}
|
||||
$Data = null;
|
||||
if (($Data = Cacher::getCache("HumExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, 30)) == false){
|
||||
$Query = "SELECT to_char(max(timestamp), 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND hum=".$this->maxHum." OR hum=".$this->minHum." GROUP BY hum ORDER BY hum ASC LIMIT 2";
|
||||
$Data = $this->connection->fetchQueryResultSet($Query);
|
||||
Cacher::setCache("HumExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, $Data);
|
||||
}
|
||||
$this->minDate = $Data[0]['text_timestamp'];
|
||||
$this->maxDate = $Data[1]['text_timestamp'];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,9 +32,13 @@ class Press{
|
|||
/* Funktion, die die Klasse mit den Weten initialisiert */
|
||||
function _fetchPressData(){
|
||||
|
||||
$nowData = null;
|
||||
if (($nowData = Cacher::getCache("PressNow_ID_".$this->sensId, 3)) == false){
|
||||
/* Aktuelle Luftdruck bestimmen */
|
||||
$nowQuery = "SELECT press, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1";
|
||||
$nowData = $this->connection->fetchQueryResultLine($nowQuery);
|
||||
Cacher::setCache("PressNow_ID_".$this->sensId, $nowData);
|
||||
}
|
||||
|
||||
/* Bestimmte Werte den Klassenvariablen zuordnen */
|
||||
$this->nowPress = $nowData['press'];
|
||||
|
|
@ -48,11 +52,15 @@ class Press{
|
|||
}
|
||||
|
||||
function _fetchMinMaxDate(){
|
||||
if($this->maxHum == "nc" || $this->minHum == "nc"){
|
||||
if($this->maxPress == "nc" || $this->minPress == "nc"){
|
||||
$this->_fetchMinMax();
|
||||
}
|
||||
$Data = null;
|
||||
if (($Data = Cacher::getCache("PressExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, 130)) == false){
|
||||
$Query = "SELECT to_char(max(timestamp), 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND press=".$this->maxPress." OR press=".$this->minPress." GROUP BY press ORDER BY press ASC LIMIT 2";
|
||||
$Data = $this->connection->fetchQueryResultSet($Query);
|
||||
Cacher::setCache("PressExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, $Data);
|
||||
}
|
||||
$this->minDate = $Data[0]['text_timestamp'];
|
||||
$this->maxDate = $Data[1]['text_timestamp'];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,13 +34,21 @@ class Rain{
|
|||
|
||||
/* Momentane Werte aus der Datenbank holen */
|
||||
function _getNowValues($interval){
|
||||
if (($result = Cacher::getCache("NowRain_ID_".$this->sensId."_Interval_".$interval, 7)) == false){
|
||||
$result = $this->connection->fetchQueryResultLine("SELECT sum(count) as rain FROM ".$this->table." WHERE sens_id=".$this->sensId." AND timestamp>(select (current_timestamp - INTERVAL '".$interval."'))");
|
||||
Cacher::setCache("NowRain_ID_".$this->sensId."_Interval_".$interval, $result);
|
||||
}
|
||||
return $result['rain'];
|
||||
}
|
||||
|
||||
/* Maximal gemessene Werte aus der Datenbank holen */
|
||||
function _getMaxValues($unit, $dateFormat){ // unit = hour, minute, ...
|
||||
return $this->connection->fetchQueryResultLine("SELECT to_char(ts, '".$dateFormat."') as date, val FROM ".$this->table."_".$unit." WHERE sens_id=".$this->sensId." ORDER BY val DESC, ts DESC LIMIT 1");
|
||||
$res = null;
|
||||
if (($res = Cacher::getCache("MaxRain_ID_".$this->sensId."_Unit_".$unit."_Format_".$dateFormat, 30)) == false ){
|
||||
$res = $this->connection->fetchQueryResultLine("SELECT to_char(ts, '".$dateFormat."') as date, val FROM ".$this->table."_".$unit." WHERE sens_id=".$this->sensId." ORDER BY val DESC, ts DESC LIMIT 1");
|
||||
Cacher::setCache("MaxRain_ID_".$this->sensId."_Unit_".$unit."_Format_".$dateFormat, $res);
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -71,7 +79,10 @@ class Rain{
|
|||
|
||||
function get_now_all(){
|
||||
if($this->nowAll == "nc");
|
||||
if (( $this->nowAll = Cacher::getCache("AllRainNow_ID_".$this->sensId, 20)) == false){
|
||||
$this->nowAll = $this->connection->fetchQueryResultLine("SELECT sum(count) as rain FROM ".$this->table." WHERE sens_id=".$this->sensId);
|
||||
Cacher::setCache("AllRainNow_ID_".$this->sensId, $this->nowAll);
|
||||
}
|
||||
return round($this->nowAll['rain'] * 0.001,3);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
|
||||
include_once($path."php_inc/connection.inc.php");
|
||||
include_once($path."php_inc/cacher.inc.php");
|
||||
|
||||
$sensor_data = NULL;
|
||||
$extrema_fields = array(
|
||||
|
|
@ -47,11 +48,14 @@ class Sensor{
|
|||
function _fetchSensorData($sensId, &$connection){
|
||||
global $sensor_data;
|
||||
if ($sensor_data == NULL){
|
||||
if (($sensor_data = Cacher::getCache("SensorData", 20)) == false){
|
||||
$query = "SELECT sensoren.*, typen.tabelle FROM sensoren, typen where typen.typ = sensoren.typ";
|
||||
$result = $connection->getRawResult($query);
|
||||
while ($array = pg_fetch_assoc($result)){
|
||||
$sensor_data[$array['id']] = $array;
|
||||
}
|
||||
Cacher::setCache("SensorData", $sensor_data);
|
||||
}
|
||||
}
|
||||
return $sensor_data[$sensId];
|
||||
}
|
||||
|
|
@ -87,6 +91,7 @@ class Sensor{
|
|||
function get_sensor_extrema($sensId, &$connection, $field){
|
||||
global $extrema_fields, $sensor_extrema;
|
||||
if (! array_key_exists($sensId, $sensor_extrema)){
|
||||
if (($sensor_extrema[$sensId] = Cacher::getCache("SensorExtrema_ID_".$sensId, 10)) == false){
|
||||
$data = Sensor::_fetchSensorData($sensId, &$connection);
|
||||
$query = "SELECT ";
|
||||
for ($i = 0; $i < count($extrema_fields[$data['typ']]); $i++){
|
||||
|
|
@ -98,6 +103,8 @@ class Sensor{
|
|||
$query .= "FROM ".$data['tabelle']." WHERE sens_id = ".$sensId;
|
||||
$res = $connection->fetchQueryResultLine($query);
|
||||
$sensor_extrema[$sensId] = $res;
|
||||
Cacher::setCache("SensorExtrema_ID_".$sensId, $sensor_extrema[$sensId]);
|
||||
}
|
||||
}
|
||||
return array('min' => $sensor_extrema[$sensId]['min_'.$field], 'max' => $sensor_extrema[$sensId]['max_'.$field]);
|
||||
}
|
||||
|
|
@ -110,6 +117,7 @@ class Sensor{
|
|||
function get_sensor_average($sensId, &$connection, $field, $init_interval = 0){
|
||||
global $extrema_fields, $sensor_average;
|
||||
if ((! array_key_exists($init_interval, $sensor_average)) || (! array_key_exists($sensId, $sensor_average[$init_interval]))){
|
||||
if (($sensor_average[$init_interval][$sensId] = Cacher::getCache("SensorAverage_ID_".$sensId."_Interval_".$init_interval, 10)) == false){
|
||||
$interval = $init_interval;
|
||||
$data = Sensor::_fetchSensorData($sensId, &$connection);
|
||||
$query = "SELECT count(".$extrema_fields[$data['typ']][0].") AS count, ";
|
||||
|
|
@ -132,6 +140,8 @@ class Sensor{
|
|||
}
|
||||
$res['interval'] = $interval;
|
||||
$sensor_average[$init_interval][$sensId] = $res;
|
||||
Cacher::setCache("SensorAverage_ID_".$sensId."_Interval_".$init_interval, $sensor_average[$init_interval][$sensId]);
|
||||
}
|
||||
}
|
||||
return array('average' => $sensor_average[$init_interval][$sensId][$field], 'count' => $sensor_average[$init_interval][$sensId]['count'], 'interval' => $sensor_average[$init_interval][$sensId]['interval']);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,9 +32,13 @@ class Temp{
|
|||
/* Funktion, die die Klasse mit den Weten initialisiert */
|
||||
function _fetchTempData(){
|
||||
|
||||
$nowData = null;
|
||||
if (($nowData = Cacher::getCache("TempNow_ID_".$this->sensId, 3)) == false){
|
||||
/* Aktuelle Temperatur bestimmen */
|
||||
$nowQuery = "SELECT temp, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1";
|
||||
$nowData = $this->connection->fetchQueryResultLine($nowQuery);
|
||||
Cacher::setCache("TempNow_ID_".$this->sensId, $nowData);
|
||||
}
|
||||
|
||||
/* Bestimmte Werte den Klassenvariablen zuordnen */
|
||||
$this->nowTemp = $nowData['temp'];
|
||||
|
|
@ -48,11 +52,15 @@ class Temp{
|
|||
}
|
||||
|
||||
function _fetchMinMaxDate(){
|
||||
if($this->maxHum == "nc" || $this->minHum == "nc"){
|
||||
if($this->maxTemp == "nc" || $this->minTemp == "nc"){
|
||||
$this->_fetchMinMax();
|
||||
}
|
||||
$Data = null;
|
||||
if (($Data = Cacher::getCache("TempExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, 30)) == false){
|
||||
$Query = "SELECT to_char(max(timestamp), 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND temp=".$this->maxTemp." OR temp=".$this->minTemp." GROUP BY temp ORDER BY temp ASC LIMIT 2";
|
||||
$Data = $this->connection->fetchQueryResultSet($Query);
|
||||
Cacher::setCache("TempExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, $Data);
|
||||
}
|
||||
$this->minDate = $Data[0]['text_timestamp'];
|
||||
$this->maxDate = $Data[1]['text_timestamp'];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,13 +32,21 @@ class Wind{
|
|||
/* Funktion, die die Klasse mit den Weten initialisiert */
|
||||
function _fetchWindData(){
|
||||
|
||||
$nowData = null;
|
||||
if (($nowData = Cacher::getCache("WindNow_ID_".$this->sensId, 3)) == false){
|
||||
/* Aktuelle Wind bestimmen */
|
||||
$nowQuery = "SELECT geschw as wind, richt as dir, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1";
|
||||
$nowData = $this->connection->fetchQueryResultLine($nowQuery);
|
||||
Cacher::setCache("WindNow_ID_".$this->sensId, $nowData);
|
||||
}
|
||||
|
||||
$maxData = null;
|
||||
if (($nowData = Cacher::getCache("WindMax_ID_".$this->sensId, 3)) == false){
|
||||
/* Max und Min-Werte bestimmen */
|
||||
$maxQuery = "SELECT geschw as wind, richt as dir, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND geschw=(SELECT max(geschw) FROM ".$this->table." WHERE sens_id=".$this->sensId.") ORDER BY timestamp DESC LIMIT 1";
|
||||
$maxData = $this->connection->fetchQueryResultLine($maxQuery);
|
||||
Cacher::setCache("WindMax_ID_".$this->sensId, $maxData);
|
||||
}
|
||||
|
||||
/* Bestimmte Werte den Klassenvariablen zuordnen */
|
||||
$this->nowWind = $nowData['wind'];
|
||||
|
|
@ -126,8 +134,12 @@ class Wind{
|
|||
|
||||
/* liefert den Durchschnittswert in einem bestimmtem Interval */
|
||||
function _getAverage($interval){
|
||||
$avData = null;
|
||||
if(($avData = Cacher::getCache("WindAv_ID_".$this->sensId."_INTERVAL_".$interval, 10)) == false){
|
||||
$avQuery = "SELECT avg(geschw) as average, count(geschw) as count FROM ".$this->table." WHERE sens_id=".$this->sensId." AND timestamp>(select (current_timestamp - INTERVAL '".$interval."'))";
|
||||
$avData = $this->connection->fetchQueryResultLine($avQuery);
|
||||
Cacher::setCache("WindAv_ID_".$this->sensId."_INTERVAL_".$interval, $avData);
|
||||
}
|
||||
return $avData;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue