Completed
Push — master ( 67eae9...f0f1ec )
by
unknown
01:23
created

generate_statistics()   B

Complexity

Conditions 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 25
rs 8.5806
cc 4
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 random
22
from datetime import datetime
23
from uuid import uuid4
24
from optparse import make_option
25
from multiprocessing import Pool, cpu_count
26
from multiprocessing.dummy import Pool as ThreadPool
27
from functools import partial
28
29
from django.core.management.base import BaseCommand
30
31
from omaha.models import Version, Channel
32
from omaha.statistics import userid_counting
33
from sparkle.models import SparkleVersion
34
35
NUMBER_UNIQUE = 1000
36
uuids = dict(
37
    win=["{%s}" % uuid4() for i in range(NUMBER_UNIQUE)],
38
    mac=["{%s}" % uuid4() for i in range(NUMBER_UNIQUE)]
39
)
40
41
42
def get_random_uuid(platform):
43
    return random.choice(uuids[platform])
44
45
46
def generate_statistics(i, versions, channels, year):
47
    year = year
48
49
    if i % 100 == 0:
50
        print('=> %s' % i)
51
    version = random.choice(versions)
52
    platform = version.platform.name if getattr(version, 'platform', None) else 'mac'
53
    channel = random.choice(channels)
54
    if platform == 'win':
55
        app_list = [dict(
56
            appid=version.app.id,
57
            version=str(version.version),
58
            tag=channel,
59
        )]
60
    else:
61
        app_list = [dict(
62
            appid=version.app.id,
63
            version=str(version.short_version),
64
            tag=version.channel
65
        )]
66
    month = random.choice(range(1, 13))
67
    day = random.choice(range(1, 28))
68
    date = datetime(year, month, day)
69
    userid = get_random_uuid(platform)
70
    userid_counting(userid, app_list, platform, now=date)
71
72
73
def run_worker(data, versions, channels, year):
74
    t_pool = ThreadPool()
75
    t_pool.imap_unordered(partial(generate_statistics,
76
                                  versions=versions,
77
                                  channels=channels,
78
                                  year=year), data)
79
    t_pool.close()
80
    t_pool.join()
81
82
83
class Command(BaseCommand):
84
    help = 'A command for generating fake statistics'
85
    option_list = BaseCommand.option_list + (
86
        make_option('--count',
87
                    dest='count',
88
                    default='1000',
89
                    type=int,
90
                    help='Total number of data values (default: 1000)'),
91
        make_option('--year',
92
                    dest='year',
93
                    default=datetime.now().year,
94
                    type=int,
95
                    help='Year of statistics (default: Current year)'),
96
    )
97
98
    def handle(self, *args, **options):
99
        user_count = options['count'] + 1
100
        year = options['year']
101
        users = range(1, user_count)
102
        versions = list(Version.objects.select_related('app', 'platform').filter_by_enabled())
103
        versions += list(SparkleVersion.objects.select_related('app').filter_by_enabled())
104
        channels = list(Channel.objects.all())
105
106
        job_size = int(user_count / (cpu_count() or 1 * 2)) or 1
107
        job_data = [users[i:i + job_size] for i in range(0, len(users), job_size)]
108
        pool = Pool()
109
        pool.imap_unordered(partial(run_worker, versions=versions, channels=channels, year=year), job_data)
110
        pool.close()
111
        pool.join()