Python 框架学习 Django篇 (八) 代码优化、数据库冗余处理
  wAzzyqwRgjCe 2023年12月02日 28 0


我们开发软件系统的时候,需要不断的反思我们代码里面是否有可以优化的地方。而优化的重点之一,就是把冗余的代码优化为可以复用的库。我们在前面编写了一些功能,但是其中存在很多冗余的方法

mgr/medicine.py
mgr/k8s.py
mgr/medicine.py

打开这3个文件我们可以看到他们的入口函数dispatcher  实际的代码相似度非常高,该函数的大体代码基本类似,不同之处,只是分配给哪些函数处理

 

Python 框架学习 Django篇 (八) 代码优化、数据库冗余处理_python

像这样的冗余代码如果持续增多,那么后续的维护成本也会变大,比如我想要将认证的返回值进行修改,那么我就需要去3个文件中挨个进行配置,所以我们要将这种相似的代码单独做成一个公共程序

一、冗余代码优化

1、添加公共函数目录

#在当前应用项目(mgr)下创建一个lib目录,并创建公共代码文件
lib
 |-handler.py

2、修改K8S.py存量代码

我们发现 请求消息给哪个函数处理, 完全是由 请求消息里面的action参数决定的, 所以,我们可以修改上面这3个代码文件,先删除原先的入口函数dispatcher函数(我前面几个定义的名称都不一致,这块可以改成一样的了)

#应用公共函数
from lib.handler import dispatcherBase

#当前函数所支持请求类型
Action2Handler = {
    'list_customer': listcustomers,
    'add_customer': addcustomer,
    'modify_customer': modifycustomer,
    'del_customer': deletecustomer,
}

def dispatcher(request):
    return dispatcherBase(request, Action2Handler)

我们定义一个什么样的action用什么函数处理的一张表 Action2Handler ,然后dispatcher 函数可以简单到直接调用 dispatcherBase, 并且把Action2Handler 作为参数传递给给它。剩下的就交由 dispatcherBase 去处理了,下面我们把原先的入口函数删除后,将上面的代码添加到3个文件最下面,注意修改Action2Handler变量中的请求参数和对应调用的函数名称

Django_demo/mgr/order.py

from django.http import JsonResponse
from django.db import  transaction
from django.db.models import F
# 导入 Order 对象定义
from  paas.models import  Order,OrderMedicine



def addorder(request):

    info  = request.params['data']

    # 从请求消息中 获取要添加订单的信息
    # 并且插入到数据库中


    with transaction.atomic():
        new_order = Order.objects.create(name=info['name'], customer_id=info['customerid'])

        batch = [OrderMedicine(order_id=new_order.id,medicine_id=mid,amount=1)
                 for mid in info['medicineids']]

        #  在多对多关系表中 添加了 多条关联记录
        OrderMedicine.objects.bulk_create(batch)


    return JsonResponse({'ret': 0,'id':new_order.id})

def listorder(request):
    # 返回一个 QuerySet 对象 ,包含所有的表记录
    qs = Order.objects \
        .annotate(
        customer_name=F('customer__name'),
        medicines_name=F('medicines__name')
    ) \
        .values(
        'id','name','create_date','customer_name','medicines_name'
    )

    # 将 QuerySet 对象 转化为 list 类型
    retlist = list(qs)

    # 可能有 ID相同,药品不同的订单记录, 需要合并
    newlist = []
    id2order = {}
    for one in retlist:
        orderid = one['id']
        if orderid not in id2order:
            newlist.append(one)
            id2order[orderid] = one
        else:
            id2order[orderid]['medicines_name'] += ' | ' + one['medicines_name']

    return JsonResponse({'ret': 0, 'retlist': newlist})




##################################新的入口函数################################



#添加导入公共函数
from .lib.handler import dispatcherBase

#传入本地函数和方法
Action2Handler = {
    'list_order': listorder,
    'add_order': addorder,
}
#重新定义入口函数
def dispatcher(request):
    return dispatcherBase(request, Action2Handler)

 3、定义公共函数

Django_demo/mgr/lib/handler.py

import json

from django.http import JsonResponse


def dispatcherBase(request,action2HandlerTable):
    # 根据session判断用户是否是登录的管理员用户
    if 'usertype' not in request.session:
        return JsonResponse({
            'ret': 302,
            'msg': '未登录',
            'redirect': '/mgr/sign.html'},
            status=302)

    if request.session['usertype'] != 'mgr':
        return JsonResponse({
            'ret': 302,
            'msg': '用户非mgr类型',
            'redirect': '/mgr/sign.html'},
            status=302)

    # 将请求参数统一放入request 的 params 属性中,方便后续处理

    # GET请求 参数 在 request 对象的 GET属性中
    if request.method == 'GET':
        request.params = request.GET

    # POST/PUT/DELETE 请求 参数 从 request 对象的 body 属性中获取
    elif request.method in ['POST','PUT','DELETE']:
        # 根据接口,POST/PUT/DELETE 请求的消息体都是 json格式
        request.params = json.loads(request.body)


    # 根据不同的action分派给不同的函数进行处理
    action = request.params['action']
    print(action)
    if action in action2HandlerTable:
        handlerFunc = action2HandlerTable[action]
        return handlerFunc(request)

    else:
        return JsonResponse({'ret': 1, 'msg': 'action参数错误'})

前面的认证转换和参数获取和之前的代码是一致的,区别在于获取到action参数后的操作

# 根据不同的action分派给不同的函数进行处理
    action = request.params['action']
    if action in action2HandlerTable:
        handlerFunc = action2HandlerTable[action]
        return handlerFunc(request)

这段代码就是根据action参数的值,到 action2HandlerTable 查找出对应的 函数处理 ,可以根据这个方法去修改其他的两个代码文件

4、测试请求

import  requests,pprint

#添加认证
payload = {
    'username': 'root',
    'password': '12345678'
}
#发送登录请求
response = requests.post('http://127.0.0.1:8000/api/mgr/signin',data=payload)
#拿到请求中的认证信息进行访问
set_cookie = response.headers.get('Set-Cookie')




# 构建添加 客户信息的 消息体,是json格式
payload = {
    "action":"list_order",
}
url='http://127.0.0.1:8000/api/mgr/orders/'

if set_cookie:
    # 将Set-Cookie字段的值添加到请求头中
    headers = {'Cookie': set_cookie}

    # 发送请求给web服务
    response = requests.post(url,json=payload,headers=headers)
    pprint.pprint(response.json())

返回

{'ret': 0,
 'retlist': [{'create_date': '2023-10-26T01:15:09.718Z',
              'customer_name': 'zhangsan',
              'id': 13,
              'medicines_name': 'gmkl',
              'name': '天山订单'},
             {'create_date': '2023-10-26T01:14:29.897Z',
              'customer_name': 'zhangsan',
              'id': 12,
              'medicines_name': 'gmkl',
              'name': '天山订单'},
             {'create_date': '2023-10-26T01:13:35.943Z',
              'customer_name': 'zhangsan',
              'id': 11,
              'medicines_name': 'gmkl',
              'name': 'ts'},
             {'create_date': '2023-10-25T03:08:00Z',
              'customer_name': 'zhangsan',
              'id': 5,
              'medicines_name': 'gmkl',
              'name': 'test'}]}

5、补全其他案例

Django_demo/mgr/k8s.py

from django.http import JsonResponse

from paas.models import PaasInfo

def listcustomers(request):
    # 返回一个 QuerySet 对象 ,包含所有的表记录
    qs = PaasInfo.objects.values()

    # 将 QuerySet 对象 转化为 list 类型
    # 否则不能 被 转化为 JSON 字符串
    retlist = list(qs)

    return JsonResponse({'ret': 0, 'retlist': retlist})
def addcustomer(request):
    info = request.params['data']

    # 从请求消息中 获取要添加客户的信息
    # 并且插入到数据库中
    # 返回值 就是对应插入记录的对象
    record = PaasInfo.objects.create(ClusterName=info['ClusterName'] ,
                                     NodeSum=info['NodeSum'] ,
                                     PrometheusAddress=info['PrometheusAddress'])

    return JsonResponse({'ret': 0, 'id':record.id})
def modifycustomer(request):

    # 从请求消息中 获取修改客户的信息
    # 找到该客户,并且进行修改操作

    customerid = request.params['id']
    newdata    = request.params['newdata']
    print(customerid,newdata)

    try:
        # 根据 id 从数据库中找到相应的客户记录
        customer = PaasInfo.objects.get(id=customerid)
    except PaasInfo.DoesNotExist:
        return  {
            'ret': 1,
            'msg': f'id 为`{customerid}`的客户不存在'
        }


    if 'ClusterName' in  newdata:
        customer.ClusterName = newdata['ClusterName']
    if 'NodeSum' in  newdata:
        customer.NodeSum = newdata['NodeSum']
    if 'PrometheusAddress' in  newdata:
        customer.PrometheusAddress = newdata['PrometheusAddress']

    # 注意,一定要执行save才能将修改信息保存到数据库

    customer.save()

    return JsonResponse({'ret': 0})
def deletecustomer(request):
    customerid = request.params['id']

    try:
        # 根据 id 从数据库中找到相应的客户记录
        customer = PaasInfo.objects.get(id=customerid)
    except PaasInfo.DoesNotExist:
        return  {
            'ret': 1,
            'msg': f'id 为`{customerid}`的客户不存在'
        }

    # delete 方法就将该记录从数据库中删除了
    customer.delete()

    return JsonResponse({'ret': 0})


#重定义入口函数
from .lib.handler import dispatcherBase

Action2Handler = {
    'list_customer': listcustomers,
    'add_customer': addcustomer,
    'modify_customer': modifycustomer,
    'del_customer': deletecustomer,
}

def dispatcher(request):
    return dispatcherBase(request, Action2Handler)

Django_demo/mgr/medicine.py

from django.http import JsonResponse

# 导入 Medicine 对象定义(这块可能显示模块导入不正常,忽略)
from  paas.models import  Medicine

import json



def listmedicine(request):
    # 返回一个 QuerySet 对象 ,包含所有的表记录
    qs = Medicine.objects.values()

    # 将 QuerySet 对象 转化为 list 类型
    # 否则不能 被 转化为 JSON 字符串
    retlist = list(qs)

    return JsonResponse({'ret': 0, 'retlist': retlist})

def addmedicine(request):

    info    = request.params['data']

    # 从请求消息中 获取要添加客户的信息
    # 并且插入到数据库中
    medicine = Medicine.objects.create(name=info['name'] ,
                                       sn=info['sn'] ,
                                       desc=info['desc'])


    return JsonResponse({'ret': 0, 'id':medicine.id})

def modifymedicine(request):

    # 从请求消息中 获取修改客户的信息
    # 找到该客户,并且进行修改操作

    medicineid = request.params['id']
    newdata    = request.params['newdata']

    try:
        # 根据 id 从数据库中找到相应的客户记录
        medicine = Medicine.objects.get(id=medicineid)
    except Medicine.DoesNotExist:
        return  {
            'ret': 1,
            'msg': f'id 为`{medicineid}`的药品不存在'
        }


    if 'name' in  newdata:
        medicine.name = newdata['name']
    if 'sn' in  newdata:
        medicine.sn = newdata['sn']
    if 'desc' in  newdata:
        medicine.desc = newdata['desc']

    # 注意,一定要执行save才能将修改信息保存到数据库
    medicine.save()

    return JsonResponse({'ret': 0})

def deletemedicine(request):

    medicineid = request.params['id']

    try:
        # 根据 id 从数据库中找到相应的药品记录
        medicine = Medicine.objects.get(id=medicineid)
    except Medicine.DoesNotExist:
        return  {
            'ret': 1,
            'msg': f'id 为`{medicineid}`的客户不存在'
        }

    # delete 方法就将该记录从数据库中删除了
    medicine.delete()

    return JsonResponse({'ret': 0})




from .lib.handler import dispatcherBase

Action2Handler = {
    'list_medicine': listmedicine,
    'add_medicine': addmedicine,
    'modify_medicine': modifymedicine,
    'del_medicine': deletemedicine,
}

def dispatcher(request):
    return dispatcherBase(request, Action2Handler)

Django_demo/mgr/urls.py

from django.urls import path
from .views import login
from .sign_in_out import signin,signout

#重定义路由名称
from .k8s import dispatcher as k8s
from .order import dispatcher as order
from .medicine import dispatcher as medicine
urlpatterns = [
    path('customers/', k8s),
    path('medicines/', medicine),
    path('orders/', order),

    path('signin', signin),
    path('signout', signout),
    path('login',login)


]

二、数据库冗余

现在我们的 mgr/order.py 里面用 listorder 函数列出订单。如果一个订单里面有多个药品,就会产生多条记录。为了解决这个问题,我们不得不用python代码来处理冗余,像下面这样

def listorder(request):
    # 返回一个 QuerySet 对象 ,包含所有的表记录
    qs = Order.objects\
            .annotate(
                customer_name=F('customer__name'),
                medicines_name=F('medicines__name')
            )\
            .values(
                'id','name','create_date','customer_name','medicines_name'
            )

    # 将 QuerySet 对象 转化为 list 类型
    retlist = list(qs)

    # 可能有 ID相同,药品不同的订单记录, 需要合并
    newlist = []
    id2order = {}
    for one in retlist:
        orderid = one['id']
        if orderid not in id2order:
            newlist.append(one)
            id2order[orderid] = one
        else:
            id2order[orderid]['medicines_name'] += ' | ' + one['medicines_name']

    return JsonResponse({'ret': 0, 'retlist': newlist})

 这样做其实有一些问题,首先他会使得我们的代码增加了额外的去除重复记录的功能,并且还会代理性能问题,比如当大量用户登录时需要列出订单信息,需要服务程序从数据库获取到数据后,再去执行去除重复记录的任务

 1、冗余问题思路

我们可以修改数据库表的设计,就在订单表(order) 里面 直接存入订单包含的药品信息

这样就不用去关联表(orderMedicine) 去获取关联药品的信息,从而也不需要去除重复代码

这样,就不需要 从 OrderMedicine 表里面 去获取关联药品信息了,当然也不需要去除重复的代码了。

      但又有了新问题,如果说我们希望订单表里面有药品信息,需要有药品的id、名称、数量,而且有可能存在多种药品,关键是不同的订单和药品的数量也是不同的,对于这种情况,我们通常可以使用一个字段, 里面存储 json格式的字符串,记录可变数量的数据信息。

Django_demo/paas/models.py

class Order(models.Model):
    # 订单名
    name = models.CharField(max_length=200,null=True,blank=True)
    # 创建日期
    create_date = models.DateTimeField(default=datetime.datetime.now)
    # 客户
    customer = models.ForeignKey(Customer,on_delete=models.PROTECT)
    # 订单购买的药品,和Medicine表是多对多 的关系
    medicines = models.ManyToManyField(Medicine, through='OrderMedicine')



    # 为了提高效率,这里存放 订单 medicines 冗余数据
    medicinelist =  models.CharField(max_length=2000,null=True,blank=True)

 我们在订单表中添加了一个medicinelist的字段,里面用json格式来存储订单中的药品

[
    {"id": 1, "name": "青霉素", "amount": 20}, 
    {"id": 2, "name": "来适可", "amount": 100}
]


id 表示药品的id

name 表示 药品的名字

amount 表示 药品的数量

 上面的例子就表示该订单中有id 为 1 和 2 的两种药品 数量分别是 20 和 100

这个字段最大长度可达2000个字符,通常足以存储订单中的药品信息了

python manage.py makemigrations common
python manage.py migrate

2、修改接口参数

我们之前在请求接口的时候使用的是如下格式

{
    "action":"add_order",
    "data":{
        "name":"华山医院订单002",
        "customerid":3,
        "medicineids":[1,2]
    }
}

这里面只有药品的id,没有药品的 名称和数量。我们在开发的时候,经常会遇到当前的接口设计不能满足新的需求,需要修改的情况。这时候就要和 接口的设计者 , 以及接口对接的开发团队进行沟通, 说明为什么你需要修改接口

3、修改请求api格式

添加订单格式
{
    "action":"add_order",
    "data":{
        "name":"华山医院订单002",
        "customerid":3,
        "medicinelist":[
            {"id":16,"amount":5,"name":"环丙沙星"},
            {"id":15,"amount":5,"name":"克林霉素"}
        ]
    }
}
列出订单格式
{
        "id": 2, 
        "name": "华山医院订单002", 
        "create_date": "2018-12-27T14:10:37.208Z", 
        "customer_name": "华山医院",
        "medicinelist":[
            {"id":16,"amount":5,"name":"环丙沙星"},
            {"id":15,"amount":5,"name":"克林霉素"}
        ]
    }

既然接口变动了,前端的开发团队 要根据修改后的接口,修改他们的代码,保证按照新的接口实现消息格式, 上面这个是前端发请求api所要携带的参数

4、修改后端代码逻辑

修改添加逻辑

Django_demo/mgr/order.py

def addorder(request):
    info = request.params['data']

    with transaction.atomic():
        medicinelist  = info['medicinelist']

        new_order = Order.objects.create(name=info['name'],
            customer_id=info['customerid'],

            # 写入json格式的药品数据到 medicinelist 字段中
            medicinelist=json.dumps(medicinelist,ensure_ascii=False),)

        batch = [OrderMedicine(order_id=new_order.id,
                               medicine_id=medicine['id'],
                               amount=medicine['amount'])
                 for medicine in medicinelist]

        OrderMedicine.objects.bulk_create(batch)

    return JsonResponse({'ret': 0, 'id': new_order.id})
修改列出表逻辑

Django_demo/mgr/order.py

def listorder(request):
    qs = Order.objects \
        .annotate(
                customer_name=F('customer__name')
        )\
        .values(
        'id', 'name', 'create_date',
        'customer_name',
        'medicinelist'
    )

    # 将 QuerySet 对象 转化为 list 类型
    retlist = list(qs)

    return JsonResponse({'ret': 0, 'retlist': retlist})

5、添加数据

import  requests,pprint

#添加认证
payload = {
    'username': 'root',
    'password': '12345678'
}
#发送登录请求
response = requests.post('http://127.0.0.1:8000/api/mgr/signin',data=payload)
#拿到请求中的认证信息进行访问
set_cookie = response.headers.get('Set-Cookie')




# 构建添加 客户信息的 消息体,是json格式
payload = {
    "action":"add_order",
    "data":{
        "name":"华山医院订单002",
        "customerid":1,
        "medicinelist":[
            {"id":6,"amount":5,"name":"gmkl"},
        ]
    }
}
url='http://127.0.0.1:8000/api/mgr/orders/'

if set_cookie:
    # 将Set-Cookie字段的值添加到请求头中
    headers = {'Cookie': set_cookie}

    # 发送请求给web服务
    response = requests.post(url,json=payload,headers=headers)
    print(response)

注意

payload = {
    "action":"add_order",
    "data":{
        "name":"华山医院订单002",
        "customerid":1,
        "medicinelist":[
            {"id":6,"amount":5,"name":"gmkl"},
        ]
    }
}

因为我们是要添加订单,所以客户id ("customerid":1,)  和药品id + 名称是必须要先知道的,或者我们在发起请求的时候不带这种id,去到后端的时候去根据数据库查询客户和药品的id再写入

6、调用查询数据

import  requests,pprint

#添加认证
payload = {
    'username': 'root',
    'password': '12345678'
}
#发送登录请求
response = requests.post('http://127.0.0.1:8000/api/mgr/signin',data=payload)
#拿到请求中的认证信息进行访问
set_cookie = response.headers.get('Set-Cookie')




# 构建添加 客户信息的 消息体,是json格式
payload = {
    "action":"list_order",
    "data":{
        "customer_name":"华山医院订单002",

    }
}
url='http://127.0.0.1:8000/api/mgr/orders/'

if set_cookie:
    # 将Set-Cookie字段的值添加到请求头中
    headers = {'Cookie': set_cookie}

    # 发送请求给web服务
    response = requests.post(url,json=payload,headers=headers)
    pprint.pprint(response.json())

返回

{'create_date': '2023-11-02T09:05:27.095Z',
              'customer_name': 'zhangsan',
              'id': 19,
              'medicinelist': '[{"id": 6, "amount": 5, "name": "gmkl"}]',
              'name': '华山医院订单002'}]}

7、关于前后方案的

   上面这么做的好处很明显,列出订单的代码就比较简单了,不需要执行去重的任务。

性能也提高了, 只要查询一张表,并且不要执行去重的任务,但是有可能出现其他的问题


     冗余数据,我们本来记录在 OrderMedicine 表 中的, 现在我们还需要记录在 Order 表中,这么一说咋还不如之前的方法呢? 这样每次需要写两张表的性能反而是下降了吧


   但是除此之外还需要考虑到,是修改订单的请求多,还是查询订单的请求多,那么一定是查询的要多很多,每次查询都要跑去重,和只有修改的时候才会写两张表之间选择,肯定是后者的方案更好


     可能有小伙伴觉得,写两张表还更麻烦了,反正订单里面有药品信息,那么干脆不用OrderMedicine表不就更简单了吗,这样做,最大的问题是, 如果以后我们需要统计药品被采购的信息就非常麻烦了,因为,现在我们在数据库中存储的订单中的药品信息,是以字符串类型存储的 json信息,不能直接使用SQL语句进行过滤查询,只能把所有的记录读取出来进行分析,那样性能会非常低

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月02日 0

暂无评论

推荐阅读
wAzzyqwRgjCe