-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Invocable scripts API (#119)
- Loading branch information
Showing
18 changed files
with
4,116 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
/** | ||
* Show to use Invocable scripts Cloud API to create custom endpoints that query data | ||
* | ||
* Invocable Scripts are supported only in InfluxDB Cloud, currently there is no support in InfluxDB OSS. | ||
*/ | ||
|
||
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'; | ||
$token = 'my-token'; | ||
|
||
// | ||
// Creating client | ||
// | ||
$client = new Client([ | ||
"url" => "https://us-west-2-1.aws.cloud2.influxdata.com", | ||
"token" => $token, | ||
"bucket" => $bucket, | ||
"org" => $organization, | ||
"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 | ||
// | ||
$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; | ||
|
||
// | ||
// Invoke a script | ||
// | ||
print "\n------- Invoke to FluxTables -------\n"; | ||
$tables = $scriptsApi->invokeScript($createdScript->getId(), ["bucket_name" => $bucket]); | ||
foreach ($tables as $table) { | ||
foreach ($table->records as $record) { | ||
print "\n${record['time']} ${record['location']}: ${record['_field']} ${record['_value']}"; | ||
} | ||
} | ||
|
||
// | ||
// Stream of FluxRecords | ||
// | ||
print "\n"; | ||
print "\n------- Invoke to Stream of FluxRecords -------\n"; | ||
$records = $scriptsApi->invokeScriptStream($createdScript->getId(), ["bucket_name" => $bucket]); | ||
foreach ($records->each() as $record) { | ||
print "\n${record['time']} ${record['location']}: ${record['_field']} ${record['_value']}"; | ||
} | ||
|
||
// | ||
// RAW | ||
// | ||
print "\n"; | ||
print "\n------- Invoke to Raw-------\n"; | ||
$raw = $scriptsApi->invokeScriptRaw($createdScript->getId(), ["bucket_name" => $bucket]); | ||
print "RAW output:\n\n ${raw}"; | ||
|
||
// | ||
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.