Skip to content

Commit

Permalink
日志完善,同时修复 #76 中编辑命令报错的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hhyo committed Apr 5, 2019
1 parent cef6873 commit abb066b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
14 changes: 11 additions & 3 deletions themis/rule_analysis/db/db_operat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import MySQLdb
from django.db import connection
from sql.models import Instance
import logging

logger = logging.getLogger('default')


class DbOperat(object):
Expand Down Expand Up @@ -32,9 +35,14 @@ def get_db_cursor(self):
return self.cursor

def execute(self, sql):
self.cursor.execute(sql)
results = self.cursor.fetchall()
return results
try:
self.cursor.execute(sql)
results = self.cursor.fetchall()
except Exception as e:
logger.error(f"Themis执行MySQL语句出错,语句:{sql},错误信息:{e}")
raise Exception(e)
else:
return results

def close(self):
self.cursor.close()
Expand Down
9 changes: 8 additions & 1 deletion themis/rule_analysis/db/mongo_operat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import pymongo
from django.conf import settings
import logging

logger = logging.getLogger('default')


class MongoOperat(object):
Expand Down Expand Up @@ -44,7 +47,11 @@ def command(self, rule_cmd, nolock=True):
rule_cmd = "db.rule.find({'db_type' : 'O'})"
client.command(rule_cmd)
"""
self.db.command("eval", rule_cmd, nolock)
try:
self.db.command("eval", rule_cmd, nolock)
except Exception as e:
logger.error(f"Themis执行Mongo命令出错,命令:{rule_cmd},错误信息:{e}")
raise Exception(e)

def find(self, collection, sql, condition=None):
result = self.get_collection(collection).find(sql, condition)
Expand Down
23 changes: 12 additions & 11 deletions themis/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
from themis.rule_analysis.db.mongo_operat import MongoOperat
from sql.models import Instance

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

import logging

logger = logging.getLogger('default')
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


class BaseHandler(LoginRequiredMixin, PermissionRequiredMixin, View):
Expand Down Expand Up @@ -170,6 +169,9 @@ def get(self, request):
def post(self, request):
rule_name = request.POST.get("id")
db_type = request.POST.get("dbtype")
flag = request.POST.get("flag")
oldvalue = request.POST.get("value", None)
value = request.POST.get("value", None)
if db_type == "Oracle":
dbtype = "O"
elif db_type == "Mysql":
Expand All @@ -178,22 +180,18 @@ def post(self, request):
raise APIError(u"db类型不正确", 30055)
if not rule_name:
raise APIError(u"规则名称不正确", 30057)
flag = request.POST.get("flag")

rule_name = rule_name.split("$")[0]
record = self.mongo_client.get_collection("rule"). \
find_one({"rule_name": rule_name, "db_type": dbtype})
if not record:
raise APIError(u"没有相关规则", 30055)
if flag == "maxscore":
value = request.POST.get("value", None)
oldvalue = request.POST.get("value", None)
self.mongo_client.get_collection("rule").update_one(
{"rule_name": rule_name, "db_type": dbtype},
{"$set": {"max_score": str(value)}}
)
elif flag == "status":
value = request.POST.get("value", None)
oldvalue = request.POST.get("oldvalue", None)
if value not in ["ON", "OFF"] or oldvalue not in ["ON", "OFF"]:
raise APIError(u"状态不正确", 30054)
self.mongo_client.get_collection("rule").update_one(
Expand All @@ -202,10 +200,9 @@ def post(self, request):
)
elif flag == "weight":
try:
value = float(request.POST.get("value", None))
value = float(value)
except Exception:
raise APIError(u"设置错误", 30059)
oldvalue = request.POST.get("oldvalue", None)
self.mongo_client.get_collection("rule").update_one(
{"rule_name": rule_name, "db_type": dbtype},
{"$set": {"weight": value}}
Expand All @@ -216,14 +213,18 @@ def post(self, request):
if len(record['input_parms']) < int(flag[-1]):
raise APIError(u"设置错误", 30055)
try:
value = float(request.POST.get("value", None))
value = float(value)
except Exception:
raise APIError(u"设置错误", 30059)
oldvalue = request.POST.get("oldvalue", None)
self.mongo_client.get_collection("rule").update_one(
{"rule_name": rule_name, "db_type": dbtype},
{"$set": {edit_parm: value}}
)
elif flag == "rule_cmd":
self.mongo_client.get_collection("rule").update_one(
{"rule_name": rule_name, "db_type": dbtype},
{"$set": {'rule_cmd': value}}
)
context = {
"message": u"规则设置成功",
"errcode": 80025,
Expand Down

0 comments on commit abb066b

Please sign in to comment.