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

Adds test for PHPCS #266

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/workflows/phpcs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: PHPCS check

on: pull_request

permissions:
actions: read
checks: read
contents: read
deployments: none
issues: read
packages: none
pull-requests: read
repository-projects: none
security-events: none
statuses: read

jobs:
phpcs:
name: PHPCS
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
lfs: false
persist-credentials: false
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
tools: cs2pr
- name: Install dependencies
run:
composer init --name=matomo/queuedtracking --quiet;
composer --no-plugins config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true -n;
composer config repositories.matomo-coding-standards vcs https://github.com/matomo-org/matomo-coding-standards -n;
composer require matomo-org/matomo-coding-standards:dev-master;
composer install --dev --prefer-dist --no-progress --no-suggest
- name: Check PHP code styles
id: phpcs
run: ./vendor/bin/phpcs --report-full --standard=phpcs.xml --report-checkstyle=./phpcs-report.xml
- name: Show PHPCS results in PR
if: ${{ always() && steps.phpcs.outcome == 'failure' }}
run: cs2pr ./phpcs-report.xml --prepend-filename
2 changes: 1 addition & 1 deletion Commands/LockStatus.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand All @@ -15,7 +16,6 @@

class LockStatus extends ConsoleCommand
{

protected function configure()
{
$this->setName('queuedtracking:lock-status');
Expand Down
88 changes: 51 additions & 37 deletions Commands/Monitor.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand All @@ -15,7 +16,6 @@

class Monitor extends ConsoleCommand
{

protected function configure()
{
$this->setName('queuedtracking:monitor');
Expand All @@ -38,7 +38,7 @@ protected function doExecute(): int
}

$output->write(str_repeat("\r\n", 100));
$output->write("\e[".(100)."A");
$output->write("\e[" . (100) . "A");

$iterations = $this->getIterationsFromArg();
if ($iterations !== null) {
Expand All @@ -63,22 +63,25 @@ protected function doExecute(): int
}

$output->writeln(sprintf('Up to <info>%d</> workers will be used', $manager->getNumberOfAvailableQueues()));
$output->writeln(sprintf('Processor will start once there are at least <info>%s</> request sets in the queue',
$manager->getNumberOfRequestsToProcessAtSameTime()));
$output->writeln(sprintf(
'Processor will start once there are at least <info>%s</> request sets in the queue',
$manager->getNumberOfRequestsToProcessAtSameTime()
));
$iterationCount = 0;

$qCurrentPage = 1;
$qCount = count($queues);
$qPerPAge = min(max($this->getPerPageFromArg(), 1), $qCount);
$qPageCount = ceil($qCount / $qPerPAge);

readline_callback_handler_install('', function() {});
stream_set_blocking (STDIN, false);

readline_callback_handler_install('', function () {
});
stream_set_blocking(STDIN, false);

$output->writeln(str_repeat("-", 30));
$output->writeln("<fg=black;bg=white;options=bold>".str_pad(" Q INDEX", 10).str_pad(" | REQUEST SETS", 20)."</>");
$output->writeln("<fg=black;bg=white;options=bold>" . str_pad(" Q INDEX", 10) . str_pad(" | REQUEST SETS", 20) . "</>");
$output->writeln(str_repeat("-", 30));

$lastStatsTimer = microtime(true) - 2;
$lastSumInQueue = false;
$diffSumInQueue = 0;
Expand All @@ -87,13 +90,12 @@ protected function doExecute(): int
$output->write(str_repeat("\r\n", $qPerPAge + 5));

while (1) {
if (microtime(true) - $lastStatsTimer >= 2 || $keyPressed != "")
{
$output->write("\e[".($qPerPAge + 5)."A");
if (microtime(true) - $lastStatsTimer >= 2 || $keyPressed != "") {
$output->write("\e[" . ($qPerPAge + 5) . "A");

$qCurrentPage = min(max($qCurrentPage, 1), $qPageCount);
$memory = $backend->getMemoryStats(); // I know this will only work with redis currently as it is not defined in backend interface etc. needs to be refactored once we add another backend

$sumInQueue = 0;
foreach ($queues as $sumQ) {
$sumInQueue += $sumQ->getNumberOfRequestSetsInQueue();
Expand All @@ -102,31 +104,34 @@ protected function doExecute(): int
if ($lastSumInQueue !== false) {
$diffSumInQueue = $lastSumInQueue - $sumInQueue;
$diffRps = round($diffSumInQueue / (microtime(true) - $lastStatsTimer), 2);
$diffSumInQueue = $diffSumInQueue < 0 ? "<fg=red;options=bold>".abs($diffRps)."</>" : "<fg=green;options=bold>{$diffRps}</>";
$diffSumInQueue = $diffSumInQueue < 0 ? "<fg=red;options=bold>" . abs($diffRps) . "</>" : "<fg=green;options=bold>{$diffRps}</>";
}

$numInQueue = 0;
for ($idxPage = 0; $idxPage < $qPerPAge; $idxPage++) {
$idx = ($qCurrentPage - 1) * $qPerPAge + $idxPage;
if (isset($queues[$idx])) {
$q = $queues[$idx]->getNumberOfRequestSetsInQueue();
$numInQueue += (int)$q;
$output->writeln(str_pad($idx, 10, " ", STR_PAD_LEFT)." | ".str_pad(number_format($q), 16, " ", STR_PAD_LEFT));
$output->writeln(str_pad($idx, 10, " ", STR_PAD_LEFT) . " | " . str_pad(number_format($q), 16, " ", STR_PAD_LEFT));
} else {
$output->writeln(str_pad("", 10)." | ".str_pad("", 16));
$output->writeln(str_pad("", 10) . " | " . str_pad("", 16));
}
}

$output->writeln(str_repeat("-", 30));
$output->writeln("<fg=black;bg=white;options=bold>".str_pad(" ".($qCount)." Q", 10)." | ".str_pad(number_format($sumInQueue)." R", 16)."</>");
$output->writeln("<fg=black;bg=white;options=bold>" . str_pad(" " . ($qCount) . " Q", 10) . " | " . str_pad(number_format($sumInQueue) . " R", 16) . "</>");
$output->writeln(str_repeat("-", 30));
$output->writeln(sprintf(
"Q [%s-%s] | <info>page %s/%s</> | <comment>press (0-9.,q) or arrow(L,R,U,D)</> | diff/sec %s \n".
"%s used memory (%s peak). <info>%d</> workers active.".str_repeat(" ", 15),
($idx - $qPerPAge + 1),
$idx, $qCurrentPage, $qPageCount, $diffSumInQueue,
$memory['used_memory_human'] ?? 'Unknown',
$memory['used_memory_peak_human'] ?? 'Unknown',
"Q [%s-%s] | <info>page %s/%s</> | <comment>press (0-9.,q) or arrow(L,R,U,D)</> | diff/sec %s \n" .
"%s used memory (%s peak). <info>%d</> workers active." . str_repeat(" ", 15),
($idx - $qPerPAge + 1),
$idx,
$qCurrentPage,
$qPageCount,
$diffSumInQueue,
$memory['used_memory_human'] ?? 'Unknown',
$memory['used_memory_peak_human'] ?? 'Unknown',
$lock->getNumberOfAcquiredLocks()
));

Expand All @@ -145,26 +150,35 @@ protected function doExecute(): int
$keyPressed = strlen($keyStroke) == 3 ? $keyStroke[2] : (strlen($keyStroke) > 0 ? $keyStroke[0] : "");
if ($keyPressed != "" and in_array($keyPressed, array(".", ",", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "q"))) {
switch ($keyPressed) {
case "0": case "1": case "2": case "3": case "4":
case "5": case "6": case "7": case "8": case "9":
$keyPressed = $keyPressed != "0" ? $keyPressed : "10";
$qCurrentPage = floor(($qCurrentPage - 0.1) / 10) * 10 + (int)$keyPressed; break;
case "C":
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
$keyPressed = $keyPressed != "0" ? $keyPressed : "10";
$qCurrentPage = floor(($qCurrentPage - 0.1) / 10) * 10 + (int)$keyPressed;
break;
case "C":
$qCurrentPage++;
break;
case "D":
case "D":
$qCurrentPage--;
break;
case "A":
case "A":
$qCurrentPage += 10;
break;
case "B":
case "B":
$qCurrentPage -= 10;
break;
case ",":
case ",":
$qCurrentPage = 1;
break;
case ".":
case ".":
$qCurrentPage = $qPageCount;
break;
case "q":
Expand Down Expand Up @@ -204,7 +218,7 @@ private function getIterationsFromArg()
* Loads the `perpage` argument from the commands arguments.
*
* @return int|null
*/
*/
private function getPerPageFromArg()
{
$perPage = $this->getInput()->getOption('perpage');
Expand Down
3 changes: 1 addition & 2 deletions Commands/PrintQueuedRequests.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand All @@ -15,7 +16,6 @@

class PrintQueuedRequests extends ConsoleCommand
{

protected function configure()
{
$this->setName('queuedtracking:print-queued-requests');
Expand Down Expand Up @@ -54,7 +54,6 @@ protected function doExecute(): int
$output->writeln(var_export($requests, 1));

$output->writeln(sprintf('<info>These were the requests of queue %s. Use <comment>--queue-id=%s</comment> to print only information for this queue.</info>', $thisQueueId, $thisQueueId));

}

return self::SUCCESS;
Expand Down
23 changes: 17 additions & 6 deletions Commands/Process.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand All @@ -20,16 +21,26 @@

class Process extends ConsoleCommand
{

protected function configure()
{
$this->setName('queuedtracking:process');
$this->addRequiredValueOption('queue-id', null, 'If set, will only work on that specific queue. For example "0" or "1" (if there are multiple queues). Not recommended when only one worker is in use. If for example 4 workers are in use, you may want to use 0, 1, 2, or 3.');
$this->addRequiredValueOption('force-num-requests-process-at-once', null, 'If defined, it overwrites the setting of how many requests will be picked out of the queue and processed at once. Must be a number which is >= 1. By default, the configured value from the settings will be used. This can be useful for example if you want to process every single request within the queue. If otherwise a batch size of say 100 is configured, then there may be otherwise 99 requests left in the queue. It can be also useful for testing purposes.');
$this->addRequiredValueOption(
'queue-id',
null,
'If set, will only work on that specific queue. For example "0" or "1" (if there are multiple queues). Not recommended when only one worker is in use. If for example 4 workers are in use, you may want to use 0, 1, 2, or 3.'
);
$this->addRequiredValueOption(
'force-num-requests-process-at-once',
null,
'If defined, it overwrites the setting of how many requests will be picked out of the queue and processed at once. Must be a number which is >= 1. By default, the configured value from the settings will be used.' .
' This can be useful for example if you want to process every single request within the queue.' .
' If otherwise a batch size of say 100 is configured, then there may be otherwise 99 requests left in the queue. It can be also useful for testing purposes.'
);
$this->addRequiredValueOption('cycle', 'c', 'The proccess will automatically loop for "n" cycle time(s), set "0" to infinite.', 1);
$this->addRequiredValueOption('sleep', 's', 'Take a nap for "n" second(s) before recycle, minimum is 1 second.', 1);
$this->addRequiredValueOption('delay', 'd', 'Delay before finished', 0);
$this->setDescription('Processes all queued tracking requests in case there are enough requests in the queue and in case they are not already in process by another script. To keep track of the queue use the <comment>--verbose</comment> option or execute the <comment>queuedtracking:monitor</comment> command.');
$this->setDescription('Processes all queued tracking requests in case there are enough requests in the queue and in case they are not already in process by another script. To keep track of the queue use the <comment>--verbose</comment>' .
' option or execute the <comment>queuedtracking:monitor</comment> command.');
}

/**
Expand Down Expand Up @@ -91,7 +102,7 @@ protected function doExecute(): int
}
$numberOfProcessCycle = (int)$numberOfProcessCycle;
$infiniteCycle = $numberOfProcessCycle == 0;

$delayedBeforeFinish = (int)$input->getOption('delay');

$napster = max(1, $input->getOption('sleep'));
Expand Down Expand Up @@ -156,7 +167,7 @@ protected function doExecute(): int
if ($delayedBeforeFinish > 0) {
sleep($delayedBeforeFinish);
}

return self::SUCCESS;
}

Expand Down
19 changes: 10 additions & 9 deletions Commands/Test.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -91,7 +92,7 @@ protected function doExecute(): int

$extension = new \ReflectionExtension('redis');
$output->writeln('PHPRedis version: ' . $extension->getVersion());
} catch(\Exception $e) {
} catch (\Exception $e) {
$output->writeln('No PHPRedis extension (not a problem if sentinel is used):' . $e->getMessage());
}
}
Expand All @@ -107,12 +108,14 @@ protected function doExecute(): int

$redis = $backend->getConnection();
if ($isUsingRedis && !$shouldSkipCheckingMemoryConfigValues) {

$evictionPolicy = $this->getRedisConfig($redis, 'maxmemory-policy');
$output->writeln('MaxMemory Eviction Policy config: ' . $evictionPolicy);

if ($evictionPolicy !== 'allkeys-lru' && $evictionPolicy !== 'noeviction') {
$output->writeln('<error>The eviction policy can likely lead to errors when memory is low. We recommend to use eviction policy <comment>allkeys-lru</comment> or alternatively <comment>noeviction</comment>. Read more here: http://redis.io/topics/lru-cache</error>');
$output->writeln(
'<error>The eviction policy can likely lead to errors when memory is low. We recommend to use eviction policy <comment>allkeys-lru</comment> or alternatively <comment>noeviction</comment>.' .
' Read more here: http://redis.io/topics/lru-cache</error>'
);
}

$evictionPolicy = $this->getRedisConfig($redis, 'maxmemory');
Expand All @@ -126,7 +129,7 @@ protected function doExecute(): int
$output->writeln('Redis is connected: ' . (int) $redis->isConnected());
}

if ($backend->testConnection()){
if ($backend->testConnection()) {
$output->writeln('Connection works in general');
} else {
$output->writeln('Connection does not actually work: ' . $redis->getLastError());
Expand All @@ -144,7 +147,7 @@ protected function doExecute(): int
if (!$backend->setIfNotExists('foo', 'bar', 5)) {
$message = "setIfNotExists(foo, bar, 1) does not work, most likely we won't be able to acquire a lock: " . $backend->getLastError();
$output->writeln($message);
} else{
} else {
$initialTtl = $backend->getTimeToLive('foo');
if ($initialTtl >= 3000 && $initialTtl <= 5000) {
$output->writeln('Initial expire seems to be set correctly');
Expand Down Expand Up @@ -195,7 +198,6 @@ protected function doExecute(): int
$backend->appendValuesToList('fooList', array('value1', 'value2', 'value3'));
$values = $backend->getFirstXValuesFromList('fooList', 2);
if ($values == array('value1', 'value2')) {

$backend->removeFirstXValuesFromList('fooList', 1);
$backend->removeFirstXValuesFromList('fooList', 1);
$values = $backend->getFirstXValuesFromList('fooList', 2);
Expand All @@ -204,7 +206,6 @@ protected function doExecute(): int
} else {
$output->writeln('List feature seems to work only partially: ' . var_export($values, 1));
}

} else {
$output->writeln('<error>List feature seems to not work fine: ' . $redis->getLastError() . '</error>');
}
Expand Down Expand Up @@ -247,11 +248,11 @@ private function testRedis($redis, $method, $params, $keyToCleanUp)

$result = call_user_func_array(array($redis, $method), $params);

$paramsMapped = array_map(function($item) {
$paramsMapped = array_map(function ($item) {
if (is_string($item)) {
return $item;
}

return str_replace(["\r", "\n", " "], '', var_export($item, true));
}, $params);
$paramsInline = implode(', ', $paramsMapped);
Expand Down
Loading
Loading