142 lines
4.5 KiB
PHP
142 lines
4.5 KiB
PHP
<h1>EQN</h1>
|
|
<i>Testing procedure Redis per EQN...</i><hr/>
|
|
<?php
|
|
|
|
require 'redis.php';
|
|
require 'stopWatch.php';
|
|
|
|
# avvio misura tempi...
|
|
StopWatch::start();
|
|
|
|
function redis_error($error) {
|
|
// throw new error($error);
|
|
}
|
|
|
|
$redis = new redis_cli ();
|
|
$redis->connect("redis01");
|
|
$redis->set_error_function('redis_error');
|
|
|
|
$server = gethostname();
|
|
echo "Server ".$server." inizializzato, proseguo test<hr/>";
|
|
|
|
#------------------------------------------------------
|
|
# VALORI DI CONFIGURAZIONE (portare su file ext!!!)
|
|
#------------------------------------------------------
|
|
# ScoreList Posizioni
|
|
$semaforo = "EQN:USERS:SEMAPHORE";
|
|
$tblPos = "EQN:USERS:POS";
|
|
$tblLast= "EQN:USERS:UPD";
|
|
$tblCanPos = "EQN:CANDIDATE:POS";
|
|
$tblCanLast= "EQN:CANDIDATE:UPD";
|
|
# timeout (x semaforo) IN SECONDI x gestione update POSIZIONI sul DB (cancello vecchi, inserisco nuovi): 60 sec (o meno...)
|
|
$tOutSem_s = 60;
|
|
# età massima dei valori a sistema (40 min x cancellare vecchi...)
|
|
$maxAge_m = 40;
|
|
# controllo se serve cleanUp
|
|
$semOk=0;
|
|
|
|
# Salva posizione utente
|
|
function salvaPosizione($redis, $tblPos, $tblLast, $User_ID, $Lat, $Lon, $DTime) {
|
|
# salvo dataora verifica
|
|
$redis->cmd('ZADD', $tblLast, $DTime, $User_ID )
|
|
// # salvo posizione Geolocalizzata
|
|
->cmd('GEOADD',$tblPos, $Lon, $Lat, $User_ID )
|
|
->set();
|
|
}
|
|
|
|
# Verifica SE ci sia semaforo rosso oppure se sia necessario a fine programma fare cleanUp
|
|
function checkSemaforo($redis, $semaforo, $tOutSem_s) {
|
|
// verifico semaforo...
|
|
$answ=$redis->cmd('EXISTS', $semaforo )->get();
|
|
// se NON c'è lo creo
|
|
if($answ==0)
|
|
{
|
|
$redis->cmd('SET', $semaforo, date("YmdHis"))
|
|
->cmd('EXPIRE', $semaforo, $tOutSem_s)
|
|
->set();
|
|
}
|
|
return $answ;
|
|
}
|
|
# effettua pulizia tabelle REDIS UTENTI (POS e UPD)
|
|
function cleanUp($redis, $tblPos, $tblLast, $DTime) {
|
|
echo "DTime Max: ".$DTime."<br/>";
|
|
// recupero in blocco elenco user_ID vecchi...
|
|
$answ=$redis->cmd('ZRANGEBYSCORE', $tblLast, 0, $DTime)
|
|
->set();
|
|
// costruisco stringa comando x dati GEO vecchi
|
|
$array=$answ[0];
|
|
# imposto transazione...
|
|
$redis->cmd('MULTI');
|
|
# inizio scrittura...
|
|
for ($i = 0; $i < count($array); ++$i) {
|
|
$redis->cmd('ZREM', $tblPos, $array[$i]);
|
|
}
|
|
// costruisco stringa comando x calcellazione dati vecchi DTime
|
|
$redis->cmd('ZREMRANGEBYSCORE', $tblLast, 0, $DTime);
|
|
# chiavo vera esecuzione transazione
|
|
$redis->cmd('EXEC')->set();
|
|
}
|
|
# rimuove record di un singolo user_ID su richiesta dalle 2 tabelle
|
|
function remUserId($redis, $tblPos, $tblLast, $uID) {
|
|
$redis->cmd('MULTI');
|
|
$redis->cmd('ZREM', $tblPos, $uID);
|
|
$redis->cmd('ZREM', $tblLast, $uID);
|
|
$redis->cmd('EXEC')->set();
|
|
}
|
|
# conta quanti record ci siano nelle tab indicata
|
|
function cntRec($redis, $tblReq) {
|
|
// conteggio!
|
|
$answ=$redis->cmd('ZCOUNT', $tblReq, '-inf', '+inf')->set();
|
|
// ritorno valore
|
|
return $answ[0];
|
|
}
|
|
#restituisce gli utenti entro un raggio in KM(!) da certe coordinate
|
|
function cntInRadius($redis, $tblPos, $center_lat , $center_lon, $km){
|
|
$answ=$redis->cmd('GEORADIUS', $tblPos, $center_lon, $center_lat, $km, 'km')->set();
|
|
return count($answ[0]);
|
|
}
|
|
|
|
#restituisce latitudine e longitudine medie dei record all'interno di un certo raggio
|
|
function avgInRadius($redis, $tblPos, $center_lat , $center_lon, $km){
|
|
$answ=$redis->cmd('GEORADIUS', $tblPos, $center_lon, $center_lat, $km, 'km','WITHCOORD')->set();
|
|
$answ=$answ[0];
|
|
|
|
$avg_lat=0;
|
|
$avg_lon=0;
|
|
for ($i=0;$i<count($answ);$i++){
|
|
$avg_lat=$avg_lat+$answ[$i][1][1];
|
|
$avg_lon=$avg_lon+$answ[$i][1][0];
|
|
}
|
|
if (count($answ)>0){
|
|
$avg_lat=$avg_lat/count($answ);
|
|
$avg_lon=$avg_lon/count($answ);
|
|
}
|
|
return array($avg_lat,$avg_lon);
|
|
}
|
|
|
|
|
|
$cnt=cntInRadius($redis,$tblCanPos,19.4,-99.15,30);
|
|
var_dump($cnt);
|
|
#ECHO "trovati ".$cnt."records <br/>";
|
|
echo "Elapsed time (ms): ".StopWatch::elapsed()." <br/>";
|
|
|
|
$lat_lon=avgInRadius($redis,$tblCanPos,19.4,-99.5,22);
|
|
print($lat_lon[0].' '.$lat_lon[1]);
|
|
|
|
# mostro infine count record rimasti!!!
|
|
$num1=cntRec($redis, $tblLast);
|
|
$num2=cntRec($redis, $tblPos);
|
|
$num3=cntRec($redis, $tblCanLast);
|
|
$num4=cntRec($redis, $tblCanPos);
|
|
echo "<hr/>Check EQN:USERS: ".$num1." valori in ".$tblLast." | ".$num2." valori in ".$tblPos."<br/>";
|
|
echo "<hr/>Check EQN:USERS: ".$num3." valori in ".$tblCanLast." | ".$num4." valori in ".$tblCanPos."<br/>";
|
|
echo "Elapsed time (ms): ".StopWatch::elapsed()." <br/>";
|
|
|
|
|
|
// esempio:
|
|
// http://srv.earthquakenetwork.it/test_redis.php
|
|
// https://github.com/ziogas/PHP-Redis-implementation
|
|
// https://stackoverflow.com/questions/33196237/how-to-set-expire-when-using-redis-geoadd
|
|
// https://redis.io/commands/zremrangebyrank
|
|
?>
|