Completed
Pull Request — master (#169)
by
unknown
01:19
created

generate_fake_hour()   A

Complexity

Conditions 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 3
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
from __future__ import print_function
22
from builtins import range, bytes
23
24
import random
25
import uuid
26
27
from django.core.management.base import BaseCommand
28
from django.utils import timezone
29
from django.test import RequestFactory
30
31
from freezegun import freeze_time
32
33
from sparkle.statistics import collect_statistics
34
from sparkle.models import SparkleVersion
35
from omaha.models import Application
36
37
def generate_events(app_name, channel, **options):
38
    app_id = Application.objects.get(name=app_name).id
39
    versions = SparkleVersion.objects.filter_by_enabled(app__name=app_name)
40
    versions = map(lambda x: x.short_version, versions)
41
    request_factory = RequestFactory()
42
43
    def generate_fake_hour():
44
        for version in versions:
45
            for i in range(random.randint(0, 20)):
46
                id = uuid.UUID(int=i)
47
                request = request_factory.get('/sparkle/%s/%s/appcast.xml?appVersionShort=%s&deviceID=%s' % (
48
                    app_name,
49
                    channel,
50
                    version,
51
                    id
52
                ))
53
                collect_statistics(request, app_id, channel)
54
55
    start = timezone.now() - timezone.timedelta(days=1)
56
57
    for hourdelta in range(1, 25):
58
        if hourdelta % 10 == 0:
59
            print('=> ', hourdelta)
60
        with freeze_time(start + timezone.timedelta(hours=hourdelta)):
61
            generate_fake_hour()
62
63
64
class Command(BaseCommand):
65
    args = '<app_name> <channel>'
66
    help = 'A command for generating fake live statistics'
67
68
    def handle(self, app_name, channel, *args, **options):
69
        generate_events(app_name, channel, **options)
70