Compare commits
4 Commits
190dfd7c34
...
5129cca099
Author | SHA1 | Date | |
---|---|---|---|
5129cca099 | |||
35bcbd1979 | |||
fef167c46e | |||
29d04ac4ad |
@ -15,10 +15,10 @@ class Sources {
|
||||
$output = compact('sources');
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function get(Request $request, Response $response, ModelFactory $factory, $currency_id, $url): Response {
|
||||
$source = $factory->find(Source::class)->where([['currency_id', $currency_id], ['url', $url]])->one();
|
||||
public function get(Request $request, Response $response, ModelFactory $factory, $source_id): Response {
|
||||
$source = $factory->find(Source::class)->one($source_id);
|
||||
$output = [
|
||||
'get_data' => compact('currency_id', 'url'),
|
||||
'get_data' => compact('source_id'),
|
||||
'source' => null
|
||||
];
|
||||
if ($source) {
|
||||
@ -45,13 +45,13 @@ class Sources {
|
||||
];
|
||||
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());
|
||||
$output = [
|
||||
'get_data' => compact('currency_id', 'url'),
|
||||
'get_data' => compact('source_id'),
|
||||
'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;
|
||||
if ($source) {
|
||||
$edited = $source->edit($post);
|
||||
@ -60,9 +60,9 @@ class Sources {
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function delete(Request $request, Response $response, ModelFactory $factory, $currency_id, $url): Response {
|
||||
$source = $factory->find(Source::class)->where([['currency_id', $currency_id], ['url', $url]])->one();
|
||||
$output = ['get_data' => compact('currency_id', 'url'), 'source' => null, 'deleted' => false];
|
||||
public function delete(Request $request, Response $response, ModelFactory $factory, $source_id): Response {
|
||||
$source = $factory->find(Source::class)->one($source_id);
|
||||
$output = ['get_data' => compact('source_id'), 'source' => null, 'deleted' => false];
|
||||
if ($source) {
|
||||
$output['source'] = $source->asArray();
|
||||
$status = $source->delete();
|
||||
|
86
app/common/Controller/Update.php
Normal file
86
app/common/Controller/Update.php
Normal 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);
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@
|
||||
"provm/models": "dev-master",
|
||||
"provm/controller": "dev-master",
|
||||
"vlucas/phpdotenv": "^5.3",
|
||||
"guzzlehttp/guzzle": "^7.3",
|
||||
"nesbot/carbon": "^2.46"
|
||||
},
|
||||
"license": "MIT",
|
||||
|
@ -13,6 +13,7 @@ server {
|
||||
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-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
|
||||
add_header 'Content-Type' 'application/json';
|
||||
|
||||
location ~ \.php {
|
||||
try_files $uri =404;
|
||||
|
@ -1,9 +1,16 @@
|
||||
<?php
|
||||
use ProVM\Money\Common\Controller\API;
|
||||
|
||||
include_once 'currencies.php';
|
||||
include_once 'values.php';
|
||||
include_once 'sources.php';
|
||||
$files = new DirectoryIterator(implode(DIRECTORY_SEPARATOR, [
|
||||
__DIR__,
|
||||
'api'
|
||||
]));
|
||||
foreach ($files as $file) {
|
||||
if ($file->isDir()) {
|
||||
continue;
|
||||
}
|
||||
include_once $file->getRealPath();
|
||||
}
|
||||
|
||||
$app->get('/', API::class);
|
||||
|
||||
|
@ -6,7 +6,7 @@ $app->group('/sources', function($app) {
|
||||
$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->delete('/delete[/]', [Sources::class, 'delete']);
|
||||
$app->get('[/]', [Sources::class, 'get']);
|
4
app/resources/routes/api/update.php
Normal file
4
app/resources/routes/api/update.php
Normal 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
8
app/setup/api/setups.php
Normal 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
1
automation/crontab
Normal file
@ -0,0 +1 @@
|
||||
0 2 * * */1 curl http://app-server/update
|
70
automation/src/money/main.py
Normal file
70
automation/src/money/main.py
Normal 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)
|
@ -20,13 +20,19 @@ final class CreateSources extends AbstractMigration
|
||||
{
|
||||
$this->table('sources', [
|
||||
'id' => false,
|
||||
'primary_key' => ['currency_id', 'url'],
|
||||
'primary_key' => ['id'],
|
||||
'engine' => 'InnoDB',
|
||||
'encoding' => 'utf8mb4',
|
||||
'collation' => 'utf8mb4_general_ci',
|
||||
'comment' => '',
|
||||
'row_format' => 'DYNAMIC',
|
||||
])
|
||||
->addColumn('id', 'integer', [
|
||||
'null' => false,
|
||||
'limit' => '10',
|
||||
'signed' => false,
|
||||
'identity' => 'enable',
|
||||
])
|
||||
->addColumn('currency_id', 'integer', [
|
||||
'null' => false,
|
||||
'limit' => '10',
|
||||
@ -47,6 +53,10 @@ final class CreateSources extends AbstractMigration
|
||||
'encoding' => 'utf8mb4',
|
||||
'after' => 'url',
|
||||
])
|
||||
->addIndex(['currency_id'], [
|
||||
'name' => 'currency_id',
|
||||
'unique' => false,
|
||||
])
|
||||
->create();
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ services:
|
||||
- ./app/docker/nginx.conf:/etc/nginx/conf.d/default.conf
|
||||
depends_on:
|
||||
- app-php
|
||||
|
||||
app-php:
|
||||
container_name: money_app_php
|
||||
build:
|
||||
@ -21,6 +20,11 @@ services:
|
||||
- .:/code
|
||||
ports:
|
||||
- 9123:9000
|
||||
app-cron:
|
||||
image: sleeck/crond
|
||||
volumes:
|
||||
- ./automation/crontab:/etc/cron.d/auto-crontab
|
||||
- ./automation/logs:/var/log/cron
|
||||
|
||||
ui-server:
|
||||
container_name: money_ui
|
||||
@ -32,7 +36,6 @@ services:
|
||||
- ./ui/docker/nginx.conf:/etc/nginx/conf.d/default.conf
|
||||
depends_on:
|
||||
- ui-php
|
||||
|
||||
ui-php:
|
||||
container_name: money_ui_php
|
||||
build:
|
||||
@ -53,7 +56,6 @@ services:
|
||||
- ./ws/docker/nginx.conf:/etc/nginx/conf.d/default.conf
|
||||
depends_on:
|
||||
- ws-php
|
||||
|
||||
ws-php:
|
||||
container_name: money_ws_php
|
||||
restart: unless-stopped
|
||||
@ -63,7 +65,6 @@ services:
|
||||
volumes:
|
||||
- .:/code
|
||||
|
||||
|
||||
db:
|
||||
container_name: money_db
|
||||
image: mariadb:latest
|
||||
@ -77,7 +78,6 @@ services:
|
||||
MYSQL_PASSWORD: 'money_pass'
|
||||
volumes:
|
||||
- dbdata:/var/lib/mysql
|
||||
|
||||
adminer:
|
||||
container_name: money_adminer
|
||||
image: adminer:latest
|
||||
|
@ -3,15 +3,16 @@ namespace ProVM\Money;
|
||||
|
||||
use Carbon\CarbonInterval;
|
||||
use ProVM\Common\Alias\Model;
|
||||
use ProVM\Common\Factory\Model as ModelFactory;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property Currency $currency_id
|
||||
* @property string $url
|
||||
* @property \DateInterval $frecuency
|
||||
*/
|
||||
class Source extends Model {
|
||||
public static $_table = 'sources';
|
||||
public static $_id_column = ['currency_id', 'url'];
|
||||
|
||||
protected $currency;
|
||||
public function currency(): ?Currency {
|
||||
@ -22,7 +23,7 @@ class Source extends Model {
|
||||
}
|
||||
public function frecuency(\DateInterval $frecuency = null) {
|
||||
if ($frecuency == null) {
|
||||
return new \CarbonInterval($this->fecuency);
|
||||
return CarbonInterval::createFromDateString($this->frecuency);
|
||||
}
|
||||
$this->frecuency = CarbonInterval::getDateIntervalSpec($frecuency);
|
||||
}
|
||||
@ -30,15 +31,16 @@ class Source extends Model {
|
||||
protected static $fields = ['currency_id', 'url', 'frecuency'];
|
||||
public static function add(ModelFactory $factory, $info) {
|
||||
$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();
|
||||
$created = false;
|
||||
$result = (object) compact('input', 'source', 'created');
|
||||
if (!$value) {
|
||||
if (!$source) {
|
||||
$source = $factory->create(Source::class, $input);
|
||||
$created = $source->save();
|
||||
$result->created = $created;
|
||||
}
|
||||
$result->value = $source->asArray();
|
||||
$result->source = $source->asArray();
|
||||
return $result;
|
||||
}
|
||||
public function edit($info): bool {
|
||||
@ -46,9 +48,6 @@ class Source extends Model {
|
||||
$edited = false;
|
||||
foreach ($data as $field => $value) {
|
||||
if ($this->{$field} != $value) {
|
||||
if ($field == 'currency_id' or $field == 'url') {
|
||||
continue;
|
||||
}
|
||||
$this->{$field} = $value;
|
||||
$edited = true;
|
||||
}
|
||||
@ -61,7 +60,7 @@ class Source extends Model {
|
||||
public function asArray(): array {
|
||||
$output = parent::asArray();
|
||||
$output['currency'] = $this->currency()->asArray();
|
||||
$output['frecuency'] = $this->frecuency()->format('Y-m-d H:i:s');
|
||||
$output['frecuency'] = $this->frecuency()->spec();
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
@ -71,6 +71,18 @@
|
||||
<label>Url</label>
|
||||
<input type="text" name="url" />
|
||||
</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ía(s)</option>
|
||||
<option value="week">Semana(s)</option>
|
||||
<option value="month">Mes(es)</option>
|
||||
<option value="year">Año(s)</option>
|
||||
</select>
|
||||
</div>
|
||||
<button class="ui button">Agregar</button>
|
||||
</form>
|
||||
</div>
|
||||
@ -84,17 +96,27 @@
|
||||
add_button: '#add_source',
|
||||
add_modal: '#add_sources',
|
||||
loading: '',
|
||||
loaded: false,
|
||||
sources: [],
|
||||
setup: function() {
|
||||
$(this.id).hide()
|
||||
$(this.add_button).css('cursor', 'pointer').click((e) => {
|
||||
this.add()
|
||||
})
|
||||
this.buildModal()
|
||||
},
|
||||
get: function(currency_id, data) {
|
||||
this.sources = data.sources
|
||||
this.populate(currency_id)
|
||||
socket.sendMessage('currency.values', {currency_id: '{{$currency_id}}'})
|
||||
if (!this.loaded) {
|
||||
this.sources = data.sources
|
||||
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() {
|
||||
$(this.add_modal).modal()
|
||||
@ -127,14 +149,15 @@
|
||||
doAdd: function() {
|
||||
let form = $(this.add_modal).find('form')
|
||||
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.loading).modal('show')
|
||||
$.post(url, JSON.stringify(info), (data) => {
|
||||
if (data.sources[0].created) {
|
||||
this.get()
|
||||
this.get('{{$currency_id}}')
|
||||
}
|
||||
}, 'json').then(() => {
|
||||
$(this.loading).modal('hide')
|
||||
@ -163,6 +186,7 @@
|
||||
add_modal: '#add_values',
|
||||
loading: '',
|
||||
values: [],
|
||||
loaded: false,
|
||||
setup: function() {
|
||||
$(this.id).hide()
|
||||
this.buildModal()
|
||||
@ -171,12 +195,20 @@
|
||||
})
|
||||
},
|
||||
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.populate(currency_id)
|
||||
}
|
||||
$(this.loading).modal('hide')
|
||||
socket.conn.close()
|
||||
})
|
||||
},
|
||||
buildModal: function() {
|
||||
this.getCurrencies()
|
||||
@ -229,12 +261,12 @@
|
||||
value: form.find("[name='valor']").val(),
|
||||
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.loading).modal('show')
|
||||
$.post(url, JSON.stringify(info), (data) => {
|
||||
if (data.values[0].created) {
|
||||
this.get()
|
||||
this.get('{{$currency_id}}')
|
||||
}
|
||||
}, 'json').then(() => {
|
||||
$(this.loading).modal('hide')
|
||||
|
Reference in New Issue
Block a user