-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
executable file
·316 lines (301 loc) · 9.14 KB
/
database.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python3
import pymongo
import json
import requests
import random
import os
import sys
import signal
import time
import string
import names
import pandas as pd
import subprocess as sp
import warnings
warnings.filterwarnings("ignore")
def empty_mongo(database, collections, host="localhost", port=27017):
client = pymongo.MongoClient(host, port)
db = client[database]
for collection in collections:
db.drop_collection(collection)
return
headers = { "Content-type": "application/json" }
def random_date(start, end, prop):
format = "%Y-%m-%d"
stime = time.mktime(time.strptime(start, format))
etime = time.mktime(time.strptime(end, format))
ptime = stime + prop * (etime - stime)
return time.strftime(format, time.localtime(ptime))
def get_ids(url, name):
params = {"count": 10000}
ids = requests.get(url, params=params, headers=headers, verify=False)
ids = ids.json()[name]
return list(map(lambda x: x["_id"], ids))
def login(url):
global headers
signup = url + "/signup"
data = dict(
username="admin",
email="admin@admin",
password="admin"
)
res = requests.post(
signup,
data=json.dumps(data),
headers=headers,
verify=False
)
print("Update user to admin in compass, then press enter...", end="")
input()
url += "/login"
data = dict(
username="admin",
password="admin",
)
res = requests.post(
url,
headers=headers,
data=json.dumps(data),
verify=False
)
token = res.json()["token"]
headers["x-observatory-auth"] = token
def pass_gen(size=8, chars=string.ascii_letters + string.digits):
return "".join(random.choices(chars, k=size))
def post(url, data):
requests.post(
url,
data=json.dumps(data),
headers=headers,
verify=False,
)
def fill_users(url):
url += "/signup"
DOMAINS = [
"gmail.com",
"yahoo.com",
"hotmail.com",
"softlab.ntua.gr",
"cslab.ntua.gr",
"corelab.ntua.gr",
]
MALE = 500
FEMALE = 500
USERS = []
NAMES = []
for _ in range(MALE):
f, l = "", ""
while True:
f = names.get_first_name(gender="male")
l = names.get_last_name()
if (f, l) not in NAMES:
break
username = f"{f}{l}"
email = f"{f}{l}@{random.choice(DOMAINS)}"
password = pass_gen()
USERS.append((f, l, email, username, password))
NAMES.append((f, l))
for _ in range(MALE):
f, l = "", ""
while True:
f = names.get_first_name(gender="female")
l = names.get_last_name()
if (f, l) not in NAMES:
break
username = f"{f}{l}"
email = f"{f}{l}@{random.choice(DOMAINS)}"
password = pass_gen()
USERS.append((f, l, email, username, password))
NAMES.append((f, l))
for user in USERS:
post(
url,
dict(
username=user[3],
email=user[2],
password=user[4],
)
)
def fill_shops(url, geojson):
url += "/shops"
tags = [f"shoptag{i}" for i in range(1, 11)]
df = pd.read_csv(geojson, sep=",", header=0)
length = df.shape[0]
shoptags = []
for _ in range(length):
k = random.randint(1, 10)
shoptags.append(random.sample(tags, k=k))
df["tags"] = shoptags
for _, r in df.iterrows():
post(
url,
dict(
name=r["name"],
lng=r["lng"],
lat=r["lat"],
brand=r["brand"],
tags=r["tags"],
address=r["address"],
)
)
def fill_products(url):
url += "/products"
PRODUCTS = [
"Unleaded",
"Super Unleaded",
"Diesel",
"Gas",
"Heating Oil",
"Car Oils",
"Firewood",
"Charcoal",
"Liquid Gas",
"Windscreen Wipers",
"Car Wash",
"Car Wash Full",
"Car Perfume",
"Floor Mats",
"Polishing Oitment",
"Steering Wheel Cover",
"Spark Plug",
"Organic Cleaning",
"Engine Cleaning",
"Fire Extinguishers",
"Triangle",
"Car Pharmacy",
]
DESCRIPTIONS = [
"Car fuel with 95-97 octanes",
"Car fuel with 100+ octanes",
"Car fuel for diesel cars",
"Car fuel for gas cars",
"Low price usually, used for boilers",
"Necessary so that the engine does not get stuck",
"Wood used to keep a fire going in a fireplace",
"Used primarily for roasting/barbecue",
"For household usage, replaces electric stove",
"Used when raining to wipe the front and back screen of car",
"Wash the outside of the car",
"Inside and outside car wash",
"Liquid or paper used to make the inside of a car smell great",
"Mats used in each of the cars' seats to protect the inside of the car from litter",
"Used to polish the outside of the car",
"Used to protect the steering wheel from wear and tear, also makes it more comfortable",
"Device for delivering electric current from an ignition system to the combustion chamber of a spark-ignition engine to ignite the compressed fuel/air mixture by an electric spark, while containing combustion pressure within the engine",
"Thorough car clean both inside and outside",
"Clean the engine of the car from rust and dust",
"Active fire protection device used to extinguish or control small fires, often in emergency situations",
"Used to alert other drivers that a car is stopped",
"Box containing main pharmaceutical products, used when having accidents"
]
CATEGORIES = [
"Car Fuel",
"Car Fuel",
"Car Fuel",
"Car Fuel",
"Boiler Fuel",
"Car Essentials",
"Home Usage",
"Home Usage",
"Home Usage",
"Accessories",
"Cleaning",
"Cleaning",
"Accessories",
"Accessories",
"Cleaning",
"Accessories",
"Car Essentials",
"Cleaning",
"Cleaning",
"Car Essentials",
"Car Essentials",
"Car Essentials",
]
TAGS = [
["Car", "Fuel", "Unleaded"],
["Car", "Fuel", "Unleaded"],
["Car", "Fuel", "Diesel", "Petrol"],
["Car", "Fuel", "Gas", "Tank"],
["Oil", "Petrol", "Heat", "Tank", "Home"],
["Car", "Oil", "Maintenance"],
["Wood", "Home", "Fire"],
["Fire", "Barbecue", "Home"],
["Stove", "Fire", "Tank"],
["Car", "Water"],
["Car", "Wash", "Water"],
["Car", "Wash", "Water", "Dry-Clean"],
["Car", "Perfume", "Aroma"],
["Car", "Mat", "Floor", "Inside"],
["Car", "Polish", "Wash"],
["Car", "Leather"],
["Car", "Spark"],
["Car", "Wash"],
["Car", "Maintenance"],
["Fire"],
["Car", "Accident"],
["Car", "Pharmacy", "Health"],
]
for p, c, d, t in zip(PRODUCTS, CATEGORIES, DESCRIPTIONS, TAGS):
post(
url,
dict(
name=p,
category=c,
description=d,
tags=t,
)
)
def fill_prices(url):
users = get_ids(url, "users")
shops = get_ids(url + "/shops", "shops")
products = get_ids(url + "/products", "products")
url += "/prices"
for shop in shops:
blacklist = []
for _ in range(5):
user = random.choice(users)
product = 0
while True:
product = random.choice(products)
if product not in blacklist:
blacklist.append(product)
break
price = float(f"{random.uniform(0.5, 10.0):.2f}")
dateFrom = random_date("2016-01-01", "2019-01-01", random.random())
dateTo = random_date(dateFrom, "2019-02-01", random.random())
post(
url,
dict(
userId=user,
productId=product,
shopId=shop,
value=price,
dateFrom=dateFrom,
dateTo=dateTo,
)
)
def fill_backend(geojson, url="https://localhost:8765/observatory/api"):
s1 = time.time()
fill_users(url)
print(f"Users time elapsed: {time.time() - s1:.4f}s")
s2 = time.time()
fill_shops(url, geojson)
print(f"Shops elapsed: {time.time() - s2:.4f}s")
s3 = time.time()
fill_products(url)
print(f"Products elapsed: {time.time() - s3:.4f}s")
s4 = time.time()
fill_prices(url)
print(f"Prices elapsed: {time.time() - s4:.4f}s")
if __name__ == "__main__":
empty_mongo("softeng",
["prices",
"shops",
"products",
"users",
"blacklists"])
login("https://localhost:8765/observatory/api")
start = time.time()
fill_backend(os.path.abspath(sys.argv[1]))
print(f"Time elapsed: {time.time() - start:.4f}s")