Skip to content

Commit

Permalink
add session id evalutate event
Browse files Browse the repository at this point in the history
  • Loading branch information
esposimo committed Apr 18, 2024
1 parent 99551dd commit ada4434
Show file tree
Hide file tree
Showing 9 changed files with 351 additions and 64 deletions.
125 changes: 125 additions & 0 deletions src/mock_crawler_2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
require_once './vendor/autoload.php';

use Illuminate\Database\Capsule\Manager as Capsule;
use pagopa\crawler\Memcached;


const MEMCACHED_HOST = '172.17.0.3';
const MEMCACHED_PORT = "11211";
const DB_HOST = '172.17.0.2';
const DB_PORT = '5432';
const DB_DATABASE = 'postgres';
const DB_USERNAME = 'postgres';
const DB_PASSWORD = 'admin';
const TRANSACTION_TABLE = "transaction_%s";
const TRANSACTION_DETAILS_TABLE = "transaction_details_%s";
const TRANSACTION_EVENTS_TABLE = "transaction_events_%s";
const TRANSACTION_RE_TABLE = "transaction_re_%s";

const METADATA_TABLE = "metadata_%s";

const EXTRA_INFO_TABLE = "extra_info_%s";

$capsule = new Capsule;

$capsule->addConnection([
'driver' => 'pgsql',
'host' => DB_HOST,
'port' => DB_PORT,
'database' => DB_DATABASE,
'username' => DB_USERNAME,
'password' => DB_PASSWORD,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci'
]);

$capsule->setAsGlobal();
$capsule->bootEloquent();
$memcache = new Memcached();

const REDIS_HOST = '172.17.0.6';
const REDIS_PORT = '6379';


$connection =
[
'scheme' => 'tcp',
'host' => REDIS_HOST,
'port' => REDIS_PORT
];



$redis_cache = new \pagopa\crawler\RedisCache($connection);

\pagopa\crawler\MapEvents::init();

//Capsule::statement('truncate table transaction_2024, transaction_details_2024, transaction_events_2024;');
//Capsule::statement('update transaction_re_2024 set state=:state', [':state' => 'TO_LOAD']);

$a = new \pagopa\crawler\paymentlist\req\activatePaymentNotice(new \DateTime('2023-09-01'),'activatePaymentNotice', 'REQ', $redis_cache);
$a->run();

$a = new \pagopa\crawler\paymentlist\resp\activatePaymentNotice(new \DateTime('2023-09-01'),'activatePaymentNotice', 'RESP', $redis_cache);
$a->run();


$a = new \pagopa\crawler\paymentlist\req\nodoInviaCarrelloRPT(new \DateTime('2023-09-01'),'nodoInviaCarrelloRPT', 'REQ', $redis_cache);
$a->run();

$a = new \pagopa\crawler\paymentlist\resp\nodoInviaCarrelloRPT(new \DateTime('2023-09-01'),'nodoInviaCarrelloRPT', 'RESP', $redis_cache);
$a->run();
//die();


$a = new \pagopa\crawler\paymentlist\req\nodoAttivaRPT(new \DateTime('2023-09-01'), 'nodoAttivaRPT', 'REQ', $redis_cache);
$a->run();

$a = new \pagopa\crawler\paymentlist\resp\nodoAttivaRPT(new \DateTime('2023-09-01'), 'nodoAttivaRPT', 'RESP', $redis_cache);
$a->run();


$a = new \pagopa\crawler\paymentlist\req\nodoInviaRPT(new \DateTime('2023-09-01'), 'nodoInviaRPT', 'REQ', $redis_cache);
$a->run();

$a = new \pagopa\crawler\paymentlist\resp\nodoInviaRPT(new \DateTime('2023-09-01'), 'nodoInviaRPT', 'RESP', $redis_cache);
$a->run();


$a = new \pagopa\crawler\paymentlist\req\pspNotifyPayment(new \DateTime('2023-09-01'), 'pspNotifyPayment', 'REQ', $redis_cache);
$a->run();

$a = new \pagopa\crawler\paymentlist\resp\pspNotifyPayment(new \DateTime('2023-09-01'), 'pspNotifyPayment', 'RESP', $redis_cache);
$a->run();


$a = new \pagopa\crawler\paymentlist\req\pspInviaCarrelloRPT(new DateTime('2023-09-01'), 'pspInviaCarrelloRPT', 'REQ', $redis_cache);
$a->run();

$a = new \pagopa\crawler\paymentlist\resp\pspInviaCarrelloRPT(new DateTime('2023-09-01'), 'pspInviaCarrelloRPT', 'RESP', $redis_cache);
$a->run();


$a = new \pagopa\crawler\paymentlist\req\pspInviaCarrelloRPTCarte(new DateTime('2023-09-01'), 'pspInviaCarrelloRPTCarte', 'REQ', $redis_cache);
$a->run();

$a = new \pagopa\crawler\paymentlist\resp\pspInviaCarrelloRPTCarte(new DateTime('2023-09-01'), 'pspInviaCarrelloRPTCarte', 'RESP', $redis_cache);
$a->run();


$a = new \pagopa\crawler\paymentlist\req\sendPaymentOutcome(new DateTime('2023-09-01'), 'sendPaymentOutcome', 'REQ', $redis_cache);
$a->run();

$a = new \pagopa\crawler\paymentlist\resp\sendPaymentOutcome(new DateTime('2023-09-01'), 'sendPaymentOutcome', 'RESP', $redis_cache);
$a->run();

$a = new \pagopa\crawler\paymentlist\req\nodoInviaRT(new DateTime('2023-09-01'), 'nodoInviaRT', 'REQ', $redis_cache);
$a->run();

$a = new \pagopa\crawler\paymentlist\resp\nodoInviaRT(new DateTime('2023-09-01'), 'nodoInviaRT', 'RESP', $redis_cache);
$a->run();

die();
//$a = new \pagopa\crawler\paymentlist\resp\activatePaymentNotice(new \DateTime('2024-03-10'),'activatePaymentNotice', 'RESP', $memcache);
//$a->run();
102 changes: 51 additions & 51 deletions src/mock_insert.php

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/prod_clean_run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

php flush_cache.php ;
#php mock_insert.php ;
php mock_crawler_2.php ;
14 changes: 10 additions & 4 deletions src/src/crawler/AbstractEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ public function __construct(array $eventData)
$payload = '';
if (array_key_exists('payload', $eventData))
{
$payload = (is_resource($eventData["payload"])) ? base64_decode(stream_get_contents($eventData["payload"])) : base64_decode($eventData["payload"]);
$this->data["payload"] = $payload;
$payload = (is_resource($eventData["payload"])) ? stream_get_contents($eventData["payload"]) : $eventData["payload"];
if ((empty($payload)))
{
$payload = base64_encode("NO_PAYLOAD");
}
$this->data["payload"] = base64_decode($payload);
}
$date = new \DateTime($eventData['inserted_timestamp']);
$this->instance = new TransactionRe($date, $eventData);
Expand Down Expand Up @@ -88,7 +92,8 @@ public function getSottoTipoEvento(): string
*/
public function getSessionId() : string|null
{
return $this->getColumn('sessionid');
$value = $this->getColumn('sessionid');
return (empty($value)) ? null : $value;
}


Expand All @@ -98,7 +103,8 @@ public function getSessionId() : string|null
*/
public function getSessionIdOriginal(): string|null
{
return $this->getColumn('sessionidoriginal');
$value = $this->getColumn('sessionidoriginal');
return (empty($value)) ? null : $value;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/src/crawler/PaymentListInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function delFromCache(string $key) : void;
* @param mixed $value
* @return void
*/
public function addValueCache(string $key, mixed $value) : void;
public function addValueCache(string $key, mixed $value, int $ttl = 86400) : void;


/**
Expand Down
4 changes: 2 additions & 2 deletions src/src/crawler/RedisCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function setValue(string $key, mixed $value, int $expiration = 0): void
/**
* @inheritDoc
*/
public function addValue(string $key, mixed $value): void
public function addValue(string $key, mixed $value, $ttl = 86400): void
{
$cached = $this->client->get($key);
// se cached esiste
Expand All @@ -77,7 +77,7 @@ public function addValue(string $key, mixed $value): void
{
$to_add = json_encode(array($value));
}
$this->client->set($key, $to_add, 'EX', 86400);
$this->client->set($key, $to_add, 'EX', $ttl);
}

/**
Expand Down
123 changes: 121 additions & 2 deletions src/src/crawler/paymentlist/AbstractPaymentList.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public function run(): void
->limit($limit)
->get();

echo 'Lancio la procedura per ' .$this->getMethodName() . ' ' .$this->getType() .PHP_EOL;
foreach($events as $event)
{
$this->createEventInstance((array) $event);
Expand Down Expand Up @@ -234,9 +235,9 @@ public function delFromCache(string $key): void
/**
* @inheritDoc
*/
public function addValueCache(string $key, mixed $value): void
public function addValueCache(string $key, mixed $value, int $ttl = 86400): void
{
$this->cache->addValue($key, $value, 86400);
$this->cache->addValue($key, $value, $ttl);
}

/**
Expand Down Expand Up @@ -299,6 +300,11 @@ public function runAnalysisSingleEvent() : void
try {
$state = 'LOADED';
$message = null;
$session_key = null;
if (!is_null($this->getEvent()->getSessionId()))
{
$session_key = base64_encode(sprintf('session_id_%s_%s', $this->getEvent()->getSessionId(), $this->getMethodName()));
}
if ($this->isValidPayment())
{
// l'evento può essere associato ad uno o più pagamente e/o tentativi?
Expand Down Expand Up @@ -326,6 +332,7 @@ public function runAnalysisSingleEvent() : void
// add workflow to $cache_value and return new value
}
$this->setCache($cache_key, $new_cache_data);
$this->addSessionIdInCache($cache_key);
// store new cache in $cache_key
}
else
Expand All @@ -347,6 +354,7 @@ public function runAnalysisSingleEvent() : void
// add details to cache value and return a new cache value
// add workflow to cache_value and return a new cache value
// save cache_value in $cache_data
$this->addSessionIdInCache($cache_key);
}
}
else
Expand Down Expand Up @@ -379,6 +387,7 @@ public function runAnalysisSingleEvent() : void
// add workflow to $cache_value and return new value
}
$this->setCache($cache_key, $new_cache_data);
$this->addSessionIdInCache($cache_key);

// store new cache in $cache_key
}
Expand All @@ -395,6 +404,7 @@ public function runAnalysisSingleEvent() : void
$cached_object = new CacheObject($this->detailsPayment($cached_object, $i));
$cached_object = $this->workflow($cached_object, $i);
$this->addValueCache($cache_key, $cached_object);
$this->addSessionIdInCache($cache_key);
// store cache
}
}
Expand All @@ -407,6 +417,31 @@ public function runAnalysisSingleEvent() : void
}
}
}
else if (($this->isSessionIdInCache()))
{
// se non è un pagamento valido (ovvero non ha uno tra iuv e id dominio, provo a verificare se c'è la sessione
$key = $this->getSessionIdCacheKey();
$cache_data = $this->getFromCache($key);
foreach($cache_data as $payment_key)
{
// per ogni cache key associata a questo session id, mi prendo i dati della chiave ($payment_key)
$payment_in_cache = $this->getFromCache($payment_key);
$new_cache_data = [];
foreach($payment_in_cache as $ck => $payment)
{
// ed effettuo aggiornamento dati e cache
$refresh_cache = $this->updateTransaction(new CacheObject($payment), $ck);
$refresh_cache = $this->updateDetails(new CacheObject($refresh_cache), $ck);
$refresh_cache = $this->updateMetadataDetails(new CacheObject($refresh_cache), $ck);
$refresh_cache = $this->createExtraInfo(new CacheObject($refresh_cache), $ck);
$refresh_cache = $this->workflow(new CacheObject($refresh_cache), $ck);
$new_cache_data[] = $refresh_cache;
}
$this->setCache($payment_key, $new_cache_data); // aggiorno la cache per quanto riguarda i pagamenti
// dovrei eliminare la chiave $payment_key da $key
$this->deletePaymentKeyFromSessionCache($payment_key, $key);
}
}
else
{
$state = 'REJECTED';
Expand Down Expand Up @@ -546,4 +581,88 @@ public function createExtraInfo(CacheObject $cache, int $index = 0): array|null
}


/**
* Restituisce true/false se una chiave session id è in cache
* @return bool
*/
public function isSessionIdInCache() : bool
{
$value = $this->getEvent()->getSessionId();
if (is_null($value))
{
return false;
}
$key = base64_encode(sprintf('session_id_%s_%s', $value, $this->getMethodName()));
return $this->hasInCache($key);
}

/**
* Restituisce la chiave dove storicizzare tutte le chiavi della cache relative ai pagamenti impattati dal session id
* @return string|null
*/
public function getSessionIdCacheKey() : string|null
{
$value = $this->getEvent()->getSessionId();
if (is_null($value))
{
return null;
}
return base64_encode(sprintf('session_id_%s_%s', $value, $this->getMethodName()));
}


/**
* Aggiunge una chiave cache, relativa ad una lista di pagamenti, al session id dell'evento analizzato
* @param string $key
* @return void
*/
public function addSessionIdInCache(string $key) : void
{
$session = $this->getEvent()->getSessionId();
if (!is_null($session)) {
if ($this->isSessionIdInCache())
{
// esiste già una chiave in cache con i pagamenti associati al session id
$cache_data = $this->getFromCache($this->getSessionIdCacheKey());
if (!in_array($key, $cache_data))
{
$this->addValueCache($this->getSessionIdCacheKey(), $key, 1200);
}
}
else
{
// non esiste una chiave in cache con i pagamenti associati al session id
$this->addValueCache($this->getSessionIdCacheKey(), $key, 1200);
}
}
}


/**
* Elimina una chiave $payment_key dalla lista dei pagamenti associati al session id $key
* @param string $payment_key
* @param string $key
* @return void
*/
public function deletePaymentKeyFromSessionCache(string $payment_key, string $key) : void
{
$value = $this->getFromCache($key);
if (is_array($value))
{
// se è un array , lo ciclo ed elimino $payment_key
$found = array_search($payment_key, $value);
if ($found !== false)
{
unset($value[$found]);
}
if (count($value) == 0)
{
$this->delFromCache($key);
}
else
{
$this->setCache($key, array_values($value));
}
}
}
}
Loading

0 comments on commit ada4434

Please sign in to comment.