Skip to content

Commit

Permalink
Rename log_requests.py to request_factory.py, fix sonar warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Nov 21, 2024
1 parent a850ce7 commit ea3d56a
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/commons/esclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from urllib3.exceptions import InsecureRequestWarning

from app.amqp.amqp import AmqpClient
from app.commons import logging, log_requests, log_merger
from app.commons import logging, request_factory, log_merger
from app.commons.model.launch_objects import ApplicationConfig, Response, Launch, TestItem, BulkResponse
from app.commons.model.ml import TrainInfo, ModelType
from app.utils import utils, text_processing
Expand Down
11 changes: 7 additions & 4 deletions app/commons/log_requests.py → app/commons/request_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""This module contains functions for preparing requests to Vector DB such as Elasticsearch and OpenSearch."""

from datetime import datetime
from typing import Any

from app.commons.model.launch_objects import Launch, TestItem, Log, TestItemInfo
from app.commons.prepared_log import PreparedLogMessage, PreparedLogMessageClustering
from app.utils import utils, text_processing
from app.utils.log_preparation import clean_message
from utils.utils import compute_if_absent


def create_log_template() -> dict:
Expand Down Expand Up @@ -66,7 +69,7 @@ def transform_issue_type_into_lowercase(issue_type):

def _fill_launch_test_item_fields(log_template: dict, launch: Launch, test_item: TestItem, project: str):
log_template['_index'] = project
source = log_template['_source']
source = compute_if_absent(log_template, '_source', {})
source["launch_id"] = launch.launchId
source["launch_name"] = launch.launchName
source["launch_number"] = getattr(launch, 'launchNumber', 0)
Expand All @@ -83,7 +86,7 @@ def _fill_launch_test_item_fields(log_template: dict, launch: Launch, test_item:

def _fill_common_fields(log_template: dict, log: Log, prepared_log: PreparedLogMessage) -> None:
log_template['_id'] = log.logId
source = log_template['_source']
source = compute_if_absent(log_template, '_source', {})
source['cluster_id'] = str(log.clusterId)
source['cluster_message'] = log.clusterMessage
source['log_level'] = log.logLevel
Expand All @@ -101,7 +104,7 @@ def _fill_common_fields(log_template: dict, log: Log, prepared_log: PreparedLogM
def _fill_log_fields(log_template: dict, log: Log, number_of_lines: int) -> dict[str, Any]:
prepared_log = PreparedLogMessage(log.message, number_of_lines)
_fill_common_fields(log_template, log, prepared_log)
source = log_template['_source']
source = compute_if_absent(log_template, '_source', {})
source["detected_message"] = prepared_log.exception_message_no_numbers
source["detected_message_with_numbers"] = prepared_log.exception_message
source["log_time"] = datetime(*log.logTime[:6]).strftime("%Y-%m-%d %H:%M:%S")
Expand Down Expand Up @@ -145,7 +148,7 @@ def prepare_log(launch: Launch, test_item: TestItem, log: Log, project: str) ->

def _fill_test_item_info_fields(log_template: dict, test_item_info: TestItemInfo, project: str) -> dict[str, Any]:
log_template["_index"] = project
source = log_template['_source']
source = compute_if_absent(log_template, '_source', {})
source["launch_id"] = test_item_info.launchId
source["launch_name"] = test_item_info.launchName
source["launch_number"] = getattr(test_item_info, 'launchNumber', 0)
Expand Down
2 changes: 1 addition & 1 deletion app/service/auto_analyzer_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from time import time, sleep

from app.amqp.amqp import AmqpClient
from app.commons import logging, log_requests, object_saving, log_merger
from app.commons import logging, request_factory, object_saving, log_merger
from app.commons.esclient import EsClient
from app.commons.model.launch_objects import AnalysisResult, BatchLogInfo, AnalysisCandidate, SuggestAnalysisResult, \
SearchConfig, ApplicationConfig, Launch
Expand Down
2 changes: 1 addition & 1 deletion app/service/cluster_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from sklearn.feature_extraction.text import CountVectorizer

from app.amqp.amqp import AmqpClient
from app.commons import clusterizer, logging, log_requests, log_merger
from app.commons import clusterizer, logging, request_factory, log_merger
from app.commons.esclient import EsClient
from app.commons.model.launch_objects import (ClusterResult, ClusterInfo, SearchConfig, ApplicationConfig,
LaunchInfoForClustering)
Expand Down
2 changes: 1 addition & 1 deletion app/service/namespace_finder_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from time import time

from app.commons import logging, namespace_finder, log_requests
from app.commons import logging, namespace_finder, request_factory
from app.commons.model.launch_objects import ApplicationConfig, Launch
from app.utils import utils

Expand Down
2 changes: 1 addition & 1 deletion app/service/search_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import elasticsearch
import elasticsearch.helpers

from app.commons import logging, similarity_calculator, object_saving, log_requests, log_merger
from app.commons import logging, similarity_calculator, object_saving, request_factory, log_merger
from app.commons.esclient import EsClient
from app.commons.model.launch_objects import SearchLogInfo, Log, SearchConfig, ApplicationConfig
from app.machine_learning.models.weighted_similarity_calculator import WeightedSimilarityCalculator
Expand Down
2 changes: 1 addition & 1 deletion app/service/suggest_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import elasticsearch.helpers

from app.amqp.amqp import AmqpClient
from app.commons import logging, similarity_calculator, object_saving, log_requests, log_merger
from app.commons import logging, similarity_calculator, object_saving, request_factory, log_merger
from app.commons.esclient import EsClient
from app.commons.model.launch_objects import SuggestAnalysisResult, SearchConfig, ApplicationConfig, TestItemInfo, \
AnalyzerConf
Expand Down
10 changes: 10 additions & 0 deletions app/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,13 @@ def create_path(query: dict, path: tuple[str, ...], value: Any) -> Any:
if last_element not in current_node:
current_node[last_element] = value
return current_node[last_element]


def compute_if_absent(on: dict[str, Any], key: str, default_value: Any) -> Any:
"""Compute value for key in dictionary if it is absent.
It is here just to mute SonarLint warning about possible KeyError.
"""
if key not in on:
on[key] = default_value
return on[key]

0 comments on commit ea3d56a

Please sign in to comment.