|
| 1 | +""" |
| 2 | +Uncoder IO Commercial Edition License |
| 3 | +----------------------------------------------------------------- |
| 4 | +Copyright (c) 2024 SOC Prime, Inc. |
| 5 | +
|
| 6 | +This file is part of the Uncoder IO Commercial Edition ("CE") and is |
| 7 | +licensed under the Uncoder IO Non-Commercial License (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + https://github.com/UncoderIO/UncoderIO/blob/main/LICENSE |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +----------------------------------------------------------------- |
| 17 | +""" |
| 18 | + |
| 19 | +importre |
| 20 | +fromtypingimportUnion |
| 21 | + |
| 22 | +fromapp.translator.core.models.query_containerimportRawQueryContainer,TokenizedQueryContainer |
| 23 | +fromapp.translator.core.parserimportPlatformQueryParser |
| 24 | +fromapp.translator.platforms.base.aql.constimportNUM_VALUE_PATTERN,SINGLE_QUOTES_VALUE_PATTERN |
| 25 | +fromapp.translator.platforms.base.aql.mappingimportAQLMappings,aql_mappings |
| 26 | +fromapp.translator.platforms.base.aql.tokenizerimportAQLTokenizer |
| 27 | +fromapp.translator.tools.utilsimportget_match_group |
| 28 | + |
| 29 | + |
| 30 | +classAQLQueryParser(PlatformQueryParser): |
| 31 | +tokenizer=AQLTokenizer() |
| 32 | +mappings:AQLMappings=aql_mappings |
| 33 | + |
| 34 | +log_source_functions= ("LOGSOURCENAME","LOGSOURCEGROUPNAME","LOGSOURCETYPENAME","CATEGORYNAME") |
| 35 | +log_source_function_pattern=r"\(?(?P<key>___func_name___\([a-zA-Z]+\))(?:\s+like\s+|\s+ilike\s+|\s*=\s*)'(?P<value>[%a-zA-Z\s]+)'\s*\)?\s+(?:and|or)?\s"# noqa: E501 |
| 36 | + |
| 37 | +log_source_key_types= ("devicetype","category","qid","qideventcategory") |
| 38 | +log_source_pattern=rf"___source_type___(?:\s+like\s+|\s+ilike\s+|\s*=\s*)(?:{SINGLE_QUOTES_VALUE_PATTERN}|{NUM_VALUE_PATTERN})(?:\s+(?:and|or)\s+|\s+)?"# noqa: E501 |
| 39 | +num_value_pattern=r"[0-9]+" |
| 40 | +multi_num_log_source_pattern= ( |
| 41 | +rf"___source_type___\s+in\s+\((?P<value>(?:{num_value_pattern}(?:\s*,\s*)?)+)\)(?:\s+(?:and|or)\s+|\s+)?" |
| 42 | + ) |
| 43 | +str_value_pattern=r"""(?:')(?P<s_q_value>(?:[:a-zA-Z\*0-9=+%#\-\/\\,_".$&^@!\(\)\{\}\s]|'')+)(?:')""" |
| 44 | +multi_str_log_source_pattern= ( |
| 45 | +rf"""___source_type___\s+in\s+\((?P<value>(?:{str_value_pattern}(?:\s*,\s*)?)+)\)(?:\s+(?:and|or)\s+|\s+)?""" |
| 46 | + ) |
| 47 | + |
| 48 | +table_pattern=r"\sFROM\s(?P<table>[a-zA-Z\.\-\*]+)\sWHERE\s" |
| 49 | + |
| 50 | +def__clean_query(self,query:str)->str: |
| 51 | +forfunc_nameinself.log_source_functions: |
| 52 | +pattern=self.log_source_function_pattern.replace("___func_name___",func_name) |
| 53 | +whilesearch:=re.search(pattern,query,flags=re.IGNORECASE): |
| 54 | +pos_start=search.start() |
| 55 | +pos_end=search.end() |
| 56 | +query=query[:pos_start]+query[pos_end:] |
| 57 | + |
| 58 | +returnquery |
| 59 | + |
| 60 | +@staticmethod |
| 61 | +def__parse_multi_value_log_source( |
| 62 | +match:re.Match,query:str,pattern:str |
| 63 | + )->tuple[str,Union[list[str],list[int]]]: |
| 64 | +value=match.group("value") |
| 65 | +pos_start=match.start() |
| 66 | +pos_end=match.end() |
| 67 | +query=query[:pos_start]+query[pos_end:] |
| 68 | +returnquery,re.findall(pattern,value) |
| 69 | + |
| 70 | +def__parse_log_sources(self,query:str)->tuple[dict[str,Union[list[str],list[int]]],str]: |
| 71 | +log_sources= {} |
| 72 | + |
| 73 | +ifsearch:=re.search(self.table_pattern,query,flags=re.IGNORECASE): |
| 74 | +pos_end=search.end() |
| 75 | +query=query[pos_end:] |
| 76 | + |
| 77 | +forlog_source_keyinself.log_source_key_types: |
| 78 | +pattern=self.log_source_pattern.replace("___source_type___",log_source_key) |
| 79 | +whilesearch:=re.search(pattern,query,flags=re.IGNORECASE): |
| 80 | +num_value=get_match_group(search,group_name="num_value") |
| 81 | +str_value=get_match_group(search,group_name="s_q_value") |
| 82 | +value=num_valueandint(num_value)orstr_value |
| 83 | +log_sources.setdefault(log_source_key, []).append(value) |
| 84 | +pos_start=search.start() |
| 85 | +pos_end=search.end() |
| 86 | +query=query[:pos_start]+query[pos_end:] |
| 87 | + |
| 88 | +pattern=self.multi_num_log_source_pattern.replace("___source_type___",log_source_key) |
| 89 | +ifsearch:=re.search(pattern,query,flags=re.IGNORECASE): |
| 90 | +query,values=self.__parse_multi_value_log_source(search,query,self.num_value_pattern) |
| 91 | +values= [int(v)forvinvalues] |
| 92 | +log_sources.setdefault(log_source_key, []).extend(values) |
| 93 | + |
| 94 | +pattern=self.multi_str_log_source_pattern.replace("___source_type___",log_source_key) |
| 95 | +ifsearch:=re.search(pattern,query,flags=re.IGNORECASE): |
| 96 | +query,values=self.__parse_multi_value_log_source(search,query,self.str_value_pattern) |
| 97 | +log_sources.setdefault(log_source_key, []).extend(values) |
| 98 | + |
| 99 | +returnlog_sources,query |
| 100 | + |
| 101 | +def_parse_query(self,text:str)->tuple[str,dict[str,Union[list[str],list[int]]]]: |
| 102 | +query=self.__clean_query(text) |
| 103 | +log_sources,query=self.__parse_log_sources(query) |
| 104 | +returnquery,log_sources |
| 105 | + |
| 106 | +defparse(self,raw_query_container:RawQueryContainer)->TokenizedQueryContainer: |
| 107 | +query,log_sources=self._parse_query(raw_query_container.query) |
| 108 | +tokens,source_mappings=self.get_tokens_and_source_mappings(query,log_sources) |
| 109 | +fields_tokens=self.get_fields_tokens(tokens=tokens) |
| 110 | +meta_info=raw_query_container.meta_info |
| 111 | +meta_info.query_fields=fields_tokens |
| 112 | +meta_info.source_mapping_ids= [source_mapping.source_idforsource_mappinginsource_mappings] |
| 113 | +returnTokenizedQueryContainer(tokens=tokens,meta_info=meta_info) |