-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhq_services.py
308 lines (237 loc) · 10.9 KB
/
hq_services.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
import json
from posixpath import basename
import random
import shutil
from time import sleep
from uuid import uuid4
import importlib_resources
from sites import *
from files import putfile
from helpers import *
from common import *
from streams import consume, produce
from tables import find_document_by_id, upsert_document
logger = logging.getLogger(__name__)
services = HQSite['services']
tiles = HQSite['tiles']
assets = HQSite['assets']
# HQ SERVICES
def image_feed_service(host: str, user: str, password: str):
"""
Simulate recieving events from IMAGE API, send random notification messages every IMAGE_FEED_INTERVAL seconds to TOPIC_IMAGEFEED,
and save notification message into TABLE
"""
service = services["imagefeed"]
stream_path = f"{HQ_VOLUME_PATH}/{STREAM_PIPELINE}"
table_path = HQ_IMAGETABLE
# load mock feed data from file
input_data = None
with open(
importlib_resources.files("main").joinpath(
IMAGE_FEED_FILE
),
"r",
) as f:
input_data = json.load(f)
# logger.debug("IMAGE feed data: %s", input_data)
# notify user
service["active"] = True
items = input_data["collection"]["items"]
logger.debug("Loaded %d messages for IMAGE Feed", len(items))
output_topic_path = f"{stream_path}:{TOPIC_IMAGEFEED}"
# skip if service is disabled by user -- shouldn't come here - possibly safe to remove
if not service["active"]:
logger.debug("is disabled")
return
count = random.randrange(5) + 1
# pick "count" random items
for item in random.sample(items, count):
# assign a unique ID for table
item["_id"] = str(uuid4().hex[:12])
if upsert_document(host=host, user=user, password=password, table=table_path, json_dict=item):
logger.info(
"Event notification from IMAGE: %s",
item["data"][0]["title"]
)
else:
logger.warning("Saving event notification failed for %s", item['data'][0]['title'])
# push message to pipeline stream
message = {
"title": item["data"][0]["title"],
"description": item["data"][0]["description"],
"tablename": table_path.split("/").pop(),
"assetID": item["_id"],
"messageCreatorID": "ezshow",
}
if produce(stream=stream_path, topic=TOPIC_IMAGEFEED, record=json.dumps(message)):
logger.info("Published image received event to %s", output_topic_path)
# notify ui that we have new message
# app.storage.general["imagefeed_count"] = app.storage.general.get("imagefeed_count", 0) + 1
service["count"] += 1
# update dashboard tiles with new message
tiles.append(
tuple(["IMAGE Feed Service", f"Asset: {message['assetID']}", "New asset available from IMAGE", None])
)
else:
logger.warning("Publish failed for assetID %s", message['assetID'])
# add random delay between events (0 to 1 sec)
sleep(random.random())
# add delay to publishing
# sleep(app.storage.general.get('imagefeed_delay', 1.0))
def image_download_service(host: str, user: str, password: str):
"""
Subscribe to TOPIC_IMAGEFEED and download/save assets (images) for published events into IMAGE_FILE_LOCATION
Update TABLE with new download location and publish an event to notify downloaded/failed image to TOPIC_IMAGE_DOWNLOADED
"""
service = services["imagedownload"]
stream_path = f"{HQ_VOLUME_PATH}/{STREAM_PIPELINE}"
table_path = HQ_IMAGETABLE
input_topic = TOPIC_IMAGEFEED
output_topic = TOPIC_IMAGE_DOWNLOAD
service["active"] = True
# skip if service is disabled by user
if not service["active"]:
logger.debug("is disabled")
return
downloaded = 0
failed = 0
for msg in consume(stream=stream_path, topic=input_topic):
try:
record: dict = json.loads(msg)
logger.debug("Received: %s", record['title'])
doc = find_document_by_id(host=host, user=user, password=password, table=table_path, docid=record["assetID"])
# logger.debug("Found: %s", str(doc))
if doc is None:
logger.warning("Asset not found for: %s", record["assetID"])
failed += 1
else:
# lazily get the last part of href - should be the filename
assetUrl = doc["links"][0]["href"]
imageFilename = assetUrl.split("/").pop()
logger.info("Downloading asset for %s", doc['data'][0]['title'])
newMessage = {
"title": doc["data"][0]["title"],
"description": doc["data"][0]["description"],
"filename": imageFilename,
"assetID": doc["_id"],
}
# put file to downloadLocation
response = putfile(
host=host,
user=user,
password=password,
file=f"images/{imageFilename}",
destfolder=IMAGE_FILE_LOCATION
)
if response is not None:
logger.debug(
"Asset saved in folder %s/%s", HQ_VOLUME_PATH, IMAGE_FILE_LOCATION
)
# update DB with new location
doc["imageDownloadLocation"] = f"{HQ_VOLUME_PATH}/{IMAGE_FILE_LOCATION}/{imageFilename}"
if upsert_document(host=host, user=user, password=password, table=table_path, json_dict=doc):
logger.debug("Document updated with new location")
newMessage['status'] = "success"
downloaded += 1
else:
logger.warning("Failed to update document %s for asset location", doc['_id'])
newMessage['status'] = "failed"
failed += 1
# Publish image saved message to output stream
if produce(stream=stream_path, topic=output_topic, record=json.dumps(newMessage)):
logger.debug("Published image download event to %s", f"{stream_path}:{output_topic}")
# notify ui that we have new message
service["count"] += 1
# update dashboard with a tile
tiles.append(
tuple(["Image Download Service", doc['data'][0]['title'], doc['data'][0]['description'], doc["imageDownloadLocation"]])
)
else:
logger.warning("Publish to %s failed for %s", f"{stream_path}:{output_topic}", newMessage)
except Exception as error:
logger.warning(error)
# logger.debug("FINISHED -> OK: %d, FAIL: %d", downloaded, failed)
def asset_broadcast_service():
"""
Subscribe to IMAGE_DOWNLOAD topic and publish to ASSET_BROADCAST to notify edge clusters for new asset availability
"""
service = services["assetbroadcast"]
local_stream_path = f"{HQ_VOLUME_PATH}/{STREAM_PIPELINE}"
replicated_stream_path = HQ_STREAM_REPLICATED
input_topic = TOPIC_IMAGE_DOWNLOAD
output_topic = TOPIC_ASSET_BROADCAST
service["active"] = True
# skip if service is disabled by user
if not service["active"]:
logger.debug("is disabled")
for msg in consume(stream=local_stream_path, topic=input_topic):
record = json.loads(msg)
logger.debug("Received: %s", record['title'])
# skip assets that failed to download
if record["status"] == "failed":
logger.warning("Record has failed, not publishing: %s", record["status"])
continue
try:
# Publish to output topic on the replicated stream
if produce(
stream=replicated_stream_path, topic=output_topic, record=json.dumps(record)
):
# update message status
record["status"] = "broadcast"
logger.info(
"Published asset to %s", f"{replicated_stream_path}:{output_topic}"
)
# notify ui that we have new message
service["count"] += 1
# update dashboard with a tile
tiles.append(
tuple(["Asset Broadcast Service", f"Broadcasting: {record['assetID']}", record['title'], None])
)
else:
# update message status
record["status"] = "failed"
logger.warning(
"Publish to %s failed for %s",
f"{replicated_stream_path}:{output_topic}",
record,
)
except Exception as error:
logger.warning(error)
def asset_response_service(host: str, user: str, password: str):
"""
Monitor ASSET_REQUEST topic for the requests from Edge
"""
service = services["assetresponse"]
stream_path = HQ_STREAM_REPLICATED
table_path = HQ_IMAGETABLE
input_topic = TOPIC_ASSET_REQUEST
service["active"] = True
# skip if service is disabled by user
if not service["active"]:
logger.debug("is disabled")
try:
for msg in consume(stream=stream_path, topic=input_topic):
record = json.loads(msg)
logger.info("Responding to asset request for: %s", record['title'])
doc = find_document_by_id(host=host, user=user, password=password, table=table_path, docid=record["assetID"])
logger.debug("Found: %s", str(doc['data'][0]['title']))
if doc is None:
logger.warning("Asset not found for: %s", record['assetID'])
else:
logger.info("Copying asset from %s", doc["imageDownloadLocation"])
filename = basename(doc['imageDownloadLocation'])
shutil.copyfile(f"/mapr/{HQSite['clusterName']}{doc['imageDownloadLocation']}", f"/mapr/{HQSite['clusterName']}{HQ_MISSION_FILES}/{filename}")
# # FIX: this is the flexible option, doesn't require /mapr mount - but don't want to use async here
# async for out in run_command(
# f"hadoop fs -cp {doc['imageDownloadLocation']} {HQ_MISSION_FILES}"
# ):
# logger.debug(out.strip())
logger.info("%s sent to mission volume", doc['imageDownloadLocation'])
# notify ui that we have new message
service["count"] += 1
# update dashboard with a tile
tiles.append(
tuple(["Asset Response Service", f"Processing: {record['assetID']}", record['title'], None])
)
except Exception as error:
logger.warning("Error in asset response service: %s", str(error))