Skip to content

Commit

Permalink
added stats aggregates listing
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeZ99 committed Jul 17, 2016
1 parent 67e5c60 commit 7d879e2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
45 changes: 45 additions & 0 deletions lib/doofinder_management_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
*
Expand Down
4 changes: 3 additions & 1 deletion lib/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class NotAllowed extends Exception {}

class BadRequest extends Exception {}

class NotFound extends Exception {}

class QuotaExhausted extends Exception {}

class WrongResponse extends Exception {}
Expand All @@ -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:
Expand Down

0 comments on commit 7d879e2

Please sign in to comment.