This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
/
search.py
227 lines (200 loc) · 7.13 KB
/
search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from decimal import Decimal
from django.db.models import Count, Q, Sum
import elasticutils
import pyes.exceptions as pyes
import amo
from mkt import MKT_CUT
from mkt.inapp_pay.models import InappPayment
from mkt.webapps.models import Installed
from stats.models import Contribution
def get_finance_total(qs, addon, field=None, **kwargs):
"""
sales/revenue/refunds per app overall
field -- breakdown field name contained by kwargs
"""
q = Q()
if field:
kwargs_copy = {field: kwargs[field]}
q = handle_kwargs(q, field, kwargs)
revenue = (qs.values('addon').filter(q, refund=None, **kwargs).
annotate(revenue=Sum('amount')))
sales = (qs.values('addon').filter(q, refund=None, **kwargs).
annotate(sales=Count('id')))
refunds = (qs.filter(q, refund__isnull=False, **kwargs).
values('addon').annotate(refunds=Count('id')))
document = {
'addon': addon,
'count': sales[0]['sales'] if sales.count() else 0,
'revenue': cut(revenue[0]['revenue'] if revenue.count() else 0),
'refunds': refunds[0]['refunds'] if refunds.count() else 0,
}
if field:
# Edge case, handle None values.
if kwargs_copy[field] == None:
kwargs_copy[field] = ''
document[field] = kwargs_copy[field]
return document
def get_finance_total_inapp(qs, addon, inapp_name='', field=None, **kwargs):
"""
sales/revenue/refunds per in-app overall
field -- breakdown field name contained by kwargs
"""
q = Q()
if field:
kwargs_copy = {field: kwargs[field]}
q = handle_kwargs(q, field, kwargs, join_field='contribution__')
revenue = (qs.filter(q, contribution__refund=None, **kwargs).
values('config__addon').annotate(
revenue=Sum('contribution__amount')))
sales = (qs.filter(q, contribution__refund=None, **kwargs).
values('config__addon').
annotate(sales=Count('id')))
refunds = (qs.filter(q, contribution__refund__isnull=False, **kwargs).
values('config__addon').annotate(refunds=Count('id')))
document = {
'addon': addon,
'inapp': inapp_name,
'count': sales[0]['sales'] if sales.count() else 0,
'revenue': cut(revenue[0]['revenue'] if revenue.count() else 0),
'refunds': refunds[0]['refunds'] if refunds.count() else 0,
}
if field:
# Edge case, handle None values.
if kwargs_copy[field] == None:
kwargs_copy[field] = ''
document[field] = kwargs_copy[field]
return document
def get_finance_daily(contribution):
"""
sales per day
revenue per day
refunds per day
"""
addon_id = contribution['addon']
date = contribution['created'].date()
return {
'date': date,
'addon': addon_id,
'count': Contribution.objects.filter(
addon__id=addon_id,
refund=None,
created__year=date.year,
created__month=date.month,
created__day=date.day).count() or 0,
'revenue': cut(Contribution.objects.filter(
addon__id=addon_id,
refund=None,
type=amo.CONTRIB_PURCHASE,
created__year=date.year,
created__month=date.month,
created__day=date.day).aggregate(Sum('amount'))['amount__sum']
or 0),
'refunds': Contribution.objects.filter(
addon__id=addon_id,
refund__isnull=False,
created__year=date.year,
created__month=date.month,
created__day=date.day).count() or 0,
}
def get_finance_daily_inapp(payment):
"""
sales per day for inapp
revenue per day for inapp
refunds per day for inapp
"""
addon_id = payment['config__addon']
inapp = payment['name']
date = payment['created'].date()
return {
'date': date,
'addon': addon_id,
'inapp': inapp,
'count': InappPayment.objects.filter(
config__addon__id=addon_id,
contribution__refund=None,
created__year=date.year,
created__month=date.month,
created__day=date.day).count() or 0,
'revenue': cut(InappPayment.objects.filter(
config__addon__id=addon_id,
contribution__refund=None,
contribution__type=amo.CONTRIB_PURCHASE,
created__year=date.year,
created__month=date.month,
created__day=date.day).aggregate(
Sum('contribution__amount'))['contribution__amount__sum']
or 0),
'refunds': InappPayment.objects.filter(
contribution__addon__id=addon_id,
contribution__refund__isnull=False,
created__year=date.year,
created__month=date.month,
created__day=date.day).count() or 0,
}
def get_installed_daily(installed):
"""
installs per day
"""
addon_id = installed['addon']
date = installed['created'].date()
return {
'date': date,
'addon': addon_id,
'count': Installed.objects.filter(
created__year=date.year,
created__month=date.month,
created__day=date.day).count()
}
def setup_mkt_indexes():
"""
Define explicit ES mappings for models. If a field is not explicitly
defined and a field is inserted, ES will dynamically guess the type and
insert it, in a schemaless manner.
"""
es = elasticutils.get_es()
for model in [Contribution, InappPayment]:
index = model._get_index()
try:
es.create_index_if_missing(index)
except pyes.ElasticSearchException:
pass
mapping = {
'properties': {
'id': {'type': 'long'},
'date': {'format': 'dateOptionalTime',
'type': 'date'},
'count': {'type': 'long'},
'revenue': {'type': 'double'},
# Try to tell ES not to 'analyze' the field to querying with
# hyphens and lowercase letters.
'currency': {'type': 'string',
'index': 'not_analyzed'},
'source': {'type': 'string',
'index': 'not_analyzed'},
'inapp': {'type': 'string',
'index': 'not_analyzed'}
}
}
es.put_mapping(model._meta.db_table, mapping,
model._get_index())
def cut(revenue):
"""
Takes away Marketplace's cut from developers' revenue.
"""
return Decimal(str(round(revenue * MKT_CUT, 2)))
def handle_kwargs(q, field, kwargs, join_field=None):
"""
Processes kwargs to combine '' and None values and make it ready for
filters. Returns Q object to use in filter.
"""
if join_field:
join_field += field
kwargs[join_field] = kwargs[field]
# Have '' and None have the same meaning.
if not kwargs[field]:
q = Q(**{field + '__in': ['', None]})
del(kwargs[field])
# We are using the join field to filter so get rid of the plain one.
if join_field:
del(kwargs[field])
return q