From 183e5f44520ae9c23f47a3e798dca80123034172 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 10 Nov 2015 22:33:19 +0100 Subject: [PATCH] command for stats --- app/console | 2 + src/Gist/Command/StatsCommand.php | 106 ++++++++++++++++++++++++++++++ src/Gist/Service/GistService.php | 6 ++ 3 files changed, 114 insertions(+) create mode 100644 src/Gist/Command/StatsCommand.php diff --git a/app/console b/app/console index b7ddd2b..9a2e551 100755 --- a/app/console +++ b/app/console @@ -3,10 +3,12 @@ use Gist\Command\CreateCommand; use Gist\Command\UpdateCommand; +use Gist\Command\StatsCommand; $app = require __DIR__.'/bootstrap.php'; $app['console']->add(new CreateCommand()); $app['console']->add(new UpdateCommand()); +$app['console']->add(new StatsCommand()); $app['console']->run(); diff --git a/src/Gist/Command/StatsCommand.php b/src/Gist/Command/StatsCommand.php new file mode 100644 index 0000000..4cd12b3 --- /dev/null +++ b/src/Gist/Command/StatsCommand.php @@ -0,0 +1,106 @@ +setName('stats') + ->setDescription('Show stats about GIST') + ->setHelp(<<getSilexApplication()['gist']; + $gists = $gistService->getGists(); + + $languages = []; + $withEncryption = []; + $commits = []; + $total = 0; + + foreach ($gists as $gist) { + $total++; + + if (!array_key_exists($gist->getType(), $languages)) { + $languages[$gist->getType()] = 0; + $withEncryption[$gist->getType()] = 0; + $commits[$gist->getType()] = 0; + } + + if ($gist->getCipher()) { + $withEncryption[$gist->getType()]++; + } + + $languages[$gist->getType()]++; + try { + $count = count($gistService->getHistory($gist)); + $commits[$gist->getType()] += $count; + } catch(GitException $e) { + + } + } + + $output->writeln(['Gists statistics', '']); + + $table = new Table($output); + $table + ->setHeaders(array('With encryption', 'Without encryption', 'Commits', 'Total')) + ->setRows(array( + array( + $total - $v = array_sum($withEncryption), + $v, + array_sum($commits), + $total + ), + )) + ; + $table->render(); + + ksort($languages); + ksort($withEncryption); + ksort($commits); + + $output->writeln(['', 'Details by type', '']); + + $table->setHeaders(array( + 'Type', + 'With encryption', + 'Without encryption', + 'Commits', + 'Total', + )); + + $rows = []; + + foreach ($languages as $lang => $total) { + $totalWithoutEncyption = $total - $withEncryption[$lang]; + $totalWithEncryption = $total - $totalWithoutEncyption; + + $rows[] = array( + $lang, + $totalWithEncryption, + $totalWithoutEncyption, + $commits[$lang], + $total, + ); + } + + $table->setRows($rows); + $table->render(); + } +} diff --git a/src/Gist/Service/GistService.php b/src/Gist/Service/GistService.php index 1027cfc..4010101 100644 --- a/src/Gist/Service/GistService.php +++ b/src/Gist/Service/GistService.php @@ -7,6 +7,7 @@ use GitWrapper\GitWorkingCopy; use GitWrapper\GitWrapper; use GitWrapper\GitCommand; use GeSHi; +use Gist\Model\GistQuery; /** * Class GistService @@ -30,6 +31,11 @@ class GistService $this->geshi = $geshi; } + public function getGists() + { + return GistQuery::create()->find(); + } + public function getHistory(Gist $gist) { $command = GitCommand::getInstance('log', '--format=medium', $gist->getFile());