This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
161 lines (126 loc) · 4.58 KB
/
main.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
import os
from dotenv import load_dotenv
import numpy as np
from time import time, sleep
from datetime import datetime
import json
import pickle
from tqdm import tqdm
from multiprocessing import Pool
from kafka import KafkaProducer
from sklearn.datasets import load_svmlight_files
from msgHandler import MsgHandler
def load_svmlight_batched(n=120):
svmformat_file = "/opt/dataset/maliciousurl/url_svmlight/"
# svmformat_file = "/var/datastore/url_svmlight/"
# svmformat_file = "/home/gaspard/Documents/CS_3A/work_3A/pfe_rapids/url_class/datastore/url_svmlight/"
files_names = []
for i in range(n):
files_names.append(svmformat_file + "Day" + str(i) + ".svm")
data = load_svmlight_files(files=files_names)
data_raw = []
for i in range(n):
X = data[2 * i]
y = data[2 * i + 1]
for j in range(X.shape[0]):
data_raw.append((X[j], y[j]))
return data_raw
def work(handler, url_features_topic_name, interval=10, batch_size=5, n_urls_days=5):
try:
print("Starts loading the data")
data_raw = load_svmlight_batched(n=n_urls_days)
size_data_raw = len(data_raw)
if n_urls_days and n_urls_days < size_data_raw:
pass
else:
n_urls_days = size_data_raw
print("Starts producing fake urls")
print(size_data_raw)
# split data in bacth
batchs = [
data_raw[i : i + batch_size] for i in range(0, len(data_raw), batch_size)
]
for batch in tqdm(batchs):
time_start = time()
for data in batch:
serialized_data = pickle.dumps(data)
handler.sendMsg(
topic_name=url_features_topic_name, key=None, msg=serialized_data,
)
time_finish = time()
time_sleep = max(0, interval - (time_finish - time_start) * 1000)
if time_sleep == 0:
print("send took to long: ", (time_finish - time_start) * 1000)
sleep(time_sleep)
finally:
handler.closeAll()
def pool_worker(batch):
load_dotenv()
url_features_topic_name = os.getenv("URL_FEATURES_TOPIC")
stats_topic_name = os.getenv("STATS_TOPIC")
interval = int(os.getenv("INTERVAL"))
broker_list = os.getenv("BROKER_LIST")
producerProperties = {"bootstrap_servers": broker_list}
producer = KafkaProducer(**producerProperties, api_version=(2, 3, 0))
try:
time_start = time()
for data in batch:
serialized_data = pickle.dumps(data)
producer.send(url_features_topic_name, key=None, value=serialized_data)
metrics = {
"timestamp": str(datetime.now()),
"nb_url_sent": 1,
}
producer.send(
stats_topic_name, key=None, value=json.dumps(metrics).encode("utf-8"),
)
time_finish = time()
time_sleep = max(0, interval - (time_finish - time_start) * 1000)
if time_sleep == 0:
print("send took to long: ", (time_finish - time_start) * 1000)
sleep(time_sleep * 0.001)
except Exception as e:
# msg to logger
print("error: ", e)
finally:
producer.close()
def classic_main():
# Chargement des variables d'environnement
load_dotenv()
broker_list = os.getenv("BROKER_LIST")
interval = int(os.getenv("INTERVAL"))
batch_size = int(os.getenv("BATCH_SIZE"))
n_urls_days = int(os.getenv("N_URLS_DAYS"))
url_features_topic_name = os.getenv("URL_FEATURES_TOPIC")
consumers_attr = []
handler = MsgHandler(broker_list, consumers_attr=consumers_attr, producer=True)
print("Starts Working")
work(
handler=handler,
url_features_topic_name=url_features_topic_name,
interval=interval,
batch_size=batch_size,
n_urls_days=n_urls_days,
)
print("End Working")
def pool_main():
load_dotenv()
batch_size = int(os.getenv("BATCH_SIZE"))
n_urls_days = int(os.getenv("N_URLS_DAYS"))
n_processes = int(os.getenv("N_PROCESSES"))
print("Starts loading the data")
data_raw = load_svmlight_batched(n=n_urls_days)
size_data_raw = len(data_raw)
if n_urls_days and n_urls_days < size_data_raw:
pass
else:
n_urls_days = size_data_raw
print("Starts producing fake urls")
print(size_data_raw)
# split data in bacth
batchs = [data_raw[i : i + batch_size] for i in range(0, len(data_raw), batch_size)]
p = Pool(processes=n_processes)
with p:
p.map(pool_worker, batchs)
if __name__ == "__main__":
pool_main()