コード例 #1
0
ファイル: Data.php プロジェクト: poiuty/midas
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $intervals = [300, 900, 1800, 7200, 14400, 86400];
     $base = Wallet::where('base', true)->get();
     $all = Wallet::all();
     foreach ($base as $base_wallet) {
         foreach ($all as $market_wallet) {
             if ($market_wallet->id == $base_wallet->id) {
                 continue;
             }
             foreach ($intervals as $interval) {
                 OrdersTransaction::selectRaw('
                         FLOOR(MIN(unix_timestamp(`created_at`))/' . $interval . ')*' . $interval . ' AS date,
                         SUM(amount) AS volume,
                         SUBSTRING_INDEX(MIN(CONCAT(unix_timestamp(`created_at`), \'_\', price)), \'_\', -1) AS `open`,
                         MAX(price) AS high,
                         MIN(price) AS low,
                         SUBSTRING_INDEX(MAX(CONCAT(unix_timestamp(`created_at`), \'_\', price)), \'_\', -1) AS `close`
                       ')->where(function (\Illuminate\Database\Eloquent\Builder $query) use($base_wallet, $market_wallet) {
                     $query->where('wallet_id', $base_wallet->id)->where('want_wallet_id', $market_wallet->id);
                 })->where('created_at', '>=', DB::raw('DATE_SUB(NOW(), INTERVAL 24 HOUR)'))->groupBy(DB::raw('FLOOR(unix_timestamp(`created_at`)/' . $interval . ')'))->orderBy('created_at', 'asc')->chunk(100000, function ($orders) use($base_wallet, $market_wallet, $interval) {
                     foreach ($orders as $key => $order) {
                         try {
                             $cache = OrdersChart::firstOrNew(['wallet_id' => $base_wallet->id, 'want_wallet_id' => $market_wallet->id, 'date' => $order->date, 'interval' => $interval]);
                             $cache->date = $order->date;
                             $cache->open = $order->open;
                             $cache->high = $order->high;
                             $cache->low = $order->low;
                             $cache->close = $order->close;
                             $cache->volume = $order->volume;
                             $cache->wallet_id = $base_wallet->id;
                             $cache->want_wallet_id = $market_wallet->id;
                             $cache->interval = $interval;
                             $cache->save();
                         } catch (\Exception $e) {
                         }
                     }
                 });
             }
         }
     }
 }