|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\InteractionMethodsMOD\GetMethods; |
|
6
|
|
|
use Carbon\Carbon; |
|
7
|
|
|
use DB; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
|
|
10
|
|
|
use GuzzleHttp\Client; |
|
11
|
|
|
|
|
12
|
|
|
use App\Http\Requests; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class ExchangeHistoryTableFeeder |
|
16
|
|
|
* @package App\Http\Controllers |
|
17
|
|
|
*/ |
|
18
|
|
|
class ExchangeHistoryTableFeeder extends Controller |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
*Elimina les dades antigues i n'insereix unes de noves |
|
23
|
|
|
*/ |
|
24
|
|
|
public function getSymbolsFormDB() |
|
25
|
|
|
{ |
|
26
|
|
|
DB::table('exchange_history')->truncate(); |
|
27
|
|
|
|
|
28
|
|
|
$symbols = DB::table('companies')->select('symbol')->get(); |
|
29
|
|
|
|
|
30
|
|
|
for($i=0; $i<count($symbols); $i++) |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
$symbol=$symbols[$i]->symbol; |
|
33
|
|
|
$gm = new GetMethods(); |
|
34
|
|
|
$this->httpCall($gm,$symbol); |
|
35
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Aconegueix les dades de la api i fa la crida de la funció que les guardara a la BD |
|
42
|
|
|
* |
|
43
|
|
|
* @param GetMethods $interaction_methods |
|
44
|
|
|
* @param $symbol |
|
45
|
|
|
*/ |
|
46
|
|
|
public function httpCall(GetMethods $interaction_methods, $symbol) |
|
47
|
|
|
{ |
|
48
|
|
|
$glz_cli=new Client(); |
|
49
|
|
|
|
|
50
|
|
|
$data = $interaction_methods->interactiveChart($glz_cli,$symbol); |
|
51
|
|
|
|
|
52
|
|
|
$this->storeDataInDB($data); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* |
|
57
|
|
|
* Persisteix les dades al MYSQL |
|
58
|
|
|
* |
|
59
|
|
|
* @param $data |
|
60
|
|
|
*/ |
|
61
|
|
|
public function storeDataInDB($data) |
|
62
|
|
|
{ |
|
63
|
|
|
DB::table('exchange_history')->insert([ |
|
64
|
|
|
[ |
|
65
|
|
|
'symbol' => $data->Elements[0]->Symbol, |
|
66
|
|
|
'positions' => json_encode($data->Positions), |
|
67
|
|
|
'dates' => json_encode($data->Dates), |
|
68
|
|
|
'values' => json_encode($data->Elements[0]->DataSeries->close->values), |
|
69
|
|
|
'max_date' => $data->Elements[0]->DataSeries->close->maxDate, |
|
70
|
|
|
'min_date' => $data->Elements[0]->DataSeries->close->minDate, |
|
71
|
|
|
'min_value' => $data->Elements[0]->DataSeries->close->min, |
|
72
|
|
|
'max_value' => $data->Elements[0]->DataSeries->close->max, |
|
73
|
|
|
'created_at' => Carbon::now()->toDateTimeString(), |
|
74
|
|
|
'updated_at' => Carbon::now()->toDateTimeString() |
|
75
|
|
|
] |
|
76
|
|
|
]); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: