-
Notifications
You must be signed in to change notification settings - Fork 0
/
reports.py
248 lines (198 loc) · 6.69 KB
/
reports.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
from calendar import month_abbr
from matplotlib.pyplot import plot, stackplot, pie, bar, xlim, ylim, xticks, legend, show
from utils import prepare, getstores, getsongs, getcountries
def streamsbystoreovertime(data):
"""Streams by store over time"""
stores = getstores(data)
periods = []
streams = [[] for _ in stores]
for period in data:
count = [0]*len(stores)
periods.append(month_abbr[period.month] + ' ' + period.year[2:])
for sale in period.sales:
if sale.method == 'Streaming':
for i in range(len(stores)):
if sale.store == stores[i]:
count[i] += sale.units
break
for i in range(len(count)):
streams[i].append(count[i])
cloud = 0
mp3 = 0
unlimited = 0
for i in range(len(stores)):
if stores[i] == 'Amazon Cloud':
cloud = i
stores[i] = 'Amazon'
elif stores[i] == 'Amazon MP3':
mp3 = i
elif stores[i] == 'Amazon Unlimited':
unlimited = i
elif stores[i] == 'YouTube Music Key':
stores[i] = 'YouTube'
for i in range(len(streams[0])):
streams[cloud][i] += streams[unlimited][i] + streams[mp3][i]
for i in sorted([mp3, unlimited], reverse=True):
del stores[i], streams[i]
plots = range(len(periods))
prepare('line')
stackplot(plots, streams, labels=stores)
xlim(0, len(periods) - 1)
xticks(plots, periods, rotation=90)
legend()
show()
def streamsbysongovertime(data):
"""Streams by song over time"""
songs = getsongs(data)
periods = []
streams = [[] for _ in songs]
for period in data:
count = [0]*len(songs)
periods.append(month_abbr[period.month] + ' ' + period.year[2:])
for sale in period.sales:
if sale.method == 'Streaming':
for i in range(len(songs)):
if sale.song == songs[i]:
count[i] += sale.units
break
for i in range(len(count)):
streams[i].append(count[i])
plots = range(len(periods))
prepare('line')
stackplot(plots, streams, labels=songs)
xlim(0, len(periods) - 1)
xticks(plots, periods, rotation=90)
legend()
show()
def streamsbycountry(data):
"""Streams by country"""
countries = getcountries(data)
streams = [0]*len(countries)
for period in data:
for sale in period.sales:
if sale.method == 'Streaming':
for i in range(len(countries)):
if sale.country == countries[i]:
streams[i] += sale.units
break
sort = sorted(zip(streams, countries))
streams = [count for count, country in sort]
countries = [country for count, country in sort]
prepare('pie')
pie(streams, labels=countries, startangle=90)
show()
def valuebycountry(data):
"""Value by country"""
countries = getcountries(data)
totals = [0]*len(countries)
streams = [0]*len(countries)
plots = range(len(countries))
for period in data:
for sale in period.sales:
if sale.method == 'Streaming':
for i in plots:
if sale.country == countries[i]:
totals[i] += sale.total
streams[i] += sale.units
break
values = [totals[i]/streams[i] for i in plots]
sort = sorted(zip(values, countries), reverse=True)
values = [value for value, country in sort]
countries = [country for value, country in sort]
prepare('bar')
bar(plots, values)
xticks(plots, countries)
show()
def valuebystore(data):
"""Value by store"""
stores = getstores(data)
for i in range(len(stores)):
if stores[i] == 'Amazon MP3':
del stores[i]
break
totals = [0] * len(stores)
streams = [0] * len(stores)
for period in data:
for sale in period.sales:
if sale.method == 'Streaming':
for i in range(len(stores)):
if sale.store == stores[i]:
totals[i] += sale.total
streams[i] += sale.units
break
cloud = 0
unlimited = 0
for i in range(len(stores)):
if stores[i] == 'Amazon Cloud':
cloud = i
stores[i] = 'Amazon'
elif stores[i] == 'Amazon Unlimited':
unlimited = i
elif stores[i] == 'YouTube Music Key':
stores[i] = 'YouTube'
totals[cloud] += totals[unlimited]
for i in sorted([unlimited], reverse=True):
del stores[i], totals[i]
plots = range(len(stores))
values = [totals[i] / streams[i] for i in plots]
sort = sorted(zip(values, stores), reverse=True)
values = [value for value, store in sort]
stores = [store for value, store in sort]
prepare('bar')
bar(plots, values)
xticks(plots, stores)
show()
def downloadsovertime(data):
"""Downloads over time"""
periods = []
downloads = []
for period in data:
count = 0
periods.append(month_abbr[period.month] + ' ' + period.year[2:])
for sale in period.sales:
if sale.method == 'Download':
count += sale.units
downloads.append(count)
plots = range(len(periods))
prepare('line')
plot(plots, downloads)
xlim(0, len(periods) - 1)
ylim(bottom=0)
xticks(plots, periods, rotation=90)
show()
def totalbystore(data):
"""Total by store"""
stores = getstores(data)
for i in range(len(stores)):
if stores[i] == 'Amazon MP3':
del stores[i]
break
totals = [0] * len(stores)
for period in data:
for sale in period.sales:
if sale.method == 'Streaming':
for i in range(len(stores)):
if sale.store == stores[i]:
totals[i] += sale.total
break
cloud = 0
unlimited = 0
for i in range(len(stores)):
if stores[i] == 'Amazon Cloud':
cloud = i
stores[i] = 'Amazon'
elif stores[i] == 'Amazon Unlimited':
unlimited = i
elif stores[i] == 'YouTube Music Key':
stores[i] = 'YouTube'
totals[cloud] += totals[unlimited]
for i in sorted([unlimited], reverse=True):
del stores[i], totals[i]
sort = sorted(zip(totals, stores), reverse=True)
totals = [total for total, store in sort]
stores = [store for total, store in sort]
prepare('bar')
plots = range(len(stores))
bar(plots, totals)
xticks(plots, stores)
show()