Backend coins and update
This commit is contained in:
@ -63,4 +63,108 @@ class Coins {
|
|||||||
$output['deleted'] = $status;
|
$output['deleted'] = $status;
|
||||||
return $this->withJson($response, $output);
|
return $this->withJson($response, $output);
|
||||||
}
|
}
|
||||||
|
public function values(Request $request, Response $response, ModelFactory $factory, $coin_id): Response {
|
||||||
|
$coin = $factory->find(Coin::class)->one($coin_id);
|
||||||
|
if (!$coin) {
|
||||||
|
return $this->withJson($response, ['coin' => null, 'values' => []]);
|
||||||
|
}
|
||||||
|
$values = $coin->values();
|
||||||
|
if ($values === null) {
|
||||||
|
return $this->withJson($response, [
|
||||||
|
'coin' => $coin->toArray(),
|
||||||
|
'values' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
usort($values, function($a, $b) {
|
||||||
|
return $a->dateTime()->timestamp - $b->dateTime()->timestamp;
|
||||||
|
});
|
||||||
|
$values = array_map(function($item) {
|
||||||
|
$arr = $item->toArray();
|
||||||
|
$arr['formatted'] = $item->unit()->format($item->value);
|
||||||
|
return $arr;
|
||||||
|
}, $values);
|
||||||
|
$output = [
|
||||||
|
'coin' => $coin->toArray(),
|
||||||
|
'values' => $values
|
||||||
|
];
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
public function valuesMonth(Request $request, Response $response, ModelFactory $factory, $coin_id): Response {
|
||||||
|
$coin = $factory->find(Coin::class)->one($coin_id);
|
||||||
|
if (!$coin) {
|
||||||
|
return $this->withJson($response, ['coin' => null, 'values' => []]);
|
||||||
|
}
|
||||||
|
$values = $coin->values('month');
|
||||||
|
if ($values === null) {
|
||||||
|
return $this->withJson($response, [
|
||||||
|
'coin' => $coin->toArray(),
|
||||||
|
'values' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
usort($values, function($a, $b) {
|
||||||
|
return $a->dateTime()->timestamp - $b->dateTime()->timestamp;
|
||||||
|
});
|
||||||
|
$values = array_map(function($item) {
|
||||||
|
$arr = $item->toArray();
|
||||||
|
$arr['formatted'] = $item->unit()->format($item->value);
|
||||||
|
return $arr;
|
||||||
|
}, $values);
|
||||||
|
$output = [
|
||||||
|
'coin' => $coin->toArray(),
|
||||||
|
'values' => $values
|
||||||
|
];
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
public function valuesSixMonths(Request $request, Response $response, ModelFactory $factory, $coin_id): Response {
|
||||||
|
$coin = $factory->find(Coin::class)->one($coin_id);
|
||||||
|
if (!$coin) {
|
||||||
|
return $this->withJson($response, ['coin' => null, 'values' => []]);
|
||||||
|
}
|
||||||
|
$values = $coin->values('months');
|
||||||
|
if ($values === null) {
|
||||||
|
return $this->withJson($response, [
|
||||||
|
'coin' => $coin->toArray(),
|
||||||
|
'values' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
usort($values, function($a, $b) {
|
||||||
|
return $a->dateTime()->timestamp - $b->dateTime()->timestamp;
|
||||||
|
});
|
||||||
|
$values = array_map(function($item) {
|
||||||
|
$arr = $item->toArray();
|
||||||
|
$arr['formatted'] = $item->unit()->format($item->value);
|
||||||
|
return $arr;
|
||||||
|
}, $values);
|
||||||
|
$output = [
|
||||||
|
'coin' => $coin->toArray(),
|
||||||
|
'values' => $values
|
||||||
|
];
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
public function valuesYear(Request $request, Response $response, ModelFactory $factory, $coin_id): Response {
|
||||||
|
$coin = $factory->find(Coin::class)->one($coin_id);
|
||||||
|
if (!$coin) {
|
||||||
|
return $this->withJson($response, ['coin' => null, 'values' => []]);
|
||||||
|
}
|
||||||
|
$values = $coin->values('year');
|
||||||
|
if ($values === null) {
|
||||||
|
return $this->withJson($response, [
|
||||||
|
'coin' => $coin->toArray(),
|
||||||
|
'values' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
usort($values, function($a, $b) {
|
||||||
|
return $a->dateTime()->timestamp - $b->dateTime()->timestamp;
|
||||||
|
});
|
||||||
|
$values = array_map(function($item) {
|
||||||
|
$arr = $item->toArray();
|
||||||
|
$arr['formatted'] = $item->unit()->format($item->value);
|
||||||
|
return $arr;
|
||||||
|
}, $values);
|
||||||
|
$output = [
|
||||||
|
'coin' => $coin->toArray(),
|
||||||
|
'values' => $values
|
||||||
|
];
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,18 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Crypto\Common\Service;
|
namespace ProVM\Crypto\Common\Service;
|
||||||
|
|
||||||
|
use GuzzleHttp\Client;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use ProVM\Common\Factory\Model as Factory;
|
use ProVM\Common\Factory\Model as Factory;
|
||||||
use ProVM\Crypto\Coin;
|
use ProVM\Crypto\Coin;
|
||||||
|
use ProVM\Crypto\Value;
|
||||||
|
|
||||||
class Update {
|
class Update {
|
||||||
protected $factory;
|
protected $factory;
|
||||||
protected $execs;
|
protected $client;
|
||||||
public function __construct(Factory $factory, array $executables) {
|
public function __construct(Factory $factory, Client $client) {
|
||||||
$this->factory = $factory;
|
$this->factory = $factory;
|
||||||
$this->execs = $executables;
|
$this->client = $client;
|
||||||
$this->load();
|
$this->load();
|
||||||
}
|
}
|
||||||
public function load() {
|
public function load() {
|
||||||
@ -22,39 +24,154 @@ class Update {
|
|||||||
}
|
}
|
||||||
protected $coins;
|
protected $coins;
|
||||||
public function register(int $coin_id, int $type = 0) {
|
public function register(int $coin_id, int $type = 0) {
|
||||||
/*if (array_search($coin_id, $this->coins[$type]) !== false) {
|
if (array_search($coin_id, $this->coins[$type]) !== false) {
|
||||||
return;
|
return false;
|
||||||
}*/
|
}
|
||||||
$this->coins[$type] []= $coin_id;
|
$this->coins[$type] []= $coin_id;
|
||||||
$this->getHistorical($coin_id, $type);
|
$this->getHistorical($coin_id, $type);
|
||||||
$check = \ORM::for_table('coin_registers')->where('coin_id', $coin_id)->find_one();
|
$check = \ORM::for_table('coin_registers')->where('coin_id', $coin_id)->find_one();
|
||||||
if (!$check) {
|
if (!$check) {
|
||||||
\ORM::raw_execute("INSERT INTO coin_registers (coin_id, type) VALUES (?, ?)", [$coin_id, $type]);
|
return \ORM::raw_execute("INSERT INTO coin_registers (coin_id, type, date_Time) VALUES (?, ?, NOW())", [$coin_id, $type]);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
protected function getHistorical(int $coin_id, int $type) {
|
protected function getHistorical(int $coin_id, int $type) {
|
||||||
$coin = $this->factory->find(Coin::class)->one($coin_id);
|
$coin = $this->factory->find(Coin::class)->one($coin_id);
|
||||||
$f = Carbon::now();
|
$f = Carbon::now();
|
||||||
$exe = [$this->execs[$type]];
|
$url = [];
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
case 0:
|
case 0:
|
||||||
$exe []= '-i ' . $coin->identifier;
|
return $this->getHistoricalCrypto($coin);
|
||||||
$exe []= '-c usd,clp';
|
|
||||||
$exe []= 'hist -hi';
|
|
||||||
$exe []= '-f ' . $f->copy()->subYears(10)->timestamp;
|
|
||||||
$exe []= '-t ' . $f->timestamp();
|
|
||||||
break;
|
|
||||||
case 1:
|
case 1:
|
||||||
$exe []= '-i ' . $coin->identifier;
|
return $this->getHistoricalIndicador($coin);
|
||||||
$exe []= 'hist -hi';
|
default:
|
||||||
$exe []= '-s ' . $f->copy()->subYears(10)->year;
|
return false;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
!d(implode(' ', $exe));
|
}
|
||||||
$output = shell_exec(implode(' ', $exe));
|
protected function getHistoricalCrypto(Coin $coin) {
|
||||||
!d($output);
|
$f = Carbon::now();
|
||||||
|
$url = [];
|
||||||
|
$url []= 'crypto';
|
||||||
|
$url []= 'historical';
|
||||||
|
$url []= $coin->identifier;
|
||||||
|
$url []= $f->copy()->subYears(10)->timestamp;
|
||||||
|
$url []= $f->timestamp;
|
||||||
|
$url = implode('/', $url);
|
||||||
|
$to = $this->factory->find(Coin::class)->where([['code', 'USD']])->one();
|
||||||
|
$response = $this->client->get($url);
|
||||||
|
$results = json_decode($response->getBody()->getContents());
|
||||||
|
$created = [];
|
||||||
|
foreach ($results->prices as $result) {
|
||||||
|
$d = Carbon::createFromTimestamp(substr($result[0], 0, -3));
|
||||||
|
$value = $result[1];
|
||||||
|
$data = [
|
||||||
|
'date_time' => $d->format('Y-m-d H:i:s'),
|
||||||
|
'coin_id' => $coin->id,
|
||||||
|
'value' => $value,
|
||||||
|
'unit_id' => $to->id
|
||||||
|
];
|
||||||
|
$value = Value::add($this->factory, $data);
|
||||||
|
$status = $value->save();
|
||||||
|
$created []= [
|
||||||
|
'value' => $value->toArray(),
|
||||||
|
'created' => $status
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return $created;
|
||||||
|
}
|
||||||
|
protected function getHistoricalIndicador(Coin $coin) {
|
||||||
|
$f = Carbon::now();
|
||||||
|
$urls = [];
|
||||||
|
for ($i = 10; $i >= 0; $i --) {
|
||||||
|
$url = [];
|
||||||
|
$url []= 'indicador';
|
||||||
|
$url []= 'historical';
|
||||||
|
$url []= $coin->identifier;
|
||||||
|
$url []= $f->copy()->subYears($i)->year;
|
||||||
|
$urls []= implode('/', $url);
|
||||||
|
}
|
||||||
|
$to = $this->factory->find(Coin::class)->where([['code', 'CLP']])->one();
|
||||||
|
$created = [];
|
||||||
|
foreach ($urls as $url) {
|
||||||
|
$response = $this->client->get($url);
|
||||||
|
$results = json_decode($response->getBody()->getContents());
|
||||||
|
foreach ($results->serie as $result) {
|
||||||
|
$d = Carbon::parse($result->fecha);
|
||||||
|
$value = $result->valor;
|
||||||
|
$data = [
|
||||||
|
'date_time' => $d->format('Y-m-d H:i:s'),
|
||||||
|
'coin_id' => $coin->id,
|
||||||
|
'value' => $value,
|
||||||
|
'unit_id' => $to->id
|
||||||
|
];
|
||||||
|
$value = Value::add($this->factory, $data);
|
||||||
|
$status = $value->save();
|
||||||
|
$created []= [
|
||||||
|
'value' => $value->toArray(),
|
||||||
|
'created' => $status
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $created;
|
||||||
}
|
}
|
||||||
public function run() {
|
public function run() {
|
||||||
|
$created = [];
|
||||||
|
$created = array_merge($created, $this->getCryptos($this->coins[0]));
|
||||||
|
$created = array_merge($created, $this->getIndicadores($this->coins[1]));
|
||||||
|
return $created;
|
||||||
|
}
|
||||||
|
protected function getCryptos(array $coins) {
|
||||||
|
$created = [];
|
||||||
|
foreach ($coins as $coin_id) {
|
||||||
|
$coin = $this->factory->find(Coin::class)->one($coin_id);
|
||||||
|
$url = [];
|
||||||
|
$url []= 'crypto';
|
||||||
|
$url []= $coin->identifier;
|
||||||
|
$url = implode('/', $url);
|
||||||
|
$to = $this->factory->find(Coin::class)->where([['code', 'USD']])->one();
|
||||||
|
$response = $this->client->get($url);
|
||||||
|
$results = json_decode($response->getBody()->getContents());
|
||||||
|
$data = [
|
||||||
|
'date_time' => $results->n->last_updated_at,
|
||||||
|
'coin_id' => $coin->id,
|
||||||
|
'value' => 1 / $results->n->usd,
|
||||||
|
'unit_id' => $to->id
|
||||||
|
];
|
||||||
|
$value = Value::add($this->factory, $data);
|
||||||
|
$status = $value->save();
|
||||||
|
$created []= [
|
||||||
|
'value' => $value->toArray(),
|
||||||
|
'created' => $status
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return $created;
|
||||||
|
}
|
||||||
|
protected function getIndicadores(array $coins) {
|
||||||
|
$created = [];
|
||||||
|
foreach ($coins as $coin_id) {
|
||||||
|
$coin = $this->factory->find(Coin::class)->one($coin_id);
|
||||||
|
$f = Carbon::now();
|
||||||
|
$url = [];
|
||||||
|
$url []= 'indicador';
|
||||||
|
$url []= $coin->identifier;
|
||||||
|
$url []= $f->format('d-m-Y');
|
||||||
|
$url = implode('/', $url);
|
||||||
|
$to = $this->factory->find(Coin::class)->where([['code', 'CLP']])->one();
|
||||||
|
$response = $this->client->get($url);
|
||||||
|
$results = json_decode($response->getBody()->getContents());
|
||||||
|
$data = [
|
||||||
|
'date_time' => $results->serie[0]->fecha,
|
||||||
|
'coin_id' => $coin->id,
|
||||||
|
'value' => $results->serie[0]->valor,
|
||||||
|
'unit_id' => $to->id
|
||||||
|
];
|
||||||
|
$value = Value::add($this->factory, $data);
|
||||||
|
$status = $value->save();
|
||||||
|
$created []= [
|
||||||
|
'value' => $value->toArray(),
|
||||||
|
'created' => $status
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return $created;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,8 @@
|
|||||||
"provm/models": "^1.0-rc",
|
"provm/models": "^1.0-rc",
|
||||||
"spatie/crypto": "^2.0",
|
"spatie/crypto": "^2.0",
|
||||||
"robmorgan/phinx": "^0.12.5",
|
"robmorgan/phinx": "^0.12.5",
|
||||||
"nesbot/carbon": "^2.49"
|
"nesbot/carbon": "^2.49",
|
||||||
|
"guzzlehttp/guzzle": "^7.3"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^9.5",
|
"phpunit/phpunit": "^9.5",
|
||||||
|
@ -9,5 +9,11 @@ $app->group('/coins', function($app) {
|
|||||||
$app->group('/coin/{coin_id}', function($app) {
|
$app->group('/coin/{coin_id}', function($app) {
|
||||||
$app->put('/edit', [Coins::class, 'edit']);
|
$app->put('/edit', [Coins::class, 'edit']);
|
||||||
$app->delete('/delete', [Coins::class, 'delete']);
|
$app->delete('/delete', [Coins::class, 'delete']);
|
||||||
|
$app->group('/values', function($app) {
|
||||||
|
$app->get('/month', [Coins::class, 'valuesMonth']);
|
||||||
|
$app->get('/months', [Coins::class, 'valuesSixMonths']);
|
||||||
|
$app->get('/year', [Coins::class, 'valuesYear']);
|
||||||
|
$app->get('[/]', [Coins::class, 'values']);
|
||||||
|
});
|
||||||
$app->get('[/]', [Coins::class, 'show']);
|
$app->get('[/]', [Coins::class, 'show']);
|
||||||
});
|
});
|
||||||
|
@ -23,16 +23,5 @@ return [
|
|||||||
]);
|
]);
|
||||||
return (object) $arr;
|
return (object) $arr;
|
||||||
},
|
},
|
||||||
'coingecko' => function(Container $c) {
|
'python_api' => $_ENV['PYTHON_API']
|
||||||
return implode(DIRECTORY_SEPARATOR, [
|
|
||||||
$c->get('locations')->bin,
|
|
||||||
'coingecko'
|
|
||||||
]);
|
|
||||||
},
|
|
||||||
'mindicador' => function(Container $c) {
|
|
||||||
return implode(DIRECTORY_SEPARATOR, [
|
|
||||||
$c->get('locations')->bin,
|
|
||||||
'mindicador'
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
];
|
];
|
||||||
|
@ -12,13 +12,13 @@ return [
|
|||||||
ProVM\Common\Factory\Model::class => function(Container $container) {
|
ProVM\Common\Factory\Model::class => function(Container $container) {
|
||||||
return new ProVM\Crypto\Common\Factory\Model();
|
return new ProVM\Crypto\Common\Factory\Model();
|
||||||
},
|
},
|
||||||
|
GuzzleHttp\Client::class => function(Container $container) {
|
||||||
|
return new GuzzleHttp\Client(['base_uri' => $container->get('python_api')]);
|
||||||
|
},
|
||||||
ProVM\Crypto\Common\Service\Update::class => function(Container $container) {
|
ProVM\Crypto\Common\Service\Update::class => function(Container $container) {
|
||||||
return new ProVM\Crypto\Common\Service\Update(
|
return new ProVM\Crypto\Common\Service\Update(
|
||||||
$container->get(ProVM\Crypto\Common\Factory\Model::class),
|
$container->get(ProVM\Crypto\Common\Factory\Model::class),
|
||||||
[
|
$container->get(GuzzleHttp\Client::class)
|
||||||
$container->get('coingecko'),
|
|
||||||
$container->get('mindicador')
|
|
||||||
]
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Crypto;
|
namespace ProVM\Crypto;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
use ProVM\Common\Alias\Model;
|
use ProVM\Common\Alias\Model;
|
||||||
use ProVM\Common\Factory\Model as Factory;
|
use ProVM\Common\Factory\Model as Factory;
|
||||||
|
|
||||||
@ -16,19 +17,52 @@ use ProVM\Common\Factory\Model as Factory;
|
|||||||
*/
|
*/
|
||||||
class Coin extends Model {
|
class Coin extends Model {
|
||||||
protected static $_table = 'coins';
|
protected static $_table = 'coins';
|
||||||
protected static $fields = ['code', 'name', 'prefix', 'suffix', 'decimals', 'ref_url'];
|
protected static $fields = ['code', 'name', 'identifier', 'prefix', 'suffix', 'decimals', 'ref_url'];
|
||||||
|
|
||||||
public function format(float $value): string {
|
public function format(float $value): string {
|
||||||
$output = [];
|
$output = [];
|
||||||
if ($this->prefix == '') {
|
if ($this->prefix != '') {
|
||||||
$output []= $this->prefix;
|
$output []= $this->prefix;
|
||||||
}
|
}
|
||||||
$output []= number_format($value, $this->decimals ?? 0, ',', '.');
|
$output []= number_format($value, $this->decimals ?? 0, ',', '.');
|
||||||
if ($this->suffix == '') {
|
if ($this->suffix != '') {
|
||||||
$output []= $this->suffix;
|
$output []= $this->suffix;
|
||||||
}
|
}
|
||||||
return implode(' ', $output);
|
return implode(' ', $output);
|
||||||
}
|
}
|
||||||
|
protected $values;
|
||||||
|
public function values($period = null) {
|
||||||
|
if ($this->values === null) {
|
||||||
|
$this->values = $this->parentOf(Value::class, [Model::CHILD_KEY => 'coin_id']);
|
||||||
|
}
|
||||||
|
if ($this->values === null) {
|
||||||
|
return $this->values;
|
||||||
|
}
|
||||||
|
if ($period === null) {
|
||||||
|
return $this->values;
|
||||||
|
}
|
||||||
|
$f = Carbon::now();
|
||||||
|
switch ($period) {
|
||||||
|
case 'month':
|
||||||
|
case 'mes':
|
||||||
|
$m = $f->copy()->subMonths(1);
|
||||||
|
return array_filter($this->values, function($item) use ($m) {
|
||||||
|
return ($item->dateTime()->greaterThanOrEqualTo($m));
|
||||||
|
});
|
||||||
|
case 'months':
|
||||||
|
case 'meses':
|
||||||
|
$m = $f->copy()->subMonths(6);
|
||||||
|
return array_filter($this->values, function($item) use ($m) {
|
||||||
|
return ($item->dateTime()->greaterThanOrEqualTo($m));
|
||||||
|
});
|
||||||
|
case 'year':
|
||||||
|
case 'año':
|
||||||
|
$m = $f->copy()->subYears(1);
|
||||||
|
return array_filter($this->values, function($item) use ($m) {
|
||||||
|
return ($item->dateTime()->greaterThanOrEqualTo($m));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static function find(Factory $factory, $input) {
|
public static function find(Factory $factory, $input) {
|
||||||
return $factory->find(Coin::class)->where([['code', $input->code]])->one();
|
return $factory->find(Coin::class)->where([['code', $input->code]])->one();
|
||||||
|
Reference in New Issue
Block a user