public function defaultAction() { // Line chart $memcache = new \Memcached(); $stats1 = $memcache->get('stats1'); if ($stats1 === false) { $stats = Stat::select(DB::raw('max(`packages`) as packages'), DB::raw('sum(`added`) as added'), DB::raw('date(`created_at`) as date'))->orderBy('date', 'asc')->groupBy('date')->get(); $stats1 = [['Date', 'Total', 'New']]; foreach ($stats as $stat) { $stats1[] = [$stat->date, (int) $stat->packages, (int) $stat->added]; } $memcache->set('stats1', $stats1, self::DAY); } // Domain table $memcache = new \Memcached(); $stats2 = $memcache->get('stats2'); if ($stats2 === false) { $stats2 = Package::select(['domain', DB::raw('count(domain) as count')])->groupBy('domain')->where('domain', '<>', '')->orderBy('count', 'desc')->get()->toArray(); $memcache->set('stats2', $stats2, self::DAY); } // Types table $memcache = new \Memcached(); $stats3 = $memcache->get('stats3'); if ($stats3 === false || true) { $stats3 = Package::select(['type', DB::raw('count(type) as count')])->groupBy('type')->where('type', '<>', '')->having('count', '>', 9)->orderBy('count', 'desc')->get()->toArray(); $memcache->set('stats3', $stats3, self::DAY); } $this->_setTitle('Stats'); return new StatsView($stats1, $stats2, $stats3); }
public function ajaxSearchPackageTypes() { $data = $this->_getRequest()->query->all(); $type = Arrays::value($data, 'search', ''); $memcache = new \Memcached(); $key = 'searchTypes-' . $type; $searchTypes = $memcache->get($key); if ($searchTypes === false) { $paginate = Package::select('type')->where('type', '<>', '')->where('type', 'like', '%' . $type . '%')->groupBy('type')->orderBy('type', 'asc')->get(); $searchTypes = []; foreach ($paginate as $item) { $searchTypes[] = ['id' => $item->type, 'text' => $item->type]; } $memcache->set($key, $searchTypes, self::WEEK); } return ['results' => $searchTypes, 'lastPage' => 1]; }
private function _refreshPackage(Package $package) { // Get latest info try { $packgist = new Packagist(); $data = $packgist->package($package->author, $package->name); } catch (\Exception $e) { return null; } //print_r($data);exit; // Save package data $package->description = $data['description']; $package->downloads = $data['downloads']['total']; $package->downloads_m = $data['downloads']['monthly']; $package->downloads_d = $data['downloads']['daily']; $package->stars = $data['favers']; $package->type = $data['type']; $package->repo = $data['repository']; $package->domain = parse_url($data['repository'], PHP_URL_HOST); $package->save(); // Maintainers $ids = []; foreach ($data['maintainers'] as $maintainer) { $model = Maintainer::firstOrCreate(['name' => $maintainer['name']]); $ids[] = $model->id; } $package->maintainers()->sync($ids); // Get the latets version usort($data['versions'], function ($a, $b) { $a = $a['time']; $b = $b['time']; if ($a == $b) { return 0; } return $a > $b ? -1 : 1; }); if (isset($data['versions'][0])) { $data = $data['versions'][0]; // Authors $ids = []; if (isset($data['authors'])) { foreach ($data['authors'] as $author) { if (isset($author['email']) && $author['email']) { $model = Author::firstOrNew(['email' => $author['email']]); foreach ($author as $field => $value) { if ($value) { $value = Transliterator::unaccent($value); $model->{$field} = $value; } } $model->save(); $ids[] = $model->id; } } } $package->authors()->sync($ids); // Tags $ids = []; if (isset($data['keywords'])) { foreach ($data['keywords'] as $tag) { $model = Tag::firstOrCreate(['name' => $tag]); $ids[] = $model->id; } } $package->tags()->sync($ids); // Dependencies $ids = []; if (isset($data['require'])) { foreach ($data['require'] as $fullNname => $version) { $explode = explode('/', $fullNname, 2); list($author, $name) = array_pad($explode, 2, ''); $model = Package::where('author', '=', $author)->where('name', '=', $name)->get()->first(); if ($model) { $ids[$model->id] = ['version' => $version]; } } } $package->dependencies()->sync($ids); } // Mark as updated even if nothing changed. $package->touch(); return $package; }