1
|
|
|
# coding: utf8 |
2
|
|
|
|
3
|
|
|
""" |
4
|
|
|
This software is licensed under the Apache 2 license, quoted below. |
5
|
|
|
|
6
|
|
|
Copyright 2014 Crystalnix Limited |
7
|
|
|
|
8
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not |
9
|
|
|
use this file except in compliance with the License. You may obtain a copy of |
10
|
|
|
the License at |
11
|
|
|
|
12
|
|
|
http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
|
14
|
|
|
Unless required by applicable law or agreed to in writing, software |
15
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
16
|
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
17
|
|
|
License for the specific language governing permissions and limitations under |
18
|
|
|
the License. |
19
|
|
|
""" |
20
|
|
|
|
21
|
|
|
import __builtin__ |
22
|
|
|
import random |
23
|
|
|
from datetime import datetime |
24
|
|
|
from uuid import uuid4 |
25
|
|
|
from optparse import make_option |
26
|
|
|
from multiprocessing import Pool, cpu_count |
27
|
|
|
from multiprocessing.dummy import Pool as ThreadPool |
28
|
|
|
from functools import partial |
29
|
|
|
|
30
|
|
|
from django.core.management.base import BaseCommand |
31
|
|
|
|
32
|
|
|
from omaha.models import Version, Channel |
33
|
|
|
from omaha.statistics import userid_counting |
34
|
|
|
from omaha.tests.utils import create_app_xml |
35
|
|
|
from sparkle.models import SparkleVersion |
36
|
|
|
from sparkle.statistics import userid_counting as mac_userid_counting |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
events = [dict(eventtype="2", eventresult="1", errorcode="0", extracode1="0"), |
40
|
|
|
dict(eventtype="2", eventresult="1", errorcode="0", extracode1="0"), |
41
|
|
|
dict(eventtype="2", eventresult="1", errorcode="0", extracode1="0"), |
42
|
|
|
dict(eventtype="2", eventresult="1", errorcode="0", extracode1="0"), |
43
|
|
|
dict(eventtype="4", eventresult="1", errorcode="0", extracode1="0")] |
44
|
|
|
|
45
|
|
|
NUMBER_UNIQUE = 1000 |
46
|
|
|
uuids = ["{%s}" % uuid4() for i in range(NUMBER_UNIQUE)] |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
def get_random_uuid(platform): |
50
|
|
|
return random.choice(uuids) |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def generate_statistics(i, versions, channels, year): |
54
|
|
|
year = year |
55
|
|
|
|
56
|
|
|
if i % 100 == 0: |
57
|
|
|
print('=> %s' % i) |
58
|
|
|
version = random.choice(versions) |
59
|
|
|
platform = version.platform.name if getattr(version, 'platform', None) else 'mac' |
60
|
|
|
channel = random.choice(channels) |
61
|
|
|
if platform != 'mac': |
62
|
|
|
app = create_app_xml(appid=version.app.id, |
63
|
|
|
version=str(version.version), |
64
|
|
|
tag=channel.name, |
65
|
|
|
events=[random.choice(events)]) |
66
|
|
|
app_list = [app] |
67
|
|
|
else: |
68
|
|
|
app_list = dict( |
69
|
|
|
appid=version.app.id, |
70
|
|
|
version=str(version.short_version), |
71
|
|
|
tag=version.channel |
72
|
|
|
) |
73
|
|
|
month = random.choice(range(1, 13)) |
74
|
|
|
day = random.choice(range(1, 28)) |
75
|
|
|
date = datetime(year, month, day) |
76
|
|
|
userid = get_random_uuid(platform) |
77
|
|
|
if platform == 'mac': |
78
|
|
|
mac_userid_counting(userid, app_list, platform, now=date) |
79
|
|
|
else: |
80
|
|
|
userid_counting(userid, app_list, platform, now=date) |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
def run_worker(data, versions, channels, year): |
84
|
|
|
t_pool = ThreadPool() |
85
|
|
|
t_pool.imap_unordered(partial(generate_statistics, |
86
|
|
|
versions=versions, |
87
|
|
|
channels=channels, |
88
|
|
|
year=year), data) |
89
|
|
|
t_pool.close() |
90
|
|
|
t_pool.join() |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
class Command(BaseCommand): |
94
|
|
|
help = 'A command for generating fake statistics' |
95
|
|
|
option_list = BaseCommand.option_list + ( |
96
|
|
|
make_option('--count', |
97
|
|
|
dest='count', |
98
|
|
|
default='1000', |
99
|
|
|
type=int, |
100
|
|
|
help='Total number of data values (default: 1000)'), |
101
|
|
|
make_option('--year', |
102
|
|
|
dest='year', |
103
|
|
|
default=datetime.now().year, |
104
|
|
|
type=int, |
105
|
|
|
help='Year of statistics (default: Current year)'), |
106
|
|
|
) |
107
|
|
|
|
108
|
|
|
def handle(self, *args, **options): |
109
|
|
|
user_count = options['count'] + 1 |
110
|
|
|
year = options['year'] |
111
|
|
|
users = range(1, user_count) |
112
|
|
|
versions = list(Version.objects.select_related('app', 'platform').filter_by_enabled()) |
113
|
|
|
versions += list(SparkleVersion.objects.select_related('app').filter_by_enabled()) |
114
|
|
|
channels = list(Channel.objects.all()) |
115
|
|
|
|
116
|
|
|
job_size = int(user_count / (cpu_count() or 1 * 2)) or 1 |
117
|
|
|
job_data = [users[i:i + job_size] for i in range(0, len(users), job_size)] |
118
|
|
|
pool = Pool() |
119
|
|
|
pool.imap_unordered(partial(run_worker, versions=versions, channels=channels, year=year), job_data) |
120
|
|
|
pool.close() |
121
|
|
|
pool.join() |