Skip to content

Commit a5d6932

Browse files
committed
optimized advisor for memory and speed, added stdin input support
1 parent 389e693 commit a5d6932

File tree

1 file changed

+54
-50
lines changed
  • data-platform/autonomous-database/autonomous-json/mongodb-compatibility-advisor-23ai/files

1 file changed

+54
-50
lines changed

data-platform/autonomous-database/autonomous-json/mongodb-compatibility-advisor-23ai/files/advisor.py

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import argparse
22
import sys
3+
import re
4+
from collections import defaultdict
35

46
supported_keywords = ["$gt","$gte","$lt","$and","$not","$or","$nor","$ne","$eq","$in","$lte","$nin","$exists","$type","$regex","$text","$near","$nearSphere","$size","$natural","$inc","$min","$max",
57
"$rename","$set","$addToSet","$pop","$pull","$push","$pullAll","$each","$position","$sort","$bit","$count","$limit","$match","$skip","$slice","$mod","$geoIntersects",
68
"$geoWithin","$all","$elemMatch","$rand","$mul","$setOnInsert","$abs","$addFields","$bucket","$collStats","$facet","$group","$out","$project","$replaceRoot","$sortByCount",
79
"$unionWith","$unset","$unwind","$add","$arrayElemAt","$arrayToObject","$bottom","$ceil","$cmp","$concatArrays","$concat","$cond","$dateFromString","$dateToString","$divide",
810
"$exp","$filter","$first","$firstN","$floor","$ifNull","$isArray","$isNumber","$last","$let","$literal","$ln","$ltrim","$mergeObjects","$multiply","$objectToArray","$pow",
911
"$reverseArray","$round","$rtrim","$setUnion","$sortArray","$sqrt","$subtract","$top","$toString","$toLower","$toUpper","$trim","$trunc","$zip","$currentDate","$lastN","$reduce",
10-
"$strcasecmp","$sql","$stdDevPop","$stdDevSamp","$replaceWith","$sample","$avg","$indexOfArray","$indexOfCP","$log","$log10","$sum","$switch","$toBool","$toDate","$toDouble"]
11-
12+
"$strcasecmp","$sql","$stdDevPop","$stdDevSamp","$replaceWith","$sample","$avg","$indexOfArray","$indexOfCP","$log","$log10","$sum","$switch","$toBool","$toDate","$toDouble"]
13+
1214
not_supported_keywords=["$expr","$jsonSchema","$box","$center","$centerSphere","$maxDistance","$minDistance","$polygon","$bitsAllClear","$bitsAllSet","$bitsANyClear","$bitsAnySet","$currentData",
1315
"$accumulator","$acos","$acosh","$bucketAuto","$changeStream","$currentOp","$densify","$documents","$fill","$geoNear","$graphLookup","$indexStats","$lookup","$merge","$redact",
1416
"$search","$searchMeta","$setWindowFields","$allElementsTrue","$anyElementTrue","$asin","$asinh","$atan","$atan2","$atanh","$binarySize","$bottomN",
@@ -23,57 +25,59 @@
2325

2426
def main(argv):
2527
parser=argparse.ArgumentParser()
26-
parser.add_argument("--file",dest="file",help="Set the MongoDB log file to analyze")
28+
parser.add_argument("--file",dest="file",help="Set the MongoDB log file to analyze (if not specified, reads from stdin)")
2729

2830
argv = parser.parse_args()
2931

30-
if argv.file is None:
31-
parser.error("--file is required")
32-
33-
mongo_log = read_log(argv.file)
34-
supported_dictionary,not_supported_dictionary,operation_dictionary = search(mongo_log)
35-
generate_report(supported_dictionary,not_supported_dictionary,operation_dictionary)
36-
37-
38-
def read_log(file):
39-
with open(file, 'r') as f:
40-
text = f.readlines()
41-
return text
42-
43-
44-
def search(input_file):
45-
46-
supported_dictionary = dict()
47-
not_supported_dictionary = dict()
48-
operation_dictionary=dict()
49-
50-
for line in input_file:
51-
for keyword in supported_keywords:
52-
if keyword in line:
53-
if not keyword in supported_dictionary:
54-
supported_dictionary[keyword]=1
55-
else:
56-
supported_dictionary[keyword]+=1
57-
58-
for keyword in not_supported_keywords:
59-
if keyword in line:
60-
if not keyword in not_supported_dictionary:
61-
not_supported_dictionary[keyword]=1
62-
else:
63-
not_supported_dictionary[keyword]+=1
64-
65-
for keyword in operations:
66-
if keyword in line:
67-
if not keyword in operation_dictionary:
68-
operation_dictionary[keyword]=1
69-
else:
70-
operation_dictionary[keyword]+=1
71-
72-
73-
return supported_dictionary,not_supported_dictionary,operation_dictionary
74-
75-
76-
32+
if argv.file:
33+
input_file = open(argv.file, 'r')
34+
else:
35+
input_file = sys.stdin
36+
37+
try:
38+
supported_dictionary,not_supported_dictionary,operation_dictionary = search(input_file)
39+
generate_report(supported_dictionary,not_supported_dictionary,operation_dictionary)
40+
finally:
41+
if argv.file:
42+
input_file.close()
43+
44+
def search(input_source):
45+
supported_dictionary = defaultdict(int)
46+
not_supported_dictionary = defaultdict(int)
47+
operation_dictionary = defaultdict(int)
48+
49+
supported_pattern = '|'.join(f'[" ]{re.escape(kw)}[":]' for kw in supported_keywords)
50+
not_supported_pattern = '|'.join(f'[" ]{re.escape(kw)}[":]' for kw in not_supported_keywords)
51+
operations_pattern = '|'.join(map(re.escape, operations))
52+
53+
supported_regex = re.compile(f'({supported_pattern})')
54+
not_supported_regex = re.compile(f'({not_supported_pattern})')
55+
operations_regex = re.compile(f'({operations_pattern})')
56+
57+
for i, line in enumerate(input_source):
58+
unique_supported = set()
59+
unique_not_supported = set()
60+
unique_operations = set()
61+
62+
for match in supported_regex.finditer(line):
63+
key = match.group(0).strip('"').strip(':').strip(' ')
64+
if key not in unique_supported:
65+
supported_dictionary[key] += 1
66+
unique_supported.add(key)
67+
68+
for match in not_supported_regex.finditer(line):
69+
key = match.group(0).strip('"').strip(':').strip(' ')
70+
if key not in unique_not_supported:
71+
not_supported_dictionary[key] += 1
72+
unique_not_supported.add(key)
73+
74+
for match in operations_regex.finditer(line):
75+
key = match.group(0)
76+
if key not in unique_operations:
77+
operation_dictionary[key] += 1
78+
unique_operations.add(key)
79+
80+
return dict(supported_dictionary), dict(not_supported_dictionary), dict(operation_dictionary)
7781

7882
def generate_report(supported_dictionary,not_supported_dictionary,operations_dictionary):
7983

0 commit comments

Comments
 (0)