Project

General

Profile

1 33214 eri.katsar
<?php
2
require_once('./js/log4php/Logger.php');
3
require_once('./paths.php');
4
require_once('./MYDB.php');
5
6
class PromoteCharts {
7
	private $log;
8
	private $db;
9
	private $cache = null; //predis
10
	public function __construct(){
11
12
		global $redis_host;
13
		global $redis_port;
14
		global $redis_scheme;
15
		global $host;
16
17
	$this->log = Logger::getLogger(__CLASS__);
18
         Logger::configure('./js/log4php/log4php.xml');
19
20
 if(class_exists("Predis\Client")){
21
22
                        try {
23
//todo changed timeout from 0 to -1
24
                        $this->cache = new Predis\Client(array("scheme" => $redis_scheme,"host" => $redis_host,"port" => $redis_port,"read_write_timeout" => -1));
25
                                $this->log->info("redis host: ".$redis_host." and redis port: ".$redis_port);
26
                        } catch(Exception $e) {
27
                                $this->log->error("Error connecting to Redis server: ".$e->getMessage());
28
                                $this->cache = null;
29
                        }
30
                }
31
        else{
32
            $this->log->info("cache does not exist"); //predis
33
                        exit;
34
        }
35
	}
36
37
38
public function replace()
39
{
40
		global $redis_host;
41
		global $redis_port;
42
		global $redis_scheme;
43
        $this->cache = new Predis\Client(array("scheme" => $redis_scheme, "host" => $redis_host, "port" => $redis_port));
44
	$this->cache->connect(); //predis
45
46
47
//now promote all other entries
48
 $keys = $this->cache->keys("*");
49
50
 $this->log->info("Promoting data queries...");
51
 $this->log->info("*******************************************");
52
  for($x=0;$x<count($keys);$x++) {
53
                        $this->log->info("Looking up for key: ".$keys[$x]);
54
if ((strpos($keys[$x],'chart')===false)&&(strpos($keys[$x],'table')===false)&&(strpos($keys[$x],'STATS')===false) )
55
{
56
try
57
{
58
$query = $this->cache->hget($keys[$x],'query');
59
$persistent= $this->cache->hget($keys[$x],'persistent');
60
if(($query!=null)&&($persistent))
61
62
{                                  $this->log->info("going to replace the results for key: ".$keys[$x]."  :  \n ".$query."\n");
63
                                               $fetchMode = $this->cache->hget($keys[$x], 'fetchMode');
64
                                                $shadowResults = $this->cache->hget($keys[$x],'shadow');
65
                                                $result = $this->cache->hget($keys[$x], 'results');
66
$backup = $this->cache->hget($keys[$x], 'backup');
67
                                                //$this->log->info("Currently stored backup".$backup);
68
                                               //$this->log->info("With currently stored results".$result );
69
 $this->cache->hmset($keys[$x],array("results" =>$shadowResults,"query" =>$query,"persistent" =>$persistent,"shadow" =>$shadowResults,"fetchMode" =>$fetchMode,"backup" =>$backup));
70
//$this->log->info(" Promoted Stored Entry : ");
71
}
72
else{
73
$this->log->info("going to delete the key: ".$keys[$x]);
74
$this->cache->del($keys[$x]);
75
}
76
77
}//catch
78
catch(Exception $e) {
79
80
$this->log->error('Error : '.$e->getMessage());
81
82
$this->log->info("going to delete problematic key: ".$keys[$x]);
83
$this->cache->del($keys[$x]);
84
}
85
86
87
        }
88
 else
89
{
90
91
 $this->log->info("Ignoring chart key: ".$keys[$x]);
92
if (strpos($keys[$x],'STATS')===false)
93
{
94
//todO  here should we delete chart entries so that changes are visible in portal?
95
$this->log->info("going to delete the key: ".$keys[$x]);
96
$this->cache->del($keys[$x]);
97
}
98
}
99
}
100
$this->log->info("All Shadow Entries Promoted Sucessfully!");
101
$this->cache->quit();
102
103
}
104
}
105
?>