From 8bd15a302a8e92c8b75d00c2a363818661c77e88 Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Thu, 11 Sep 2014 14:50:31 +0200 Subject: [PATCH 01/15] Added HTTP client --- .gitignore | 1 + .travis.yml | 10 + README.md | 36 + composer.json | 10 +- composer.lock | 967 +++++++++++++++++- phpunit.xml.dist | 20 + spec/InfluxDB/Adapter/GuzzleAdapterSpec.php | 29 + spec/InfluxDB/Adapter/UdpAdapterSpec.php | 6 +- spec/InfluxDB/OptionsSpec.php | 37 + src/InfluxDB/Adapter/AdapterInterface.php | 8 +- src/InfluxDB/Adapter/ConnectableInterface.php | 8 + src/InfluxDB/Adapter/GuzzleAdapter.php | 40 + src/InfluxDB/Adapter/UdpAdapter.php | 31 +- src/InfluxDB/Options.php | 89 ++ tests/InfluxDB/ClientTest.php | 61 ++ tests/bootstrap.php | 11 + 16 files changed, 1325 insertions(+), 39 deletions(-) create mode 100644 phpunit.xml.dist create mode 100644 spec/InfluxDB/Adapter/GuzzleAdapterSpec.php create mode 100644 spec/InfluxDB/OptionsSpec.php create mode 100644 src/InfluxDB/Adapter/ConnectableInterface.php create mode 100644 src/InfluxDB/Adapter/GuzzleAdapter.php create mode 100644 src/InfluxDB/Options.php create mode 100644 tests/InfluxDB/ClientTest.php create mode 100644 tests/bootstrap.php diff --git a/.gitignore b/.gitignore index 22d0d82f8..54527ba06 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ vendor +tags diff --git a/.travis.yml b/.travis.yml index 9473c3205..38b665b5b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,9 +4,19 @@ php: - 5.5 - 5.6 +before_install: + - sudo apt-get update + - sudo apt-get install libcurl4-openssl-dev libjson0-dev + - wget http://s3.amazonaws.com/influxdb/influxdb_latest_amd64.deb + - sudo useradd influxdb + - sudo dpkg -i influxdb_latest_amd64.deb + - travis_retry sudo service influxdb restart + - sudo service influxdb status + before_script: - composer selfupdate - composer install --prefer-source script: - vendor/bin/phpspec run + - vendor/bin/phpunit diff --git a/README.md b/README.md index 6d4a9547d..0bca6b8dd 100644 --- a/README.md +++ b/README.md @@ -33,3 +33,39 @@ Or place it in your require section } ``` +## Send data using HTTP json API + +Actually we using Guzzle as HTTP client + +```php +$influx->mark("tcp.test", ["mark" => "element"]); +``` + +## Prepare lib dependencies + +Use your DiC or Service Locator in order to provide a configured client + +```php +setHost("analytics.mine.domain.tld"); +$options->setPort(8086); +$options->setUsername("root"); +$options->setPassword("root"); + +$guzzleHttp = new GuzzleHttpClient(); +$adapter = new GuzzleAdapter($guzzleHttp, $options); +$adapter->setDatabase("mine"); + +$influx = new Client(); +$influx->setAdapter($adapter); + +$influx->mark("tcp.test", ["mark" => "element"]); +``` + diff --git a/composer.json b/composer.json index c9db8a5a6..758ac8fdb 100644 --- a/composer.json +++ b/composer.json @@ -2,8 +2,14 @@ "name": "corley/influxdb-sdk", "license": "MIT", "description": "Send your app metrics to InfluxDB", + "require": { + "guzzlehttp/guzzle": "~4" + }, "require-dev": { - "phpspec/phpspec": "2.*" + "phpspec/phpspec": "2.*", + "phpunit/phpunit": "4.*", + "zendframework/zend-stdlib": "~2", + "crodas/influx-php": "dev-master" }, "homepage": "http://www.corley.it/", "keywords": ["influxdb", "udp", "sdk"], @@ -23,7 +29,7 @@ ], "autoload": { "psr-0": { - "InfluxDB": "./src/" + "InfluxDB": ["./src/", "./tests"] } } } diff --git a/composer.lock b/composer.lock index 4abb61a3f..57dd307bc 100644 --- a/composer.lock +++ b/composer.lock @@ -4,11 +4,274 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "ebef29b39aea3cab015d3bf51e00121e", + "hash": "6ba22c8acf314366cc2d58a2cfaccbd3", "packages": [ - + { + "name": "guzzlehttp/guzzle", + "version": "4.2.2", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle.git", + "reference": "9c4fbbf6457768f5036fbd88f1229f3fca812a5d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/9c4fbbf6457768f5036fbd88f1229f3fca812a5d", + "reference": "9c4fbbf6457768f5036fbd88f1229f3fca812a5d", + "shasum": "" + }, + "require": { + "ext-json": "*", + "guzzlehttp/streams": "~2.1", + "php": ">=5.4.0" + }, + "require-dev": { + "ext-curl": "*", + "phpunit/phpunit": "~4.0", + "psr/log": "~1.0" + }, + "suggest": { + "ext-curl": "Guzzle will use specific adapters if cURL is present" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.2-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\": "src/" + }, + "files": [ + "src/functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle is a PHP HTTP client library and framework for building RESTful web service clients", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "rest", + "web service" + ], + "time": "2014-09-08 22:11:58" + }, + { + "name": "guzzlehttp/streams", + "version": "2.1.0", + "source": { + "type": "git", + "url": "https://github.com/guzzle/streams.git", + "reference": "f91b721d73f0e561410903b3b3c90a5d0e40b534" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/streams/zipball/f91b721d73f0e561410903b3b3c90a5d0e40b534", + "reference": "f91b721d73f0e561410903b3b3c90a5d0e40b534", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Stream\\": "src/" + }, + "files": [ + "src/functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Provides a simple abstraction over streams of data (Guzzle 4+)", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "Guzzle", + "stream" + ], + "time": "2014-08-17 21:15:53" + } ], "packages-dev": [ + { + "name": "crodas/influx-php", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/crodas/InfluxPHP.git", + "reference": "205bd51cb44dec70d0da06ba87e47f5c0986bdb4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/crodas/InfluxPHP/zipball/205bd51cb44dec70d0da06ba87e47f5c0986bdb4", + "reference": "205bd51cb44dec70d0da06ba87e47f5c0986bdb4", + "shasum": "" + }, + "type": "library", + "autoload": { + "classmap": [ + "lib/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD" + ], + "authors": [ + { + "name": "César D. Rodas", + "email": "crodas@php.net" + } + ], + "description": "Simple client for InfluxDB", + "time": "2014-09-04 17:26:53" + }, + { + "name": "ocramius/instantiator", + "version": "1.1.3", + "source": { + "type": "git", + "url": "https://github.com/Ocramius/Instantiator.git", + "reference": "e24a12178906ff2e7471b8aaf3a0eb789b59f881" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Ocramius/Instantiator/zipball/e24a12178906ff2e7471b8aaf3a0eb789b59f881", + "reference": "e24a12178906ff2e7471b8aaf3a0eb789b59f881", + "shasum": "" + }, + "require": { + "ocramius/lazy-map": "1.0.*", + "php": "~5.3" + }, + "require-dev": { + "athletic/athletic": "~0.1.8", + "ext-pdo": "*", + "ext-phar": "*", + "phpunit/phpunit": "~4.0", + "squizlabs/php_codesniffer": "2.0.*@ALPHA" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "psr-0": { + "Instantiator\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com", + "homepage": "http://ocramius.github.com/" + } + ], + "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", + "homepage": "https://github.com/Ocramius/Instantiator", + "keywords": [ + "constructor", + "instantiate" + ], + "time": "2014-08-25 14:48:16" + }, + { + "name": "ocramius/lazy-map", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/Ocramius/LazyMap.git", + "reference": "7fe3d347f5e618bcea7d39345ff83f3651d8b752" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Ocramius/LazyMap/zipball/7fe3d347f5e618bcea7d39345ff83f3651d8b752", + "reference": "7fe3d347f5e618bcea7d39345ff83f3651d8b752", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "athletic/athletic": "~0.1.6", + "phpmd/phpmd": "1.5.*", + "phpunit/phpunit": ">=3.7", + "satooshi/php-coveralls": "~0.6", + "squizlabs/php_codesniffer": "1.4.*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-0": { + "LazyMap\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com", + "homepage": "http://ocramius.github.com/", + "role": "Developer" + } + ], + "description": "A library that provides lazy instantiation logic for a map of objects", + "homepage": "https://github.com/Ocramius/LazyMap", + "keywords": [ + "lazy", + "lazy instantiation", + "lazy loading", + "map", + "service location" + ], + "time": "2013-11-09 22:30:54" + }, { "name": "phpdocumentor/reflection-docblock", "version": "2.0.3", @@ -223,6 +486,650 @@ ], "time": "2014-07-18 21:26:55" }, + { + "name": "phpunit/php-code-coverage", + "version": "2.0.11", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-code-coverage.git", + "reference": "53603b3c995f5aab6b59c8e08c3a663d2cc810b7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/53603b3c995f5aab6b59c8e08c3a663d2cc810b7", + "reference": "53603b3c995f5aab6b59c8e08c3a663d2cc810b7", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "phpunit/php-file-iterator": "~1.3", + "phpunit/php-text-template": "~1.2", + "phpunit/php-token-stream": "~1.3", + "sebastian/environment": "~1.0", + "sebastian/version": "~1.0" + }, + "require-dev": { + "ext-xdebug": ">=2.1.4", + "phpunit/phpunit": "~4.1" + }, + "suggest": { + "ext-dom": "*", + "ext-xdebug": ">=2.2.1", + "ext-xmlwriter": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "include-path": [ + "" + ], + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", + "homepage": "https://github.com/sebastianbergmann/php-code-coverage", + "keywords": [ + "coverage", + "testing", + "xunit" + ], + "time": "2014-08-31 06:33:04" + }, + { + "name": "phpunit/php-file-iterator", + "version": "1.3.4", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-file-iterator.git", + "reference": "acd690379117b042d1c8af1fafd61bde001bf6bb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/acd690379117b042d1c8af1fafd61bde001bf6bb", + "reference": "acd690379117b042d1c8af1fafd61bde001bf6bb", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "File/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "include-path": [ + "" + ], + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "FilterIterator implementation that filters files based on a list of suffixes.", + "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", + "keywords": [ + "filesystem", + "iterator" + ], + "time": "2013-10-10 15:34:57" + }, + { + "name": "phpunit/php-text-template", + "version": "1.2.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-text-template.git", + "reference": "206dfefc0ffe9cebf65c413e3d0e809c82fbf00a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/206dfefc0ffe9cebf65c413e3d0e809c82fbf00a", + "reference": "206dfefc0ffe9cebf65c413e3d0e809c82fbf00a", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "Text/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "include-path": [ + "" + ], + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Simple template engine.", + "homepage": "https://github.com/sebastianbergmann/php-text-template/", + "keywords": [ + "template" + ], + "time": "2014-01-30 17:20:04" + }, + { + "name": "phpunit/php-timer", + "version": "1.0.5", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-timer.git", + "reference": "19689d4354b295ee3d8c54b4f42c3efb69cbc17c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/19689d4354b295ee3d8c54b4f42c3efb69cbc17c", + "reference": "19689d4354b295ee3d8c54b4f42c3efb69cbc17c", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "PHP/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "include-path": [ + "" + ], + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Utility class for timing", + "homepage": "https://github.com/sebastianbergmann/php-timer/", + "keywords": [ + "timer" + ], + "time": "2013-08-02 07:42:54" + }, + { + "name": "phpunit/php-token-stream", + "version": "1.3.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-token-stream.git", + "reference": "f8d5d08c56de5cfd592b3340424a81733259a876" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/f8d5d08c56de5cfd592b3340424a81733259a876", + "reference": "f8d5d08c56de5cfd592b3340424a81733259a876", + "shasum": "" + }, + "require": { + "ext-tokenizer": "*", + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Wrapper around PHP's tokenizer extension.", + "homepage": "https://github.com/sebastianbergmann/php-token-stream/", + "keywords": [ + "tokenizer" + ], + "time": "2014-08-31 06:12:13" + }, + { + "name": "phpunit/phpunit", + "version": "4.2.5", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit.git", + "reference": "c3abe5953d1e60a0bf23012b1bc8c4d07f4832d7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/c3abe5953d1e60a0bf23012b1bc8c4d07f4832d7", + "reference": "c3abe5953d1e60a0bf23012b1bc8c4d07f4832d7", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-json": "*", + "ext-pcre": "*", + "ext-reflection": "*", + "ext-spl": "*", + "php": ">=5.3.3", + "phpunit/php-code-coverage": "~2.0", + "phpunit/php-file-iterator": "~1.3.1", + "phpunit/php-text-template": "~1.2", + "phpunit/php-timer": "~1.0.2", + "phpunit/phpunit-mock-objects": "~2.2", + "sebastian/comparator": "~1.0", + "sebastian/diff": "~1.1", + "sebastian/environment": "~1.0", + "sebastian/exporter": "~1.0", + "sebastian/version": "~1.0", + "symfony/yaml": "~2.0" + }, + "suggest": { + "phpunit/php-invoker": "~1.1" + }, + "bin": [ + "phpunit" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.2.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "include-path": [ + "", + "../../symfony/yaml/" + ], + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "The PHP Unit Testing framework.", + "homepage": "http://www.phpunit.de/", + "keywords": [ + "phpunit", + "testing", + "xunit" + ], + "time": "2014-09-06 18:38:27" + }, + { + "name": "phpunit/phpunit-mock-objects", + "version": "2.2.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", + "reference": "b241b18d87a47093f20fae8b0ba40379b00bd53a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/b241b18d87a47093f20fae8b0ba40379b00bd53a", + "reference": "b241b18d87a47093f20fae8b0ba40379b00bd53a", + "shasum": "" + }, + "require": { + "ocramius/instantiator": "~1.0", + "php": ">=5.3.3", + "phpunit/php-text-template": "~1.2" + }, + "require-dev": { + "phpunit/phpunit": "~4.2" + }, + "suggest": { + "ext-soap": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.2.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Mock Object library for PHPUnit", + "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", + "keywords": [ + "mock", + "xunit" + ], + "time": "2014-09-06 17:32:37" + }, + { + "name": "sebastian/comparator", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/comparator.git", + "reference": "f7069ee51fa9fb6c038e16a9d0e3439f5449dcf2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/f7069ee51fa9fb6c038e16a9d0e3439f5449dcf2", + "reference": "f7069ee51fa9fb6c038e16a9d0e3439f5449dcf2", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "sebastian/diff": "~1.1", + "sebastian/exporter": "~1.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + }, + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + } + ], + "description": "Provides the functionality to compare PHP values for equality", + "homepage": "http://www.github.com/sebastianbergmann/comparator", + "keywords": [ + "comparator", + "compare", + "equality" + ], + "time": "2014-05-02 07:05:58" + }, + { + "name": "sebastian/diff", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/diff.git", + "reference": "1e091702a5a38e6b4c1ba9ca816e3dd343df2e2d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/1e091702a5a38e6b4c1ba9ca816e3dd343df2e2d", + "reference": "1e091702a5a38e6b4c1ba9ca816e3dd343df2e2d", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + }, + { + "name": "Kore Nordmann", + "email": "mail@kore-nordmann.de" + } + ], + "description": "Diff implementation", + "homepage": "http://www.github.com/sebastianbergmann/diff", + "keywords": [ + "diff" + ], + "time": "2013-08-03 16:46:33" + }, + { + "name": "sebastian/environment", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/environment.git", + "reference": "79517609ec01139cd7e9fded0dd7ce08c952ef6a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/79517609ec01139cd7e9fded0dd7ce08c952ef6a", + "reference": "79517609ec01139cd7e9fded0dd7ce08c952ef6a", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "4.0.*@dev" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Provides functionality to handle HHVM/PHP environments", + "homepage": "http://www.github.com/sebastianbergmann/environment", + "keywords": [ + "Xdebug", + "environment", + "hhvm" + ], + "time": "2014-02-18 16:17:19" + }, + { + "name": "sebastian/exporter", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/exporter.git", + "reference": "1f9a98e6f5dfe0524cb8c6166f7c82f3e9ae1529" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/1f9a98e6f5dfe0524cb8c6166f7c82f3e9ae1529", + "reference": "1f9a98e6f5dfe0524cb8c6166f7c82f3e9ae1529", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "4.0.*@dev" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + }, + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net", + "role": "Lead" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + } + ], + "description": "Provides the functionality to export PHP variables for visualization", + "homepage": "http://www.github.com/sebastianbergmann/exporter", + "keywords": [ + "export", + "exporter" + ], + "time": "2014-02-16 08:26:31" + }, + { + "name": "sebastian/version", + "version": "1.0.3", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/version.git", + "reference": "b6e1f0cf6b9e1ec409a0d3e2f2a5fb0998e36b43" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/b6e1f0cf6b9e1ec409a0d3e2f2a5fb0998e36b43", + "reference": "b6e1f0cf6b9e1ec409a0d3e2f2a5fb0998e36b43", + "shasum": "" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library that helps with managing the version number of Git-hosted PHP projects", + "homepage": "https://github.com/sebastianbergmann/version", + "time": "2014-03-07 15:35:33" + }, { "name": "symfony/console", "version": "v2.5.4", @@ -428,15 +1335,65 @@ "description": "Symfony Yaml Component", "homepage": "http://symfony.com", "time": "2014-08-31 03:22:04" + }, + { + "name": "zendframework/zend-stdlib", + "version": "2.3.1", + "target-dir": "Zend/Stdlib", + "source": { + "type": "git", + "url": "https://github.com/zendframework/Component_ZendStdlib.git", + "reference": "c1f4830018b5d4f034d32fa01a9e17ea176f56f6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/zendframework/Component_ZendStdlib/zipball/c1f4830018b5d4f034d32fa01a9e17ea176f56f6", + "reference": "c1f4830018b5d4f034d32fa01a9e17ea176f56f6", + "shasum": "" + }, + "require": { + "php": ">=5.3.23" + }, + "require-dev": { + "zendframework/zend-eventmanager": "self.version", + "zendframework/zend-serializer": "self.version", + "zendframework/zend-servicemanager": "self.version" + }, + "suggest": { + "zendframework/zend-eventmanager": "To support aggregate hydrator usage", + "zendframework/zend-serializer": "Zend\\Serializer component", + "zendframework/zend-servicemanager": "To support hydrator plugin manager usage" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.3-dev", + "dev-develop": "2.4-dev" + } + }, + "autoload": { + "psr-0": { + "Zend\\Stdlib\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "keywords": [ + "stdlib", + "zf2" + ], + "time": "2014-04-15 15:28:48" } ], "aliases": [ ], "minimum-stability": "stable", - "stability-flags": [ - - ], + "stability-flags": { + "crodas/influx-php": 20 + }, "prefer-stable": false, "platform": [ diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 000000000..ada74cb23 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,20 @@ + + + + tests + + + + + src + + + diff --git a/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php b/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php new file mode 100644 index 000000000..9cc94be79 --- /dev/null +++ b/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php @@ -0,0 +1,29 @@ +getTcpEndpointFor(Argument::Any())->willReturn("localhost"); + $this->beConstructedWith($client, $options); + } + + function it_is_initializable() + { + $this->shouldHaveType('InfluxDB\Adapter\GuzzleAdapter'); + } + + function it_should_send_data_via_post(Client $client, Options $options) + { + $client->post("localhost", ['body' => json_encode(['pippo'])])->shouldBeCalledTimes(1); + + $this->send(["pippo"]); + } +} diff --git a/spec/InfluxDB/Adapter/UdpAdapterSpec.php b/spec/InfluxDB/Adapter/UdpAdapterSpec.php index 1e65698b9..5d1aefc6a 100644 --- a/spec/InfluxDB/Adapter/UdpAdapterSpec.php +++ b/spec/InfluxDB/Adapter/UdpAdapterSpec.php @@ -4,12 +4,13 @@ namespace spec\InfluxDB\Adapter; use PhpSpec\ObjectBehavior; use Prophecy\Argument; +use InfluxDB\Options; class UdpAdapterSpec extends ObjectBehavior { - function let() + function let(Options $options) { - $this->beConstructedWith(); + $this->beConstructedWith($options); } function it_is_initializable() @@ -20,5 +21,6 @@ class UdpAdapterSpec extends ObjectBehavior function it_should_implement_adapter_interface() { $this->shouldImplement("InfluxDB\Adapter\AdapterInterface"); + $this->shouldImplement("InfluxDB\Adapter\ConnectableInterface"); } } diff --git a/spec/InfluxDB/OptionsSpec.php b/spec/InfluxDB/OptionsSpec.php new file mode 100644 index 000000000..bca74574f --- /dev/null +++ b/spec/InfluxDB/OptionsSpec.php @@ -0,0 +1,37 @@ +shouldHaveType('InfluxDB\Options'); + } + + function it_should_create_a_valid_tcp_endpoint() + { + $this->getTcpEndpointFor("mine") + ->shouldReturn("http://localhost:1806/db/mine/series?u=root&p=root"); + } + + function it_should_allows_option_override_for_tcp_endpoint() + { + $this->setHost("influx.1.prod.tld"); + $this->setPort(19385); + $this->setUsername("walter"); + $this->setPassword("walter"); + $this->getTcpEndpointFor("me") + ->shouldReturn("http://influx.1.prod.tld:19385/db/me/series?u=walter&p=walter"); + } + + function it_should_allows_https_for_tcp_endpoint() + { + $this->setProtocol("https"); + $this->getTcpEndpointFor("me") + ->shouldReturn("https://localhost:1806/db/me/series?u=root&p=root"); + } +} diff --git a/src/InfluxDB/Adapter/AdapterInterface.php b/src/InfluxDB/Adapter/AdapterInterface.php index a1509c311..764bca1f6 100644 --- a/src/InfluxDB/Adapter/AdapterInterface.php +++ b/src/InfluxDB/Adapter/AdapterInterface.php @@ -1,13 +1,7 @@ httpClient = $httpClient; + $this->options = $options; + } + + public function getDatabase() + { + return $this->database; + } + + public function setDatabase($database) + { + $this->database = $database; + return $this; + } + + public function send($message) + { + $httpMessage = [ + "body" => json_encode($message) + ]; + $endpoint = $this->options->getTcpEndpointFor($this->getDatabase()); + + $this->httpClient->post($endpoint, $httpMessage); + } +} diff --git a/src/InfluxDB/Adapter/UdpAdapter.php b/src/InfluxDB/Adapter/UdpAdapter.php index 661e2e064..729be12c6 100644 --- a/src/InfluxDB/Adapter/UdpAdapter.php +++ b/src/InfluxDB/Adapter/UdpAdapter.php @@ -2,26 +2,16 @@ namespace InfluxDB\Adapter; -class UdpAdapter implements AdapterInterface +use InfluxDB\Options; + +class UdpAdapter implements AdapterInterface, ConnectableInterface { - private $host; - private $port; - private $username; - private $password; - private $name = __NAMESPACE__; + private $options; private $socket; - public function __construct( - $host = "127.0.0.1", - $port = 5551, - $user = "root", - $password = "root" - ) + public function __construct(Options $options) { - $this->host = $host; - $this->port = $port; - $this->username = $user; - $this->password = $password; + $this->options = $options; } public function getSocket() @@ -29,11 +19,6 @@ class UdpAdapter implements AdapterInterface return $this->socket; } - public function getName() - { - return $this->name; - } - public function connect() { $this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); @@ -42,12 +27,12 @@ class UdpAdapter implements AdapterInterface public function disconnect() { - socket_close($this->getSocket()); + socket_close($this->getSocket()); } public function send($message) { $message = json_encode($message); socket_sendto($this->getSocket(), $message, strlen($message), 0, $this->host, $this->port); - } + } } diff --git a/src/InfluxDB/Options.php b/src/InfluxDB/Options.php new file mode 100644 index 000000000..5acaebf23 --- /dev/null +++ b/src/InfluxDB/Options.php @@ -0,0 +1,89 @@ +host = "localhost"; + $this->port = 8086; + $this->username = "root"; + $this->password = "root"; + $this->setProtocol("http"); + } + + public function getProtocol() + { + return $this->protocol; + } + + public function setProtocol($protocol) + { + $this->protocol = $protocol; + return $this; + } + + public function getHost() + { + return $this->host; + } + + public function setHost($host) + { + $this->host = $host; + return $this; + } + + public function getPort() + { + return $this->port; + } + + public function setPort($port) + { + $this->port = $port; + return $this; + } + + public function getUsername() + { + return $this->username; + } + + public function setUsername($username) + { + $this->username = $username; + return $this; + } + + public function getPassword() + { + return $this->password; + } + + public function setPassword($password) + { + $this->password = $password; + return $this; + } + + public function getTcpEndpointFor($database) + { + return sprintf( + "%s://%s:%d/db/%s/series?u=%s&p=%s", + $this->getProtocol(), + $this->getHost(), + $this->getPort(), + $database, + $this->getUsername(), + $this->getPassword() + ); + } +} diff --git a/tests/InfluxDB/ClientTest.php b/tests/InfluxDB/ClientTest.php new file mode 100644 index 000000000..e7226baf9 --- /dev/null +++ b/tests/InfluxDB/ClientTest.php @@ -0,0 +1,61 @@ +options = $options; + + $this->object = new Client(); + $this->hydrator = new ArraySerializable(); + + $client = new Crodas( + $options["tcp"]["host"], + $options["tcp"]["port"], + $options["tcp"]["username"], + $options["tcp"]["password"] + ); + $client->deleteDatabase($options["tcp"]["database"]); + $client->createDatabase($options["tcp"]["database"]); + + $this->anotherClient = $client; + } + + public function testHttpApiWorksCorrectly() + { + $tcpOptions = $this->options["tcp"]; + + $options = new Options(); + $options->setHost($tcpOptions["host"]); + $options->setPort($tcpOptions["port"]); + $options->setUsername($tcpOptions["username"]); + $options->setPassword($tcpOptions["password"]); + + $guzzleHttp = new GuzzleHttpClient(); + $adapter = new GuzzleAdapter($guzzleHttp, $options); + $adapter->setDatabase($tcpOptions["database"]); + + $influx = new Client(); + $influx->setAdapter($adapter); + + $influx->mark("tcp.test", ["mark" => "element"]); + + $cursor = $this->anotherClient->getDatabase("mine")->query("select * from tcp.test"); + $this->assertCount(1, $cursor); + $this->assertEquals("element", $cursor[0]->mark); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 000000000..cf38911f3 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,11 @@ + [ + "host" => "localhost", + "port" => 8086, + "protocol" => "http", + "database" => "mine", + "username" => "root", + "password" => "root", + ] +]; From 2aad2b1bb0513bcb1af979006f0767ac52529fb4 Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Thu, 11 Sep 2014 15:06:53 +0200 Subject: [PATCH 02/15] Fixes infludb default port --- spec/InfluxDB/OptionsSpec.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/InfluxDB/OptionsSpec.php b/spec/InfluxDB/OptionsSpec.php index bca74574f..5ac0c4004 100644 --- a/spec/InfluxDB/OptionsSpec.php +++ b/spec/InfluxDB/OptionsSpec.php @@ -15,7 +15,7 @@ class OptionsSpec extends ObjectBehavior function it_should_create_a_valid_tcp_endpoint() { $this->getTcpEndpointFor("mine") - ->shouldReturn("http://localhost:1806/db/mine/series?u=root&p=root"); + ->shouldReturn("http://localhost:8086/db/mine/series?u=root&p=root"); } function it_should_allows_option_override_for_tcp_endpoint() @@ -32,6 +32,6 @@ class OptionsSpec extends ObjectBehavior { $this->setProtocol("https"); $this->getTcpEndpointFor("me") - ->shouldReturn("https://localhost:1806/db/me/series?u=root&p=root"); + ->shouldReturn("https://localhost:8086/db/me/series?u=root&p=root"); } } From 08a1b75ea240b3c99cff4751a2bc74100b006af6 Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Thu, 11 Sep 2014 15:13:12 +0200 Subject: [PATCH 03/15] Fixes exception missing database --- tests/InfluxDB/ClientTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/InfluxDB/ClientTest.php b/tests/InfluxDB/ClientTest.php index e7226baf9..34cb19ad5 100644 --- a/tests/InfluxDB/ClientTest.php +++ b/tests/InfluxDB/ClientTest.php @@ -29,7 +29,11 @@ class ClientTest extends \PHPUnit_Framework_TestCase $options["tcp"]["username"], $options["tcp"]["password"] ); - $client->deleteDatabase($options["tcp"]["database"]); + try { + $client->deleteDatabase($options["tcp"]["database"]); + } catch (\Exception $e) { + // nothing... + } $client->createDatabase($options["tcp"]["database"]); $this->anotherClient = $client; From 16f8b9ec48a172b87d68b18897d1b49b69e99226 Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Thu, 11 Sep 2014 19:52:17 +0200 Subject: [PATCH 04/15] Fixes import guzzle as http client description --- README.md | 4 ++-- tests/InfluxDB/ClientTest.php | 12 +++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0bca6b8dd..93e50a6b4 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Use your DiC or Service Locator in order to provide a configured client use InfluxDB\Client; use InfluxDB\Options; -use InfluxDB\Adapter\GuzzleAdapter; +use InfluxDB\Adapter\GuzzleAdapter as InfluxHttpAdapter; use GuzzleHttp\Client as GuzzleHttpClient; $options = new Options(); @@ -60,7 +60,7 @@ $options->setUsername("root"); $options->setPassword("root"); $guzzleHttp = new GuzzleHttpClient(); -$adapter = new GuzzleAdapter($guzzleHttp, $options); +$adapter = new InfluxHttpAdapter($guzzleHttp, $options); $adapter->setDatabase("mine"); $influx = new Client(); diff --git a/tests/InfluxDB/ClientTest.php b/tests/InfluxDB/ClientTest.php index 34cb19ad5..3125fedf0 100644 --- a/tests/InfluxDB/ClientTest.php +++ b/tests/InfluxDB/ClientTest.php @@ -1,17 +1,15 @@ options = $options; $this->object = new Client(); - $this->hydrator = new ArraySerializable(); $client = new Crodas( $options["tcp"]["host"], @@ -39,7 +36,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase $this->anotherClient = $client; } - public function testHttpApiWorksCorrectly() + public function testGuzzleHttpApiWorksCorrectly() { $tcpOptions = $this->options["tcp"]; @@ -50,7 +47,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase $options->setPassword($tcpOptions["password"]); $guzzleHttp = new GuzzleHttpClient(); - $adapter = new GuzzleAdapter($guzzleHttp, $options); + $adapter = new InfluxHttpAdapter($guzzleHttp, $options); $adapter->setDatabase($tcpOptions["database"]); $influx = new Client(); @@ -58,7 +55,8 @@ class ClientTest extends \PHPUnit_Framework_TestCase $influx->mark("tcp.test", ["mark" => "element"]); - $cursor = $this->anotherClient->getDatabase("mine")->query("select * from tcp.test"); + $cursor = $this->anotherClient->getDatabase($tcpOptions["database"]) + ->query("select * from tcp.test"); $this->assertCount(1, $cursor); $this->assertEquals("element", $cursor[0]->mark); } From ca09343134f4f2259e9e6b0fcc4d57bb6d24f267 Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Fri, 12 Sep 2014 08:24:21 +0200 Subject: [PATCH 05/15] Moved database into options --- README.md | 6 ++++-- composer.json | 5 +++++ composer.lock | 2 +- spec/InfluxDB/Adapter/GuzzleAdapterSpec.php | 2 +- spec/InfluxDB/OptionsSpec.php | 9 ++++++--- src/InfluxDB/Adapter/GuzzleAdapter.php | 13 +------------ src/InfluxDB/Options.php | 17 +++++++++++++++-- tests/InfluxDB/ClientTest.php | 2 +- 8 files changed, 34 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 93e50a6b4..c4603c7d5 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,10 @@ Send metrics to InfluxDB. ```php +$options = new \InfluxDB\Options(); + $client = new \InfluxDB\Client(); -$client->setAdapter(new \InfluxDB\Adapter\UdpAdapter()); +$client->setAdapter(new \InfluxDB\Adapter\UdpAdapter($options)); $client->mark("search", [ "query" => "php" @@ -58,10 +60,10 @@ $options->setHost("analytics.mine.domain.tld"); $options->setPort(8086); $options->setUsername("root"); $options->setPassword("root"); +$options->setDatabase("mine"); $guzzleHttp = new GuzzleHttpClient(); $adapter = new InfluxHttpAdapter($guzzleHttp, $options); -$adapter->setDatabase("mine"); $influx = new Client(); $influx->setAdapter($adapter); diff --git a/composer.json b/composer.json index 758ac8fdb..82c20d1f5 100644 --- a/composer.json +++ b/composer.json @@ -31,5 +31,10 @@ "psr-0": { "InfluxDB": ["./src/", "./tests"] } + }, + "suggests": { + "fabpot/pimple": "Allows to prepare the client dependencies in order to require an initialized instance", + "zendframework/zend-servicemanager": "Use a service locator in order to prepare a valid instance", + "symfony/dependency-injection": "Prepare a valid instance via the symfony DiC" } } diff --git a/composer.lock b/composer.lock index 57dd307bc..98d05b929 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "6ba22c8acf314366cc2d58a2cfaccbd3", + "hash": "1b622dda38682502a547657384b6fe7f", "packages": [ { "name": "guzzlehttp/guzzle", diff --git a/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php b/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php index 9cc94be79..73f8f0ffa 100644 --- a/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php +++ b/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php @@ -11,7 +11,7 @@ class GuzzleAdapterSpec extends ObjectBehavior { function let(Client $client, Options $options) { - $options->getTcpEndpointFor(Argument::Any())->willReturn("localhost"); + $options->getTcpEndpoint()->willReturn("localhost"); $this->beConstructedWith($client, $options); } diff --git a/spec/InfluxDB/OptionsSpec.php b/spec/InfluxDB/OptionsSpec.php index 5ac0c4004..44ea4fb4a 100644 --- a/spec/InfluxDB/OptionsSpec.php +++ b/spec/InfluxDB/OptionsSpec.php @@ -14,7 +14,8 @@ class OptionsSpec extends ObjectBehavior function it_should_create_a_valid_tcp_endpoint() { - $this->getTcpEndpointFor("mine") + $this->setDatabase("mine"); + $this->getTcpEndpoint() ->shouldReturn("http://localhost:8086/db/mine/series?u=root&p=root"); } @@ -24,14 +25,16 @@ class OptionsSpec extends ObjectBehavior $this->setPort(19385); $this->setUsername("walter"); $this->setPassword("walter"); - $this->getTcpEndpointFor("me") + $this->setDatabase("me"); + $this->getTcpEndpoint() ->shouldReturn("http://influx.1.prod.tld:19385/db/me/series?u=walter&p=walter"); } function it_should_allows_https_for_tcp_endpoint() { $this->setProtocol("https"); - $this->getTcpEndpointFor("me") + $this->setDatabase("me"); + $this->getTcpEndpoint() ->shouldReturn("https://localhost:8086/db/me/series?u=root&p=root"); } } diff --git a/src/InfluxDB/Adapter/GuzzleAdapter.php b/src/InfluxDB/Adapter/GuzzleAdapter.php index 9e69e726a..fc7bf7fb7 100644 --- a/src/InfluxDB/Adapter/GuzzleAdapter.php +++ b/src/InfluxDB/Adapter/GuzzleAdapter.php @@ -17,23 +17,12 @@ class GuzzleAdapter implements AdapterInterface $this->options = $options; } - public function getDatabase() - { - return $this->database; - } - - public function setDatabase($database) - { - $this->database = $database; - return $this; - } - public function send($message) { $httpMessage = [ "body" => json_encode($message) ]; - $endpoint = $this->options->getTcpEndpointFor($this->getDatabase()); + $endpoint = $this->options->getTcpEndpoint(); $this->httpClient->post($endpoint, $httpMessage); } diff --git a/src/InfluxDB/Options.php b/src/InfluxDB/Options.php index 5acaebf23..e084a1848 100644 --- a/src/InfluxDB/Options.php +++ b/src/InfluxDB/Options.php @@ -10,6 +10,8 @@ class Options private $password; private $protocol; + private $database; + public function __construct() { $this->host = "localhost"; @@ -74,14 +76,25 @@ class Options return $this; } - public function getTcpEndpointFor($database) + public function getDatabase() + { + return $this->database; + } + + public function setDatabase($database) + { + $this->database = $database; + return $this; + } + + public function getTcpEndpoint() { return sprintf( "%s://%s:%d/db/%s/series?u=%s&p=%s", $this->getProtocol(), $this->getHost(), $this->getPort(), - $database, + $this->getDatabase(), $this->getUsername(), $this->getPassword() ); diff --git a/tests/InfluxDB/ClientTest.php b/tests/InfluxDB/ClientTest.php index 3125fedf0..05690b741 100644 --- a/tests/InfluxDB/ClientTest.php +++ b/tests/InfluxDB/ClientTest.php @@ -45,10 +45,10 @@ class ClientTest extends \PHPUnit_Framework_TestCase $options->setPort($tcpOptions["port"]); $options->setUsername($tcpOptions["username"]); $options->setPassword($tcpOptions["password"]); + $options->setDatabase($tcpOptions["database"]); $guzzleHttp = new GuzzleHttpClient(); $adapter = new InfluxHttpAdapter($guzzleHttp, $options); - $adapter->setDatabase($tcpOptions["database"]); $influx = new Client(); $influx->setAdapter($adapter); From b835c3a044d155208027fde3d8d8e52a3f760f93 Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Fri, 12 Sep 2014 20:20:23 +0200 Subject: [PATCH 06/15] Added query method for Http adapter --- README.md | 45 ++++++++++++++++++ composer.json | 2 +- spec/InfluxDB/Adapter/GuzzleAdapterSpec.php | 21 ++++++++- spec/InfluxDB/ClientSpec.php | 33 +++++++++++++ spec/InfluxDB/OptionsSpec.php | 12 ++--- src/InfluxDB/Adapter/GuzzleAdapter.php | 23 ++++++++-- src/InfluxDB/Adapter/QueryableInterface.php | 7 +++ src/InfluxDB/Client.php | 27 +++++++++++ src/InfluxDB/Options.php | 8 ++-- tests/InfluxDB/ClientTest.php | 51 +++++++++++++++++---- 10 files changed, 203 insertions(+), 26 deletions(-) create mode 100644 src/InfluxDB/Adapter/QueryableInterface.php diff --git a/README.md b/README.md index c4603c7d5..f0ffe18c3 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,51 @@ Actually we using Guzzle as HTTP client $influx->mark("tcp.test", ["mark" => "element"]); ``` +## Query InfluxDB + +You can query the time series database using the query method. + +```php +$influx->query("select * from mine"); +$influx->query("select * from mine", "s"); // with time_precision +``` + +You can query the database only if the adapter is queryable (implements `QueryableInterface`), +actually `GuzzleAdapter`. + +The adapter returns the json decoded body of the InfluxDB response, something like: + +``` +array(1) { + [0] => + class stdClass#1 (3) { + public $name => + string(8) "tcp.test" + public $columns => + array(3) { + [0] => + string(4) "time" + [1] => + string(15) "sequence_number" + [2] => + string(4) "mark" + } + public $points => + array(1) { + [0] => + array(3) { + [0] => + int(1410545635590) + [1] => + int(2390001) + [2] => + string(7) "element" + } + } + } +} +``` + ## Prepare lib dependencies Use your DiC or Service Locator in order to provide a configured client diff --git a/composer.json b/composer.json index 82c20d1f5..e6b7245c2 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ ], "autoload": { "psr-0": { - "InfluxDB": ["./src/", "./tests"] + "InfluxDB\\": ["./src/", "./tests"] } }, "suggests": { diff --git a/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php b/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php index 73f8f0ffa..3befd1b72 100644 --- a/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php +++ b/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php @@ -11,7 +11,9 @@ class GuzzleAdapterSpec extends ObjectBehavior { function let(Client $client, Options $options) { - $options->getTcpEndpoint()->willReturn("localhost"); + $options->getHttpSeriesEndpoint()->willReturn("localhost"); + $options->getUsername()->willReturn("one"); + $options->getPassword()->willReturn("two"); $this->beConstructedWith($client, $options); } @@ -22,8 +24,23 @@ class GuzzleAdapterSpec extends ObjectBehavior function it_should_send_data_via_post(Client $client, Options $options) { - $client->post("localhost", ['body' => json_encode(['pippo'])])->shouldBeCalledTimes(1); + $client->post("localhost", [ + 'auth' => ["one", "two"], + 'body' => json_encode(['pippo']) + ])->shouldBeCalledTimes(1); $this->send(["pippo"]); } + + function it_should_query_data(Client $client, Options $options) + { + $client->get("select * from tcp.test", [])->willReturn([]); + $this->query("select * from tcp.test")->shouldReturn([]); + } + + function it_should_query_data_with_time_precision(Client $client, Options $options) + { + $client->get("select * from tcp.test", ["time_precision" => "s"])->willReturn([]); + $this->query("select * from tcp.test", "s")->shouldReturn([]); + } } diff --git a/spec/InfluxDB/ClientSpec.php b/spec/InfluxDB/ClientSpec.php index ce1918e31..28a006d5d 100644 --- a/spec/InfluxDB/ClientSpec.php +++ b/spec/InfluxDB/ClientSpec.php @@ -4,6 +4,8 @@ namespace spec\InfluxDB; use PhpSpec\ObjectBehavior; use Prophecy\Argument; +use InfluxDB\Adapter\GuzzleAdapter; +use InfluxDB\Adapter\UdpAdapter; class ClientSpec extends ObjectBehavior { @@ -31,4 +33,35 @@ class ClientSpec extends ObjectBehavior "title" => "Autogrill" ]); } + + function it_should_query_on_querable_adapter(GuzzleAdapter $adapter) + { + $this->setAdapter($adapter); + $adapter->query("select * from tcp.test", false)->willReturn([]); + + $this->query("select * from tcp.test")->shouldReturn([]); + } + + function it_should_query_with_time_precision(GuzzleAdapter $adapter) + { + $this->setAdapter($adapter); + $adapter->query("select * from tcp.test", "s")->willReturn([]); + + $this->query("select * from tcp.test", "s")->shouldReturn([]); + } + + function it_should_query_but_skip_invalid_time_precision(GuzzleAdapter $adapter) + { + $this->setAdapter($adapter); + $adapter->query("select * from tcp.test", false)->willReturn([]); + + $this->query("select * from tcp.test", "r")->shouldReturn([]); + } + + function it_should_thrown_an_exception_on_unquerable_adapter(UdpAdapter $adapter) + { + $this->setAdapter($adapter); + + $this->shouldThrow("\\BadMethodCallException")->duringQuery("select * from tcp.test"); + } } diff --git a/spec/InfluxDB/OptionsSpec.php b/spec/InfluxDB/OptionsSpec.php index 44ea4fb4a..d926faf83 100644 --- a/spec/InfluxDB/OptionsSpec.php +++ b/spec/InfluxDB/OptionsSpec.php @@ -15,8 +15,8 @@ class OptionsSpec extends ObjectBehavior function it_should_create_a_valid_tcp_endpoint() { $this->setDatabase("mine"); - $this->getTcpEndpoint() - ->shouldReturn("http://localhost:8086/db/mine/series?u=root&p=root"); + $this->getHttpSeriesEndpoint() + ->shouldReturn("http://localhost:8086/db/mine/series"); } function it_should_allows_option_override_for_tcp_endpoint() @@ -26,15 +26,15 @@ class OptionsSpec extends ObjectBehavior $this->setUsername("walter"); $this->setPassword("walter"); $this->setDatabase("me"); - $this->getTcpEndpoint() - ->shouldReturn("http://influx.1.prod.tld:19385/db/me/series?u=walter&p=walter"); + $this->getHttpSeriesEndpoint() + ->shouldReturn("http://influx.1.prod.tld:19385/db/me/series"); } function it_should_allows_https_for_tcp_endpoint() { $this->setProtocol("https"); $this->setDatabase("me"); - $this->getTcpEndpoint() - ->shouldReturn("https://localhost:8086/db/me/series?u=root&p=root"); + $this->getHttpSeriesEndpoint() + ->shouldReturn("https://localhost:8086/db/me/series"); } } diff --git a/src/InfluxDB/Adapter/GuzzleAdapter.php b/src/InfluxDB/Adapter/GuzzleAdapter.php index fc7bf7fb7..da27104d0 100644 --- a/src/InfluxDB/Adapter/GuzzleAdapter.php +++ b/src/InfluxDB/Adapter/GuzzleAdapter.php @@ -5,7 +5,7 @@ namespace InfluxDB\Adapter; use GuzzleHttp\Client; use InfluxDB\Options; -class GuzzleAdapter implements AdapterInterface +class GuzzleAdapter implements AdapterInterface, QueryableInterface { private $httpClient; private $options; @@ -20,10 +20,27 @@ class GuzzleAdapter implements AdapterInterface public function send($message) { $httpMessage = [ + "auth" => [$this->options->getUsername(), $this->options->getPassword()], "body" => json_encode($message) ]; - $endpoint = $this->options->getTcpEndpoint(); + $endpoint = $this->options->getHttpSeriesEndpoint(); - $this->httpClient->post($endpoint, $httpMessage); + return $this->httpClient->post($endpoint, $httpMessage); + } + + public function query($query, $timePrecision = false) + { + $options = [ + "auth" => [$this->options->getUsername(), $this->options->getPassword()], + 'query' => [ + "q" => $query, + ] + ]; + if ($timePrecision) { + $options["query"]["time_precision"] = $timePrecision; + } + + $endpoint = $this->options->getHttpSeriesEndpoint(); + return $this->httpClient->get($endpoint, $options)->json(); } } diff --git a/src/InfluxDB/Adapter/QueryableInterface.php b/src/InfluxDB/Adapter/QueryableInterface.php new file mode 100644 index 000000000..fa604b069 --- /dev/null +++ b/src/InfluxDB/Adapter/QueryableInterface.php @@ -0,0 +1,7 @@ +getAdapter()->send([$data]); } + + public function query($query, $timePrecision = false) + { + if (!($this->getAdapter() instanceOf QueryableInterface)) { + throw new \BadMethodCallException("You can query the database only if the adapter supports it!"); + } + + $timePrecision = $this->clearTimePrecision($timePrecision); + + return $this->getAdapter()->query($query, $timePrecision); + } + + private function clearTimePrecision($timePrecision) + { + switch ($timePrecision) { + case 's': + case 'u': + case 'ms': + break; + default: + $timePrecision = false; + } + + return $timePrecision; + } } diff --git a/src/InfluxDB/Options.php b/src/InfluxDB/Options.php index e084a1848..02ff5ef44 100644 --- a/src/InfluxDB/Options.php +++ b/src/InfluxDB/Options.php @@ -87,16 +87,14 @@ class Options return $this; } - public function getTcpEndpoint() + public function getHttpSeriesEndpoint() { return sprintf( - "%s://%s:%d/db/%s/series?u=%s&p=%s", + "%s://%s:%d/db/%s/series", $this->getProtocol(), $this->getHost(), $this->getPort(), - $this->getDatabase(), - $this->getUsername(), - $this->getPassword() + $this->getDatabase() ); } } diff --git a/tests/InfluxDB/ClientTest.php b/tests/InfluxDB/ClientTest.php index 05690b741..ec6581510 100644 --- a/tests/InfluxDB/ClientTest.php +++ b/tests/InfluxDB/ClientTest.php @@ -16,9 +16,6 @@ class ClientTest extends \PHPUnit_Framework_TestCase public function setUp() { $options = include __DIR__ . '/../bootstrap.php'; - $this->options = $options; - - $this->object = new Client(); $client = new Crodas( $options["tcp"]["host"], @@ -34,11 +31,8 @@ class ClientTest extends \PHPUnit_Framework_TestCase $client->createDatabase($options["tcp"]["database"]); $this->anotherClient = $client; - } - public function testGuzzleHttpApiWorksCorrectly() - { - $tcpOptions = $this->options["tcp"]; + $tcpOptions = $options["tcp"]; $options = new Options(); $options->setHost($tcpOptions["host"]); @@ -47,17 +41,56 @@ class ClientTest extends \PHPUnit_Framework_TestCase $options->setPassword($tcpOptions["password"]); $options->setDatabase($tcpOptions["database"]); + $this->options = $options; + $guzzleHttp = new GuzzleHttpClient(); $adapter = new InfluxHttpAdapter($guzzleHttp, $options); $influx = new Client(); $influx->setAdapter($adapter); + $this->object = $influx; + } - $influx->mark("tcp.test", ["mark" => "element"]); + public function testGuzzleHttpApiWorksCorrectly() + { + $this->object->mark("tcp.test", ["mark" => "element"]); - $cursor = $this->anotherClient->getDatabase($tcpOptions["database"]) + $cursor = $this->anotherClient->getDatabase($this->options->getDatabase()) ->query("select * from tcp.test"); $this->assertCount(1, $cursor); $this->assertEquals("element", $cursor[0]->mark); } + + public function testGuzzleHttpQueryApiWorksCorrectly() + { + $this->object->mark("tcp.test", ["mark" => "element"]); + + $body = $this->object->query("select * from tcp.test"); + + $this->assertCount(1, $body); + $this->assertEquals("tcp.test", $body[0]["name"]); + $this->assertEquals("element", $body[0]["points"][0][2]); + } + + public function testGuzzleHttpQueryApiWithMultipleData() + { + $this->object->mark("tcp.test", ["mark" => "element"]); + $this->object->mark("tcp.test", ["mark" => "element2"]); + $this->object->mark("tcp.test", ["mark" => "element3"]); + + $body = $this->object->query("select mark from tcp.test", "s"); + + $this->assertCount(3, $body[0]["points"]); + $this->assertEquals("tcp.test", $body[0]["name"]); + } + + public function testGuzzleHttpQueryApiWithTimePrecision() + { + $this->object->mark("tcp.test", ["mark" => "element"]); + + $body = $this->object->query("select mark from tcp.test", "s"); + + $this->assertCount(1, $body[0]["points"]); + $this->assertEquals("tcp.test", $body[0]["name"]); + } } From eacc092debf596cf64060c61131888b38ed799eb Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Fri, 12 Sep 2014 23:05:29 +0200 Subject: [PATCH 07/15] Fixes spec on guzzle adapter --- spec/InfluxDB/Adapter/GuzzleAdapterSpec.php | 26 +++++++++++++++++---- tests/InfluxDB/ClientTest.php | 7 +++--- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php b/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php index 3befd1b72..5c61ce1bb 100644 --- a/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php +++ b/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php @@ -6,6 +6,7 @@ use PhpSpec\ObjectBehavior; use Prophecy\Argument; use GuzzleHttp\Client; use InfluxDB\Options; +use GuzzleHttp\Message\Response; class GuzzleAdapterSpec extends ObjectBehavior { @@ -34,13 +35,30 @@ class GuzzleAdapterSpec extends ObjectBehavior function it_should_query_data(Client $client, Options $options) { - $client->get("select * from tcp.test", [])->willReturn([]); - $this->query("select * from tcp.test")->shouldReturn([]); + $client->get( + "localhost", + [ + "auth" => ["one", "two"], + "query" => [ + "q" => "select * from tcp.test", + ] + ] + )->willReturn(new Response(200,[],null)); + $this->query("select * from tcp.test")->shouldReturn(null); } function it_should_query_data_with_time_precision(Client $client, Options $options) { - $client->get("select * from tcp.test", ["time_precision" => "s"])->willReturn([]); - $this->query("select * from tcp.test", "s")->shouldReturn([]); + $client->get( + "localhost", + [ + "auth" => ["one", "two"], + "query" => [ + "time_precision" => "s", + "q" => "select * from tcp.test", + ] + ] + )->willReturn(new Response(200, [], null)); + $this->query("select * from tcp.test", "s")->shouldReturn(null); } } diff --git a/tests/InfluxDB/ClientTest.php b/tests/InfluxDB/ClientTest.php index ec6581510..566150f7d 100644 --- a/tests/InfluxDB/ClientTest.php +++ b/tests/InfluxDB/ClientTest.php @@ -55,10 +55,9 @@ class ClientTest extends \PHPUnit_Framework_TestCase { $this->object->mark("tcp.test", ["mark" => "element"]); - $cursor = $this->anotherClient->getDatabase($this->options->getDatabase()) - ->query("select * from tcp.test"); - $this->assertCount(1, $cursor); - $this->assertEquals("element", $cursor[0]->mark); + $body = $this->object->query("select * from tcp.test"); + $this->assertCount(1, $body[0]["points"]); + $this->assertEquals("element", $body[0]["points"][0][2]); } public function testGuzzleHttpQueryApiWorksCorrectly() From 5b5baf20dd2722d05aace1e25b118da9fe1f6128 Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Fri, 12 Sep 2014 23:22:37 +0200 Subject: [PATCH 08/15] Fixes wrong namespace for connectable --- spec/InfluxDB/ClientSpec.php | 28 +++++++++++++++++-- src/InfluxDB/Adapter/AdapterInterface.php | 2 +- src/InfluxDB/Adapter/ConnectableInterface.php | 2 +- src/InfluxDB/Adapter/GuzzleAdapter.php | 3 +- src/InfluxDB/Adapter/UdpAdapter.php | 1 - src/InfluxDB/Client.php | 15 ++++++++-- 6 files changed, 43 insertions(+), 8 deletions(-) diff --git a/spec/InfluxDB/ClientSpec.php b/spec/InfluxDB/ClientSpec.php index 28a006d5d..888f21b8c 100644 --- a/spec/InfluxDB/ClientSpec.php +++ b/spec/InfluxDB/ClientSpec.php @@ -1,11 +1,12 @@ setAdapter($adapter); } + function it_is_initializable() { $this->shouldHaveType('InfluxDB\Client'); } - function it_should_send_data(\InfluxDB\Adapter\AdapterInterface $adapter) + function it_should_be_connectable(ConnectableInterface $adapter) + { + $adapter->connect()->willReturn(true)->shouldBeCalledTimes(1); + + $this->setAdapter($adapter); + $this->connect()->shouldReturn(true); + } + + function it_should_be_disconnectable(ConnectableInterface $adapter) + { + $adapter->disconnect()->willReturn(true)->shouldBeCalledTimes(1); + + $this->setAdapter($adapter); + $this->disconnect()->shouldReturn(true); + } + + function it_should_not_call_connect(AdapterInterface $adapter) + { + $this->setAdapter($adapter); + $this->connect()->shouldReturn(false); + } + + function it_should_send_data(AdapterInterface $adapter) { $adapter->send([[ "name" => "video.search", diff --git a/src/InfluxDB/Adapter/AdapterInterface.php b/src/InfluxDB/Adapter/AdapterInterface.php index 764bca1f6..d0b9e539a 100644 --- a/src/InfluxDB/Adapter/AdapterInterface.php +++ b/src/InfluxDB/Adapter/AdapterInterface.php @@ -1,5 +1,5 @@ [$this->options->getUsername(), $this->options->getPassword()], "body" => json_encode($message) ]; - $endpoint = $this->options->getHttpSeriesEndpoint(); + $endpoint = $this->options->getHttpSeriesEndpoint(); return $this->httpClient->post($endpoint, $httpMessage); } @@ -36,6 +36,7 @@ class GuzzleAdapter implements AdapterInterface, QueryableInterface "q" => $query, ] ]; + if ($timePrecision) { $options["query"]["time_precision"] = $timePrecision; } diff --git a/src/InfluxDB/Adapter/UdpAdapter.php b/src/InfluxDB/Adapter/UdpAdapter.php index 729be12c6..2cb026ede 100644 --- a/src/InfluxDB/Adapter/UdpAdapter.php +++ b/src/InfluxDB/Adapter/UdpAdapter.php @@ -1,5 +1,4 @@ getAdapter()->connect(); + $result = false; + if ($this->getAdapter() instanceOf ConnectableInterface) { + $result = $this->getAdapter()->connect(); + } + + return $result; } public function disconnect() { - return $this->getAdapter()->disconnect(); + $result = false; + if ($this->getAdapter() instanceOf ConnectableInterface) { + $result = $this->getAdapter()->disconnect(); + } + + return $result; } public function mark($name, array $values) From 723ff0ecd2c6d0550401bd9389b327f7a5192260 Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Fri, 12 Sep 2014 23:33:27 +0200 Subject: [PATCH 09/15] Updated readme.md --- README.md | 91 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index f0ffe18c3..6fc9ea862 100644 --- a/README.md +++ b/README.md @@ -3,47 +3,54 @@ * [![Build Status](https://travis-ci.org/corley/influxdb-php-sdk.svg?branch=master)](https://travis-ci.org/corley/influxdb-php-sdk) * [![Dependency Status](https://www.versioneye.com/user/projects/54104e789e1622492d000025/badge.svg?style=flat)](https://www.versioneye.com/user/projects/54104e789e1622492d000025) -Send metrics to InfluxDB. +Send metrics to InfluxDB and query for any data. + +Add new points: ```php -$options = new \InfluxDB\Options(); - -$client = new \InfluxDB\Client(); -$client->setAdapter(new \InfluxDB\Adapter\UdpAdapter($options)); - -$client->mark("search", [ - "query" => "php" +$client->mark("app.search", [ + "key" => "this is my search" ]); ``` -## Install it - -Just use composer - -```shell -php composer.phar require corley/influxdb-sdk:* -``` - -Or place it in your require section - -```json -{ - "require": { - // ... - "corley/influxdb-sdk": "*" - } -} -``` - -## Send data using HTTP json API - -Actually we using Guzzle as HTTP client +Retrieve existing points: ```php -$influx->mark("tcp.test", ["mark" => "element"]); +$results = $client->query("select * from app.search"); ``` -## Query InfluxDB +## InfluxDB client adapters + +Actually we supports two adapters + + * UDP/IP - in order to send data via UDP (datagram) + * HTTP JSON - in order to send/retrieve using HTTP (connection oriented) + +### Using UDP/IP Adapter + +Actually "socket" php library is used for UDP/IP adapter + +``` +$options = new Options(); +$adapter = new UdpAdapter($options); + +$client = new Client(); +$client->setAdapter($adapter); +``` + +### Using HTTP Adapter + +Actually Guzzle is used as HTTP client library + +``` +$options = new Options(); +$adapter = new GuzzleAdapter($options); + +$client = new Client(); +$client->setAdapter($adapter); +``` + +### Query InfluxDB You can query the time series database using the query method. @@ -88,6 +95,26 @@ array(1) { } ``` +## Install it + +Just use composer + +```shell +php composer.phar require corley/influxdb-sdk:dev-master +``` + +Or place it in your require section + +```json +{ + "require": { + // ... + "corley/influxdb-sdk": "dev-master" + } +} +``` + + ## Prepare lib dependencies Use your DiC or Service Locator in order to provide a configured client From 1a06353b24ed1f137f8b6442b2da1f18e02cb8d0 Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Sat, 13 Sep 2014 00:10:45 +0200 Subject: [PATCH 10/15] Removed connectable interface --- .travis.yml | 1 + scripts/influxdb_conf.toml | 187 ++++++++++++++++++ src/InfluxDB/Adapter/ConnectableInterface.php | 8 - src/InfluxDB/Adapter/GuzzleAdapter.php | 5 + src/InfluxDB/Adapter/UdpAdapter.php | 23 +-- src/InfluxDB/Client.php | 20 -- tests/InfluxDB/ClientTest.php | 51 +++++ tests/bootstrap.php | 9 +- 8 files changed, 256 insertions(+), 48 deletions(-) create mode 100644 scripts/influxdb_conf.toml delete mode 100644 src/InfluxDB/Adapter/ConnectableInterface.php diff --git a/.travis.yml b/.travis.yml index 38b665b5b..bc5c8ba79 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,7 @@ before_install: - wget http://s3.amazonaws.com/influxdb/influxdb_latest_amd64.deb - sudo useradd influxdb - sudo dpkg -i influxdb_latest_amd64.deb + - sudo cp ./scripts/influxdb_conf.toml /opt/influxdb/shared/config.toml - travis_retry sudo service influxdb restart - sudo service influxdb status diff --git a/scripts/influxdb_conf.toml b/scripts/influxdb_conf.toml new file mode 100644 index 000000000..e0c505a7b --- /dev/null +++ b/scripts/influxdb_conf.toml @@ -0,0 +1,187 @@ +# Welcome to the InfluxDB configuration file. + +# If hostname (on the OS) doesn't return a name that can be resolved by the other +# systems in the cluster, you'll have to set the hostname to an IP or something +# that can be resolved here. +# hostname = "" + +bind-address = "0.0.0.0" + +# Once every 24 hours InfluxDB will report anonymous data to m.influxdb.com +# The data includes raft name (random 8 bytes), os, arch and version +# We don't track ip addresses of servers reporting. This is only used +# to track the number of instances running and the versions which +# is very helpful for us. +# Change this option to true to disable reporting. +reporting-disabled = false + +[logging] +# logging level can be one of "debug", "info", "warn" or "error" +level = "info" +file = "/opt/influxdb/shared/log.txt" # stdout to log to standard out + +# Configure the admin server +[admin] +port = 8083 # binding is disabled if the port isn't set +assets = "/opt/influxdb/current/admin" + +# Configure the http api +[api] +port = 8086 # binding is disabled if the port isn't set +# ssl-port = 8084 # Ssl support is enabled if you set a port and cert +# ssl-cert = /path/to/cert.pem + +# connections will timeout after this amount of time. Ensures that clients that misbehave +# and keep alive connections they don't use won't end up connection a million times. +# However, if a request is taking longer than this to complete, could be a problem. +read-timeout = "5s" + +[input_plugins] + + # Configure the graphite api + [input_plugins.graphite] + enabled = false + # port = 2003 + # database = "" # store graphite data in this database + # udp_enabled = true # enable udp interface on the same port as the tcp interface + + # Configure the udp api + #[input_plugins.udp] + #enabled = true + #port = 4444 + # database = "" + + # Configure multiple udp apis each can write to separate db. Just + # repeat the following section to enable multiple udp apis on + # different ports. + [[input_plugins.udp_servers]] # array of tables + enabled = true + port = 5551 + database = "mine" + +# Raft configuration +[raft] +# The raft port should be open between all servers in a cluster. +# However, this port shouldn't be accessible from the internet. + +port = 8090 + +# Where the raft logs are stored. The user running InfluxDB will need read/write access. +dir = "/opt/influxdb/shared/data/raft" + +# election-timeout = "1s" + +[storage] + +dir = "/opt/influxdb/shared/data/db" +# How many requests to potentially buffer in memory. If the buffer gets filled then writes +# will still be logged and once the local storage has caught up (or compacted) the writes +# will be replayed from the WAL +write-buffer-size = 10000 + +# the engine to use for new shards, old shards will continue to use the same engine +default-engine = "rocksdb" + +# The default setting on this is 0, which means unlimited. Set this to something if you want to +# limit the max number of open files. max-open-files is per shard so this * that will be max. +max-open-shards = 0 + +# The default setting is 100. This option tells how many points will be fetched from LevelDb before +# they get flushed into backend. +point-batch-size = 100 + +# The number of points to batch in memory before writing them to leveldb. Lowering this number will +# reduce the memory usage, but will result in slower writes. +write-batch-size = 5000000 + +# The server will check this often for shards that have expired that should be cleared. +retention-sweep-period = "10m" + +[storage.engines.leveldb] + +# Maximum mmap open files, this will affect the virtual memory used by +# the process +max-open-files = 1000 + +# LRU cache size, LRU is used by leveldb to store contents of the +# uncompressed sstables. You can use `m` or `g` prefix for megabytes +# and gigabytes, respectively. +lru-cache-size = "200m" + +[storage.engines.rocksdb] + +# Maximum mmap open files, this will affect the virtual memory used by +# the process +max-open-files = 1000 + +# LRU cache size, LRU is used by rocksdb to store contents of the +# uncompressed sstables. You can use `m` or `g` prefix for megabytes +# and gigabytes, respectively. +lru-cache-size = "200m" + +[storage.engines.hyperleveldb] + +# Maximum mmap open files, this will affect the virtual memory used by +# the process +max-open-files = 1000 + +# LRU cache size, LRU is used by rocksdb to store contents of the +# uncompressed sstables. You can use `m` or `g` prefix for megabytes +# and gigabytes, respectively. +lru-cache-size = "200m" + +[storage.engines.lmdb] + +map-size = "100g" + +[cluster] +# A comma separated list of servers to seed +# this server. this is only relevant when the +# server is joining a new cluster. Otherwise +# the server will use the list of known servers +# prior to shutting down. Any server can be pointed to +# as a seed. It will find the Raft leader automatically. + +# Here's an example. Note that the port on the host is the same as the raft port. +# seed-servers = ["hosta:8090","hostb:8090"] + +# Replication happens over a TCP connection with a Protobuf protocol. +# This port should be reachable between all servers in a cluster. +# However, this port shouldn't be accessible from the internet. + +protobuf_port = 8099 +protobuf_timeout = "2s" # the write timeout on the protobuf conn any duration parseable by time.ParseDuration +protobuf_heartbeat = "200ms" # the heartbeat interval between the servers. must be parseable by time.ParseDuration +protobuf_min_backoff = "1s" # the minimum backoff after a failed heartbeat attempt +protobuf_max_backoff = "10s" # the maxmimum backoff after a failed heartbeat attempt + +# How many write requests to potentially buffer in memory per server. If the buffer gets filled then writes +# will still be logged and once the server has caught up (or come back online) the writes +# will be replayed from the WAL +write-buffer-size = 1000 + +# the maximum number of responses to buffer from remote nodes, if the +# expected number of responses exceed this number then querying will +# happen sequentially and the buffer size will be limited to this +# number +max-response-buffer-size = 100 + +# When queries get distributed out to shards, they go in parallel. This means that results can get buffered +# in memory since results will come in any order, but have to be processed in the correct time order. +# Setting this higher will give better performance, but you'll need more memory. Setting this to 1 will ensure +# that you don't need to buffer in memory, but you won't get the best performance. +concurrent-shard-query-limit = 10 + +[wal] + +dir = "/opt/influxdb/shared/data/wal" +flush-after = 1000 # the number of writes after which wal will be flushed, 0 for flushing on every write +bookmark-after = 1000 # the number of writes after which a bookmark will be created + +# the number of writes after which an index entry is created pointing +# to the offset of the first request, default to 1k +index-after = 1000 + +# the number of requests per one log file, if new requests came in a +# new log file will be created +requests-per-logfile = 10000 diff --git a/src/InfluxDB/Adapter/ConnectableInterface.php b/src/InfluxDB/Adapter/ConnectableInterface.php deleted file mode 100644 index 1b7a79643..000000000 --- a/src/InfluxDB/Adapter/ConnectableInterface.php +++ /dev/null @@ -1,8 +0,0 @@ -options->getHttpSeriesEndpoint(); + try { return $this->httpClient->get($endpoint, $options)->json(); + } catch (\Exception $e) { + var_dump((string)$e->getResponse()->getBody(true)); + die(); + } } } diff --git a/src/InfluxDB/Adapter/UdpAdapter.php b/src/InfluxDB/Adapter/UdpAdapter.php index 2cb026ede..e2dcf8a51 100644 --- a/src/InfluxDB/Adapter/UdpAdapter.php +++ b/src/InfluxDB/Adapter/UdpAdapter.php @@ -3,35 +3,20 @@ namespace InfluxDB\Adapter; use InfluxDB\Options; -class UdpAdapter implements AdapterInterface, ConnectableInterface +class UdpAdapter implements AdapterInterface { private $options; - private $socket; public function __construct(Options $options) { $this->options = $options; } - public function getSocket() - { - return $this->socket; - } - - public function connect() - { - $this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); - return $this; - } - - public function disconnect() - { - socket_close($this->getSocket()); - } - public function send($message) { + $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); $message = json_encode($message); - socket_sendto($this->getSocket(), $message, strlen($message), 0, $this->host, $this->port); + socket_sendto($socket, $message, strlen($message), 0, $this->options->getHost(), $this->options->getPort()); + socket_close($socket); } } diff --git a/src/InfluxDB/Client.php b/src/InfluxDB/Client.php index 501afcefd..2b9017025 100644 --- a/src/InfluxDB/Client.php +++ b/src/InfluxDB/Client.php @@ -20,26 +20,6 @@ class Client return $this->adapter; } - public function connect() - { - $result = false; - if ($this->getAdapter() instanceOf ConnectableInterface) { - $result = $this->getAdapter()->connect(); - } - - return $result; - } - - public function disconnect() - { - $result = false; - if ($this->getAdapter() instanceOf ConnectableInterface) { - $result = $this->getAdapter()->disconnect(); - } - - return $result; - } - public function mark($name, array $values) { $data =[]; diff --git a/tests/InfluxDB/ClientTest.php b/tests/InfluxDB/ClientTest.php index 566150f7d..e4d5b20a1 100644 --- a/tests/InfluxDB/ClientTest.php +++ b/tests/InfluxDB/ClientTest.php @@ -3,11 +3,13 @@ namespace InfluxDB; use InfluxDB\Adapter\GuzzleAdapter as InfluxHttpAdapter; use InfluxDB\Options; +use InfluxDB\Adapter\UdpAdapter; use GuzzleHttp\Client as GuzzleHttpClient; use crodas\InfluxPHP\Client as Crodas; class ClientTest extends \PHPUnit_Framework_TestCase { + private $rawOptions; private $object; private $options; @@ -16,6 +18,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase public function setUp() { $options = include __DIR__ . '/../bootstrap.php'; + $this->rawOptions = $options; $client = new Crodas( $options["tcp"]["host"], @@ -30,6 +33,13 @@ class ClientTest extends \PHPUnit_Framework_TestCase } $client->createDatabase($options["tcp"]["database"]); + try { + $client->deleteDatabase($options["udp"]["database"]); + } catch (\Exception $e) { + // nothing... + } + $client->createDatabase($options["udp"]["database"]); + $this->anotherClient = $client; $tcpOptions = $options["tcp"]; @@ -51,6 +61,9 @@ class ClientTest extends \PHPUnit_Framework_TestCase $this->object = $influx; } + /** + * @group tcp + */ public function testGuzzleHttpApiWorksCorrectly() { $this->object->mark("tcp.test", ["mark" => "element"]); @@ -60,6 +73,9 @@ class ClientTest extends \PHPUnit_Framework_TestCase $this->assertEquals("element", $body[0]["points"][0][2]); } + /** + * @group tcp + */ public function testGuzzleHttpQueryApiWorksCorrectly() { $this->object->mark("tcp.test", ["mark" => "element"]); @@ -71,6 +87,9 @@ class ClientTest extends \PHPUnit_Framework_TestCase $this->assertEquals("element", $body[0]["points"][0][2]); } + /** + * @group tcp + */ public function testGuzzleHttpQueryApiWithMultipleData() { $this->object->mark("tcp.test", ["mark" => "element"]); @@ -83,6 +102,9 @@ class ClientTest extends \PHPUnit_Framework_TestCase $this->assertEquals("tcp.test", $body[0]["name"]); } + /** + * @group tcp + */ public function testGuzzleHttpQueryApiWithTimePrecision() { $this->object->mark("tcp.test", ["mark" => "element"]); @@ -92,4 +114,33 @@ class ClientTest extends \PHPUnit_Framework_TestCase $this->assertCount(1, $body[0]["points"]); $this->assertEquals("tcp.test", $body[0]["name"]); } + + /** + * @group udp + */ + public function testUdpIpWriteData() + { + $rawOptions = $this->rawOptions; + $options = new Options(); + $options->setUsername($rawOptions["udp"]["username"]); + $options->setPassword($rawOptions["udp"]["password"]); + $options->setPort($rawOptions["udp"]["port"]); + + $adapter = new UdpAdapter($options); + $object = new Client(); + $object->setAdapter($adapter); + + $object->mark("udp.test", ["mark" => "element"]); + $object->mark("udp.test", ["mark" => "element1"]); + $object->mark("udp.test", ["mark" => "element2"]); + $object->mark("udp.test", ["mark" => "element3"]); + + // Wait UDP/IP message arrives + usleep(200e3); + + $body = $this->object->query("select * from udp.test"); + + $this->assertCount(4, $body[0]["points"]); + $this->assertEquals("udp.test", $body[0]["name"]); + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index cf38911f3..9dd58c2cc 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -7,5 +7,12 @@ return [ "database" => "mine", "username" => "root", "password" => "root", - ] + ], + "udp" => [ + "host" => "localhost", + "port" => 5551, + "database" => "mine", + "username" => "root", + "password" => "root" + ], ]; From 5a00852f7361d7497f64cf3a76d98a6ef1f0011a Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Sat, 13 Sep 2014 00:14:05 +0200 Subject: [PATCH 11/15] Removed Udp spec for connectable --- spec/InfluxDB/Adapter/UdpAdapterSpec.php | 1 - spec/InfluxDB/ClientSpec.php | 22 ---------------------- 2 files changed, 23 deletions(-) diff --git a/spec/InfluxDB/Adapter/UdpAdapterSpec.php b/spec/InfluxDB/Adapter/UdpAdapterSpec.php index 5d1aefc6a..48e14198f 100644 --- a/spec/InfluxDB/Adapter/UdpAdapterSpec.php +++ b/spec/InfluxDB/Adapter/UdpAdapterSpec.php @@ -21,6 +21,5 @@ class UdpAdapterSpec extends ObjectBehavior function it_should_implement_adapter_interface() { $this->shouldImplement("InfluxDB\Adapter\AdapterInterface"); - $this->shouldImplement("InfluxDB\Adapter\ConnectableInterface"); } } diff --git a/spec/InfluxDB/ClientSpec.php b/spec/InfluxDB/ClientSpec.php index 888f21b8c..a0ee5ae02 100644 --- a/spec/InfluxDB/ClientSpec.php +++ b/spec/InfluxDB/ClientSpec.php @@ -20,28 +20,6 @@ class ClientSpec extends ObjectBehavior $this->shouldHaveType('InfluxDB\Client'); } - function it_should_be_connectable(ConnectableInterface $adapter) - { - $adapter->connect()->willReturn(true)->shouldBeCalledTimes(1); - - $this->setAdapter($adapter); - $this->connect()->shouldReturn(true); - } - - function it_should_be_disconnectable(ConnectableInterface $adapter) - { - $adapter->disconnect()->willReturn(true)->shouldBeCalledTimes(1); - - $this->setAdapter($adapter); - $this->disconnect()->shouldReturn(true); - } - - function it_should_not_call_connect(AdapterInterface $adapter) - { - $this->setAdapter($adapter); - $this->connect()->shouldReturn(false); - } - function it_should_send_data(AdapterInterface $adapter) { $adapter->send([[ From e9a487ae2afac2ecb611072ab6eefe4a1c42a6e6 Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Sat, 13 Sep 2014 00:18:21 +0200 Subject: [PATCH 12/15] set phpspec in non-interactive mode --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index bc5c8ba79..38ea92490 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,5 +19,5 @@ before_script: - composer install --prefer-source script: - - vendor/bin/phpspec run + - vendor/bin/phpspec run -n - vendor/bin/phpunit From b5cb30a50d07a082533753840441d00452bbbced Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Sat, 13 Sep 2014 08:19:51 +0200 Subject: [PATCH 13/15] Added database management for HTTP client --- scripts/influxdb_conf.toml | 2 +- spec/InfluxDB/Adapter/GuzzleAdapterSpec.php | 26 +++++++++++ src/InfluxDB/Adapter/GuzzleAdapter.php | 38 +++++++++++++--- src/InfluxDB/Adapter/QueryableInterface.php | 3 ++ src/InfluxDB/Adapter/UdpAdapter.php | 2 +- src/InfluxDB/Client.php | 24 ++++++++++ src/InfluxDB/Options.php | 16 +++++++ tests/InfluxDB/ClientTest.php | 50 +++++++++++---------- tests/bootstrap.php | 4 +- 9 files changed, 133 insertions(+), 32 deletions(-) diff --git a/scripts/influxdb_conf.toml b/scripts/influxdb_conf.toml index e0c505a7b..4c946e0b3 100644 --- a/scripts/influxdb_conf.toml +++ b/scripts/influxdb_conf.toml @@ -57,7 +57,7 @@ read-timeout = "5s" [[input_plugins.udp_servers]] # array of tables enabled = true port = 5551 - database = "mine" + database = "udp.test" # Raft configuration [raft] diff --git a/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php b/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php index 5c61ce1bb..815fa87a7 100644 --- a/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php +++ b/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php @@ -13,6 +13,7 @@ class GuzzleAdapterSpec extends ObjectBehavior function let(Client $client, Options $options) { $options->getHttpSeriesEndpoint()->willReturn("localhost"); + $options->getHttpDatabaseEndpoint()->willReturn("localhost"); $options->getUsername()->willReturn("one"); $options->getPassword()->willReturn("two"); $this->beConstructedWith($client, $options); @@ -61,4 +62,29 @@ class GuzzleAdapterSpec extends ObjectBehavior )->willReturn(new Response(200, [], null)); $this->query("select * from tcp.test", "s")->shouldReturn(null); } + + function it_should_list_all_databases(Client $client, Options $options) + { + $client->get( + "localhost", + [ + "auth" => ["one", "two"] + ] + )->shouldBeCalledTimes(1)->willReturn(new Response(200, [], null)); + + $this->getDatabases()->shouldReturn(null); + } + + function it_should_create_a_new_database(Client $client, Options $options) + { + $client->post( + "localhost", + [ + "auth" => ["one", "two"], + "body" => json_encode(["name" => "db_name"]) + ] + )->shouldBeCalledTimes(1)->willReturn(new Response(200, [], null)); + + $this->createDatabase("db_name")->shouldReturn(null); + } } diff --git a/src/InfluxDB/Adapter/GuzzleAdapter.php b/src/InfluxDB/Adapter/GuzzleAdapter.php index bd715057b..45ad43ea3 100644 --- a/src/InfluxDB/Adapter/GuzzleAdapter.php +++ b/src/InfluxDB/Adapter/GuzzleAdapter.php @@ -42,11 +42,39 @@ class GuzzleAdapter implements AdapterInterface, QueryableInterface } $endpoint = $this->options->getHttpSeriesEndpoint(); - try { + return $this->httpClient->get($endpoint, $options)->json(); - } catch (\Exception $e) { - var_dump((string)$e->getResponse()->getBody(true)); - die(); - } + } + + public function getDatabases() + { + $options = [ + "auth" => [$this->options->getUsername(), $this->options->getPassword()], + ]; + + $endpoint = $this->options->getHttpDatabaseEndpoint(); + + return $this->httpClient->get($endpoint, $options)->json(); + } + + public function createDatabase($name) + { + $httpMessage = [ + "auth" => [$this->options->getUsername(), $this->options->getPassword()], + "body" => json_encode(["name" => $name]) + ]; + + $endpoint = $this->options->getHttpDatabaseEndpoint(); + return $this->httpClient->post($endpoint, $httpMessage)->json(); + } + + public function deleteDatabase($name) + { + $httpMessage = [ + "auth" => [$this->options->getUsername(), $this->options->getPassword()], + ]; + + $endpoint = $this->options->getHttpDatabaseEndpoint($name); + return $this->httpClient->delete($endpoint, $httpMessage)->json(); } } diff --git a/src/InfluxDB/Adapter/QueryableInterface.php b/src/InfluxDB/Adapter/QueryableInterface.php index fa604b069..164f30bf4 100644 --- a/src/InfluxDB/Adapter/QueryableInterface.php +++ b/src/InfluxDB/Adapter/QueryableInterface.php @@ -4,4 +4,7 @@ namespace InfluxDb\Adapter; interface QueryableInterface { public function query($query, $timePrecision = false); + public function getDatabases(); + public function createDatabase($name); + public function deleteDatabase($name); } diff --git a/src/InfluxDB/Adapter/UdpAdapter.php b/src/InfluxDB/Adapter/UdpAdapter.php index e2dcf8a51..88827173f 100644 --- a/src/InfluxDB/Adapter/UdpAdapter.php +++ b/src/InfluxDB/Adapter/UdpAdapter.php @@ -14,8 +14,8 @@ class UdpAdapter implements AdapterInterface public function send($message) { - $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); $message = json_encode($message); + $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); socket_sendto($socket, $message, strlen($message), 0, $this->options->getHost(), $this->options->getPort()); socket_close($socket); } diff --git a/src/InfluxDB/Client.php b/src/InfluxDB/Client.php index 2b9017025..6971d71ba 100644 --- a/src/InfluxDB/Client.php +++ b/src/InfluxDB/Client.php @@ -41,6 +41,30 @@ class Client return $this->getAdapter()->query($query, $timePrecision); } + public function getDatabases() + { + if (!($this->getAdapter() instanceOf QueryableInterface)) { + throw new \BadMethodCallException("You can query the database only if the adapter supports it!"); + } + return $this->getAdapter()->getDatabases(); + } + + public function createDatabase($name) + { + if (!($this->getAdapter() instanceOf QueryableInterface)) { + throw new \BadMethodCallException("You can query the database only if the adapter supports it!"); + } + return $this->getAdapter()->createDatabase($name); + } + + public function deleteDatabase($name) + { + if (!($this->getAdapter() instanceOf QueryableInterface)) { + throw new \BadMethodCallException("You can query the database only if the adapter supports it!"); + } + return $this->getAdapter()->deleteDatabase($name); + } + private function clearTimePrecision($timePrecision) { switch ($timePrecision) { diff --git a/src/InfluxDB/Options.php b/src/InfluxDB/Options.php index 02ff5ef44..a9478ba44 100644 --- a/src/InfluxDB/Options.php +++ b/src/InfluxDB/Options.php @@ -97,4 +97,20 @@ class Options $this->getDatabase() ); } + + public function getHttpDatabaseEndpoint($name = false) + { + $url = sprintf( + "%s://%s:%d/db", + $this->getProtocol(), + $this->getHost(), + $this->getPort() + ); + + if ($name !== false) { + $url .= "/{$name}"; + } + + return $url; + } } diff --git a/tests/InfluxDB/ClientTest.php b/tests/InfluxDB/ClientTest.php index e4d5b20a1..5d0dc8af1 100644 --- a/tests/InfluxDB/ClientTest.php +++ b/tests/InfluxDB/ClientTest.php @@ -5,7 +5,6 @@ use InfluxDB\Adapter\GuzzleAdapter as InfluxHttpAdapter; use InfluxDB\Options; use InfluxDB\Adapter\UdpAdapter; use GuzzleHttp\Client as GuzzleHttpClient; -use crodas\InfluxPHP\Client as Crodas; class ClientTest extends \PHPUnit_Framework_TestCase { @@ -20,28 +19,6 @@ class ClientTest extends \PHPUnit_Framework_TestCase $options = include __DIR__ . '/../bootstrap.php'; $this->rawOptions = $options; - $client = new Crodas( - $options["tcp"]["host"], - $options["tcp"]["port"], - $options["tcp"]["username"], - $options["tcp"]["password"] - ); - try { - $client->deleteDatabase($options["tcp"]["database"]); - } catch (\Exception $e) { - // nothing... - } - $client->createDatabase($options["tcp"]["database"]); - - try { - $client->deleteDatabase($options["udp"]["database"]); - } catch (\Exception $e) { - // nothing... - } - $client->createDatabase($options["udp"]["database"]); - - $this->anotherClient = $client; - $tcpOptions = $options["tcp"]; $options = new Options(); @@ -59,6 +36,14 @@ class ClientTest extends \PHPUnit_Framework_TestCase $influx = new Client(); $influx->setAdapter($adapter); $this->object = $influx; + + $databases = $this->object->getDatabases(); + foreach ($databases as $database) { + $this->object->deleteDatabase($database["name"]); + } + + $this->object->createDatabase($this->rawOptions["udp"]["database"]); + $this->object->createDatabase($this->rawOptions["tcp"]["database"]); } /** @@ -122,6 +107,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase { $rawOptions = $this->rawOptions; $options = new Options(); + $options->setHost($rawOptions["udp"]["host"]); $options->setUsername($rawOptions["udp"]["username"]); $options->setPassword($rawOptions["udp"]["password"]); $options->setPort($rawOptions["udp"]["port"]); @@ -138,9 +124,27 @@ class ClientTest extends \PHPUnit_Framework_TestCase // Wait UDP/IP message arrives usleep(200e3); + $this->options->setDatabase("udp.test"); $body = $this->object->query("select * from udp.test"); $this->assertCount(4, $body[0]["points"]); $this->assertEquals("udp.test", $body[0]["name"]); } + + public function testListActiveDatabses() + { + $databases = $this->object->getDatabases(); + + $this->assertCount(2, $databases); + } + + public function testCreateANewDatabase() + { + $this->object->createDatabase("walter"); + $databases = $this->object->getDatabases(); + + $this->assertCount(3, $databases); + + $this->object->deleteDatabase("walter"); + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 9dd58c2cc..99b776def 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -4,14 +4,14 @@ return [ "host" => "localhost", "port" => 8086, "protocol" => "http", - "database" => "mine", + "database" => "tcp.test", "username" => "root", "password" => "root", ], "udp" => [ "host" => "localhost", "port" => 5551, - "database" => "mine", + "database" => "udp.test", "username" => "root", "password" => "root" ], From 930593811bd7aa5004b5f4b104a394ad243df554 Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Sat, 13 Sep 2014 08:22:28 +0200 Subject: [PATCH 14/15] Removed old dependencies --- composer.json | 4 +-- composer.lock | 91 +++------------------------------------------------ 2 files changed, 5 insertions(+), 90 deletions(-) diff --git a/composer.json b/composer.json index e6b7245c2..ea532ed47 100644 --- a/composer.json +++ b/composer.json @@ -7,9 +7,7 @@ }, "require-dev": { "phpspec/phpspec": "2.*", - "phpunit/phpunit": "4.*", - "zendframework/zend-stdlib": "~2", - "crodas/influx-php": "dev-master" + "phpunit/phpunit": "4.*" }, "homepage": "http://www.corley.it/", "keywords": ["influxdb", "udp", "sdk"], diff --git a/composer.lock b/composer.lock index 98d05b929..18269867e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "1b622dda38682502a547657384b6fe7f", + "hash": "17fa2295e2cd007935278098484ab7cc", "packages": [ { "name": "guzzlehttp/guzzle", @@ -126,39 +126,6 @@ } ], "packages-dev": [ - { - "name": "crodas/influx-php", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/crodas/InfluxPHP.git", - "reference": "205bd51cb44dec70d0da06ba87e47f5c0986bdb4" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/crodas/InfluxPHP/zipball/205bd51cb44dec70d0da06ba87e47f5c0986bdb4", - "reference": "205bd51cb44dec70d0da06ba87e47f5c0986bdb4", - "shasum": "" - }, - "type": "library", - "autoload": { - "classmap": [ - "lib/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD" - ], - "authors": [ - { - "name": "César D. Rodas", - "email": "crodas@php.net" - } - ], - "description": "Simple client for InfluxDB", - "time": "2014-09-04 17:26:53" - }, { "name": "ocramius/instantiator", "version": "1.1.3", @@ -1335,65 +1302,15 @@ "description": "Symfony Yaml Component", "homepage": "http://symfony.com", "time": "2014-08-31 03:22:04" - }, - { - "name": "zendframework/zend-stdlib", - "version": "2.3.1", - "target-dir": "Zend/Stdlib", - "source": { - "type": "git", - "url": "https://github.com/zendframework/Component_ZendStdlib.git", - "reference": "c1f4830018b5d4f034d32fa01a9e17ea176f56f6" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/zendframework/Component_ZendStdlib/zipball/c1f4830018b5d4f034d32fa01a9e17ea176f56f6", - "reference": "c1f4830018b5d4f034d32fa01a9e17ea176f56f6", - "shasum": "" - }, - "require": { - "php": ">=5.3.23" - }, - "require-dev": { - "zendframework/zend-eventmanager": "self.version", - "zendframework/zend-serializer": "self.version", - "zendframework/zend-servicemanager": "self.version" - }, - "suggest": { - "zendframework/zend-eventmanager": "To support aggregate hydrator usage", - "zendframework/zend-serializer": "Zend\\Serializer component", - "zendframework/zend-servicemanager": "To support hydrator plugin manager usage" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.3-dev", - "dev-develop": "2.4-dev" - } - }, - "autoload": { - "psr-0": { - "Zend\\Stdlib\\": "" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "keywords": [ - "stdlib", - "zf2" - ], - "time": "2014-04-15 15:28:48" } ], "aliases": [ ], "minimum-stability": "stable", - "stability-flags": { - "crodas/influx-php": 20 - }, + "stability-flags": [ + + ], "prefer-stable": false, "platform": [ From 0244b151c08d76776eda5f3050f7d20889bd0b72 Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Sat, 13 Sep 2014 08:40:30 +0200 Subject: [PATCH 15/15] Updated docs --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6fc9ea862..5fbc30cb3 100644 --- a/README.md +++ b/README.md @@ -42,9 +42,12 @@ $client->setAdapter($adapter); Actually Guzzle is used as HTTP client library -``` +```php +setAdapter($adapter);