Skip to content
This repository was archived by the owner on Dec 17, 2021. It is now read-only.

Commit ae0f38f

Browse files
authored
Merge pull request #61 from splunk/feature/addon-43324-mongo-search-improv
Feature/[ADDON-43324] mongo search improvement
2 parents ed7d8e8 + 602d32d commit ae0f38f

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

splunk_connect_for_snmp_mib_server/mongo.py

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
# ########################################################################
1616
import logging
1717
import os
18+
import time
1819

1920
import pymongo
2021

2122
logger = logging.getLogger(__name__)
2223

2324

2425
class MongoRepository:
25-
def __init__(self, mongo_config):
26+
def __init__(self):
2627
self._client = pymongo.MongoClient(
2728
os.environ["MONGO_SERVICE_SERVICE_HOST"],
2829
int(os.environ["MONGO_SERVICE_SERVICE_PORT"]),
@@ -36,25 +37,43 @@ def __init__(self, mongo_config):
3637

3738

3839
class MibsRepository(MongoRepository):
40+
is_text_index_created = False
41+
3942
def __init__(self, mongo_config):
40-
super().__init__(mongo_config)
43+
super().__init__()
4144
self._mibs = self._client[mongo_config["database"]][mongo_config["collection"]]
4245

4346
def upload_files(self, mib_files_dir):
47+
tic = time.perf_counter()
4448
for filename in os.listdir(mib_files_dir):
4549
file_path = mib_files_dir + "/" + filename
4650
with open(file_path, "r") as mib_file:
4751
try:
4852
self._mibs.insert_one(
4953
dict(content=mib_file.read(), filename=filename, _id=filename)
5054
)
51-
except Exception as e:
52-
logger.error(
53-
f"Error happened during insert mib files {filename} into mongo: {e}"
55+
except Exception:
56+
logger.exception(
57+
"Error happened during insert mib files %s into mongo", filename
5458
)
59+
toc = time.perf_counter()
60+
logger.info(f"Uploading files took - {toc - tic:0.4f} seconds")
61+
62+
def create_text_index(self):
63+
tic = time.perf_counter()
64+
try:
65+
self._mibs.create_index([("content", pymongo.TEXT)], name="oid_index")
66+
except Exception:
67+
logger.exception(
68+
"Failed to create the index, searches will be performed with regex"
69+
)
70+
return
71+
toc = time.perf_counter()
72+
MibsRepository.is_text_index_created = True
73+
logger.info(f"Creating index took - {toc - tic:0.4f} seconds")
5574

5675
def search_oid(self, oid):
57-
data = self._mibs.find({"content": {"$regex": oid}})
76+
data = self.perform_correct_search(oid)
5877
if data:
5978
mib_list = []
6079
for item in data:
@@ -63,6 +82,19 @@ def search_oid(self, oid):
6382
else:
6483
return None
6584

85+
def perform_correct_search(self, oid):
86+
tic = time.perf_counter()
87+
if MibsRepository.is_text_index_created:
88+
data = self._mibs.find({"$text": {"$search": f'"{oid}"'}})
89+
else:
90+
data = self._mibs.find({"content": {"$regex": oid}})
91+
toc = time.perf_counter()
92+
logger.debug(
93+
f"We searched with {'Index' if MibsRepository.is_text_index_created else 'Regex'} "
94+
f"and the search took - {toc - tic:0.7f} seconds"
95+
)
96+
return data
97+
6698
def delete_mib(self, filename):
6799
self._mibs.delete_many({"filename": {"$regex": filename}})
68100

@@ -72,7 +104,7 @@ def clear(self):
72104

73105
class OidsRepository(MongoRepository):
74106
def __init__(self, mongo_config):
75-
super().__init__(mongo_config)
107+
super().__init__()
76108
self._oids = self._client[mongo_config["database"]][mongo_config["collection"]]
77109

78110
def contains_oid(self, oid):

splunk_connect_for_snmp_mib_server/snmp_mib_server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def upload_mibs(server_config):
3333
mibs_collection.clear()
3434
# Upload all mib files in specific dir into mongo
3535
mibs_collection.upload_files(mib_files_dir)
36+
mibs_collection.create_text_index()
3637
logger.debug("Uploaded all mib files into mongo!")
3738

3839

tests/test_translator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
)
2828
from pysnmp.smi.rfc1902 import ObjectIdentity, ObjectType
2929

30+
from splunk_connect_for_snmp_mib_server.mongo import MibsRepository
3031
from splunk_connect_for_snmp_mib_server.snmp_mib_server import upload_mibs
3132
from splunk_connect_for_snmp_mib_server.translator import Translator
3233

@@ -100,6 +101,7 @@ def setUp(self):
100101
# server_config["snmp"]["mibs"]["dir"] = "../mibs/pysnmp"
101102
self.my_translator = Translator(server_config)
102103
upload_mibs(server_config)
104+
MibsRepository.is_text_index_created = False
103105

104106
@mongomock.patch()
105107
def test_format_trap(self):

0 commit comments

Comments
 (0)