Completed
Push — master ( f684af...589bde )
by Egor
01:18
created

generate_events()   C

Complexity

Conditions 8

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 8
dl 0
loc 20
rs 6.6666

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate_fake_hour() 0 8 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
30
from freezegun import freeze_time
31
32
from omaha.parser import parse_request
33
from omaha.statistics import collect_statistics
34
from omaha.models import Version
35
36
event_install = b"""<?xml version="1.0" encoding="UTF-8"?>
37
<request protocol="3.0" version="1.3.23.0" ismachine="1" sessionid="{2882CF9B-D9C2-4edb-9AAF-8ED5FCF366F7}" userid="{%s}" installsource="otherinstallcmd" testsource="ossdev" requestid="{164FC0EC-8EF7-42cb-A49D-474E20E8D352}">
38
  <os platform="win" version="6.1" sp="" arch="x64"/>
39
  <app appid="%s" version="" nextversion="%s" lang="en" brand="" client="" installage="6">
40
    <event eventtype="9" eventresult="1" errorcode="0" extracode1="0"/>
41
    <event eventtype="5" eventresult="1" errorcode="0" extracode1="0"/>
42
    <event eventtype="2" eventresult="1" errorcode="0" extracode1="0"/>
43
  </app>
44
</request>
45
"""
46
47
def generate_events(app_id, **options):
48
    versions = Version.objects.filter_by_enabled(app__id=app_id)
49
    versions = map(lambda x: x.version, versions)
50
51
    def generate_fake_hour():
52
        for version in versions:
53
            for i in range(random.randint(0, 20)):
54
                id = uuid.UUID(int=i)
55
                request = event_install % (id, app_id, version)
56
                request = bytes(request, 'utf8')
57
                request = parse_request(request)
58
                collect_statistics(request)
59
60
    start = timezone.now() - timezone.timedelta(days=1)
61
62
    for hourdelta in range(1, 25):
63
        if hourdelta % 10 == 0:
64
            print('=> ', hourdelta)
65
        with freeze_time(start + timezone.timedelta(hours=hourdelta)):
66
            generate_fake_hour()
67
68
69
class Command(BaseCommand):
70
    args = '<app_id>'
71
    help = 'A command for generating fake live statistics'
72
73
    def handle(self, app_id, *args, **options):
74
        generate_events(app_id, **options)
75