Skip to content

Commit

Permalink
add doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Teingi committed Jul 2, 2024
1 parent f745fb4 commit 8ec1bed
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ def __init__(self):
self.parser.add_option('--password', type='string', help="tenant connection user password", default='')
self.parser.add_option('--user', type='string', help="tenant connection user name")
self.parser.add_option('--files', type='string', action="append", help="specify files")
self.parser.add_option('--level', type='string', help="The alarm level, optional parameters [critical, warn, notice, ok]", default='notice')
self.parser.add_option('-c', type='string', help='obdiag custom config', default=os.path.expanduser('~/.obdiag/config.yml'))

def init(self, cmd, args):
Expand Down
1 change: 1 addition & 0 deletions docs/analyze_sql_review.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Options:
--password=PASSWORD tenant connection user password
--user=USER tenant connection user name
--files=FILES specify files
--level=LEVEL The alarm level, optional parameters [critical, warn, notice, ok]
-c C obdiag custom config
-h, --help Show help and exit.
-v, --verbose Activate verbose output.
Expand Down
2 changes: 1 addition & 1 deletion handler/analyzer/analyze_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def __filter_max_elapsed_time_with_same_sql_id(self, data):

def __parse_sql_review(self, sql):
rules = SQLReviewRuleManager()
result = rules.manager.analyze_sql_statement(sql, self.level)
result = rules.manager.analyze_sql_statement(sql, self.stdio, self.level)
return result

def __generate_current_row_selected_keys(self, diagnostics, keys, rowspan_length):
Expand Down
6 changes: 5 additions & 1 deletion handler/analyzer/analyze_sql_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, context):
self.config_path = const.DEFAULT_CONFIG_PATH
self.analyze_files_list = None
self.directly_analyze_files = False
self.level = 'notice'

def init_inner_config(self):
self.stdio.verbose("init inner config start")
Expand Down Expand Up @@ -81,6 +82,9 @@ def init_option(self):
tenant_name_option = Util.get_option(options, 'tenant_name')
if tenant_name_option is not None:
self.tenant_name = tenant_name_option
level_option = Util.get_option(options, 'level')
if level_option:
self.level = level_option
self.db_user = db_user_option
self.db_password = db_password_option
return True
Expand Down Expand Up @@ -120,7 +124,7 @@ def __directly_analyze_files(self):
sql_list = self.__parse_sql_file(file)
for sql in sql_list:
rules = SQLReviewRuleManager()
result = rules.manager.analyze_sql_statement(sql)
result = rules.manager.analyze_sql_statement(sql, self.stdio, self.level)
sql_results[sql] = result
file_results[file] = sql_results
return file_results
Expand Down
3 changes: 2 additions & 1 deletion handler/analyzer/sql/rule_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def register_rule(self, rule_class: Type[AbstractRule]):
"""
self._registered_rules[rule_class.rule_name] = rule_class

def analyze_sql_statement(self, sql_statement, level_str='notice') -> List[Result]:
def analyze_sql_statement(self, sql_statement, stdio, level_str='notice') -> List[Result]:
"""
对SQL语句列表应用所有已注册的规则,并收集结果。
:param sql_statements: SQL语句的列表。
Expand All @@ -55,6 +55,7 @@ def analyze_sql_statement(self, sql_statement, level_str='notice') -> List[Resul
suggestion = rule_instance.suggestion(sql_statement)
if result:
if suggestion.level >= level:
stdio.verbose("rule_name:{0}, suggestion_level:{1}, suggestion:{2}".format(suggestion.rule_name, suggestion.level, suggestion.suggestion))
rule_results.append(suggestion)
else:
if level <= Level.OK:
Expand Down
51 changes: 51 additions & 0 deletions test/analyzer/test_level.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*
# Copyright (c) 2022 OceanBase
# OceanBase Diagnostic Tool is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
# http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.

"""
@time: 2024/07/02
@file: test_level.py
@desc:
"""
import unittest
from handler.analyzer.sql.rules.level import Level


class TestLevelEnum(unittest.TestCase):

def test_enum_creation_and_access(self):
self.assertEqual(Level.OK.name, 'OK')
self.assertEqual(Level.OK.value, (1, 'ok'))
self.assertEqual(Level.CRITICAL.string, 'critical')

def test_comparison_operators(self):
self.assertTrue(Level.OK < Level.NOTICE)
self.assertTrue(Level.NOTICE <= Level.NOTICE)
self.assertFalse(Level.WARN <= Level.OK)
self.assertTrue(Level.CRITICAL > Level.WARN)
self.assertTrue(Level.CRITICAL >= Level.CRITICAL)

def test_from_string(self):
self.assertEqual(Level.from_string('ok'), Level.OK)
self.assertEqual(Level.from_string('warn'), Level.WARN)

with self.assertRaises(ValueError) as context:
Level.from_string('error')
self.assertEqual(str(context.exception), "No such level: error")

def test_invalid_string(self):
with self.assertRaises(ValueError) as context:
Level.from_string('unknown')
self.assertEqual(str(context.exception), "No such level: unknown")


if __name__ == '__main__':
unittest.main()

0 comments on commit 8ec1bed

Please sign in to comment.