Skip to content

Commit

Permalink
feat: add create, update, delete, list
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar committed Mar 28, 2022
1 parent 3abf1e1 commit 48294b0
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
58 changes: 57 additions & 1 deletion examples/InvocableScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();
50 changes: 50 additions & 0 deletions src/InfluxDB2/InvocableScriptsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace InfluxDB2;

use InfluxDB2\Model\Script;
use InfluxDB2\Model\ScriptCreateRequest;
use InfluxDB2\Model\ScriptUpdateRequest;
use InfluxDB2\Service\InvocableScriptsService;

/**
Expand All @@ -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();
}
}

0 comments on commit 48294b0

Please sign in to comment.