Skip to content

Commit 23f8b4d

Browse files
committed
Added runAndDownload to Report
1 parent d1cf584 commit 23f8b4d

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

Atomx/Resources/Report.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ public function download($report)
7373
return new ReportStreamer($stream);
7474
}
7575

76+
public function runAndDownload($json, $timeout = 120)
77+
{
78+
$reportData = $this->run($json);
79+
80+
$secondsWaiting = 0;
81+
82+
while (!Report::isReady($this->status(Report::getReportId($reportData)))) {
83+
sleep(1);
84+
85+
if (++$secondsWaiting >= $timeout) {
86+
return false;
87+
}
88+
}
89+
90+
return $this->download($reportData);
91+
}
92+
7693
protected function handleResponse(Response $response)
7794
{
7895
if ($response->getStatusCode() == 200) {

tests/ReportTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,29 @@ public function testDownloadReport()
5858
var_dump($imps, $revenue);
5959
}
6060

61+
public function testRunAndDownloadReport()
62+
{
63+
$from = date('Y-m-d 00:00:00', time() - 24 * 60 * 60);
64+
$to = date('Y-m-d 00:00:00', time());
65+
66+
$report = new Report(new AtomxAccountStore());
67+
$options = array(
68+
'scope' => 'advertiser',
69+
'groups' => ['campaign_id', 'domain_id', 'day_timestamp'],
70+
'sums' => ['impressions', 'clicks', 'conversions', 'campaign_cost'],
71+
'where' => [['advertiser_network_id', '==', '1']],
72+
'from' => $from,
73+
'to' => $to,
74+
'timezone' => 'UTC'
75+
);
76+
77+
$streamer = $report->runAndDownload($options, 1);
78+
79+
$this->assertNotEquals(false, $streamer);
80+
81+
var_dump($streamer->readLine());
82+
}
83+
6184
/**
6285
* @return array
6386
*/

0 commit comments

Comments
 (0)