Completed PriceNowProvider

This commit is contained in:
José David Guillén 2022-01-13 22:28:14 +00:00
parent 03ed3a878b
commit ff74df4911
9 changed files with 357 additions and 100 deletions

View File

@ -13,4 +13,8 @@ Created src\entity\prices.php
Modified src\entity\prices.php
> php bin/console make:migration
> php bin/console doctrine:migrations:migrate
> php bin/console doctrine:migrations:migrate
> php bin/console doctrine:migrations:version YYYYMMDDHHMMSS --delete
Or, rewind the migration with:
> php bin/console doctrine:migrations:execute YYYYMMDDHHMMSS --down

View File

@ -79,5 +79,8 @@
"allow-contrib": false,
"require": "5.4.*"
}
},
"require-dev": {
"symfony/maker-bundle": "^1.36"
}
}

View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "86c60ef54bcc782cb1b23664d54d4434",
"content-hash": "f04baa2b4929ee7a60607c4a45e8e872",
"packages": [
{
"name": "api-platform/core",
@ -6564,7 +6564,152 @@
"time": "2020-09-25T08:01:41+00:00"
}
],
"packages-dev": [],
"packages-dev": [
{
"name": "nikic/php-parser",
"version": "v4.13.2",
"source": {
"type": "git",
"url": "https://github.com/nikic/PHP-Parser.git",
"reference": "210577fe3cf7badcc5814d99455df46564f3c077"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/210577fe3cf7badcc5814d99455df46564f3c077",
"reference": "210577fe3cf7badcc5814d99455df46564f3c077",
"shasum": ""
},
"require": {
"ext-tokenizer": "*",
"php": ">=7.0"
},
"require-dev": {
"ircmaxell/php-yacc": "^0.0.7",
"phpunit/phpunit": "^6.5 || ^7.0 || ^8.0 || ^9.0"
},
"bin": [
"bin/php-parse"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.9-dev"
}
},
"autoload": {
"psr-4": {
"PhpParser\\": "lib/PhpParser"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Nikita Popov"
}
],
"description": "A PHP parser written in PHP",
"keywords": [
"parser",
"php"
],
"support": {
"issues": "https://github.com/nikic/PHP-Parser/issues",
"source": "https://github.com/nikic/PHP-Parser/tree/v4.13.2"
},
"time": "2021-11-30T19:35:32+00:00"
},
{
"name": "symfony/maker-bundle",
"version": "v1.36.4",
"source": {
"type": "git",
"url": "https://github.com/symfony/maker-bundle.git",
"reference": "716eee9c8b10b33e682df1b7d80b9061887e9691"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/maker-bundle/zipball/716eee9c8b10b33e682df1b7d80b9061887e9691",
"reference": "716eee9c8b10b33e682df1b7d80b9061887e9691",
"shasum": ""
},
"require": {
"doctrine/inflector": "^1.2|^2.0",
"nikic/php-parser": "^4.11",
"php": ">=7.1.3",
"symfony/config": "^4.4|^5.0|^6.0",
"symfony/console": "^4.4|^5.0|^6.0",
"symfony/dependency-injection": "^4.4|^5.0|^6.0",
"symfony/deprecation-contracts": "^2.2|^3",
"symfony/filesystem": "^4.4|^5.0|^6.0",
"symfony/finder": "^4.4|^5.0|^6.0",
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
"symfony/http-kernel": "^4.4|^5.0|^6.0"
},
"require-dev": {
"composer/semver": "^3.0",
"doctrine/doctrine-bundle": "^1.12.3|^2.0",
"doctrine/orm": "^2.3",
"symfony/http-client": "^4.4|^5.0|^6.0",
"symfony/phpunit-bridge": "^4.4|^5.0|^6.0",
"symfony/polyfill-php80": "^1.16.0",
"symfony/process": "^4.4|^5.0|^6.0",
"symfony/security-core": "^4.4|^5.0|^6.0",
"symfony/yaml": "^4.4|^5.0|^6.0",
"twig/twig": "^2.0|^3.0"
},
"type": "symfony-bundle",
"extra": {
"branch-alias": {
"dev-main": "1.0-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Bundle\\MakerBundle\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.",
"homepage": "https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html",
"keywords": [
"code generator",
"generator",
"scaffold",
"scaffolding"
],
"support": {
"issues": "https://github.com/symfony/maker-bundle/issues",
"source": "https://github.com/symfony/maker-bundle/tree/v1.36.4"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2021-12-01T00:27:38+00:00"
}
],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],

View File

@ -8,4 +8,5 @@ return [
Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
Nelmio\CorsBundle\NelmioCorsBundle::class => ['all' => true],
ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle::class => ['all' => true],
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
];

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20220113214209 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE prices ADD h01 NUMERIC(8, 6) NOT NULL, ADD h02 NUMERIC(8, 6) NOT NULL, ADD h03 NUMERIC(8, 6) NOT NULL, ADD h04 NUMERIC(8, 6) NOT NULL, ADD h05 NUMERIC(8, 6) NOT NULL, ADD h06 NUMERIC(8, 6) NOT NULL, ADD h07 NUMERIC(8, 6) NOT NULL, ADD h08 NUMERIC(8, 6) NOT NULL, ADD h09 NUMERIC(8, 6) NOT NULL, ADD h10 NUMERIC(8, 6) NOT NULL, ADD h11 NUMERIC(8, 6) NOT NULL, ADD h12 NUMERIC(8, 6) NOT NULL, ADD h13 NUMERIC(8, 6) NOT NULL, ADD h14 NUMERIC(8, 6) NOT NULL, ADD h15 NUMERIC(8, 6) NOT NULL, ADD h16 NUMERIC(8, 6) NOT NULL, ADD h17 NUMERIC(8, 6) NOT NULL, ADD h18 NUMERIC(8, 6) NOT NULL, ADD h19 NUMERIC(8, 6) NOT NULL, ADD h20 NUMERIC(8, 6) NOT NULL, ADD h21 NUMERIC(8, 6) NOT NULL, ADD h22 NUMERIC(8, 6) NOT NULL, ADD h23 NUMERIC(8, 6) NOT NULL, DROP hour_start, DROP hour_end, CHANGE price h00 NUMERIC(8, 6) NOT NULL');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE prices ADD hour_start INT NOT NULL, ADD hour_end INT NOT NULL, ADD price NUMERIC(8, 6) NOT NULL, DROP h00, DROP h01, DROP h02, DROP h03, DROP h04, DROP h05, DROP h06, DROP h07, DROP h08, DROP h09, DROP h10, DROP h11, DROP h12, DROP h13, DROP h14, DROP h15, DROP h16, DROP h17, DROP h18, DROP h19, DROP h20, DROP h21, DROP h22, DROP h23');
}
}

View File

@ -17,10 +17,10 @@ class PriceNowProvider implements CollectionDataProviderInterface, RestrictedDat
$this->doctrine = $doctrine;
}
private function getPriceToday(): Prices {
private function getPriceToday(): ?Prices {
$em = $this->doctrine->getManager();
return $em->getRepository(Prices::class)->findOneBy(array(
'day' => new \DateTime('now')
'day' => new \DateTime('today')
));
}
private function setPriceToday(Prices $price):bool {
@ -34,63 +34,63 @@ class PriceNowProvider implements CollectionDataProviderInterface, RestrictedDat
private function getPriceHour(string $hh, Prices $price):float
{
switch($hh) {
case '00': return $priceToday->h00;
case '01': return $priceToday->h01;
case '02': return $priceToday->h02;
case '03': return $priceToday->h03;
case '04': return $priceToday->h04;
case '05': return $priceToday->h05;
case '06': return $priceToday->h06;
case '07': return $priceToday->h07;
case '08': return $priceToday->h08;
case '09': return $priceToday->h09;
case '10': return $priceToday->h10;
case '11': return $priceToday->h11;
case '12': return $priceToday->h12;
case '13': return $priceToday->h13;
case '14': return $priceToday->h14;
case '15': return $priceToday->h15;
case '16': return $priceToday->h16;
case '17': return $priceToday->h17;
case '18': return $priceToday->h18;
case '19': return $priceToday->h19;
case '20': return $priceToday->h20;
case '21': return $priceToday->h21;
case '22': return $priceToday->h22;
case '23': return $priceToday->h23;
case '00': return $price->h00;
case '01': return $price->h01;
case '02': return $price->h02;
case '03': return $price->h03;
case '04': return $price->h04;
case '05': return $price->h05;
case '06': return $price->h06;
case '07': return $price->h07;
case '08': return $price->h08;
case '09': return $price->h09;
case '10': return $price->h10;
case '11': return $price->h11;
case '12': return $price->h12;
case '13': return $price->h13;
case '14': return $price->h14;
case '15': return $price->h15;
case '16': return $price->h16;
case '17': return $price->h17;
case '18': return $price->h18;
case '19': return $price->h19;
case '20': return $price->h20;
case '21': return $price->h21;
case '22': return $price->h22;
case '23': return $price->h23;
}
return -1;
}
private function setPriceHour(string $hh, Prices $price):void
private function setPriceHour(string $hh, float $price, Prices $priceEntity):void
{
switch($hh) {
case '00': $priceToday->h00 = $p; break;
case '01': $priceToday->h01 = $p; break;
case '02': $priceToday->h02 = $p; break;
case '03': $priceToday->h03 = $p; break;
case '04': $priceToday->h04 = $p; break;
case '05': $priceToday->h05 = $p; break;
case '06': $priceToday->h06 = $p; break;
case '07': $priceToday->h07 = $p; break;
case '08': $priceToday->h08 = $p; break;
case '09': $priceToday->h09 = $p; break;
case '10': $priceToday->h10 = $p; break;
case '11': $priceToday->h11 = $p; break;
case '12': $priceToday->h12 = $p; break;
case '13': $priceToday->h13 = $p; break;
case '14': $priceToday->h14 = $p; break;
case '15': $priceToday->h15 = $p; break;
case '16': $priceToday->h16 = $p; break;
case '17': $priceToday->h17 = $p; break;
case '18': $priceToday->h18 = $p; break;
case '19': $priceToday->h19 = $p; break;
case '20': $priceToday->h20 = $p; break;
case '21': $priceToday->h21 = $p; break;
case '22': $priceToday->h22 = $p; break;
case '23': $priceToday->h23 = $p; break;
case '00': $priceEntity->h00 = $price; break;
case '01': $priceEntity->h01 = $price; break;
case '02': $priceEntity->h02 = $price; break;
case '03': $priceEntity->h03 = $price; break;
case '04': $priceEntity->h04 = $price; break;
case '05': $priceEntity->h05 = $price; break;
case '06': $priceEntity->h06 = $price; break;
case '07': $priceEntity->h07 = $price; break;
case '08': $priceEntity->h08 = $price; break;
case '09': $priceEntity->h09 = $price; break;
case '10': $priceEntity->h10 = $price; break;
case '11': $priceEntity->h11 = $price; break;
case '12': $priceEntity->h12 = $price; break;
case '13': $priceEntity->h13 = $price; break;
case '14': $priceEntity->h14 = $price; break;
case '15': $priceEntity->h15 = $price; break;
case '16': $priceEntity->h16 = $price; break;
case '17': $priceEntity->h17 = $price; break;
case '18': $priceEntity->h18 = $price; break;
case '19': $priceEntity->h19 = $price; break;
case '20': $priceEntity->h20 = $price; break;
case '21': $priceEntity->h21 = $price; break;
case '22': $priceEntity->h22 = $price; break;
case '23': $priceEntity->h23 = $price; break;
}
}
@ -106,65 +106,35 @@ class PriceNowProvider implements CollectionDataProviderInterface, RestrictedDat
*/
$priceToday = $this->getPriceToday();
echo print_r($priceToday,true);
exit;
if ( $priceToday->id<=0 ) {
if ( is_null($priceToday) ) {
die('Internet');
// Get the today's price (from internet)
$url = 'https://api.esios.ree.es/archives/70/download_json?locale=es&date=2022-01-12';
$url = 'https://api.esios.ree.es/archives/70/download_json?locale=es&date='.date('Y-m-d');
$data = file_get_contents($url);
$json = json_decode($data, true);
if ( !array_key_exists('PVPC', $json) )
throw new Exception('Unable to get the prices for today');
$ddMMyyy = explode('/',$pvpc['Dia'])[0];
$priceToday = new Prices();
$ddMMyyy = explode('/',$json['PVPC'][0]['Dia']);
$priceToday->day = new \DateTime($ddMMyyy[2].'/'.$ddMMyyy[1].'/'.$ddMMyyy[0].' 00:00:00');
foreach( $json['PVPC'] as $pvpc ) {
$hh = explode('-',$pvpc['Hora'])[0];
$p = str_replace(',','.',$pvpc['PCB']) / 1000;
$price = str_replace(',','.',$pvpc['PCB']) / 1000;
$this->setPriceHour($hh, $priceNow);
/*
switch($hh) {
case '00': $priceToday->h00 = $p; break;
case '01': $priceToday->h01 = $p; break;
case '02': $priceToday->h02 = $p; break;
case '03': $priceToday->h03 = $p; break;
case '04': $priceToday->h04 = $p; break;
case '05': $priceToday->h05 = $p; break;
case '06': $priceToday->h06 = $p; break;
case '07': $priceToday->h07 = $p; break;
case '08': $priceToday->h08 = $p; break;
case '09': $priceToday->h09 = $p; break;
case '10': $priceToday->h10 = $p; break;
case '11': $priceToday->h11 = $p; break;
case '12': $priceToday->h12 = $p; break;
case '13': $priceToday->h13 = $p; break;
case '14': $priceToday->h14 = $p; break;
case '15': $priceToday->h15 = $p; break;
case '16': $priceToday->h16 = $p; break;
case '17': $priceToday->h17 = $p; break;
case '18': $priceToday->h18 = $p; break;
case '19': $priceToday->h19 = $p; break;
case '20': $priceToday->h20 = $p; break;
case '21': $priceToday->h21 = $p; break;
case '22': $priceToday->h22 = $p; break;
case '23': $priceToday->h23 = $p; break;
}
*/
//echo "\n $dd $hh = $p";
$this->setPriceHour($hh, $price, $priceToday);
}
$this->setPriceToday($priceToday);
}
$priceNow = new PriceNow;
$priceNow->id = $priceToday->id;
$priceNow = new PriceNow();
$priceNow->id = $priceToday->getId();
$priceNow->min = min($priceToday->h00,$priceToday->h01,$priceToday->h02,$priceToday->h03,$priceToday->h04,$priceToday->h05,$priceToday->h06,$priceToday->h07,$priceToday->h08,$priceToday->h09,$priceToday->h10,$priceToday->h11,$priceToday->h12,$priceToday->h13,$priceToday->h14,$priceToday->h15,$priceToday->h16,$priceToday->h17,$priceToday->h18,$priceToday->h19,$priceToday->h20,$priceToday->h21,$priceToday->h22,$priceToday->h23);
$priceNow->max = max($priceToday->h00,$priceToday->h01,$priceToday->h02,$priceToday->h03,$priceToday->h04,$priceToday->h05,$priceToday->h06,$priceToday->h07,$priceToday->h08,$priceToday->h09,$priceToday->h10,$priceToday->h11,$priceToday->h12,$priceToday->h13,$priceToday->h14,$priceToday->h15,$priceToday->h16,$priceToday->h17,$priceToday->h18,$priceToday->h19,$priceToday->h20,$priceToday->h21,$priceToday->h22,$priceToday->h23);
$priceNow->avg = ($priceToday->h00+$priceToday->h01+$priceToday->h02+$priceToday->h03+$priceToday->h04+$priceToday->h05+$priceToday->h06+$priceToday->h07+$priceToday->h08+$priceToday->h09+$priceToday->h10+$priceToday->h11+$priceToday->h12+$priceToday->h13+$priceToday->h14+$priceToday->h15+$priceToday->h16+$priceToday->h17+$priceToday->h18+$priceToday->h19+$priceToday->h20+$priceToday->h21+$priceToday->h22+$priceToday->h23)/24;
$priceNow->now = $this->getPriceHour( Date('H'), $priceNow);
$priceNow->now = $this->getPriceHour( Date('H'), $priceToday);
// print_r($priceNow);exit;
return [$priceNow];
}

View File

@ -8,6 +8,24 @@ use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Action\NotFoundAction;
/*
* itemOperations={
* "get"={
* "method"="GET",
* "controller"=NotFoundAction::class,
* "read"=false,
* "output"=false,
* "path"="/priceNow/{id}",
* },
* },
* itemOperations={},
* collectionOperations={"get"={
* "path"="/priceNow",
* }
* }
* )
*/
/**
* @ApiResource(
* itemOperations={
@ -16,9 +34,13 @@ use ApiPlatform\Core\Action\NotFoundAction;
* "controller"=NotFoundAction::class,
* "read"=false,
* "output"=false,
* "path"="/priceNow/{id}",
* },
* },
* collectionOperations={"get"}
* collectionOperations={"get"={
* "path"="/priceNow",
* }
* }
* )
*
*/
@ -29,8 +51,8 @@ class PriceNow
*/
public ?int $id = null;
public ?int $min = null;
public ?int $max = null;
public ?int $avg = null;
public ?int $now = null;
public ?float $min = null;
public ?float $max = null;
public ?float $avg = null;
public ?float $now = null;
}

View File

@ -34,28 +34,97 @@ class Prices
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h00 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h01 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h02 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h03 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h04 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h05 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h06 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h07 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h08 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h09 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h10 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h11 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h12 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h13 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h14 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h15 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h16 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h17 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h18 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h19 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h20 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h21 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h22 = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
*/
public ?float $h23 = null;

View File

@ -117,6 +117,9 @@
"config/packages/nelmio_cors.yaml"
]
},
"nikic/php-parser": {
"version": "v4.13.2"
},
"phpdocumentor/reflection-common": {
"version": "2.2.0"
},
@ -235,6 +238,15 @@
"symfony/http-kernel": {
"version": "v5.4.2"
},
"symfony/maker-bundle": {
"version": "1.36",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "1.0",
"ref": "fadbfe33303a76e25cb63401050439aa9b1a9c7f"
}
},
"symfony/password-hasher": {
"version": "v5.4.2"
},