Skip to content

Commit 50c7fd4

Browse files
committed
sync
1 parent 327dbe2 commit 50c7fd4

File tree

75 files changed

+270
-144
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+270
-144
lines changed

uncoder-core/app/routers/translate.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from fastapi import APIRouter, Body
22

33
from app.models.translation import InfoMessage, OneTranslationData, Platform, TranslatorPlatforms
4+
from app.translator.core.context_vars import return_only_first_query_ctx_var
45
from app.translator.cti_translator import CTITranslator
56
from app.translator.translator import Translator
67

@@ -15,7 +16,9 @@ def translate_one(
1516
source_platform_id: str = Body(..., embed=True),
1617
target_platform_id: str = Body(..., embed=True),
1718
text: str = Body(..., embed=True),
19+
return_only_first_query: bool = False,
1820
) -> OneTranslationData:
21+
return_only_first_query_ctx_var.set(return_only_first_query)
1922
status, data = translator.translate_one(text=text, source=source_platform_id, target=target_platform_id)
2023
if status:
2124
return OneTranslationData(status=status, translation=data, target_platform_id=target_platform_id)
@@ -27,8 +30,11 @@ def translate_one(
2730
@st_router.post("/translate/all", tags=["translator"], description="Generate all translations")
2831
@st_router.post("/translate/all/", include_in_schema=False)
2932
def translate_all(
30-
source_platform_id: str = Body(..., embed=True), text: str = Body(..., embed=True)
33+
source_platform_id: str = Body(..., embed=True),
34+
text: str = Body(..., embed=True),
35+
return_only_first_query: bool = False,
3136
) -> list[OneTranslationData]:
37+
return_only_first_query_ctx_var.set(return_only_first_query)
3238
result = translator.translate_all(text=text, source=source_platform_id)
3339
translations = []
3440
for platform_result in result:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from app.translator.tools.custom_enum import CustomEnum
2+
3+
4+
class TimeFrameType(CustomEnum):
5+
years = "years"
6+
months = "months"
7+
days = "days"
8+
hours = "hours"
9+
minutes = "minutes"

uncoder-core/app/translator/core/exceptions/core.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
class NotImplementedException(BaseException):
2-
...
1+
class NotImplementedException(BaseException): ...
32

43

5-
class BasePlatformException(BaseException):
6-
...
4+
class BasePlatformException(BaseException): ...
75

86

97
class StrictPlatformException(BasePlatformException):

uncoder-core/app/translator/core/exceptions/iocs.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
class BaseIOCsException(BaseException):
2-
...
1+
class BaseIOCsException(BaseException): ...
32

43

5-
class IocsLimitExceededException(BaseIOCsException):
6-
...
4+
class IocsLimitExceededException(BaseIOCsException): ...
75

86

97
class EmptyIOCSException(BaseIOCsException):

uncoder-core/app/translator/core/exceptions/parser.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
class BaseParserException(BaseException):
2-
...
1+
class BaseParserException(BaseException): ...
32

43

54
class TokenizerGeneralException(BaseParserException):

uncoder-core/app/translator/core/exceptions/render.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
class BaseRenderException(BaseException):
2-
...
1+
class BaseRenderException(BaseException): ...
32

43

54
class UnexpectedLogsourceException(BaseRenderException):
@@ -8,8 +7,7 @@ def __init__(self, platform_name: str, log_source: str):
87
super().__init__(message)
98

109

11-
class FunctionRenderException(BaseRenderException):
12-
...
10+
class FunctionRenderException(BaseRenderException): ...
1311

1412

1513
class UnsupportedRenderMethod(BaseRenderException):

uncoder-core/app/translator/core/mapping.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def is_suitable(self, *args, **kwargs) -> bool:
2020
def __str__(self) -> str:
2121
raise NotImplementedError("Abstract method")
2222

23+
@property
24+
def default_source(self) -> dict:
25+
return self._default_source
26+
2327

2428
class FieldMapping:
2529
def __init__(self, generic_field_name: str, platform_field_name: str):

uncoder-core/app/translator/core/parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1616
-----------------------------------------------------------------
1717
"""
18+
1819
import re
1920
from abc import ABC, abstractmethod
2021
from typing import Union

uncoder-core/app/translator/core/render.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
limitations under the License.
1717
-----------------------------------------------------------------
1818
"""
19+
1920
from abc import ABC, abstractmethod
2021
from collections.abc import Callable
21-
from typing import Optional, Union
22+
from typing import ClassVar, Optional, Union
2223

2324
from app.translator.const import DEFAULT_VALUE_TYPE
25+
from app.translator.core.context_vars import return_only_first_query_ctx_var
2426
from app.translator.core.custom_types.tokens import LogicalOperatorType, OperatorType
2527
from app.translator.core.custom_types.values import ValueType
2628
from app.translator.core.escape_manager import EscapeManager
@@ -164,7 +166,14 @@ class QueryRender(ABC):
164166
is_single_line_comment: bool = False
165167
unsupported_functions_text = "Unsupported functions were excluded from the result query:"
166168

167-
platform_functions: PlatformFunctions = PlatformFunctions()
169+
platform_functions: PlatformFunctions = None
170+
171+
def __init__(self):
172+
self.init_platform_functions()
173+
174+
def init_platform_functions(self) -> None:
175+
self.platform_functions = PlatformFunctions()
176+
self.platform_functions.platform_query_render = self
168177

169178
def render_not_supported_functions(self, not_supported_functions: list) -> str:
170179
line_template = f"{self.comment_symbol} " if self.comment_symbol and self.is_single_line_comment else ""
@@ -192,9 +201,10 @@ class PlatformQueryRender(QueryRender):
192201
field_value_map = BaseQueryFieldValue(or_token=or_token)
193202

194203
query_pattern = "{prefix}{query}{functions}"
195-
raw_log_field_pattern: str = None
204+
raw_log_field_pattern_map: ClassVar[dict[str, str]] = None
196205

197206
def __init__(self):
207+
super().__init__()
198208
self.operator_map = {
199209
LogicalOperatorType.AND: f" {self.and_token} ",
200210
LogicalOperatorType.OR: f" {self.or_token} ",
@@ -288,7 +298,8 @@ def finalize_query(
288298
*args, # noqa: ARG002
289299
**kwargs, # noqa: ARG002
290300
) -> str:
291-
query = self.query_concatenation(prefix=prefix, search=query, functions=functions)
301+
query = self.query_pattern.format(prefix=prefix, query=query, functions=functions).strip()
302+
292303
query = self.wrap_query_with_meta_info(meta_info=meta_info, query=query)
293304
if not_supported_functions:
294305
rendered_not_supported = self.render_not_supported_functions(not_supported_functions)
@@ -329,7 +340,23 @@ def _generate_from_raw_query_container(self, query_container: RawQueryContainer)
329340
prefix="", query=query_container.query, functions="", meta_info=query_container.meta_info
330341
)
331342

343+
def process_raw_log_field(self, field: str, field_type: str) -> Optional[str]:
344+
if raw_log_field_pattern := self.raw_log_field_pattern_map.get(field_type):
345+
return raw_log_field_pattern.pattern.format(field=field)
346+
347+
def process_raw_log_field_prefix(self, field: str, source_mapping: SourceMapping) -> Optional[list]:
348+
if isinstance(field, list):
349+
list_of_prefix = []
350+
for f in field:
351+
if prepared_prefix := self.process_raw_log_field_prefix(field=f, source_mapping=source_mapping):
352+
list_of_prefix.extend(prepared_prefix)
353+
return list_of_prefix
354+
if raw_log_field_type := source_mapping.raw_log_fields.get(field):
355+
return [self.process_raw_log_field(field=field, field_type=raw_log_field_type)]
356+
332357
def generate_raw_log_fields(self, fields: list[Field], source_mapping: SourceMapping) -> str:
358+
if self.raw_log_field_pattern_map is None:
359+
return ""
333360
defined_raw_log_fields = []
334361
for field in fields:
335362
mapped_field = source_mapping.fields_mapping.get_platform_field_name(generic_field_name=field.source_name)
@@ -340,10 +367,8 @@ def generate_raw_log_fields(self, fields: list[Field], source_mapping: SourceMap
340367
)
341368
if not mapped_field and self.is_strict_mapping:
342369
raise StrictPlatformException(field_name=field.source_name, platform_name=self.details.name)
343-
if mapped_field not in source_mapping.raw_log_fields:
344-
continue
345-
field_prefix = self.raw_log_field_pattern.format(field=mapped_field)
346-
defined_raw_log_fields.append(field_prefix)
370+
if field_prefix := self.process_raw_log_field_prefix(field=mapped_field, source_mapping=source_mapping):
371+
defined_raw_log_fields.extend(field_prefix)
347372
return "\n".join(set(defined_raw_log_fields))
348373

349374
def _generate_from_tokenized_query_container(self, query_container: TokenizedQueryContainer) -> str:
@@ -374,6 +399,8 @@ def _generate_from_tokenized_query_container(self, query_container: TokenizedQue
374399
meta_info=query_container.meta_info,
375400
source_mapping=source_mapping,
376401
)
402+
if return_only_first_query_ctx_var.get() is True:
403+
return finalized_query
377404
queries_map[source_mapping.source_id] = finalized_query
378405
if not queries_map and errors:
379406
raise errors[0]

uncoder-core/app/translator/core/render_cti.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
-----------------------------------------------------------------
1818
"""
1919

20-
2120
from app.translator.core.models.iocs import IocsChunkValue
2221
from app.translator.core.models.platform_details import PlatformDetails
2322

0 commit comments

Comments
 (0)