public function getSparklinesData($ids) { $ids = explode(',', $ids); if (count($ids) > 10) { $ids = array_slice($ids, 0, 10); } $response = array(); foreach ($ids as $key => $id) { $response[$id]['total'] = Stats::where('id_link', $id)->lists('total'); $response[$id]['dif_total'] = Stats::where('id_link', $id)->lists('dif_total'); } return Response::json(array('data' => $response)); }
/** * Execute the console command. * * @return mixed */ public function fire() { try { $log = new Process(); $log->name = "get-shares"; $log->status = "running"; $log->save(); $filterDate = new DateTime('now'); $filterDate->sub(new DateInterval('P1D')); //Load shares Link::where('date', '>', $filterDate)->chunk(100, function ($links) { foreach ($links as $value) { $shares = $this->getSharesCount($value->final_url); $ref = Stats::where('id_link', $value->id)->orderBy('created_at', 'DESC')->first(); if (!$ref) { $ref = new stdClass(); $ref->total = 0; } $stat = new Stats(); $stat->id_link = $value->id; $stat->facebook = $shares['facebook'] != null ? $shares['facebook'] : $value->facebook; $stat->twitter = $shares['twitter'] != null ? $shares['twitter'] : $value->twitter; $stat->linkedin = $shares['linkedin'] != null ? $shares['linkedin'] : $value->linkedin; $stat->googleplus = $shares['googleplus'] != null ? $shares['googleplus'] : $value->googleplus; $stat->total = $stat->facebook + $stat->twitter + $stat->linkedin + $stat->googleplus; $stat->dif_total = $stat->total - $ref->total; $stat->save(); $value->facebook = $stat->facebook; $value->twitter = $stat->twitter; $value->linkedin = $stat->linkedin; $value->googleplus = $stat->googleplus; $value->total = $stat->total; $value->save(); } }); $log->status = "finished"; $log->save(); } catch (Exception $e) { $this->info($url); $this->info($e->getMessage()); } }