Skip to content

Commit afa0e02

Browse files
raymond1242Raymond Negronmgonnav
authored
BITMAKER-2695 Store job stats in a single collection per project (#167)
--------- Co-authored-by: Raymond Negron <[email protected]> Co-authored-by: emegona <[email protected]>
1 parent d14d7b3 commit afa0e02

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

database_adapters/db_adapters.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ def get_connection(self):
8888
def delete_collection_data(self, database_name, collection_name):
8989
collection = self.client[database_name][collection_name]
9090
count = collection.delete_many({}).deleted_count
91-
return count if collection.drop() else 0
91+
try:
92+
collection.drop()
93+
except PyMongoError as ex:
94+
print(ex)
95+
return count
9296

9397
def get_all_collection_data(self, database_name, collection_name):
9498
collection = self.client[database_name][collection_name]
@@ -110,6 +114,12 @@ def get_chunked_collection_data(
110114
del item["_id"]
111115
return data, next_chunk
112116

117+
def get_job_stats(self, database_name, collection_name):
118+
result = self.client[database_name]["job_stats"].find(
119+
{"_id": collection_name}, {"_id": False}
120+
)
121+
return list(result)
122+
113123
def get_paginated_collection_data(
114124
self, database_name, collection_name, page, page_size
115125
):

estela-api/api/views/job_data.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,14 @@ def list(self, request, *args, **kwargs):
162162
response["next_chunk"] = next_chunk
163163
return Response(response)
164164

165-
result = spiderdata_db_client.get_paginated_collection_data(
166-
kwargs["pid"], job_collection_name, page, page_size
167-
)
165+
if data_type == "stats":
166+
result = spiderdata_db_client.get_job_stats(
167+
kwargs["pid"], job_collection_name
168+
)
169+
else:
170+
result = spiderdata_db_client.get_paginated_collection_data(
171+
kwargs["pid"], job_collection_name, page, page_size
172+
)
168173
return Response(
169174
{
170175
"count": count,

queueing/inserter.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ def __init__(self, client, database_name, collection_name, unique, topic):
3333

3434
logging.info("New Inserter created for {}.".format(self.identifier))
3535

36+
def is_job_stats(self, collection_name):
37+
return "job_stats" == collection_name.split("-")[2]
38+
3639
def __handle_insertion_error(self, response, items):
3740
logging.warning(
3841
"The exception [{}] occurred during the insertion of {} items in {}.".format(
@@ -47,11 +50,20 @@ def __handle_insertion_error(self, response, items):
4750
producer.send(self.topic, item)
4851

4952
def __insert_items(self, reason):
50-
response = self.__client.insert_many_to_collection(
51-
self.database_name,
52-
self.collection_name,
53-
[item["payload"] for item in self.__items],
54-
)
53+
if self.is_job_stats(self.collection_name):
54+
self.__items[0]["payload"]["_id"] = self.collection_name
55+
response = self.__client.insert_one_to_collection(
56+
self.database_name,
57+
"job_stats",
58+
self.__items[0]["payload"],
59+
)
60+
else:
61+
response = self.__client.insert_many_to_collection(
62+
self.database_name,
63+
self.collection_name,
64+
[item["payload"] for item in self.__items],
65+
)
66+
5567
if response.ok:
5668
logging.info(
5769
"{} documents inserted [{}] in {}.".format(
@@ -60,6 +72,7 @@ def __insert_items(self, reason):
6072
)
6173
else:
6274
self.__handle_insertion_error(response, self.__items)
75+
6376
del self.__items[:]
6477

6578
def is_inactive(self):

0 commit comments

Comments
 (0)