Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions php/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ DB_PORT=9042
#DB_USERNAME="scylla"
#DB_PASSWORD="really-cool-cluster-password"
#DB_PORT=9042

#DB_CERT="/root/cert.txt"
1 change: 1 addition & 0 deletions php/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
}
],
"require": {
"php": "^8.1",
"fakerphp/faker": "^1.20",
"ext-cassandra": "^1.3.9-dev",
"vlucas/phpdotenv": "^5.5",
Expand Down
1 change: 1 addition & 0 deletions php/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'consistency_level' => Cassandra::CONSISTENCY_QUORUM,
'certificate_path' => env('DB_CERT'),
'port' => (int) env('DB_PORT', 9042)
]
];
2 changes: 1 addition & 1 deletion php/migrations/1-create_keyspace.cql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CREATE KEYSPACE IF NOT EXISTS carepet WITH replication = { 'class': 'NetworkTopologyStrategy', 'replication_factor': '2' };
CREATE KEYSPACE IF NOT EXISTS carepet WITH replication = { 'class': 'NetworkTopologyStrategy', 'replication_factor': '3' };
22 changes: 12 additions & 10 deletions php/src/Core/Commands/SimulateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,25 @@ public function __construct(

public function __invoke(array $args = []): int
{
$baseMemory = memory_get_usage();
$this->info('Starting Sensor simulator...');
foreach (range(0, self::AMOUNT_BASE) as $i) {
$this->info("Batch: " . $i);
#$this->info("Batch: " . $i);
[$ownerDTO, $petsDTO] = $this->generateFakeData();

$this->ownerRepository->create($ownerDTO);
$this->info(sprintf('Owner %s', $ownerDTO->id));
#$this->info(sprintf('Owner %s', $ownerDTO->id));

$petsDTO->each(function ($petDTO) {
$this->info(sprintf('Pet: %s | Owner %s', $petDTO->id->uuid(), $petDTO->ownerId));
#$this->info(sprintf('Pet: %s | Owner %s', $petDTO->id->uuid(), $petDTO->ownerId));
$this->petRepository->create($petDTO);

SensorFactory::makeMany(5, ['pet_id' => $petDTO->id])
->each($this->handleSensors());
});
#$this->info("Memory usage:" . memory_get_usage() - $baseMemory);
}
$this->info('Done :D');
#$this->info('Done :D');

return self::SUCCESS;
}
Expand All @@ -60,12 +62,12 @@ private function handleSensors(): Closure
{
return function (SensorDTO $sensorDTO) {
$this->sensorRepository->create($sensorDTO);
$this->info(sprintf(
'Sensor: %s (%s) | Pet %s',
$sensorDTO->id,
$sensorDTO->type->name,
$sensorDTO->petId
));
// $this->info(sprintf(
// 'Sensor: %s (%s) | Pet %s',
// $sensorDTO->id,
// $sensorDTO->type->name,
// $sensorDTO->petId
// ));
};
}
}
53 changes: 28 additions & 25 deletions php/src/Core/Database/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace App\Core\Database;

use App\Core\Entities\AbstractDTO;
use Cassandra\PreparedStatement;
use Cassandra\Rows;
use Cassandra\SimpleStatement;

abstract class AbstractRepository
{
Expand All @@ -13,53 +15,54 @@ abstract class AbstractRepository

public Connector $connection;

/**
* @var array<string, PreparedStatement>
*/
protected array $preparedStatements = [];

public array $keys = [];

public function __construct(Connector $connector)
{
$this->connection = $connector;

$this->preparedStatements = [
'create' => $this->connection->session->prepare(sprintf(
"INSERT INTO %s (%s) VALUES (%s)",
$this->table,
implode(', ', $this->keys),
implode(', ', array_fill(0, count($this->keys), '?'))
)),
'getById' => $this->connection->session->prepare(sprintf(
"SELECT %s FROM %s WHERE %s = ?",
implode(', ', $this->keys),
$this->table,
$this->primaryKey
))
];
}

public function getById(string $id): Rows
{
$query = sprintf("SELECT * FROM %s WHERE %s = %s", $this->table, $this->primaryKey, $id);

return $this->connection
->prepare($query)
->execute()
->get(Connector::BASE_TIMEOUT);
->session
->execute($this->preparedStatements['getById'], [$id]);
}

public function all(): Rows
{
return $this->connection
->prepare(sprintf('SELECT * FROM %s', $this->table))
->prepare(sprintf('SELECT * FROM %s LIMIT 5', $this->table))
->execute()
->get(Connector::BASE_TIMEOUT);
}

public function create(AbstractDTO $dto): void
{
$keys = array_keys($dto->toDatabase());
$dataValues = array_values($dto->toDatabase());

foreach ($dataValues as $key => $value) {
if (is_string($value) && !in_array($keys[$key], $this->keys)) {
$value = addslashes($value);
$dataValues[$key] = "'$value'";
}
}

$query = sprintf(
"INSERT INTO %s (%s) VALUES (%s)",
$this->table,
implode(', ', $keys),
implode(', ', $dataValues)
);


$this->connection
->prepare($query)
->execute();
->session
->executeAsync($this->preparedStatements['create'], ['arguments' => $dto->toDatabase()]);

}
}
32 changes: 24 additions & 8 deletions php/src/Core/Database/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,33 @@ class Connector

public function __construct(array $config)
{
$this->connectionBuilder = Cassandra::cluster()
->withContactPoints($config['nodes'])
->withDefaultConsistency($config['consistency_level'])
->withPort($config['port']);
try {

if (!empty($config['username'] && !empty($config['password']))) {
$this->connectionBuilder = $this->connectionBuilder->withCredentials($config['username'], $config['password']);

$this->connectionBuilder = Cassandra::cluster()
->withContactPoints($config['nodes'])
->withDefaultConsistency($config['consistency_level'])
->withPort($config['port']);

if (isset($config['certificate_path'])) {
$ssl = Cassandra::ssl()
->withVerifyFlags(Cassandra::VERIFY_PEER_CERT)
->withTrustedCerts($config['certificate_path'])
->build();
$this->connectionBuilder = $this->connectionBuilder->withSSL($ssl);
}

if (!empty($config['username'] && !empty($config['password']))) {
$this->connectionBuilder = $this->connectionBuilder->withCredentials($config['username'], $config['password']);
}
$this->cluster = $this->connectionBuilder->build();

$this->session = $this->cluster->connect($config['keyspace']);
} catch (\Exception $exception) {
echo $exception->getMessage();
die;
}
$this->cluster = $this->connectionBuilder->build();

$this->session = $this->cluster->connect($config['keyspace']);
}

public function setKeyspace(string $keyspace = ''): self
Expand Down
2 changes: 1 addition & 1 deletion php/src/Owner/OwnerDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function make(array $payload): self
public function toDatabase(): array
{
return [
'owner_id' => $this->id->uuid(),
'owner_id' => $this->id,
'name' => $this->name,
'address' => $this->address
];
Expand Down
7 changes: 6 additions & 1 deletion php/src/Owner/OwnerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Owner;

use App\Core\Database\AbstractRepository;
use App\Core\Database\Connector;
use App\Core\Entities\AbstractDTO;

final class OwnerRepository extends AbstractRepository
{
Expand All @@ -11,6 +13,9 @@ final class OwnerRepository extends AbstractRepository
public string $primaryKey = 'owner_id';

public array $keys = [
'owner_id'
'owner_id',
'name',
'address'
];

}
9 changes: 5 additions & 4 deletions php/src/Pet/PetDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Core\Entities\AbstractDTO;
use Cassandra\Uuid;
use Cassandra\Varint;

final class PetDTO extends AbstractDTO
{
Expand Down Expand Up @@ -44,15 +45,15 @@ public static function make(array $payload): self
public function toDatabase(): array
{
return [
'pet_id' => $this->id->uuid(),
'owner_id' => $this->ownerId->uuid(),
'chip_id' => $this->chipId,
'pet_id' => $this->id,
'owner_id' => $this->ownerId,
'chip_id' => "nah",
'species' => $this->species,
'breed' => $this->breed,
'color' => $this->color,
'gender' => $this->gender,
'age' => $this->age,
'weight' => $this->weight,
//'weight' => new \Cassandra\Decimal($this->weight),
'address' => $this->address,
'name' => $this->name,
];
Expand Down
12 changes: 10 additions & 2 deletions php/src/Pet/PetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ final class PetRepository extends AbstractRepository
public string $primaryKey = 'pet_id';

public array $keys = [
'pet_id',
'owner_id'
'pet_id' ,
'owner_id' ,
'chip_id' ,
'species' ,
'breed' ,
'color' ,
'gender' ,
'age' ,
'address' ,
'name' ,
];

public function getByOwnerId(string $ownerId): Rows
Expand Down
4 changes: 2 additions & 2 deletions php/src/Sensors/Sensor/SensorDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function __construct(
public function toDatabase(): array
{
return [
'sensor_id' => $this->id->uuid(),
'pet_id' => $this->petId->uuid(),
'sensor_id' => $this->id,
'pet_id' => $this->petId,
'type' => $this->type->name
];
}
Expand Down
12 changes: 11 additions & 1 deletion php/src/Sensors/Sensor/SensorRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Core\Database\AbstractRepository;
use App\Core\Database\Connector;
use App\Core\Entities\AbstractDTO;
use Cassandra\Rows;

final class SensorRepository extends AbstractRepository
Expand All @@ -14,9 +15,17 @@ final class SensorRepository extends AbstractRepository

public array $keys = [
'sensor_id',
'pet_id'
'pet_id',
'type'
];


public function __construct(Connector $connector)
{
parent::__construct($connector);

}

public function getSensorsByPetId(string $petId): Rows
{
$query = sprintf('SELECT * FROM %s where pet_id = %s', $this->table, $petId);
Expand All @@ -27,6 +36,7 @@ public function getSensorsByPetId(string $petId): Rows
->get(Connector::BASE_TIMEOUT);
}


public function getSensorsValuesByDateRange(string $sensorId, string $startAt, string $endAt): Rows
{
$query = sprintf(
Expand Down