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

Feature/cli table output #386

Merged
merged 8 commits into from
Dec 12, 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
62 changes: 31 additions & 31 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions src/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ public function run(): Task {
throw new RuntimeException('Failed to prepare query');
}

$queryResult = $manticoreClient
->sendRequest($query, $payload->path)
->getResult();
return TaskResult::raw($queryResult);
$resp = $manticoreClient
->sendRequest($query, $payload->path);
return TaskResult::fromResponse($resp);
};

return Task::create(
Expand Down
1 change: 1 addition & 0 deletions src/Lib/QueryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ public static function detectPluginPrefixFromRequest(Request $request): string {
foreach ($plugins as $plugin) {
$pluginPrefix = $prefix . ucfirst(Strings::camelcaseBySeparator($plugin['short'], '-'));
/** @var BasePayload<T> $pluginPayloadClass */

$pluginPayloadClass = "$pluginPrefix\\Payload";
$pluginPayloadClass::setParser(static::$sqlQueryParser);
$hasMatch = $pluginPayloadClass::hasMatch($request);
Expand Down
16 changes: 15 additions & 1 deletion src/Network/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Manticoresearch\Buddy\Core\Error\GenericError;
use Manticoresearch\Buddy\Core\Error\InvalidNetworkRequestError;
use Manticoresearch\Buddy\Core\ManticoreSearch\RequestFormat;
use Manticoresearch\Buddy\Core\Network\OutputFormat;
use Manticoresearch\Buddy\Core\Network\Request;
use Manticoresearch\Buddy\Core\Network\Response;
use Manticoresearch\Buddy\Core\Task\Column;
Expand Down Expand Up @@ -87,6 +88,8 @@ static function () use ($requestId, $body, $channel, $startTime) {
*/
public static function process(string $id, string $payload): Response {
try {
/** @var int $startTime */
$startTime = hrtime(true);
$request = Request::fromString($payload, $id);
$handler = QueryProcessor::process($request)->run();

Expand All @@ -101,7 +104,18 @@ public static function process(string $id, string $payload): Response {
$result = $handler->getResult();
}

$response = Response::fromResult($result, $request->format);
// Check if this is a cli and we need to activate Table Formatter
$outputFormat = $request->getOutputFormat();
$message = match ($outputFormat) {
// TODO: Maybe later we can use meta for time, but not now cuz no time for non select
OutputFormat::Table => $result->getTableFormatted($startTime),
default => $result->getStruct(),
};
$response = Response::fromMessageAndMeta(
$message,
$result->getMeta(),
$request->format,
);
} catch (Throwable $e) {
if (isset($request)) {
/** @var string $originalError */
Expand Down
7 changes: 3 additions & 4 deletions src/Plugin/Alias/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ public function run(): Task {
throw new RuntimeException('Failed to prepare query');
}

$queryResult = $manticoreClient
->sendRequest($query, $payload->path)
->getResult();
return TaskResult::raw($queryResult);
$queryResponse = $manticoreClient
->sendRequest($query, $payload->path);
return TaskResult::fromResponse($queryResponse);
};

return Task::create(
Expand Down
8 changes: 4 additions & 4 deletions src/Plugin/AlterDistributedTable/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ private static function createTable(string $tableName, array $options, Client $m
}
}

$request = $manticoreClient->sendRequest($sql);
if ($request->hasError()) {
throw ManticoreSearchResponseError::create((string)$request->getError());
$response = $manticoreClient->sendRequest($sql);
if ($response->hasError()) {
throw ManticoreSearchResponseError::create((string)$response->getError());
}

return TaskResult::raw($request->getResult());
return TaskResult::fromResponse($response);
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/Plugin/AlterRenameTable/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Manticoresearch\Buddy\Core\Error\ManticoreSearchClientError;
use Manticoresearch\Buddy\Core\Error\ManticoreSearchResponseError;
use Manticoresearch\Buddy\Core\ManticoreSearch\Client;
use Manticoresearch\Buddy\Core\ManticoreSearch\Response;
use Manticoresearch\Buddy\Core\Plugin\BaseHandlerWithClient;
use Manticoresearch\Buddy\Core\Task\Task;
use Manticoresearch\Buddy\Core\Task\TaskResult;
Expand Down Expand Up @@ -60,11 +61,11 @@ public function run(): Task {
}
self::createTableLike($payload->sourceTableName, $payload->destinationTableName, $client);

$result = self::attachTable($payload->sourceTableName, $payload->destinationTableName, $client);
$resp = self::attachTable($payload->sourceTableName, $payload->destinationTableName, $client);

self::dropTable($payload->sourceTableName, $client);

return TaskResult::raw($result);
return TaskResult::fromResponse($resp);
};

return Task::create(
Expand Down Expand Up @@ -103,15 +104,15 @@ private static function createTableLike(
* @param string $sourceTableName
* @param string $destinationTableName
* @param Client $client
* @return mixed
* @return Response
* @throws GenericError
* @throws ManticoreSearchClientError
*/
private static function attachTable(
string $sourceTableName,
string $destinationTableName,
Client $client
): mixed {
): Response {

$sql = /** @lang ManticoreSearch */
"ATTACH TABLE $sourceTableName TO TABLE $destinationTableName";
Expand All @@ -122,7 +123,7 @@ private static function attachTable(
'Reason: ' . $result->getError()
);
}
return $result->getResult();
return $result;
}


Expand Down
33 changes: 4 additions & 29 deletions src/Plugin/CliTable/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
namespace Manticoresearch\Buddy\Base\Plugin\CliTable;

use Manticoresearch\Buddy\Core\ManticoreSearch\Client;
use Manticoresearch\Buddy\Core\Plugin\BaseHandlerWithTableFormatter;
use Manticoresearch\Buddy\Core\Plugin\TableFormatter;
use Manticoresearch\Buddy\Core\Plugin\BaseHandlerWithClient;
use Manticoresearch\Buddy\Core\Task\Task;
use Manticoresearch\Buddy\Core\Task\TaskResult;
use RuntimeException;

/**
* This is the class to return response to the '/cli' endpoint in table format
*/
final class Handler extends BaseHandlerWithTableFormatter {
final class Handler extends BaseHandlerWithClient {

/**
* Initialize the executor
Expand All @@ -44,42 +43,18 @@ public function run(): Task {
$taskFn = static function (
Payload $payload,
Client $manticoreClient,
?TableFormatter $tableFormatter
): TaskResult {
$time0 = hrtime(true);
$resp = $manticoreClient->sendRequest(
$payload->query,
$payload->path,
disableAgentHeader: true
);
$data = null;
$total = -1;
$respBody = $resp->getBody();
$result = (array)json_decode($respBody, true);
if ($tableFormatter === null
|| (!isset($result['error']) && (!isset($result[0]) || !is_array($result[0])))
) {
return TaskResult::raw($result);
}
// Convert JSON response from Manticore to table format
if (isset($result['error'])) {
/** @var string $errorMsg */
$errorMsg = $result['error'];
if ($errorMsg !== '') {
return TaskResult::raw($tableFormatter->getTable($time0, $data, $total, $errorMsg));
}
}

if (isset($result[0]) && is_array($result[0])) {
static::processResultInfo($result[0], $data, $total);
}

return TaskResult::raw($tableFormatter->getTable($time0, $data, $total));
return TaskResult::fromResponse($resp);
};

return Task::create(
$taskFn,
[$this->payload, $this->manticoreClient, $this->tableFormatter]
[$this->payload, $this->manticoreClient]
)->run();
}

Expand Down
Loading
Loading