Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[console] Implement statistics feature #342

Merged
merged 17 commits into from May 28, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

[statistics] Add more condition to send info

  • Loading branch information...
harold20 committed May 5, 2018
commit 784351900aee4434f24ea4a64345c78a52ebe0a4
@@ -1,6 +1,8 @@
application:
share:
statistics: false
statistics:
enabled: false
last-attempted: ~
count-attempted: 0
language: 'en'
autowire:
commands:
@@ -105,7 +105,7 @@ services:
# DrupalConsoleCore Generators
console.init_generator:
class: Drupal\Console\Core\Generator\InitGenerator
arguments: ['@console.nested_array']
arguments: ['@console.nested_array', '@console.configuration_manager']
tags:
- { name: drupal.generator }
console.site_alias_generator:
@@ -2,7 +2,7 @@
namespace Drupal\Console\Core;
use Drupal\Console\Core\EventSubscriber\CalculateStatisticsListener;
use Drupal\Console\Core\EventSubscriber\SendStatisticsListener;
use Drupal\Console\Core\EventSubscriber\RemoveMessagesListener;
use Drupal\Console\Core\EventSubscriber\SaveStatisticsListener;
use Drupal\Console\Core\EventSubscriber\ShowGenerateCountCodeLinesListener;
@@ -316,7 +316,7 @@ private function registerEvents()
);
$dispatcher->addSubscriber(
new CalculateStatisticsListener(
new SendStatisticsListener(
$this->container->get('console.configuration_manager')

This comment has been minimized.

Copy link
@jmolivas

jmolivas Apr 21, 2018

Member

Remove this service and read config file directly from user home directory.

)
);
@@ -725,7 +725,9 @@ public function getData($filterNamespaces = null, $excludeNamespaces = [], $excl
$namespaces = array_diff($namespaces, $excludeNamespaces);
// filter namespaces if available
if($filterNamespaces) $namespaces = array_intersect($namespaces, $filterNamespaces);
if ($filterNamespaces) {
$namespaces = array_intersect($namespaces, $filterNamespaces);
}
foreach ($namespaces as $namespace) {
$commands = $this->all($namespace);
@@ -737,8 +739,8 @@ public function getData($filterNamespaces = null, $excludeNamespaces = [], $excl
foreach ($commands as $command) {
// Exclude command if is a chain command and was requested to exclude chain commands
if($excludeChainCommands && $command instanceof ChainCustomCommand) {
continue;
if ($excludeChainCommands && $command instanceof ChainCustomCommand) {
continue;
}
if (method_exists($command, 'getModule')) {
@@ -756,9 +758,11 @@ public function getData($filterNamespaces = null, $excludeNamespaces = [], $excl
}
// Remove namepsaces without commands
$namespaces = array_filter($namespaces, function($namespace) use( $data) {
return count($data['commands'][$namespace]) > 0;
});
$namespaces = array_filter(
$namespaces, function ($namespace) use ($data) {
return count($data['commands'][$namespace]) > 0;
}
);
$input = $this->getDefinition();
$options = [];
@@ -57,7 +57,7 @@ class InitCommand extends Command
'learning' => false,
'generate_inline' => false,
'generate_chain' => false,
'statistics' => false
'statistics' => true
];
private $directories = [
@@ -75,7 +75,12 @@ public function saveStatistics(ConsoleTerminateEvent $event)
$configGlobalAsArray = $this->configurationManager->getConfigGlobalAsArray();
//Validate if the config is enable.
if (is_null($configGlobalAsArray) || !$configGlobalAsArray['application']['share']['statistics']) {
if (is_null($configGlobalAsArray) || !$configGlobalAsArray['application']['statistics']['enabled']) {
return;
}
//Validate if attempted is 10
if ($configGlobalAsArray['application']['statistics']['count-attempted'] >= 10) {
return;
}
@@ -104,7 +109,7 @@ public function saveStatistics(ConsoleTerminateEvent $event)
$this->fs->appendToFile(
$path . date('Y-m-d') . '-pending.csv',
$path . date('Y-m-d') . '.csv',
$information . PHP_EOL
);
}
@@ -2,12 +2,12 @@
/**
* @file
* Contains \Drupal\Console\Core\EventSubscriber\CalculateStatisticsListener.
* Contains \Drupal\Console\Core\EventSubscriber\SendStatisticsListener.
*/
namespace Drupal\Console\Core\EventSubscriber;
use Drupal\Console\Core\Command\Chain\ChainCustomCommand;
use Drupal\Console\Core\Style\DrupalStyle;
use Drupal\Console\Core\Utils\ConfigurationManager;
use GuzzleHttp\Client;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
@@ -17,11 +17,11 @@
use Symfony\Component\Finder\Finder;
/**
* Class CalculateStatisticsListener
* Class SendStatisticsListener
*
* @package Drupal\Console\Core\EventSubscriber
*/
class CalculateStatisticsListener implements EventSubscriberInterface
class SendStatisticsListener implements EventSubscriberInterface
{
/**
@@ -60,10 +60,21 @@ public function calculateStatistics(ConsoleTerminateEvent $event)
return;
}
$date = date('Y-m-d');
$configGlobalAsArray = $this->configurationManager->getConfigGlobalAsArray();
//Validate if the config is enable.
if (is_null($configGlobalAsArray) || !$configGlobalAsArray['application']['share']['statistics']) {
if (is_null($configGlobalAsArray) || !$configGlobalAsArray['application']['statistics']['enabled']) {
return;
}
//Validate if attempted is 10
if ($configGlobalAsArray['application']['statistics']['count-attempted'] >= 10) {
return;
}
//Validate if last attempted was today
if ($configGlobalAsArray['application']['statistics']['last-attempted'] === $date) {
return;
}
@@ -76,10 +87,11 @@ public function calculateStatistics(ConsoleTerminateEvent $event)
$finder = new Finder();
$finder
->files()
->name('*-pending.csv')
->notName(date('Y-m-d').'-pending.csv')
->name('*.csv')
->notName($date.'.csv')
->in($path);
//Validate if finder in not null
if ($finder->count() == 0) {
return;
}
@@ -112,21 +124,37 @@ public function calculateStatistics(ConsoleTerminateEvent $event)
}
}
$client = new Client();
try {
$client = new Client();
$response = $client->post(
'https://drupalconsole.com/statistics?_format=json',
[
'headers' => [
'Accept' => 'application/json',
],
'json' => ['commands' => $commands, 'languages' => $languages]
]
);
if ($response->getStatusCode() === 200) {
$this->fs->remove($filePathToDelete);
//Reset the count attempted to 0.
$this->configurationManager->updateConfigGlobalParameter('statistics.count-attempted', 0);
}
} catch (\Exception $exception) {
$response = $client->post(
'http://drupalconsole.com/statistics?_format=json',
[
'headers' => [
'Accept' => 'application/json',
],
'json' => ['commands' => $commands, 'languages' => $languages]
]
);
//Increase the count attempted in global config.
$countAttempted = $configGlobalAsArray['application']['statistics']['count-attempted'] + 1;
$this->configurationManager->updateConfigGlobalParameter('statistics.count-attempted', $countAttempted);
if ($response->getStatusCode() === 200) {
$this->fs->remove($filePathToDelete);
/* @var DrupalStyle $io */
$io = new DrupalStyle($event->getInput(), $event->getOutput());
$io->error(trim($exception->getMessage()));
}
//Update last attempted in global config.
$this->configurationManager->updateConfigGlobalParameter('statistics.last-attempted', $date);
}
/**
@@ -6,9 +6,8 @@
*/
namespace Drupal\Console\Core\Generator;
use Drupal\Console\Core\Utils\ConfigurationManager;
use Drupal\Console\Core\Utils\NestedArray;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Parser;
/**
* Class InitGenerator
@@ -23,15 +22,21 @@ class InitGenerator extends Generator
*/
protected $nestedArray;
/**
* @var ConfigurationManager
*/
protected $configurationManager;
/**
* InitGenerator constructor.
*
* @param NestedArray $nestedArray
* @param NestedArray $nestedArray
* @param ConfigurationManager $configurationManager
*/
public function __construct(
NestedArray $nestedArray
) {
public function __construct(NestedArray $nestedArray, ConfigurationManager $configurationManager)
{
$this->nestedArray = $nestedArray;
$this->configurationManager = $configurationManager;
}
/**
@@ -45,16 +50,6 @@ public function generate(array $parameters)
$destination = $parameters['destination'];
$configParameters = $parameters['config_parameters'];
$configParameters = array_map(
function ($item) {
if (is_bool($item)) {
return $item ? 'true' : 'false';
}
return $item;
},
$configParameters
);
$configFile = $userHome . 'config.yml';
if ($destination) {
$configFile = $destination . 'config.yml';
@@ -70,10 +65,24 @@ function ($item) {
// If configFile is an override, we only change the value of statistics in the global config.
$consoleDestination = $userHome . 'config.yml';
if ($configFile !== $consoleDestination) {
$this->resetStatisticsConfig($userHome, $configParameters['statistics']);
$this->configurationManager->updateConfigGlobalParameter(
'statistics.enabled',
$configParameters['statistics']
);
unset($configParameters['statistics']);
}
$configParameters = array_map(
function ($item) {
if (is_bool($item)) {
return $item ? 'true' : 'false';
}
return $item;
},
$configParameters
);
$this->renderFile(
'core/init/config.yml.twig',
$configFile,
@@ -99,55 +108,4 @@ function ($item) {
);
}
}
/**
* Reset only the value of statistics is the init command is an override.
*
* @param $homeDirectory
* @param $statisticsValue
* @return int
*/
private function resetStatisticsConfig($homeDirectory, $statisticsValue)
{
$parser = new Parser();
$dumper = new Dumper();
$userConfigFile = $homeDirectory . 'config.yml';
if (!file_exists($userConfigFile)) {
$this->getIo()->error(
sprintf(
$this->trans('commands.settings.set.messages.missing-file'),
$userConfigFile
)
);
return 1;
}
try {
$userConfigFileParsed = $parser->parse(
file_get_contents($userConfigFile)
);
} catch (\Exception $e) {
}
$parents = array_merge(['application'], ['share', 'statistics']);
$this->nestedArray->setValue(
$userConfigFileParsed,
$parents,
filter_var($statisticsValue, FILTER_VALIDATE_BOOLEAN),
true
);
try {
$userConfigFileDump = $dumper->dump($userConfigFileParsed, 10);
} catch (\Exception $e) {
}
try {
file_put_contents($userConfigFile, $userConfigFileDump);
} catch (\Exception $e) {
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.