From 48294b0f69470b84d5760132a95992f4940871ef Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Mon, 28 Mar 2022 10:33:53 +0200 Subject: [PATCH] feat: add create, update, delete, list --- examples/InvocableScripts.php | 58 ++++++++++++++++++++++++++- src/InfluxDB2/InvocableScriptsApi.php | 50 +++++++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) diff --git a/examples/InvocableScripts.php b/examples/InvocableScripts.php index d1a98610..8e78f3b2 100644 --- a/examples/InvocableScripts.php +++ b/examples/InvocableScripts.php @@ -8,6 +8,9 @@ require __DIR__ . '/../vendor/autoload.php'; use InfluxDB2\Client; +use InfluxDB2\Model\ScriptCreateRequest; +use InfluxDB2\Model\ScriptUpdateRequest; +use InfluxDB2\Point; $organization = 'my-org'; $bucket = 'my-bucket'; @@ -24,10 +27,63 @@ "precision" => InfluxDB2\Model\WritePrecision::S ]); +// +// Prepare data +// +$point1 = Point::measurement("my_measurement") + ->addTag("location", "Prague") + ->addField("temperature", 25.3); +$point2 = Point::measurement("my_measurement") + ->addTag("location", "New York") + ->addField("temperature", 24.3); +$client->createWriteApi()->write([$point1, $point2]); + // // Creating InvocableScripts Api // -$invocableScriptsApi = $client->createInvocableScriptsApi(); +$scriptsApi = $client->createInvocableScriptsApi(); + +// +// Create Invocable Script +// +print "\n------- Create -------\n"; +$scriptQuery = 'from(bucket: params.bucket_name) |> range(start: -30d) |> limit(n:2)'; +$createRequest = new ScriptCreateRequest([ + 'name' => "my_script_" . microtime(), + 'description' => "my first try", + 'script' => $scriptQuery, + 'language' => InfluxDB2\Model\ScriptLanguage::FLUX, +]); +$createdScript = $scriptsApi->createScript($createRequest); +print $createdScript; + +// +// Update Invocable Script +// +print "\n------- Update -------\n"; +$updateRequest = new ScriptUpdateRequest([ + 'description' => "my updated description" +]); +$createdScript = $scriptsApi->updateScript($createdScript->getId(), $updateRequest); +print $createdScript; + +// +// List scripts +// +print "\n------- List -------\n"; +$scripts = $scriptsApi->findScripts(); +foreach ($scripts as $script) { + $scriptId = $script->getId(); + $scriptName = $script->getName(); + $scriptDescription = $script->getDescription(); + print " ---\n ID: $scriptId\n Name: $scriptName\n Description: $scriptDescription"; +} +// +// Delete previously created Script +// +print "\n------- Delete -------\n"; +$scriptsApi->deleteScript($createdScript->getId()); +print "Successfully deleted script: '" . $createdScript->getName(); $client->close(); diff --git a/src/InfluxDB2/InvocableScriptsApi.php b/src/InfluxDB2/InvocableScriptsApi.php index 4a7d883f..ce01721c 100644 --- a/src/InfluxDB2/InvocableScriptsApi.php +++ b/src/InfluxDB2/InvocableScriptsApi.php @@ -2,6 +2,9 @@ namespace InfluxDB2; +use InfluxDB2\Model\Script; +use InfluxDB2\Model\ScriptCreateRequest; +use InfluxDB2\Model\ScriptUpdateRequest; use InfluxDB2\Service\InvocableScriptsService; /** @@ -24,4 +27,51 @@ public function __construct(InvocableScriptsService $service) { $this->service = $service; } + + /** + * Create a script. + * + * @param ScriptCreateRequest $createRequest The script to create. + * @return Script The created script. + */ + public function createScript(ScriptCreateRequest $createRequest): Script + { + return $this->service->postScripts($createRequest); + } + + /** + * Update a script. + * + * @param string $scriptId The ID of the script to update. (required) + * @param ScriptUpdateRequest $updateRequest Script updates to apply (required) + * @return Script The updated script. + */ + public function updateScript(string $scriptId, ScriptUpdateRequest $updateRequest): Script + { + return $this->service->patchScriptsID($scriptId, $updateRequest); + } + + /** + * Delete a script. + * + * @param string $scriptId The ID of the script to delete. (required) + * @return void + */ + public function deleteScript(string $scriptId) + { + $this->service->deleteScriptsID($scriptId); + } + + /** + * List scripts. + * + * @param int|null $limit The number of scripts to return. + * @param int|null $offset The offset for pagination. + * + * @return Script[] + */ + public function findScripts(int $limit = null, int $offset = null): array + { + return $this->service->getScripts($limit, $offset)->getScripts(); + } }