Compare commits

...

4 Commits

Author SHA1 Message Date
5129cca099 Automatizado 2021-04-12 00:43:39 -04:00
35bcbd1979 Update 2021-04-12 00:43:27 -04:00
fef167c46e Uso de Websocket en mirar moneda 2021-04-12 00:41:37 -04:00
29d04ac4ad Sources 2021-04-12 00:38:51 -04:00
16 changed files with 258 additions and 39 deletions

View File

@ -15,10 +15,10 @@ class Sources {
$output = compact('sources'); $output = compact('sources');
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
public function get(Request $request, Response $response, ModelFactory $factory, $currency_id, $url): Response { public function get(Request $request, Response $response, ModelFactory $factory, $source_id): Response {
$source = $factory->find(Source::class)->where([['currency_id', $currency_id], ['url', $url]])->one(); $source = $factory->find(Source::class)->one($source_id);
$output = [ $output = [
'get_data' => compact('currency_id', 'url'), 'get_data' => compact('source_id'),
'source' => null 'source' => null
]; ];
if ($source) { if ($source) {
@ -45,13 +45,13 @@ class Sources {
]; ];
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
public function edit(Request $request, Response $response, ModelFactory $factory, $currency_id, $url) { public function edit(Request $request, Response $response, ModelFactory $factory, $source_id) {
$post = json_decode($request->getBody()->getContents()); $post = json_decode($request->getBody()->getContents());
$output = [ $output = [
'get_data' => compact('currency_id', 'url'), 'get_data' => compact('source_id'),
'post_data' => $post 'post_data' => $post
]; ];
$source = $factory->find(Source::class)->where([['currency_id', $currency_id], ['url', $url]])->one(); $source = $factory->find(Source::class)->one($source_id);
$edited = false; $edited = false;
if ($source) { if ($source) {
$edited = $source->edit($post); $edited = $source->edit($post);
@ -60,9 +60,9 @@ class Sources {
} }
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
public function delete(Request $request, Response $response, ModelFactory $factory, $currency_id, $url): Response { public function delete(Request $request, Response $response, ModelFactory $factory, $source_id): Response {
$source = $factory->find(Source::class)->where([['currency_id', $currency_id], ['url', $url]])->one(); $source = $factory->find(Source::class)->one($source_id);
$output = ['get_data' => compact('currency_id', 'url'), 'source' => null, 'deleted' => false]; $output = ['get_data' => compact('source_id'), 'source' => null, 'deleted' => false];
if ($source) { if ($source) {
$output['source'] = $source->asArray(); $output['source'] = $source->asArray();
$status = $source->delete(); $status = $source->delete();

View File

@ -0,0 +1,86 @@
<?php
namespace ProVM\Money\Common\Controller;
use Psr\Container\ContainerInterface as Container;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use GuzzleHttp\ClientInterface as Client;
use Carbon\Carbon;
use ProVM\Common\Define\Controller\Json;
use ProVM\Common\Factory\Model as ModelFactory;
use ProVM\Money\Currency;
use ProVM\Money\Source;
use ProVM\Money\Value;
class Update {
use Json;
protected function get(Client $client, string $url, bool $exception = true) {
$res = $client->get($url);
if ($res->getStatusCode() < 200 or $res->getStatusCode() >= 300) {
if ($exception) {
throw new \Exception('Url ' . $url . ' not connected.');
}
return false;
}
return json_decode($res->getBody());
}
protected function baseMap($unit) {
$map = [
'Dólar' => 'US Dollar',
'Pesos' => 'Peso Chileno'
];
return $map[$unit] ?? $unit;
}
public function __invoke(Request $request, Response $response, Client $client, ModelFactory $factory, Container $container): Response {
ini_set('max_execution_time', 300);
$sources = $factory->find(Source::class)->many();
$date = Carbon::now();
$output = ['count' => 0, 'values' => []];
foreach ($sources as $source) {
$url = str_replace([
'{year}',
'{month}',
'{day}',
'{hour}',
'{minute}',
'{second}'
], [
$date->year,
$date->month,
$date->day,
$date->hour,
$date->minute,
$date->second
], $source->url);
$b = $this->get($client, $url, false);
if ($b === false) {
continue;
}
$base = $factory->find(Currency::class)->where([
['name', '%' . $this->baseMap($b->unidad_medida) . '%', 'like']
])->one();
if (!$base) {
continue;
}
foreach ($b->serie as $info) {
$f = Carbon::parse($info->fecha);
$data = [
'currency_id' => $source->currency()->id,
'date_time' => $f->format('Y-m-d H:i:s'),
'value' => $info->valor,
'base_id' => $base->id
];
$result = Value::add($factory, $data);
$output['values'] []= $result;
if ($result->created === true) {
$output['count'] ++;
}
}
}
return $this->withJson($response, $output);
}
}

View File

@ -10,6 +10,7 @@
"provm/models": "dev-master", "provm/models": "dev-master",
"provm/controller": "dev-master", "provm/controller": "dev-master",
"vlucas/phpdotenv": "^5.3", "vlucas/phpdotenv": "^5.3",
"guzzlehttp/guzzle": "^7.3",
"nesbot/carbon": "^2.46" "nesbot/carbon": "^2.46"
}, },
"license": "MIT", "license": "MIT",

View File

@ -13,6 +13,7 @@ server {
add_header 'Access-Control-Allow-Origin' 'http://localhost:8080'; add_header 'Access-Control-Allow-Origin' 'http://localhost:8080';
add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS'; add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS';
add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept"; add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
add_header 'Content-Type' 'application/json';
location ~ \.php { location ~ \.php {
try_files $uri =404; try_files $uri =404;

View File

@ -1,9 +1,16 @@
<?php <?php
use ProVM\Money\Common\Controller\API; use ProVM\Money\Common\Controller\API;
include_once 'currencies.php'; $files = new DirectoryIterator(implode(DIRECTORY_SEPARATOR, [
include_once 'values.php'; __DIR__,
include_once 'sources.php'; 'api'
]));
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
include_once $file->getRealPath();
}
$app->get('/', API::class); $app->get('/', API::class);

View File

@ -6,7 +6,7 @@ $app->group('/sources', function($app) {
$app->get('[/]', Sources::class); $app->get('[/]', Sources::class);
}); });
$app->group('/source/{currency_id}/{url}', function($app) { $app->group('/source/{source_id}', function($app) {
$app->put('/edit[/]', [Sources::class, 'edit']); $app->put('/edit[/]', [Sources::class, 'edit']);
$app->delete('/delete[/]', [Sources::class, 'delete']); $app->delete('/delete[/]', [Sources::class, 'delete']);
$app->get('[/]', [Sources::class, 'get']); $app->get('[/]', [Sources::class, 'get']);

View File

@ -0,0 +1,4 @@
<?php
use ProVM\Money\Common\Controller\Update;
$app->get('/update', Update::class);

8
app/setup/api/setups.php Normal file
View File

@ -0,0 +1,8 @@
<?php
use Psr\Container\ContainerInterface as Container;
return [
GuzzleHttp\ClientInterface::class => function(Container $c) {
return new GuzzleHttp\Client();
}
];

1
automation/crontab Normal file
View File

@ -0,0 +1 @@
0 2 * * */1 curl http://app-server/update

View File

@ -0,0 +1,70 @@
import argparse
import keyboard
import datetime
import time
import httpx
from threading import Event, Thread
def update(url: str):
r = httpx.get(url)
def update_thread(stop: Event, url: str):
t = datetime.time(hour=2)
print('Starting update thread.')
while True:
if stop.isSet():
break
if datetime.time() == t:
print('Updating.')
update(url)
print('Sleep')
time.sleep(60 * 60 * 5)
print('Stop update thread.')
def main_thread(stop: Event):
print('Starting main thread.')
while True:
if stop.isSet():
break
try:
if keyboard.is_pressed('q'):
print('Stop')
stop.set()
break
except KeyboardInterrupt:
print('Stop2')
stop.set()
break
print('Stop main thread.')
def main(args):
print('Main')
stop_signal = Event()
threads = [
Thread(target=update_thread, args=(stop_signal, args.url, )),
Thread(target=main_thread, args=(stop_signal,))
]
[t.start() for t in threads]
while True:
try:
if True not in [t.is_alive() for t in threads]:
break
except KeyboardInterrupt:
print('Stop Main')
stop_signal.set()
break
[t.join() for t in threads]
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--url', help='API Url', default='http://localhost:8081/update')
_args = parser.parse_args()
main(_args)

View File

@ -20,13 +20,19 @@ final class CreateSources extends AbstractMigration
{ {
$this->table('sources', [ $this->table('sources', [
'id' => false, 'id' => false,
'primary_key' => ['currency_id', 'url'], 'primary_key' => ['id'],
'engine' => 'InnoDB', 'engine' => 'InnoDB',
'encoding' => 'utf8mb4', 'encoding' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci', 'collation' => 'utf8mb4_general_ci',
'comment' => '', 'comment' => '',
'row_format' => 'DYNAMIC', 'row_format' => 'DYNAMIC',
]) ])
->addColumn('id', 'integer', [
'null' => false,
'limit' => '10',
'signed' => false,
'identity' => 'enable',
])
->addColumn('currency_id', 'integer', [ ->addColumn('currency_id', 'integer', [
'null' => false, 'null' => false,
'limit' => '10', 'limit' => '10',
@ -47,6 +53,10 @@ final class CreateSources extends AbstractMigration
'encoding' => 'utf8mb4', 'encoding' => 'utf8mb4',
'after' => 'url', 'after' => 'url',
]) ])
->addIndex(['currency_id'], [
'name' => 'currency_id',
'unique' => false,
])
->create(); ->create();
} }
} }

View File

@ -11,7 +11,6 @@ services:
- ./app/docker/nginx.conf:/etc/nginx/conf.d/default.conf - ./app/docker/nginx.conf:/etc/nginx/conf.d/default.conf
depends_on: depends_on:
- app-php - app-php
app-php: app-php:
container_name: money_app_php container_name: money_app_php
build: build:
@ -21,6 +20,11 @@ services:
- .:/code - .:/code
ports: ports:
- 9123:9000 - 9123:9000
app-cron:
image: sleeck/crond
volumes:
- ./automation/crontab:/etc/cron.d/auto-crontab
- ./automation/logs:/var/log/cron
ui-server: ui-server:
container_name: money_ui container_name: money_ui
@ -32,7 +36,6 @@ services:
- ./ui/docker/nginx.conf:/etc/nginx/conf.d/default.conf - ./ui/docker/nginx.conf:/etc/nginx/conf.d/default.conf
depends_on: depends_on:
- ui-php - ui-php
ui-php: ui-php:
container_name: money_ui_php container_name: money_ui_php
build: build:
@ -53,7 +56,6 @@ services:
- ./ws/docker/nginx.conf:/etc/nginx/conf.d/default.conf - ./ws/docker/nginx.conf:/etc/nginx/conf.d/default.conf
depends_on: depends_on:
- ws-php - ws-php
ws-php: ws-php:
container_name: money_ws_php container_name: money_ws_php
restart: unless-stopped restart: unless-stopped
@ -63,7 +65,6 @@ services:
volumes: volumes:
- .:/code - .:/code
db: db:
container_name: money_db container_name: money_db
image: mariadb:latest image: mariadb:latest
@ -77,7 +78,6 @@ services:
MYSQL_PASSWORD: 'money_pass' MYSQL_PASSWORD: 'money_pass'
volumes: volumes:
- dbdata:/var/lib/mysql - dbdata:/var/lib/mysql
adminer: adminer:
container_name: money_adminer container_name: money_adminer
image: adminer:latest image: adminer:latest

View File

@ -3,15 +3,16 @@ namespace ProVM\Money;
use Carbon\CarbonInterval; use Carbon\CarbonInterval;
use ProVM\Common\Alias\Model; use ProVM\Common\Alias\Model;
use ProVM\Common\Factory\Model as ModelFactory;
/** /**
* @property int $id
* @property Currency $currency_id * @property Currency $currency_id
* @property string $url * @property string $url
* @property \DateInterval $frecuency * @property \DateInterval $frecuency
*/ */
class Source extends Model { class Source extends Model {
public static $_table = 'sources'; public static $_table = 'sources';
public static $_id_column = ['currency_id', 'url'];
protected $currency; protected $currency;
public function currency(): ?Currency { public function currency(): ?Currency {
@ -22,7 +23,7 @@ class Source extends Model {
} }
public function frecuency(\DateInterval $frecuency = null) { public function frecuency(\DateInterval $frecuency = null) {
if ($frecuency == null) { if ($frecuency == null) {
return new \CarbonInterval($this->fecuency); return CarbonInterval::createFromDateString($this->frecuency);
} }
$this->frecuency = CarbonInterval::getDateIntervalSpec($frecuency); $this->frecuency = CarbonInterval::getDateIntervalSpec($frecuency);
} }
@ -30,15 +31,16 @@ class Source extends Model {
protected static $fields = ['currency_id', 'url', 'frecuency']; protected static $fields = ['currency_id', 'url', 'frecuency'];
public static function add(ModelFactory $factory, $info) { public static function add(ModelFactory $factory, $info) {
$input = array_intersect_key((array) $info, array_combine(self::$fields, self::$fields)); $input = array_intersect_key((array) $info, array_combine(self::$fields, self::$fields));
$input['frecuency'] = CarbonInterval::createFromDateString($input['frecuency']);
$source = $factory->find(Source::class)->where([['currency_id', $input['currency_id']], ['url', $input['url']]])->one(); $source = $factory->find(Source::class)->where([['currency_id', $input['currency_id']], ['url', $input['url']]])->one();
$created = false; $created = false;
$result = (object) compact('input', 'source', 'created'); $result = (object) compact('input', 'source', 'created');
if (!$value) { if (!$source) {
$source = $factory->create(Source::class, $input); $source = $factory->create(Source::class, $input);
$created = $source->save(); $created = $source->save();
$result->created = $created; $result->created = $created;
} }
$result->value = $source->asArray(); $result->source = $source->asArray();
return $result; return $result;
} }
public function edit($info): bool { public function edit($info): bool {
@ -46,9 +48,6 @@ class Source extends Model {
$edited = false; $edited = false;
foreach ($data as $field => $value) { foreach ($data as $field => $value) {
if ($this->{$field} != $value) { if ($this->{$field} != $value) {
if ($field == 'currency_id' or $field == 'url') {
continue;
}
$this->{$field} = $value; $this->{$field} = $value;
$edited = true; $edited = true;
} }
@ -61,7 +60,7 @@ class Source extends Model {
public function asArray(): array { public function asArray(): array {
$output = parent::asArray(); $output = parent::asArray();
$output['currency'] = $this->currency()->asArray(); $output['currency'] = $this->currency()->asArray();
$output['frecuency'] = $this->frecuency()->format('Y-m-d H:i:s'); $output['frecuency'] = $this->frecuency()->spec();
return $output; return $output;
} }
} }

View File

@ -71,6 +71,18 @@
<label>Url</label> <label>Url</label>
<input type="text" name="url" /> <input type="text" name="url" />
</div> </div>
<div class="inline field">
<label>Frecuencia</label>
<input type="text" name="frecuency" />
<select name="frec_name" class="ui selection dropdown">
<option value="minute">Minuto(s)</option>
<option value="hour">Hora(s)</option>
<option value="day">D&iacute;a(s)</option>
<option value="week">Semana(s)</option>
<option value="month">Mes(es)</option>
<option value="year">A&ntilde;o(s)</option>
</select>
</div>
<button class="ui button">Agregar</button> <button class="ui button">Agregar</button>
</form> </form>
</div> </div>
@ -84,17 +96,27 @@
add_button: '#add_source', add_button: '#add_source',
add_modal: '#add_sources', add_modal: '#add_sources',
loading: '', loading: '',
loaded: false,
sources: [], sources: [],
setup: function() { setup: function() {
$(this.id).hide() $(this.id).hide()
$(this.add_button).css('cursor', 'pointer').click((e) => { $(this.add_button).css('cursor', 'pointer').click((e) => {
this.add() this.add()
}) })
this.buildModal()
}, },
get: function(currency_id, data) { get: function(currency_id, data) {
this.sources = data.sources if (!this.loaded) {
this.populate(currency_id) this.sources = data.sources
socket.sendMessage('currency.values', {currency_id: '{{$currency_id}}'}) this.populate(currency_id)
socket.sendMessage('currency.values', {currency_id: currency_id})
return
}
var url = '{{$urls->api}}/currency/' + currency_id + '/sources'
$.getJSON(url, (data) => {
this.sources = data.sources
this.populate(currency_id)
})
}, },
buildModal: function() { buildModal: function() {
$(this.add_modal).modal() $(this.add_modal).modal()
@ -127,14 +149,15 @@
doAdd: function() { doAdd: function() {
let form = $(this.add_modal).find('form') let form = $(this.add_modal).find('form')
let info = { let info = {
url: form.find("[name='url']").val() url: form.find("[name='url']").val(),
frecuency: form.find("[name='frecuency']").val() + ' ' + form.find("[name='frec_name']").val()
} }
var url = '{{$urls->api}}/currency/' + this.data.id + '/sources/add' var url = '{{$urls->api}}/currency/{{$currency_id}}/sources/add'
$(this.add_modal).modal('hide') $(this.add_modal).modal('hide')
$(this.loading).modal('show') $(this.loading).modal('show')
$.post(url, JSON.stringify(info), (data) => { $.post(url, JSON.stringify(info), (data) => {
if (data.sources[0].created) { if (data.sources[0].created) {
this.get() this.get('{{$currency_id}}')
} }
}, 'json').then(() => { }, 'json').then(() => {
$(this.loading).modal('hide') $(this.loading).modal('hide')
@ -163,6 +186,7 @@
add_modal: '#add_values', add_modal: '#add_values',
loading: '', loading: '',
values: [], values: [],
loaded: false,
setup: function() { setup: function() {
$(this.id).hide() $(this.id).hide()
this.buildModal() this.buildModal()
@ -171,12 +195,20 @@
}) })
}, },
get: function(currency_id, data) { get: function(currency_id, data) {
if (data.values.length > 0) { if (!this.loaded) {
if (data.values.length > 0) {
this.values = data.values
this.populate(currency_id)
}
$(this.loading).modal('hide')
socket.conn.close()
return
}
var url = '{{$urls->api}}/currency/' + currency_id + '/values'
$.getJSON(url, (data) => {
this.values = data.values this.values = data.values
this.populate(currency_id) this.populate(currency_id)
} })
$(this.loading).modal('hide')
socket.conn.close()
}, },
buildModal: function() { buildModal: function() {
this.getCurrencies() this.getCurrencies()
@ -229,12 +261,12 @@
value: form.find("[name='valor']").val(), value: form.find("[name='valor']").val(),
base_id: form.find('.ui.dropdown').dropdown('get value') base_id: form.find('.ui.dropdown').dropdown('get value')
} }
var url = '{{$urls->api}}/currency/' + this.data.id + '/values/add' var url = '{{$urls->api}}/currency/{{$currency_id}}/values/add'
$(this.add_modal).modal('hide') $(this.add_modal).modal('hide')
$(this.loading).modal('show') $(this.loading).modal('show')
$.post(url, JSON.stringify(info), (data) => { $.post(url, JSON.stringify(info), (data) => {
if (data.values[0].created) { if (data.values[0].created) {
this.get() this.get('{{$currency_id}}')
} }
}, 'json').then(() => { }, 'json').then(() => {
$(this.loading).modal('hide') $(this.loading).modal('hide')