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
|
|
|
from datetime import datetime |
26
|
|
|
from uuid import uuid4 |
27
|
|
|
from optparse import make_option |
28
|
|
|
|
29
|
|
|
from django.core.management.base import BaseCommand |
30
|
|
|
|
31
|
|
|
from freezegun import freeze_time |
32
|
|
|
|
33
|
|
|
from omaha.parser import parse_request |
34
|
|
|
from omaha.statistics import collect_statistics |
35
|
|
|
from omaha.models import Version |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
os_version_list = [ |
39
|
|
|
'5.0', |
40
|
|
|
'5.1', |
41
|
|
|
'5.2', |
42
|
|
|
'6.0', |
43
|
|
|
'6.1', |
44
|
|
|
'6.2', |
45
|
|
|
'6.3'] |
46
|
|
|
|
47
|
|
|
os_sp_list = [ |
48
|
|
|
'Service Pack 1', |
49
|
|
|
'Service Pack 2', |
50
|
|
|
'Service Pack 3'] |
51
|
|
|
|
52
|
|
|
os_arch_list = ['x86', 'x64'] |
53
|
|
|
|
54
|
|
|
request_tml = """<?xml version="1.0" encoding="UTF-8"?> |
55
|
|
|
<request protocol="3.0" version="1.3.23.0" ismachine="0" sessionid="{sessionid}" installsource="taggedmi" testsource="auto" requestid="{requestid}" |
56
|
|
|
userid="{userid}" > |
57
|
|
|
<os platform="win" version="{os_version}" sp="{os_sp}" arch="{os_arch}"/> |
58
|
|
|
<app appid="{app_id}" version="{version}" nextversion="{nextversion}" lang="en" brand="" client="" installage="-1"> |
59
|
|
|
{events} |
60
|
|
|
</app> |
61
|
|
|
</request>""" |
62
|
|
|
|
63
|
|
|
event_list = [ |
64
|
|
|
"""<event eventtype="9" eventresult="1" errorcode="0" extracode1="0"/> |
65
|
|
|
<event eventtype="5" eventresult="1" errorcode="0" extracode1="0"/> |
66
|
|
|
<event eventtype="1" eventresult="1" errorcode="0" extracode1="0" download_time_ms="3903" downloaded="32768" total="32768"/> |
67
|
|
|
<event eventtype="6" eventresult="1" errorcode="0" extracode1="0"/> |
68
|
|
|
<event eventtype="2" eventresult="5" errorcode="1639" extracode1="0"/>""", |
69
|
|
|
"""<event eventtype="9" eventresult="1" errorcode="0" extracode1="0"/> |
70
|
|
|
<event eventtype="5" eventresult="1" errorcode="0" extracode1="0"/> |
71
|
|
|
<event eventtype="1" eventresult="1" errorcode="0" extracode1="0"/> |
72
|
|
|
<event eventtype="6" eventresult="1" errorcode="0" extracode1="0"/> |
73
|
|
|
<event eventtype="2" eventresult="0" errorcode="-2147219195" extracode1="268435469"/>""", |
74
|
|
|
"""<event eventtype="9" eventresult="1" errorcode="0" extracode1="0"/> |
75
|
|
|
<event eventtype="5" eventresult="1" errorcode="0" extracode1="0"/> |
76
|
|
|
<event eventtype="1" eventresult="1" errorcode="0" extracode1="0" download_time_ms="3903" downloaded="32768" total="32768"/> |
77
|
|
|
<event eventtype="6" eventresult="1" errorcode="0" extracode1="0"/> |
78
|
|
|
<event eventtype="2" eventresult="5" errorcode="1639" extracode1="0"/>""", |
79
|
|
|
"""<event eventtype="9" eventresult="1" errorcode="0" extracode1="0"/> |
80
|
|
|
<event eventtype="5" eventresult="1" errorcode="0" extracode1="0"/> |
81
|
|
|
<event eventtype="1" eventresult="1" errorcode="0" extracode1="0" download_time_ms="3903" downloaded="32768" total="32768"/> |
82
|
|
|
<event eventtype="6" eventresult="1" errorcode="0" extracode1="0"/> |
83
|
|
|
<event eventtype="2" eventresult="1" errorcode="0" extracode1="0"/>""", |
84
|
|
|
] |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
def get_random_uuid(): |
88
|
|
|
return "{%s}" % uuid4() |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
def get_random_date(): |
92
|
|
|
now = datetime.now() |
93
|
|
|
month = random.choice(range(1, 13)) |
94
|
|
|
day = random.choice(range(1, 28)) |
95
|
|
|
return datetime(now.year, month, day, hour=8) |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
def generate_events(app_id, **options): |
99
|
|
|
versions = Version.objects.filter_by_enabled(app__id=app_id) |
100
|
|
|
|
101
|
|
|
userid_list = list(map(lambda x: get_random_uuid(), range(1, 25))) |
102
|
|
|
sessionid_list = list(map(lambda x: get_random_uuid(), range(1, 50))) |
103
|
|
|
|
104
|
|
|
for i in range(1, options['count'] + 1): |
105
|
|
|
if i % 10 == 0: |
106
|
|
|
print('=> ', i) |
107
|
|
|
|
108
|
|
|
version = random.choice(versions) |
109
|
|
|
request = request_tml.format( |
110
|
|
|
sessionid=random.choice(sessionid_list), |
111
|
|
|
requestid=get_random_uuid(), |
112
|
|
|
userid=random.choice(userid_list), |
113
|
|
|
os_version=random.choice(os_version_list), |
114
|
|
|
os_sp=random.choice(os_sp_list), |
115
|
|
|
os_arch=random.choice(os_arch_list), |
116
|
|
|
app_id=app_id, |
117
|
|
|
version='', |
118
|
|
|
nextversion=str(version.version), |
119
|
|
|
events=random.choice(event_list) |
120
|
|
|
) |
121
|
|
|
|
122
|
|
|
request = bytes(request, 'utf8') |
123
|
|
|
|
124
|
|
|
request_obj = parse_request(request) |
125
|
|
|
|
126
|
|
|
with freeze_time(get_random_date()): |
127
|
|
|
collect_statistics(request_obj, ip='8.8.8.8') |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
class Command(BaseCommand): |
131
|
|
|
args = '<app_id>' |
132
|
|
|
help = 'A command for generating fake data such as requests, events and statistics' |
133
|
|
|
option_list = BaseCommand.option_list + ( |
134
|
|
|
make_option('--count', |
135
|
|
|
dest='count', |
136
|
|
|
default='100', |
137
|
|
|
type=int, |
138
|
|
|
help='Total number of data values (default: 100)'), |
139
|
|
|
) |
140
|
|
|
|
141
|
|
|
def handle(self, app_id, *args, **options): |
142
|
|
|
generate_events(app_id, **options) |
143
|
|
|
|