1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use App\InteractionMethodsMOD\GetMethods; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use DB; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use Illuminate\Console\Command; |
10
|
|
|
use Symfony\Component\Config\Definition\Exception\Exception; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ExchangeHistoryTableFeeder |
14
|
|
|
* @package App\Console\Commands |
15
|
|
|
*/ |
16
|
|
|
class ExchangeHistoryTableFeeder extends Command |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The name and signature of the console command. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $signature = 'history_table:feed'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The console command description. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $description = 'Feed the table exchange_history from DB'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create a new command instance. |
34
|
|
|
* |
35
|
|
|
*/ |
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
parent::__construct(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Execute the console command. |
43
|
|
|
* |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
public function handle() |
47
|
|
|
{ |
48
|
|
|
$this->getSymbolsFormDB(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
*Elimina les dades antigues i n'insereix unes de noves |
53
|
|
|
*/ |
54
|
|
|
public function getSymbolsFormDB() |
55
|
|
|
{ |
56
|
|
|
DB::table('exchange_history')->truncate(); |
57
|
|
|
|
58
|
|
|
$symbols = DB::table('companies')->select('symbol')->get(); |
59
|
|
|
|
60
|
|
|
|
61
|
|
View Code Duplication |
for($i=0; $i<count($symbols); $i++) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
try{ |
64
|
|
|
$symbol=$symbols[$i]->symbol; |
65
|
|
|
$gm = new GetMethods(); |
66
|
|
|
$this->httpCall($gm,$symbol); |
67
|
|
|
|
68
|
|
|
sleep(4); |
69
|
|
|
}catch(Exception $e){} |
|
|
|
|
70
|
|
|
|
71
|
|
|
switch($i){ |
72
|
|
|
case 0: |
73
|
|
|
echo "0%\n"; |
74
|
|
|
break; |
75
|
|
|
case 24: |
76
|
|
|
echo "25%\n"; |
77
|
|
|
break; |
78
|
|
|
case 49: |
79
|
|
|
echo "50%\n"; |
80
|
|
|
break; |
81
|
|
|
case 74: |
82
|
|
|
echo "75%\n"; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
echo("100% Table exchange_history fed\n"); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Aconegueix les dades de la api i fa la crida de la funció que les guardara a la BD |
92
|
|
|
* |
93
|
|
|
* @param GetMethods $interaction_methods |
94
|
|
|
* @param $symbol |
95
|
|
|
*/ |
96
|
|
|
public function httpCall(GetMethods $interaction_methods, $symbol) |
97
|
|
|
{ |
98
|
|
|
$glz_cli=new Client(); |
99
|
|
|
|
100
|
|
|
$data = $interaction_methods->interactiveChart($glz_cli,$symbol); |
101
|
|
|
|
102
|
|
|
$this->storeDataInDB($data); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* |
107
|
|
|
* Persisteix les dades al MYSQL |
108
|
|
|
* |
109
|
|
|
* @param $data |
110
|
|
|
*/ |
111
|
|
|
public function storeDataInDB($data) |
112
|
|
|
{ |
113
|
|
|
DB::table('exchange_history')->insert([ |
114
|
|
|
[ |
115
|
|
|
'symbol' => $data->Elements[0]->Symbol, |
116
|
|
|
'positions' => json_encode($data->Positions), |
117
|
|
|
'dates' => json_encode($data->Dates), |
118
|
|
|
'values' => json_encode($data->Elements[0]->DataSeries->close->values), |
119
|
|
|
'max_date' => $data->Elements[0]->DataSeries->close->maxDate, |
120
|
|
|
'min_date' => $data->Elements[0]->DataSeries->close->minDate, |
121
|
|
|
'min_value' => $data->Elements[0]->DataSeries->close->min, |
122
|
|
|
'max_value' => $data->Elements[0]->DataSeries->close->max, |
123
|
|
|
'created_at' => Carbon::now()->toDateTimeString(), |
124
|
|
|
'updated_at' => Carbon::now()->toDateTimeString() |
125
|
|
|
] |
126
|
|
|
]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
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: