Skip to content

Commit

Permalink
Refactor to support automatic CLI table output for all Buddy plugins …
Browse files Browse the repository at this point in the history
…not just Show
  • Loading branch information
donhardman committed Oct 29, 2024
1 parent b8acd10 commit 27cae08
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 55 deletions.
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) {
Buddy::error($e, "[$id] processing error");
if (isset($request)) {
Expand Down
13 changes: 3 additions & 10 deletions src/Plugin/Show/CreateTableHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Manticoresearch\Buddy\Core\ManticoreSearch\Client;
use Manticoresearch\Buddy\Core\Plugin\BaseHandlerWithTableFormatter;
use Manticoresearch\Buddy\Core\Plugin\TableFormatter;
use Manticoresearch\Buddy\Core\Task\Task;
use Manticoresearch\Buddy\Core\Task\TaskResult;
use RuntimeException;
Expand Down Expand Up @@ -42,18 +41,12 @@ public function run(): Task {
// We just waiting for a thread to be done
$taskFn = static function (
Payload $payload,
Client $manticoreClient,
TableFormatter $tableFormatter
Client $manticoreClient
): TaskResult {
$time0 = hrtime(true);
// First, get response from the manticore
$query = "SHOW CREATE TABLE {$payload->table}";
/** @var array{0:array{data:array<mixed>}} */
$result = $manticoreClient->sendRequest($query, $payload->path)->getResult();
if ($payload->hasCliEndpoint) {
$total = sizeof($result[0]['data']);
return TaskResult::raw($tableFormatter->getTable($time0, $result[0]['data'], $total));
}
$result = $manticoreClient->sendRequest($query)->getResult();

// It's important to have ` and 2 spaces for Apache Superset
foreach ($result[0]['data'] as &$row) {
Expand All @@ -75,7 +68,7 @@ public function run(): Task {

return Task::create(
$taskFn,
[$this->payload, $this->manticoreClient, $this->tableFormatter]
[$this->payload, $this->manticoreClient]
)->run();
}
}
11 changes: 2 additions & 9 deletions src/Plugin/Show/ExpandedTablesHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Manticoresearch\Buddy\Core\ManticoreSearch\Client;
use Manticoresearch\Buddy\Core\Plugin\BaseHandlerWithTableFormatter;
use Manticoresearch\Buddy\Core\Plugin\TableFormatter;
use Manticoresearch\Buddy\Core\Task\Column;
use Manticoresearch\Buddy\Core\Task\Task;
use Manticoresearch\Buddy\Core\Task\TaskResult;
Expand Down Expand Up @@ -44,15 +43,13 @@ public function run(): Task {
$taskFn = static function (
Payload $payload,
Client $manticoreClient,
TableFormatter $tableFormatter
): TaskResult {
$time0 = hrtime(true);
// First, get response from the manticore
$query = 'SHOW TABLES';
if ($payload->like) {
$query .= " LIKE '{$payload->like}'";
}
$resp = $manticoreClient->sendRequest($query, $payload->path);
$resp = $manticoreClient->sendRequest($query);
/** @var array<int,array{error:string,data:array<int,array<string,string>>,total?:int,columns?:string}> $result */
$result = $resp->getResult();
$total = $result[0]['total'] ?? -1;
Expand All @@ -76,10 +73,6 @@ public function run(): Task {
}
}

if ($payload->hasCliEndpoint) {
return TaskResult::raw($tableFormatter->getTable($time0, $result[0]['data'], $total));
}

return match ($payload->tableType) {
'full' => TaskResult::withData($result[0]['data'])
->column("Tables_in_{$payload->database}", Column::String)
Expand All @@ -95,7 +88,7 @@ public function run(): Task {

return Task::create(
$taskFn,
[$this->payload, $this->manticoreClient, $this->tableFormatter]
[$this->payload, $this->manticoreClient]
)->run();
}
}
2 changes: 1 addition & 1 deletion src/Plugin/Show/FullColumnsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function run(): Task {
$taskFn = static function (Payload $payload, Client $manticoreClient): TaskResult {
$query = "DESC {$payload->table}";
/** @var array{0:array{data:array<mixed>}} */
$result = $manticoreClient->sendRequest($query, $payload->path)->getResult();
$result = $manticoreClient->sendRequest($query)->getResult();
$base = [
'Field' => '',
'Type' => '',
Expand Down
14 changes: 2 additions & 12 deletions src/Plugin/Show/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ final class Payload extends BasePayload {

public string $tableType;

public string $path;
public bool $hasCliEndpoint;

/**
* Get description for this plugin
* @return string
Expand Down Expand Up @@ -105,7 +102,6 @@ protected static function fromExpandedTablesRequest(Request $request): static {
} elseif ($m['where'] ?? '') {
$self->like = $m['where'];
}
[$self->path, $self->hasCliEndpoint] = self::getEndpointInfo($request);
return $self;
}

Expand All @@ -114,9 +110,7 @@ protected static function fromExpandedTablesRequest(Request $request): static {
* @return static
*/
protected static function fromVersionRequest(Request $request): static {
$self = new static();
[$self->path, $self->hasCliEndpoint] = self::getEndpointInfo($request);
return $self;
return new static();
}

/**
Expand All @@ -135,7 +129,6 @@ protected static function fromCreateTableRequest(Request $request): static {
$self->database = $m[1];
$self->table = $m[2];

[$self->path, $self->hasCliEndpoint] = self::getEndpointInfo($request);
return $self;
}

Expand All @@ -144,9 +137,7 @@ protected static function fromCreateTableRequest(Request $request): static {
* @return static
*/
protected static function fromSimpleRequest(Request $request): static {
$self = new static();
[$self->path, $self->hasCliEndpoint] = self::getEndpointInfo($request);
return $self;
return new static();
}

/**
Expand All @@ -173,7 +164,6 @@ protected static function fromColumnsRequest(Request $request): static {
$self = new static();
$self->database = $m[2];
$self->table = $m[1];
[$self->path, $self->hasCliEndpoint] = self::getEndpointInfo($request);
return $self;
}
/**
Expand Down
12 changes: 2 additions & 10 deletions src/Plugin/Show/QueriesHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Manticoresearch\Buddy\Core\ManticoreSearch\Client;
use Manticoresearch\Buddy\Core\Plugin\BaseHandlerWithTableFormatter;
use Manticoresearch\Buddy\Core\Plugin\TableFormatter;
use Manticoresearch\Buddy\Core\Task\Task;
use Manticoresearch\Buddy\Core\Task\TaskPool;
use Manticoresearch\Buddy\Core\Task\TaskResult;
Expand Down Expand Up @@ -50,31 +49,24 @@ public function run(): Task {
// We run in a thread anyway but in case if we need blocking
// We just waiting for a thread to be done
$taskFn = static function (
Payload $payload,
Client $manticoreClient,
TableFormatter $tableFormatter,
array $tasks
): TaskResult {
// First, get response from the manticore
$time0 = hrtime(true);
$resp = $manticoreClient->sendRequest(
'SELECT * FROM @@system.sessions',
$payload->path
'SELECT * FROM @@system.sessions'
);
$result = static::formatResponse($resp->getBody());
// Second, get our own queries and append to the final result
/** @var array{0:array{data:array<mixed>,total:int}} $result */
$result[0]['data'] = array_merge($result[0]['data'], $tasks);
$result[0]['total'] += sizeof($tasks);
if ($payload->hasCliEndpoint) {
return TaskResult::raw($tableFormatter->getTable($time0, $result[0]['data'], $result[0]['total']));
}
return TaskResult::raw($result);
};

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

Expand Down
11 changes: 2 additions & 9 deletions src/Plugin/Show/SchemasHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Manticoresearch\Buddy\Core\ManticoreSearch\Client;
use Manticoresearch\Buddy\Core\Plugin\BaseHandlerWithTableFormatter;
use Manticoresearch\Buddy\Core\Plugin\TableFormatter;
use Manticoresearch\Buddy\Core\Task\Column;
use Manticoresearch\Buddy\Core\Task\Task;
use Manticoresearch\Buddy\Core\Task\TaskResult;
Expand Down Expand Up @@ -44,24 +43,18 @@ public function run(): Task {
$taskFn = static function (
Payload $payload,
Client $manticoreClient,
TableFormatter $tableFormatter
): TaskResult {
$time0 = hrtime(true);
// First, get response from the manticore
$query = 'SHOW DATABASES';
/** @var array{0:array{data:array<mixed>}} */
$result = $manticoreClient->sendRequest($query, $payload->path)->getResult();
$total = sizeof($result[0]['data']);
if ($payload->hasCliEndpoint) {
return TaskResult::raw($tableFormatter->getTable($time0, $result[0]['data'], $total));
}
$result = $manticoreClient->sendRequest($query)->getResult();
return TaskResult::withData($result[0]['data'])
->column('Database', Column::String);
};

return Task::create(
$taskFn,
[$this->payload, $this->manticoreClient, $this->tableFormatter]
[$this->payload, $this->manticoreClient]
)->run();
}
}
6 changes: 3 additions & 3 deletions src/Plugin/Show/UnsupportedStmtHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public static function handleQuery(
if (preg_match('/^show( open)? tables from (Manticore|`Manticore`)(\s.*|$)/is', $payload->query)) {
$query = 'SHOW TABLES';
/** @var array{0:array{data:array<mixed>}} */
$result = $manticoreClient->sendRequest($query, $payload->path)->getResult();
$result = $manticoreClient->sendRequest($query)->getResult();
$data = $result[0]['data'];
return TaskResult::withData($data)
->column('Table', Column::String)
Expand All @@ -203,7 +203,7 @@ public static function handleQuery(
if (preg_match('/^show table status from( Manticore| `Manticore`)?(\s*.*|$)/is', $payload->query, $matches)) {
$query = 'SHOW TABLES';
/** @var array{0:array{data:array<int,array<mixed>>}} */
$result = $manticoreClient->sendRequest($query, $payload->path)->getResult();
$result = $manticoreClient->sendRequest($query)->getResult();
$tableNames = array_map(
fn($item) => $item['Table'] ?? '',
$result[0]['data']
Expand Down Expand Up @@ -294,7 +294,7 @@ public static function handleQuery(
if (stripos($payload->query, 'show variables') === 0) {
$query = 'SHOW VARIABLES';
/** @var array{0:array{data:array<int,array<mixed>>}} */
$result = $manticoreClient->sendRequest($query, $payload->path)->getResult();
$result = $manticoreClient->sendRequest($query)->getResult();
$condData = static::getVariablesWithCondition($result[0]['data'], $payload->query);
return TaskResult::withData($condData)
->column('Variable_name', Column::String)
Expand Down

0 comments on commit 27cae08

Please sign in to comment.