-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c23f04e
commit 94e4bea
Showing
9 changed files
with
327 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoctrineMigrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
final class Version20241008203020 extends AbstractMigration | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return 'Add downloads'; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
$this->addSql('CREATE TABLE package_downloads (package_id INT NOT NULL, historical_data JSON NOT NULL, recent_data JSON NOT NULL, total INT NOT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, merged_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(package_id))'); | ||
$this->addSql('COMMENT ON COLUMN package_downloads.updated_at IS \'(DC2Type:date_immutable)\''); | ||
$this->addSql('CREATE TABLE version_downloads (version_id INT NOT NULL, historical_data JSON NOT NULL, recent_data JSON NOT NULL, total INT NOT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, merged_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(version_id))'); | ||
$this->addSql('COMMENT ON COLUMN version_downloads.updated_at IS \'(DC2Type:date_immutable)\''); | ||
$this->addSql('ALTER TABLE package_downloads ADD CONSTRAINT FK_E19D697EF44CABFF FOREIGN KEY (package_id) REFERENCES package (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); | ||
$this->addSql('ALTER TABLE version_downloads ADD CONSTRAINT FK_45C4D6DB4BBC2705 FOREIGN KEY (version_id) REFERENCES version (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); | ||
|
||
$this->addSql('INSERT INTO package_downloads (package_id, historical_data, recent_data, total) SELECT id, \'{}\', \'{}\', 0 FROM package'); | ||
$this->addSql('INSERT INTO version_downloads (version_id, historical_data, recent_data, total) SELECT id, \'{}\', \'{}\', 0 FROM version'); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
$this->addSql('ALTER TABLE package_downloads DROP CONSTRAINT FK_E19D697EF44CABFF'); | ||
$this->addSql('ALTER TABLE version_downloads DROP CONSTRAINT FK_45C4D6DB4BBC2705'); | ||
$this->addSql('DROP TABLE package_downloads'); | ||
$this->addSql('DROP TABLE version_downloads'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
namespace CodedMonkey\Conductor\Doctrine\Entity; | ||
|
||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
abstract class AbstractDownloads | ||
{ | ||
private ?array $data = null; | ||
|
||
/** | ||
* @var array<int|numeric-string, int> Data is keyed by date in form of YYYYMMDD and as such the keys are technically seen as ints by PHP | ||
*/ | ||
#[ORM\Column(type: 'json')] | ||
protected array $historicalData = []; | ||
|
||
/** | ||
* @var array<int|numeric-string, int> Data is keyed by date in form of YYYYMMDD and as such the keys are technically seen as ints by PHP | ||
*/ | ||
#[ORM\Column(type: 'json')] | ||
protected array $recentData = []; | ||
|
||
#[ORM\Column(type: 'integer')] | ||
protected int $total = 0; | ||
|
||
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)] | ||
protected ?\DateTimeImmutable $updatedAt = null; | ||
|
||
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)] | ||
protected ?\DateTimeImmutable $mergedAt = null; | ||
|
||
/** | ||
* @param array<int|numeric-string, int> $data | ||
*/ | ||
public function setData(array $data): void | ||
{ | ||
$this->historicalData = $data; | ||
$this->recentData = []; | ||
|
||
$this->mergedAt = new \DateTimeImmutable(); | ||
|
||
$this->data = null; | ||
} | ||
|
||
/** | ||
* @param numeric-string $key | ||
*/ | ||
public function setDataPoint(string $key, int $value): void | ||
{ | ||
$this->recentData[$key] = $value; | ||
|
||
$this->data = null; | ||
} | ||
|
||
/** | ||
* @return array<int, int> Key is "YYYYMMDD" which means it always gets converted to an int by php | ||
*/ | ||
public function getData(): array | ||
{ | ||
if (null === $this->data) { | ||
$this->data = $this->doMergeData(); | ||
} | ||
|
||
return $this->data; | ||
} | ||
|
||
public function setTotal(int $total): void | ||
{ | ||
$this->total = $total; | ||
} | ||
|
||
public function getTotal(): int | ||
{ | ||
return $this->total; | ||
} | ||
|
||
public function getUpdatedAt(): ?\DateTimeImmutable | ||
{ | ||
return $this->updatedAt; | ||
} | ||
|
||
public function setUpdatedAt(\DateTimeImmutable $updatedAt): void | ||
{ | ||
$this->updatedAt = $updatedAt; | ||
} | ||
|
||
public function getMergedAt(): ?\DateTimeImmutable | ||
{ | ||
return $this->mergedAt; | ||
} | ||
|
||
public function mergeData(): void | ||
{ | ||
$data = $this->doMergeData(); | ||
|
||
$this->historicalData = $data; | ||
$this->recentData = []; | ||
|
||
$this->mergedAt = new \DateTimeImmutable(); | ||
|
||
$this->data = null; | ||
} | ||
|
||
protected function doMergeData(): array | ||
{ | ||
$data = $this->historicalData; | ||
|
||
foreach ($this->recentData as $dataKey => $dataPoint) { | ||
$data[$dataKey] ??= 0; | ||
$data[$dataKey] += $dataPoint; | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* @param numeric-string $key | ||
*/ | ||
public function increase(string $key): void | ||
{ | ||
$this->recentData[$key] ??= 0; | ||
$this->recentData[$key]++; | ||
|
||
$this->total++; | ||
|
||
$this->data = null; | ||
$this->updatedAt = new \DateTimeImmutable(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace CodedMonkey\Conductor\Doctrine\Entity; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
class PackageDownloads extends AbstractDownloads | ||
{ | ||
#[ORM\Id] | ||
#[ORM\OneToOne(inversedBy: 'downloads')] | ||
private Package $package; | ||
|
||
public function __construct(Package $package) | ||
{ | ||
$this->package = $package; | ||
} | ||
|
||
public function getPackage(): Package | ||
{ | ||
return $this->package; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace CodedMonkey\Conductor\Doctrine\Entity; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
class VersionDownloads extends AbstractDownloads | ||
{ | ||
#[ORM\Id] | ||
#[ORM\OneToOne(inversedBy: 'downloads')] | ||
private Version $version; | ||
|
||
public function __construct(Version $version) | ||
{ | ||
$this->version = $version; | ||
} | ||
|
||
public function getVersion(): Version | ||
{ | ||
return $this->version; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace CodedMonkey\Conductor\Message; | ||
|
||
readonly class TrackDownloads | ||
{ | ||
public function __construct( | ||
public array $downloads, | ||
public \DateTimeInterface $downloadedAt, | ||
) { | ||
} | ||
} |
Oops, something went wrong.