forked from ARGOeu/eosc-recommender-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rs-stream.py
executable file
·308 lines (257 loc) · 11.4 KB
/
rs-stream.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
#!/usr/bin/env python3
import time
import stomp
import argparse
import sys
import json
import logging
import ssl
import pymongo
import dateutil.parser
from datetime import datetime
# mapping recommendations
rec_map = {'publications': 'publication', 'datasets': 'dataset',
'software': 'software', 'services': 'service',
'trainings': 'training', 'other_research_product': 'other',
'similar_services': 'service'}
# Streaming connector using stomp protocol to ingest data from rs databus
# establish basic logging
logging.basicConfig(
stream=sys.stdout,
level=logging.DEBUG,
format="[%(asctime)s] %(levelname)s %(message)s",
)
def connect_subscribe(msg_queue, username, password, topic):
msg_queue.connect(username, password, wait=True)
# subscribe to the topic
msg_queue.subscribe(destination="/topic/" + topic, id=1,
ack="auto")
def main(args):
# Create a listener class to react on each message received from the queue
class UserActionsListener(stomp.ConnectionListener):
def __init__(self, conn):
self.conn = conn
# In case of error log it along with the message
def on_error(self, frame):
logging.error("error occured {}".format(frame.body))
def on_disconnect(self):
logging.warning("disconnected ...trying to reconnect")
connect_subscribe(self.conn)
def on_message(self, frame):
# process the message
message = json.loads(json.loads(frame.body))
panel = "other"
target_path = ""
source_path = ""
target_resource_id = None
source_resource_id = None
# in current mode there should be not found user_id=-1
# a -1 indicates a legacy mode,
# since it is current user_id is set with None
user_id = None
aai_uid = None
unique_id = None
if "user_id" in message:
user_id = message["user_id"]
if "aai_uid" in message:
aai_uid = message["aai_uid"] if not message["aai_uid"] == "" \
else None
if "unique_id" in message:
unique_id = str(message["unique_id"])
if "source" in message:
if "page_id" in message["source"]:
source_path = message["source"]["page_id"]
if "root" in message["source"]:
if "type" in message["source"]["root"]:
panel = message["source"]["root"]["type"]
if "resource_type" in message["source"]["root"]:
resource_type = \
message["source"]["root"]["resource_type"]
if "resource_id" in message["source"]["root"]:
target_resource_id = \
message["source"]["root"]["resource_id"]
if "target" in message:
if "page_id" in message["target"]:
target_path = message["target"]["page_id"]
record = {
"timestamp": dateutil.parser.isoparse(message["timestamp"]),
"user_id": user_id,
"aai_uid": aai_uid,
"unique_id": unique_id,
"panel": panel,
"target_path": target_path,
"source_path": source_path,
"target_resource_id": target_resource_id,
"source_resource_id": source_resource_id,
"reward": 1.0 if message["action"]["order"] else 0.0,
"type": resource_type,
"ingestion": "stream",
}
rsmetrics_db["user_actions"].insert_one(record)
# Create a listener class to react on each message received from the queue
class UserEventsListener(stomp.ConnectionListener):
def __init__(self, conn):
self.conn = conn
# In case of error log it along with the message
def on_error(self, frame):
logging.error("error occured {}".format(frame.body))
def on_disconnect(self):
logging.warning("disconnected ...trying to reconnect")
connect_subscribe(self.conn)
def on_message(self, frame):
# process the message
message = json.loads(frame.body)
# Handle resource and update resources collection
if message['model'] == 'Service':
# retrieve resource id
resource = int(message['record']['id'])
# Update resource info
# if resource is deleted, update entry as in update,
# but also set deleted_on with timestamp
if message['cud'] == 'update' or message['cud'] == 'delete':
record = {'name': message['record']['name'],
'deleted_on': datetime.fromisoformat(
message['timestamp'].replace('Z', '+00:00'))
if message['cud'] == 'delete' else None,
'scientific_domain':
message['record']['scientific_domains'],
'category':
message['record']['categories'],
'type': 'service',
'ingestion': 'stream'}
# a connection has already been established at main
result = rsmetrics_db['resources'].update_one(
{'id': resource},
{'$set': record})
if result.matched_count == 1:
logging.info("The resource {} was successfully\
{}d".format(
resource, message['cud']))
# Create resource record
elif message['cud'] == 'create':
record = {'id': resource,
'name': message['record']['name'],
'created_on': datetime.fromisoformat(
message['timestamp'].replace('Z', '+00:00')),
'deleted_on': None,
'scientific_domain':
message['record']['scientific_domains'],
'category': message['record']['categories'],
'type': 'service',
'ingestion': 'stream'}
# a connection has already been established at main
result = rsmetrics_db['resources'].insert_one(record)
if result.acknowledged == 1:
logging.info("The resource {} was successfully \
created".format(resource))
else:
logging.info("Unknown Type of resource's state")
# add resource info to streaming collection too
rsmetrics_db['service_events_streaming'].insert_one(message)
else:
rsmetrics_db['other_events_streaming'].insert_one(message)
# Create a listener class to react on each message received from the queue
class RecommendationsListener(stomp.ConnectionListener):
def __init__(self, conn):
self.conn = conn
# In case of error log it along with the message
def on_error(self, frame):
logging.error("error occured {}".format(frame.body))
def on_disconnect(self):
logging.warning("disconnected ...trying to reconnect")
connect_subscribe(self.conn)
def on_message(self, frame):
# process the message
message = json.loads(frame.body)
# in current mode there should be not found user_id=-1
# a -1 indicates a legacy mode,
# since it is current user_id is set with None
user_id = None
aai_uid = None
unique_id = None
if "user_id" in message["context"]:
user_id = message["context"]["user_id"]
if "aai_uid" in message["context"]:
aai_uid = message["context"]["aai_uid"]
aai_uid = message["context"]["aai_uid"] if not \
message["context"]["aai_uid"] == "" \
else None
if "unique_id" in message["context"]:
unique_id = str(message["context"]["unique_id"])
# handle data accordingly
record = {
"timestamp": dateutil.parser.isoparse(
message["context"]["timestamp"]),
"user_id": user_id,
"aai_uid": aai_uid,
"unique_id": unique_id,
"resource_ids": message["recommendations"],
"type": rec_map[message["panel_id"]],
"ingestion": "stream",
"provider": message["recommender_systems"][0].lower(),
}
rsmetrics_db["recommendations"].insert_one(record)
# connect to the datastore
mongo = pymongo.MongoClient(args.datastore,
uuidRepresentation="pythonLegacy")
rsmetrics_db = mongo[args.datastore.split("/")[-1]]
username, password = args.auth.split(":")
host, port = args.queue.split(":")
# create the connection to the queue
msg_queue = stomp.Connection([(host, port)], heartbeats=(10000, 5000))
msg_queue.set_ssl(for_hosts=[(host, port)], ssl_version=ssl.PROTOCOL_TLS)
# Check what kind of resource_type is going to be used
if args.data_type == "user_actions":
msg_queue.set_listener("", UserActionsListener(msg_queue))
elif args.data_type == "mp_db_events":
msg_queue.set_listener('', UserEventsListener(msg_queue))
elif args.data_type == "recommendations":
msg_queue.set_listener('', RecommendationsListener(msg_queue))
else:
logging.error(
"{} is not a supported ingestion data type".format(args.data_type)
)
sys.exit(1)
connect_subscribe(msg_queue, username, password, args.data_type)
while True:
time.sleep(2)
if not msg_queue.is_connected():
logging.warning("disconnected ...trying to reconnect")
connect_subscribe(msg_queue, username, password, args.data_type)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="RS Stream Connector")
parser.add_argument(
"-a",
"--auth",
metavar="STRING",
help="authentication in the form username:password",
required=True,
dest="auth",
)
parser.add_argument(
"-q",
"--queue",
metavar="STRING",
help="queue in the form host:port",
required=True,
dest="queue",
)
parser.add_argument(
"-t",
"--type",
metavar="STRING",
help=("type of data to be ingested "
"(e.g. user_actions, recommendations, mp_db_events)"),
required=True,
dest="data_type",
)
parser.add_argument(
"-d",
"--datastore",
metavar="STRING",
help="datastore uri",
required=True,
dest="datastore",
)
# Pass the arguments to main method
sys.exit(main(parser.parse_args()))