Skip to content

Commit 0fd160c

Browse files
committed
Reporting now uses v3 api
1 parent 855f884 commit 0fd160c

File tree

2 files changed

+11
-191
lines changed

2 files changed

+11
-191
lines changed

Atomx/Resources/Report.php

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,90 +9,20 @@
99
class Report extends AtomxClient {
1010
private $returnStream = false;
1111

12-
// Run report
13-
public function run($json)
12+
public function run($json, $timeout = 600)
1413
{
15-
return $this->postUrl('report', compact('json'));
16-
}
17-
18-
public function status($reportId)
19-
{
20-
return $this->getUrl('report/' . $reportId, ['status' => true]);
21-
}
22-
23-
public static function getReportId($report)
24-
{
25-
if (isset($report['report']['id'])) {
26-
return $report['report']['id'];
27-
}
28-
29-
return false;
30-
}
31-
32-
public static function isReady($report)
33-
{
34-
if (isset($report['report']['is_ready'])) {
35-
return $report['report']['is_ready'];
36-
}
37-
38-
return false;
39-
}
40-
41-
public static function numberOfRows($report)
42-
{
43-
if (isset($report['report']['lines']))
44-
return $report['report']['lines'];
45-
46-
return false;
47-
}
48-
49-
public static function getColumns($report)
50-
{
51-
if (!is_null($report) && isset($report['query'])) {
52-
$sumsOrMetrics = (isset($report['query']['sums']) ? $report['query']['sums'] : $report['query']['metrics']);
53-
54-
return array_merge($report['query']['groups'], $sumsOrMetrics);
55-
}
56-
57-
return false;
58-
}
59-
60-
public function download($report)
61-
{
62-
$reportId = $report['report']['id'];
63-
6414
$this->returnStream = true;
6515

66-
$stream = $this->getUrl('report/' . $reportId, [], [
67-
// 'timeout' => 0,
68-
// 'connect_timeout' => 0
16+
$stream = $this->postUrl('report?download', compact('json'), [
17+
'timeout' => $timeout,
18+
'connect_timeout' => 20
6919
]);
7020

7121
$this->returnStream = false;
7222

7323
return new ReportStreamer($stream);
7424
}
7525

76-
public function runAndDownload($json, $timeout = 120, $returnReportId = false)
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-
if ($returnReportId)
91-
return [$this->download($reportData), Report::getReportId($reportData)];
92-
else
93-
return $this->download($reportData);
94-
}
95-
9626
protected function handleResponse(Response $response)
9727
{
9828
if ($response->getStatusCode() == 200) {

tests/ReportTest.php

Lines changed: 7 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -10,128 +10,18 @@ public function testCreateAndDownloadReport()
1010
$to = date('Y-m-d 00:00:00', time());
1111

1212
$report = new Report(new AtomxAccountStore());
13-
$options = array(
14-
'scope' => 'advertiser',
15-
'groups' => ['campaign_id', 'domain_id', 'day_timestamp'],
16-
'sums' => ['impressions', 'clicks', 'conversions', 'campaign_cost'],
13+
$options = [
14+
'scope' => 'network_buy',
15+
'groups' => ['day', 'campaign_id', 'creative_id', 'country_id'],
16+
'metrics' => ['impressions', 'clicks', 'conversions', 'campaign_cost', 'advertiser_network_profit'],
1717
'where' => [['advertiser_network_id', '==', '1']],
1818
'from' => $from,
1919
'to' => $to,
2020
'timezone' => 'UTC'
21-
);
21+
];
2222

23-
//$report = $report->run($options);
23+
$report = $report->run($options);
2424

25-
//var_export($report);
26-
}
27-
28-
public function testReportIsDone()
29-
{
30-
$report = new Report(new AtomxAccountStore());
31-
$rData = $report->status(Report::getReportId($this->getReportData()));
32-
33-
var_dump($rData);
34-
35-
$this->assertEquals(true, Report::isReady($rData));
36-
}
37-
38-
public function testDownloadReport()
39-
{
40-
$report = new Report(new AtomxAccountStore());
41-
$rData = $this->getReportData();
42-
43-
$streamer = $report->download($rData);
44-
45-
var_dump(Report::numberOfRows($rData));
46-
var_dump(Report::getColumns($rData));
47-
48-
var_dump($streamer->readLine());
49-
50-
$imps = 0;
51-
$revenue = 0;
52-
53-
while (($row = $streamer->readLine()) !== false) {
54-
$imps += $row[3];
55-
$revenue += $row[6];
56-
}
57-
58-
var_dump($imps, $revenue);
59-
}
60-
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);
78-
79-
$this->assertNotEquals(false, $streamer);
80-
81-
var_dump($streamer->readLine());
82-
}
83-
84-
/**
85-
* @return array
86-
*/
87-
private function getReportData()
88-
{
89-
// TODO: Mock the response
90-
$rData = [
91-
'success' => true,
92-
'timestamp' => '2015-09-23T15:30:38.563796',
93-
'report' =>
94-
[
95-
'link' => '/v1/report/89130c7cd76c7637195224529bbae9c7',
96-
'duration' => NULL,
97-
'started' => 1443022238,
98-
'lines' => 0,
99-
'error' => NULL,
100-
'finished' => NULL,
101-
'fast' => true,
102-
'is_ready' => true,
103-
'id' => '89130c7cd76c7637195224529bbae9c7',
104-
],
105-
'query' =>
106-
[
107-
'from' => '2015-09-22 00:00:00',
108-
'timezone' => 'UTC',
109-
'sums' =>
110-
array (
111-
0 => 'impressions',
112-
1 => 'clicks',
113-
2 => 'conversions',
114-
3 => 'campaign_cost',
115-
),
116-
'groups' =>
117-
array (
118-
0 => 'campaign_id',
119-
1 => 'domain_id',
120-
2 => 'day_timestamp',
121-
),
122-
'to' => '2015-09-23 00:00:00',
123-
'where' =>
124-
array (
125-
0 =>
126-
array (
127-
0 => 'advertiser_network_id',
128-
1 => '==',
129-
2 => '1',
130-
),
131-
),
132-
'scope' => 'advertiser'
133-
]
134-
];
135-
return $rData;
25+
$this->assertEquals(['day', 'campaign_id', 'creative_id', 'country_id','impressions', 'clicks', 'conversions', 'campaign_cost', 'advertiser_network_profit'], $report->readLine());
13626
}
13727
}

0 commit comments

Comments
 (0)