예제 #1
0
def data_post():  # Python 2.7.8 验证可用
    if request.method == 'GET':
        return 'GET'

    post_data = json.loads(request.data)
    # table project
    pro_name = post_data.get('pro_name', 0)
    pro_type = post_data.get('pro_type', 0)
    pro_master = post_data.get('pro_master', 0)
    pro_operations = post_data.get('pro_operations', 0)
    # table instance
    db_usage = post_data.get(
        'db_usage', 0)  # <type 'unicode'> /?/ db_usage.encode("utf-8")
    db_type = post_data.get('db_type', 0)
    # table size
    host = post_data.get('host', 0)
    port = post_data.get('port', 0)
    size = post_data.get('size', 0)
    tactics = post_data.get('tactics', 0)
    time_cost = post_data.get('time_cost', 0)
    agent_date = post_data.get('agent_date', 0)
    agent_datetime = post_data.get('agent_datetime', 0)

    # 把数据保存到 MySQL
    # 判定项目是否存在, 或新增
    pro_obj = db.session.query(Project).filter(
        and_(Project.name == pro_name), Project.type == pro_type).first()
    if not pro_obj:
        pro_obj = Project(name=pro_name,
                          type=pro_type,
                          pm=pro_master,
                          op=pro_operations)
        db.session.add(pro_obj)
        db.session.flush()
    else:
        pro_obj.pm = pro_master
        pro_obj.op = pro_operations
        db.session.commit()

    # 判定实例是否存在, 或新增
    ins_obj = db.session.query(Instance).filter(
        and_(Instance.host == host), and_(Instance.port == port),
        Instance.project_id == pro_obj.id).first()
    if not ins_obj:
        ins_obj = Instance(host=host,
                           port=port,
                           db_usage=db_usage,
                           db_type=db_type,
                           project_id=pro_obj.id)
        db.session.add(ins_obj)
        db.session.flush()
    else:
        ins_obj.db_usage = db_usage
        ins_obj.db_type = db_type
        ins_obj.project_id = pro_obj.id
        db.session.commit()

    # 添加新数据
    new_row_obj = Size(size=str(size),
                       tactics=tactics,
                       time_cost=time_cost,
                       agent_date=agent_date,
                       agent_datetime=agent_datetime,
                       instance_id=ins_obj.id)

    db.session.add(new_row_obj)
    db.session.commit()
    return 'OK'