From 50a904e361bce4ec3b71bdfa06e5ad2b0e04541b Mon Sep 17 00:00:00 2001 From: Jason Dugdale Date: Fri, 16 Oct 2015 17:02:31 +0100 Subject: [PATCH 1/8] Adds example using the "offset" parameter in a PYLON Analysis Query --- examples/pylon/ayalysis-with-offset.php | 55 +++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 examples/pylon/ayalysis-with-offset.php diff --git a/examples/pylon/ayalysis-with-offset.php b/examples/pylon/ayalysis-with-offset.php new file mode 100644 index 0000000..64178e5 --- /dev/null +++ b/examples/pylon/ayalysis-with-offset.php @@ -0,0 +1,55 @@ + + * @copyright 2105 MediaSift Ltd. + * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) + * @link http://www.mediasift.com + */ + +// Include the DataSift library +require dirname(__FILE__) . '/../../lib/datasift.php'; + +// Include the configuration - put your username and API key in this file +require dirname(__FILE__) . '/../../config.php'; + +if (function_exists('date_default_timezone_set')) { + date_default_timezone_set('UTC'); +} + +// Authenticate +echo "Creating user...\n"; +$user = new DataSift_User(USERNAME, API_KEY, false); + +//Set your Analysis Query values +$hash = ''; // This must be the hash of an active recording +// This filter will likely get redacted; change to something related to your recording, or exclude +$filter = 'fb.content contains "some content"'; +$start = mktime(0, 0, 0, date('n'), date('j') - 7); // 7 days ago +$end = mktime(0, 0, 0, date('n'), date('j')); // This morning + +// Get the PYLON recording by hash +$pylon = DataSift_Pylon::fromHash($user, $hash); + +//Set your Analysis Query parameters +$parameters = array( + 'analysis_type' => 'timeSeries', + 'parameters' => array( + 'interval' => 'day', + 'offset' => -5 + ) +); + +try { + //Analyze the recording + $analyze = $pylon->analyze($parameters, false, $start, $end, false); +} catch (Exception $e) { + echo "Caught exception during analysis:\n"; + print_r($e); +} + +echo json_encode($analyze, true); From 963cac359727031bd68ed741216b893b97031aea Mon Sep 17 00:00:00 2001 From: Paul Mozo Date: Mon, 7 Dec 2015 10:33:36 +0000 Subject: [PATCH 2/8] Fixed spelling error in example file name --- .../pylon/{ayalysis-with-offset.php => analysis-with-offset.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/pylon/{ayalysis-with-offset.php => analysis-with-offset.php} (100%) diff --git a/examples/pylon/ayalysis-with-offset.php b/examples/pylon/analysis-with-offset.php similarity index 100% rename from examples/pylon/ayalysis-with-offset.php rename to examples/pylon/analysis-with-offset.php From 893678abda13ac505f0896a27e4a8036365f5d95 Mon Sep 17 00:00:00 2001 From: Paul Mozo Date: Tue, 2 Feb 2016 11:01:05 +0000 Subject: [PATCH 3/8] Account usage added --- lib/DataSift/Account.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/DataSift/Account.php b/lib/DataSift/Account.php index ccd63a5..9936e7b 100644 --- a/lib/DataSift/Account.php +++ b/lib/DataSift/Account.php @@ -48,6 +48,27 @@ public function __construct(DataSift_User $user) { $this->_user = $user; } + + /** + * Returns the user agent this library should use for all API calls. + * + * @return string + */ + public function usage($start = false, $end = false) + { + $params = array(); + + if ($start) + { + $params['start'] = $start; + } + if ($end) + { + $params['end'] = $end; + } + + return $this->_user->get('account/usage', $params); + } /** * Returns the user agent this library should use for all API calls. From 0216574b0ce36b1b05777e66d6f886240ce80aed Mon Sep 17 00:00:00 2001 From: Paul Mozo Date: Tue, 2 Feb 2016 15:54:30 +0000 Subject: [PATCH 4/8] Bumped API version to 1.3 Changed PYLON methods to use IDs instead of hashes Added a PYLON Update method Added a PYLON restart method --- lib/DataSift/Pylon.php | 146 +++++++++++++++++++++++++++-------------- lib/DataSift/User.php | 2 +- 2 files changed, 98 insertions(+), 50 deletions(-) diff --git a/lib/DataSift/Pylon.php b/lib/DataSift/Pylon.php index 3a5350c..1785951 100644 --- a/lib/DataSift/Pylon.php +++ b/lib/DataSift/Pylon.php @@ -50,6 +50,11 @@ class DataSift_Pylon */ private $_hash; + /** + * @var string The ID of this DataSift_Pylon recording + */ + private $_id; + /** * @var string The status of this DataSift_Pylon */ @@ -115,19 +120,18 @@ public function __construct($user, $data = false) /** * Class method to find a Subscription * - * @param string $hash + * @param string $id * * @return DataSift_Pylon */ - public function find($hash) + public function find($id) { - return new self($this->_user, self::get($this->_user, $hash)); + return new self($this->_user, self::get($this->_user, $id)); } /** * Class method to find all Subscriptions * - * @param string $hash * * @return DataSift_Pylon */ @@ -153,18 +157,18 @@ public function findAll($page = 1, $per_page = 20, $order_by = self::ORDERBY_CRE * Get an existing recordings. * * @param Datasift_User $user The Datasift user object - * @param string $hash The hash of the existing pylon + * @param string $id The id of the existing pylon * * @throws DataSift_Exception_InvalidData * * @return DataSift_Pylon */ - static public function get($user, $hash = false) + static public function get($user, $id = false) { $params = array(); - if ($hash) { - $params['hash'] = $hash; + if ($id) { + $params['id'] = $id; } return $user->get('pylon/get', $params); @@ -220,14 +224,16 @@ static public function validate($user, $csdl) /** * Load an existing pylon from hash * + * Previously called fromHash + * * @param Datasift_User $user The Datasift user object * @param string $hash The Hash of the recording * * @return DataSift_Pylon */ - static public function fromHash($user, $hash) + static public function fromId($user, $id) { - return new self($user, self::get($user, $hash)); + return new self($user, self::get($user, $id)); } /** @@ -239,12 +245,14 @@ static public function fromHash($user, $hash) */ private function load($data) { - if (empty($data)) { + if (empty($data)) + { throw new DataSift_Exception_InvalidData('No data found'); } //Assign the instance variables - foreach ($data as $key => $value) { + foreach ($data as $key => $value) + { $this->{'_'.$key} = $value; } } @@ -256,11 +264,11 @@ private function load($data) */ public function reload() { - if (strlen($this->_hash) == 0) { - throw new DataSift_Exception_InvalidData('Unable to reload pylon without a hash'); + if (strlen($this->_id) == 0) { + throw new DataSift_Exception_InvalidData('Unable to reload pylon without an ID'); } - $this->load(self::get($this->_user, $this->_hash)); + $this->load(self::get($this->_user, $this->_id)); } /** @@ -308,13 +316,23 @@ public function getName() /** * Gets the Hash * - * @return string $name The name of the pylon + * @return string $hash The hash of the pylon recording */ public function getHash() { return $this->_hash; } + /** + * Gets the ID + * + * @return string $id The ID of the pylon + */ + public function getId() + { + return $this->_id; + } + /** * Gets the current volume of this PYLON subscription * @@ -453,21 +471,32 @@ public function start($hash = false, $name = false) } /** - * Stops the pylon + * Restarts the pylon recording + * + * @param string $id If ID is provided it will be set * - * @param string $hash If hash is provided it will be set */ - public function stop($hash = false) + public function restart($id = false) { - if ($hash) { - $this->_hash = $hash; + if ($id) { + $this->_id = $id; } - if (strlen($this->_hash) == 0) { - throw new DataSift_Exception_InvalidData('Cannot stop a recording without a hash'); + $this->_user->put('pylon/start', array('id' => $this->_id)); + } + + /** + * Stops the pylon recording + * + * @param string $id If ID is provided it will be set + */ + public function stop($id = false) + { + if ($id) { + $this->_id = $id; } - $this->_user->post('pylon/stop', array('hash' => $this->_hash)); + $this->_user->post('pylon/stop', array('id' => $this->_id)); } @@ -478,14 +507,14 @@ public function stop($hash = false) * @param string $filter additional CSDL filter * @param int $start the start time of the pylon * @param int $end the end time of the pylon - * @param string $hash If hash is provided it will be set + * @param string $id If id is provided it will be set * * @return array Response from the compile */ - public function analyze($parameters, $filter = false, $start = false, $end = false, $hash = false) + public function analyze($parameters, $filter = false, $start = false, $end = false, $id = false) { - if ($hash) { - $this->_hash = $hash; + if ($id) { + $this->_id = $id; } //If parameters is not an array try and decode it @@ -497,7 +526,7 @@ public function analyze($parameters, $filter = false, $start = false, $end = fal throw new DataSift_Exception_InvalidData('Parameters must be supplied as an array or valid JSON'); } $params = array( - 'hash' => $this->_hash, + 'id' => $this->_id, 'parameters' => $parameters ); @@ -520,28 +549,24 @@ public function analyze($parameters, $filter = false, $start = false, $end = fal /** * Analyze the tags in the data set * - * @param string $hash If hash is provided it will be set + * @param string $id If ID is provided it will be set * * @return array Response from the tags endpoint */ - public function tags($hash=false) + public function tags($id = false) { - if ($hash) { - $this->_hash = $hash; + if ($id) { + $this->_id = $id; } - if (strlen($this->_hash) == 0) { - throw new DataSift_Exception_InvalidData('Unable to analyze tags without a hash'); - } - - return $this->_user->get('pylon/tags', array('hash' => $this->_hash)); + return $this->_user->get('pylon/tags', array('id' => $this->_id)); } /** * Returns a list of sample interactions (super-public) * * @param Datasift_User $user The Datasift user object - * @param string $hash The hash of the existing pylon + * @param string $id The id of the existing pylon * @param string $filter additional CSDL filter * @param int $start the start time of the pylon * @param int $end the end time of the pylon @@ -549,17 +574,15 @@ public function tags($hash=false) * * @throws DataSift_Exception_InvalidData * - * @return DataSift_Pylon + * @return array Response from the sample endpoint */ - public function sample($filter = false, $start = false, $end = false, $count = false, $hash = false) + public function sample($filter = false, $start = false, $end = false, $count = false, $id = false) { - if ($hash) { - $this->_hash = $hash; - } - if (strlen($this->_hash) == 0) { - throw new DataSift_Exception_InvalidData('Unable to start sample without a hash'); + if ($id) { + $this->_id = $id; } - $params = array('hash' => $this->_hash); + + $params = array('id' => $this->_id); if ($start) { $params['start'] = $start; @@ -576,8 +599,33 @@ public function sample($filter = false, $start = false, $end = false, $count = f if ($filter) { $params['filter'] = $filter; return $this->_user->post('pylon/sample', $params); - } else { + } + else { return $this->_user->get('pylon/sample', $params); } } -} \ No newline at end of file + + /** + * Updates a recording with a new hash and or name + * + * @param string $id The id of the existing recording + * @param string $hash The new hash of the pylon recording + * @param string $name The new updated name of the recording + * + * @throws DataSift_Exception_InvalidData + */ + public function update($id, $hash = false, $name = false) + { + $params = array('id' => $id); + + if ($hash) { + $params['hash'] = $hash; + } + if ($name) { + $params['name'] = $name; + } + + $this->_user->put('pylon/update', $params); + } + +} diff --git a/lib/DataSift/User.php b/lib/DataSift/User.php index 17f89bd..571c32b 100644 --- a/lib/DataSift/User.php +++ b/lib/DataSift/User.php @@ -84,7 +84,7 @@ class DataSift_User */ protected $_last_response = array(); - protected $_api_version = 'v1.2'; + protected $_api_version = 'v1.3'; /** * Constructor. A username and API key are required when constructing an From 82860ec0df8ba50a7cd5c503ed365a4b7ffae523 Mon Sep 17 00:00:00 2001 From: Paul Mozo Date: Wed, 3 Feb 2016 11:12:30 +0000 Subject: [PATCH 5/8] Changed PYLON tests to use IDs instead of hashes --- lib/DataSift/Pylon.php | 10 ++++++++++ tests/PylonTest.php | 22 +++++++++++----------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/DataSift/Pylon.php b/lib/DataSift/Pylon.php index 1785951..8d0166e 100644 --- a/lib/DataSift/Pylon.php +++ b/lib/DataSift/Pylon.php @@ -495,6 +495,9 @@ public function stop($id = false) if ($id) { $this->_id = $id; } + if (strlen($this->_id) == 0) { + throw new DataSift_Exception_InvalidData('Unable to reload pylon without an ID'); + } $this->_user->post('pylon/stop', array('id' => $this->_id)); } @@ -558,6 +561,9 @@ public function tags($id = false) if ($id) { $this->_id = $id; } + if (strlen($this->_id) == 0) { + throw new DataSift_Exception_InvalidData('Unable to get tags without an ID'); + } return $this->_user->get('pylon/tags', array('id' => $this->_id)); } @@ -582,6 +588,10 @@ public function sample($filter = false, $start = false, $end = false, $count = f $this->_id = $id; } + if (strlen($this->_id) == 0) { + throw new DataSift_Exception_InvalidData('Unable to retrieve pylon sample without an ID'); + } + $params = array('id' => $this->_id); if ($start) { diff --git a/tests/PylonTest.php b/tests/PylonTest.php index 3b26407..3e44201 100644 --- a/tests/PylonTest.php +++ b/tests/PylonTest.php @@ -328,15 +328,15 @@ public function testStop(){ } - public function testNoHashStop(){ + public function testNoIdStop(){ $pylon = new DataSift_Pylon($this->user); $this->setExpectedException('DataSift_Exception_InvalidData'); - $hash = ''; + $id = ''; - $pylon->stop($hash); + $pylon->stop($id); } @@ -623,14 +623,14 @@ public function testTags(){ } - public function testTagsEmptyHash(){ + public function testTagsEmptyId(){ $pylon = new DataSift_Pylon($this->user); - $hash = ""; + $id = ""; $this->setExpectedException('DataSift_Exception_InvalidData'); - $tags = $pylon->tags($hash); + $tags = $pylon->tags($id); } public function testEmptyTags(){ @@ -645,9 +645,9 @@ public function testEmptyTags(){ $pylon = new DataSift_Pylon($this->user); - $hash = "1a4268c9b924d2c48ed1946d6a7e6272"; + $id = "1a4268c9b924d2c48ed1946d6a7e6272"; - $tags = $pylon->tags($hash); + $tags = $pylon->tags($id); $this->assertCount(0, $tags, 'Amount of tags did not match'); @@ -675,7 +675,7 @@ public function testSample(){ DataSift_MockApiClient::setResponse($response); - $pylon = new DataSift_Pylon($this->user, array('hash' => '1a4268c9b924d2c48ed1946d6a7e6272')); + $pylon = new DataSift_Pylon($this->user, array('id' => '1a4268c9b924d2c48ed1946d6a7e6272')); $filter = '(fb.content any "coffee" OR fb.hashtags in "tea") AND fb.language in "en"'; $start = 1445209200; @@ -687,8 +687,8 @@ public function testSample(){ $this->assertEquals($sample['interactions']['fb']['content'], 'baz the map could, ', 'Interaction content didnt match'); } - public function testNoHash(){ - $pylon = new DataSift_Pylon($this->user, array('hash' => '')); + public function testSampleNoId(){ + $pylon = new DataSift_Pylon($this->user, array('id' => '')); $this->setExpectedException('DataSift_Exception_InvalidData'); From 42977b10aec568aeced151024ced2dbf9fe73b44 Mon Sep 17 00:00:00 2001 From: Paul Mozo Date: Fri, 26 Feb 2016 12:53:29 +0000 Subject: [PATCH 6/8] 1.7 changes and example complete --- examples/pylon/pylon.php | 12 +++++++++-- lib/DataSift/Pylon.php | 44 +++++++++++++++++++++++++++++++++------- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/examples/pylon/pylon.php b/examples/pylon/pylon.php index dffdfc6..c58af8d 100644 --- a/examples/pylon/pylon.php +++ b/examples/pylon/pylon.php @@ -21,7 +21,7 @@ echo "Creating user...\n"; $user = new DataSift_User(USERNAME, API_KEY, false); -$csdl = '(fb.content any "coffee" OR fb.hashtags in "tea") AND fb.language in "en"'; +$csdl = '(fb.content any "coffee, tea" OR fb.hashtags in "tea") AND fb.language in "en"'; //Validate the CSDL $validate = DataSift_Pylon::validate($user, $csdl); @@ -45,6 +45,14 @@ //Stop after 10 seconds sleep(10); + +//Compile some new CSDL +$pylon->setCsdl('(fb.content any "coffee, tea, flour" OR fb.hashtags in "tea") AND fb.language in "en"'); +$pylon->compile(); + +//Update the recording with the new definition +$pylon->update(); + $pylon->stop(); //Set the analyze parameters @@ -80,4 +88,4 @@ echo "There were a total of {$analyze['interactions']} interactions that matched this filter\n"; -$reloaded_pylon = DataSift_Pylon::fromHash($user, $pylon->getHash()); +$reloaded_pylon = DataSift_Pylon::fromId($user, $pylon->getId()); diff --git a/lib/DataSift/Pylon.php b/lib/DataSift/Pylon.php index 8d0166e..9fdc2d0 100644 --- a/lib/DataSift/Pylon.php +++ b/lib/DataSift/Pylon.php @@ -440,13 +440,31 @@ public function compile($csdl = false) } /** - * Starts the pylon + * Creates a new recording or restarts an existing one if an ID is present * * @param string $hash If hash is provided it will be set * @param string $name If name is provided it will be set * */ - public function start($hash = false, $name = false) + public function start($hash = false, $name = false) + { + if ($hash) { + $this->_hash = $hash; + } + + if ($name) { + $this->_name = $name; + } + + if (!empty($this->_id)) { + $this->restart(); + } + else { + $this->create(); + } + } + + public function create($hash = false, $name = false) { if ($hash) { $this->_hash = $hash; @@ -467,7 +485,11 @@ public function start($hash = false, $name = false) $params['name'] = $this->_name; } - $this->_user->post('pylon/start', $params); + $response = $this->_user->post('pylon/start', $params); + + $this->load($response); + + return $response; } /** @@ -624,17 +646,25 @@ public function sample($filter = false, $start = false, $end = false, $count = f * * @throws DataSift_Exception_InvalidData */ - public function update($id, $hash = false, $name = false) + public function update($id = false, $hash = false, $name = false) { - $params = array('id' => $id); + if ($id) { + $this->_id = $id; + } if ($hash) { - $params['hash'] = $hash; + $this->_hash = $hash; } if ($name) { - $params['name'] = $name; + $this->_name = $name; } + $params = array( + 'id' => $this->_id, + 'hash' => $this->_hash, + 'name' => $this->_name + ); + $this->_user->put('pylon/update', $params); } From ca553c325131715d7e368943dc1f13486c2bf69f Mon Sep 17 00:00:00 2001 From: Paul Mozo Date: Fri, 26 Feb 2016 14:18:03 +0000 Subject: [PATCH 7/8] Made relevant change to testStart() --- tests/PylonTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/PylonTest.php b/tests/PylonTest.php index 3e44201..58a9b97 100644 --- a/tests/PylonTest.php +++ b/tests/PylonTest.php @@ -283,9 +283,12 @@ public function testFailCompile(){ public function testStart(){ $response = array( - 'response_code' => 204, + 'response_code' => 200, 'rate_limit' => 200, 'rate_limit_remaining' => 150, + 'data' => array( + 'id' => '62721a4268c9b924d2c48ed1946d6a7e', + ) ); DataSift_MockApiClient::setResponse($response); From 96280570254f42f040bc2d1d6e97a3ac59bc4f87 Mon Sep 17 00:00:00 2001 From: Andi Miller Date: Wed, 2 Mar 2016 15:21:58 +0000 Subject: [PATCH 8/8] update the changelog --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b4bd3a..7e56545 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ Changelog ========================= + +##v.2.6.0 (2016-03-02) +### Added +* DataSift_Pylon::update added for hotswapping filters + +### Changed +* Pylon endpoints now take and return a recording ID, rather than a hash +* Moved to v1.3 API + ##v.2.5.1 ###Fixed * Fixed an issue with ```DataSift_Pylon::getStart```.