diff --git a/lib/doofinder_management_api.php b/lib/doofinder_management_api.php index 8a1ffbc..cc5bfe1 100644 --- a/lib/doofinder_management_api.php +++ b/lib/doofinder_management_api.php @@ -282,6 +282,12 @@ function deleteItem($dType, $itemId){ * - 'task_created': boolean true if a new task has been created * - 'task_id': if task created, the id of the task. */ + + function stats($from_date=null, $to_date=null){ + return new AggregatesIterator($this, $from_date, $to_date); + } + + function process(){ $result = $this->dma->managementApiCall('POST', "{$this->hashid}/tasks/process"); $taskCreated = ($result['statusCode'] == 201); @@ -413,6 +419,45 @@ function rewind(){ } } +class AggregatesIterator extends ItemsRS { + protected $last_page = 0; + protected $searchParams = array(); + + function __construct($searchEngine, $from_date=null, $to_date=null){ + $this->last_page = 0; + if($from_date!=null){ + $this->searchParams['from'] = $from_date->format("Ymd"); + } + if($to_date!=null){ + $this->searchParams['to'] = $to_date->format("Ymd"); + } + parent::__construct($searchEngine); + } + + protected function fetchResultsAndTotal(){ + $params = $this->last_page > 0 ? array("page"=>$this->last_page + 1) : array(); + try{ + $apiResponse = $this->searchEngine->dma->managementApiCall( + 'GET', + "{$this->searchEngine->hashid}/stats", + array_merge($params, $this->searchParams) + ); + $this->resultsPage = $apiResponse['response']['aggregates']; + $this->total = $apiResponse['response']['count']; + $this->last_page++; + $this->currentItem = each($this->resultsPage); + } catch (NotFound $nfe) { + $this->resultsPage = array(); + } + reset($this->resultsPage); + } + + function rewind(){ + $this->last_page = 0; + parent::rewind(); + } +} + /** * Extracts identificator from an item or task url. * diff --git a/lib/errors.php b/lib/errors.php index 4087574..875787c 100644 --- a/lib/errors.php +++ b/lib/errors.php @@ -4,6 +4,8 @@ class NotAllowed extends Exception {} class BadRequest extends Exception {} +class NotFound extends Exception {} + class QuotaExhausted extends Exception {} class WrongResponse extends Exception {} @@ -27,7 +29,7 @@ function handleErrors($statusCode, $response){ case 401: throw new NotAllowed("The user hasn't provided valid authorization: ".readError($response)); case 404: - throw new BadRequest("Not Found: ".readError($response)); + throw new NotFound("Not Found: ".readError($response)); case 409: // trying to post with an already used id throw new BadRequest("Request conflict: ".readError($response)); case 429: