Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit a4fb796

Browse files
committed
autodetect report scope
1 parent 723bd4c commit a4fb796

2 files changed

Lines changed: 52 additions & 12 deletions

File tree

atomx/__init__.py

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# -*- coding: utf-8 -*-
22

3-
from datetime import datetime
3+
from datetime import (
4+
datetime,
5+
timedelta,
6+
)
47
import requests
58
from atomx.version import API_VERSION, VERSION
69
from atomx import models
@@ -9,6 +12,7 @@
912
APIError,
1013
ModelNotFoundError,
1114
InvalidCredentials,
15+
MissingArgumentError,
1216
)
1317

1418

@@ -109,13 +113,16 @@ def search(self, query):
109113
for v in search_result[m]]
110114
return search_result
111115

112-
def report(self, scope, groups, sums, where, from_, to=None, timezone='UTC', fast=True):
116+
def report(self, scope=None, groups=None, sums=None, where=None,
117+
from_=None, to=None, timezone='UTC', fast=True):
113118
"""Create a report.
114119
115120
See the `reporting atomx wiki <http://wiki.atomx.com/doku.php?id=reporting>`_
116121
for details about parameters and available groups, sums.
117122
118123
:param str scope: either 'advertiser' or 'publisher' to select the type of report.
124+
If undefined but the groups column have an unambiguous attribute that's
125+
unique to a certain scope, it's set automatically.
119126
:param list groups: columns to group by.
120127
:param list sums: columns to sum on.
121128
:param list where: is a list of expression lists.
@@ -128,7 +135,7 @@ def report(self, scope, groups, sums, where, from_, to=None, timezone='UTC', fas
128135
and ``not in`` a list of numbers.
129136
130137
:param datetime.datetime from_: :class:`datetime.datetime` where the report
131-
should start (inclusive)
138+
should start (inclusive). (defaults to last week)
132139
:param datetime.datetime to: :class:`datetime.datetime` where the report
133140
should end (exclusive). (defaults to `datetime.now()` if undefined)
134141
:param str timezone: Timezone used for all times. (defaults to `UTC`)
@@ -139,18 +146,47 @@ def report(self, scope, groups, sums, where, from_, to=None, timezone='UTC', fas
139146
to speed up the query.
140147
:return: A :class:`atomx.models.Report` model
141148
"""
142-
report_json = locals().copy()
143-
del report_json['self']
149+
report_json = {'timezone': timezone, 'fast': fast}
150+
151+
if groups:
152+
report_json['groups'] = groups
153+
if sums:
154+
report_json['sums'] = sums
155+
elif not groups:
156+
raise MissingArgumentError('Either `groups` or `sums` have to be set.')
157+
158+
if scope is None:
159+
for i in report_json.get('groups', []) + report_json.get('sums', []):
160+
if i.split('_')[0] in ['advertiser', 'campaign', 'creative', 'pixel']:
161+
scope = 'advertiser'
162+
break
163+
else:
164+
for i in report_json.get('groups', []) + report_json.get('sums', []):
165+
if i.split('_')[0] in ['site', 'placement', 'user']:
166+
scope = 'publisher'
167+
break
168+
else:
169+
raise MissingArgumentError('Unable to detect scope automatically. '
170+
'Please set `scope` parameter.')
171+
report_json['scope'] = scope
172+
173+
if where:
174+
report_json['where'] = where
175+
176+
if from_ is None:
177+
from_ = datetime.now() - timedelta(days=7)
178+
if isinstance(from_, datetime):
179+
report_json['from'] = from_.strftime("%Y-%m-%d %H:00:00")
180+
else:
181+
report_json['from'] = from_
144182

145183
if to is None:
146-
report_json['to'] = datetime.now()
147-
if isinstance(report_json['to'], datetime):
148-
report_json['to'] = report_json['to'].strftime("%Y-%m-%d %H:00:00")
149-
if isinstance(report_json.get('from_'), datetime):
150-
report_json['from'] = report_json['from_'].strftime("%Y-%m-%d %H:00:00")
184+
to = datetime.now()
185+
if isinstance(to, datetime):
186+
report_json['to'] = to.strftime("%Y-%m-%d %H:00:00")
151187
else:
152-
report_json['from'] = report_json['from_']
153-
del report_json['from_']
188+
report_json['to'] = to
189+
154190
r = self.session.post(self.api_endpoint + 'report', json=report_json)
155191
if not r.ok:
156192
raise APIError(r.json()['error'])

atomx/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class APIError(Exception):
1212
"""Raised when the atomx api returns an error that is not caught otherwise."""
1313
pass
1414

15+
class MissingArgumentError(Exception):
16+
"""Raised when argument is missing."""
17+
pass
18+
1519
class ModelNotFoundError(Exception):
1620
"""Raised when trying to (re-)load a model that is not in the api."""
1721
pass

0 commit comments

Comments
 (0)