-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCDRGenerator.py
194 lines (122 loc) · 4.36 KB
/
CDRGenerator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# coding: utf-8
import numpy as np
import random
import csv
import uuid
import datetime
import os
import logging
import boto3
debug = 0
# Read in the area codes
def get_area_codes():
print("Getting area codes")
f = open("uk-area-code-list.csv", "r", encoding="utf-8-sig")
csvfile = csv.reader(f)
areacodes = []
for x in csvfile:
if (debug):
print (x)
areacodes.append(x[0])
f.close()
print("Number of area codes is", len(areacodes))
return areacodes
# Read in the country codes
def get_country_codes():
print("Getting country codes")
f = open("country-codes.csv", "r", encoding="utf-8-sig")
csvfile = csv.reader(f)
countrycodes = []
for x in csvfile:
if (debug):
print (x)
countrycodes.append(x[0])
f.close()
print("Number of country codes is", len(countrycodes))
return countrycodes
# Small function that return TRUE with a specified probability
def probability(p):
# p is a percentage
# Get a number between 1 and 200
r = random.random() * 100
if (r < p):
rv = 1
else:
rv = 0
return rv
# Construct a UK only MSISDN
# Calling country code is always 44
# Choose the first digit as 7 or something else - it should be a 7, 25% of the time.
# if 7 add the remaing 9 digits of the MSISDN
# If not 7 then normall choose an area code () and occasionally make a 4 digit area code up
# then add a 6 digit number
def get_uk_msisdn(areacodes):
cc = '44'
ac = []
if (probability(25)):
ac = '7' + str(random.randint(100, 999))
elif (probability(90)):
ac = areacodes[random.randint(0, len(areacodes)-1)]
else:
ac = str(random.choice(['3', '4', '5', '8', '9'])) + str(random.randint(100, 999))
msisdn = cc + ac + str(random.randint(100000, 999999))
return msisdn
# Construct any MSISDN, international allowed but mostly UK
# 95% of the time return a UK MSISDN
# 5% of the time get a valid countro code followed by 10 digits
def get_any_msisdn(areacodes, countrycodes):
if (probability(95)):
msisdn = get_uk_msisdn(areacodes)
else:
cc = countrycodes[random.randint(0, len(countrycodes)-1)]
msisdn = cc + str(random.randint(1000000000, 9999999999))
return msisdn
def get_cdr(areacodes, countrycodes):
id = str(uuid.uuid4())
start = datetime.datetime.now()
end = start + datetime.timedelta(0,random.randint(1, 999))
calling_msisdn = get_uk_msisdn(areacodes)
called_msisdn = get_any_msisdn(areacodes, countrycodes)
if (probability(80)):
call_type = 'Voice'
else:
call_type = 'SMS'
charge = str( int(random.random() * 1000) / 100 )
if (probability(80)):
call_result = 'SUCCESS'
else:
call_result = 'FAILURE'
cdr = str(id) + ', ' + calling_msisdn + ', ' + called_msisdn + ', '
cdr = cdr + str(start) + ', ' + str(end) + ', ' + call_type + ', '
cdr = cdr + charge + ', ' + call_result
return cdr
# set batches to the required number
batches = int(os.getenv('BATCHES', 2))
# set files_per_batch to the required number
files_per_batch = int(os.getenv('FILES_PER_BATCH', 3))
# set records_per_file to the required number
records_per_file = int(os.getenv('RECORDS_PER_FILE', 10))
prefix = os.getenv('PREFIX', datetime.datetime.utcnow().isoformat())
# f = open("cdr.csv", "w")
areacodes = get_area_codes ()
countrycodes = get_country_codes()
bucketName = os.getenv('BUCKET')
s3 = boto3.client('s3')
for b in range(batches):
print ("Batch ", b)
for f in range(files_per_batch):
filename = prefix + "-B" + str(b) + "F" + str(f) + "cdr.csv"
filepath = "/tmp/" + filename
fh = open(filepath, "w")
# Add header
fh.write("id, calling, called, start, end, call_type, charge, call_result\n");
for l in range(records_per_file):
cdr = get_cdr(areacodes, countrycodes)
fh.write(cdr + "\n")
fh.close()
print(l+1, "records written to file " + filepath + ".")
print("copy file " + filename + " to bucket " + bucketName)
with open(filepath, 'rb') as f:
s3.upload_fileobj(f, bucketName, filename)
print("delete local file " + filename)
os.remove(filepath)