-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom_generator.py
134 lines (101 loc) · 3.76 KB
/
random_generator.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
import random, string
import os
import time
import glob
from datetime import datetime
markov = [[0.99, 0.01, 0], [0.1, 0.8, 0.1], [0, 0.05, 0.95]]
def generateEntry(startDate, endDate, md5, ips, path):
# ip = generateIp()
# md5 = generateRandomStringLength(60)
for i in range(int(random.random()*10) + 1):
ip = random.choice(ips)
time1 = generateTime()
time2 = generateTime()
time1, time2 = compareTwoTimes(time1, time2)
with open(os.path.join(path, md5+".csv"), 'a') as f:
line = ','.join([time1, time2, ip, 'iPhone', md5])
print line
f.write(line + "\n")
def generateIp():
value1 = int(random.random()*255)
value2 = int(random.random()*255)
value3 = int(random.random()*255)
value4 = int(random.random()*255)
return str(value1) + "." + str(value2) + "." + str(value3) + "." + str(value4)
def generateTime():
hour = int(random.random()*24)
minute = int(random.random()*60)
second = int(random.random()*60)
strHour = ("0" + str(hour))[-2:]
strMinute = ("0" + str(minute))[-2:]
strSecond = ("0" + str(second))[-2:]
return strHour + ":" + strMinute + ":" + strSecond
def generateRandomStringLength(X):
return ''.join(random.choice(string.lowercase + string.digits) for x in range(X))
def compareTwoTimes(time1, time2):
if int(time1[:2]) < int(time2[:2]): return (time1, time2)
if int(time1[:2]) > int(time2[:2]): return (time2, time1)
if int(time1[3:5]) < int(time2[3:5]): return (time1, time2)
if int(time1[3:5]) > int(time2[3:5]): return (time2, time1)
if int(time1[6:8]) < int(time2[6:8]): return (time1, time2)
if int(time1[6:8]) > int(time2[6:8]): return (time2, time1)
return (time1, time2)
def parseEntry(path, filename):
startingState = 1
entries = []
with open(os.path.join(path, filename), 'r') as f:
for line in f:
# timeStart, timeEnd, ip, device, id = [item.strip() for item in line.split(",")]
timeStart, timeEnd, ip, device, id = [item.strip() for item in line.split(";")]
timeStart = datetime.strptime(timeStart, "%H:%M:%S")
timeEnd = datetime.strptime(timeEnd, "%H:%M:%S")
entries.append(Entry(timeStart, timeEnd, ip, device, id))
combinedActions = []
for entry in entries:
combinedActions.append((entry.ip, "join", entry.startTime))
combinedActions.append((entry.ip, "leave", entry.endTime))
combinedActions = sorted(combinedActions, key = lambda x: x[2])
# print combinedActions
# State 1 => not online
# State 2 => Online with 1 device
# State 3 => Online wiht >=2 devices
states = [(datetime.strptime("00:00:00", "%H:%M:%S"), 1, [])]
devicesOnline = set()
for (ip, action, time) in combinedActions:
# print (ip, action, time)
currentState = states[-1][-1]
if action == "join":
devicesOnline.add(ip)
if len(devicesOnline) == 1:
states.append((time, 2, list(devicesOnline)))
elif len(devicesOnline) > 1:
states.append((time, 3, list(devicesOnline)))
else:
states.append((time, 1, list(devicesOnline)))
if action == "leave":
# This line will not work in the current random dataset
# assert ip in devicesOnline
if ip in devicesOnline:
devicesOnline.remove(ip)
if len(devicesOnline) == 1:
states.append((time, 2, list(devicesOnline)))
elif len(devicesOnline) > 1:
states.append((time, 3, list(devicesOnline)))
else:
states.append((time, 1, list(devicesOnline)))
# Make sure everyone logged off
# assert len(devicesOnline) == 0
return states
class Entry:
def __init__(self, startTime, endTime, ip, device, id):
self.startTime = startTime
self.endTime = endTime
self.ip = ip
self.device = device
self.id = id
# print(generateIp())
# print generateTime()
# print string.lowercase
# print generateRandomStringLength(60)
# for i in range(100):
# generateEntry(1, 2, generateRandomStringLength(60), [generateIp() for x in range(3)], "fakeData")