def delete_query_data_task(desc,task_id): """ 删除图表更新任务 --- tags: - Task parameters: - name: task_id type: string required: true description: 要删除的定时更新任务的ID responses: 0: description: 删除成功 """ current_task = apscheduler.get_job(task_id) current_chart = Chart.query.filter_by(update_task_id=task_id).first() if current_task: current_task.remove() if current_chart: current_chart.update_task_id = None current_chart.put() else: return resp_hanlder(code=999,msg="图表不存在或在操作期间被删除") return resp_hanlder(result="OK") else: return resp_hanlder(code=999,msg="任务不存在或已删除")
def del_queries_by_id(desc, query_id): """ 删除保存的SQL --- tags: - SQL parameters: - name: query_id in: type: int required: true description: 要删除的查询的ID responses: o: description: ok """ auth_token = request.headers.get('Authorization') current_user_id = Users.decode_auth_token(auth_token) if SavedQuery.exist_item_by_id(query_id): current_queries = SavedQuery.get(query_id) if current_queries.created_dmp_user_id == current_user_id: current_queries.delete() return resp_hanlder(code=0, result="OK") else: return resp_hanlder(code=999, msg="您不是该查询的所有者,无权删除改查询") else: return resp_hanlder(code=999, msg="该查询不存在或已被删除")
def all(desc): if request.method == "GET": try: dbtables = [dtb.__json__() for dtb in DataTable.query.all()] return resp_hanlder(result=dbtables) except Exception as err: return resp_hanlder(err=err)
def get_queries_by_id(desc, query_id): """ 根据ID获取保存的SQL --- tags: - SQL parameters: - name: query_id in: type: int required: ture description: 查询的ID responses: 0: description: ok """ auth_token = request.headers.get('Authorization') current_user_id = Users.decode_auth_token(auth_token) if SavedQuery.exist_item_by_id(query_id): query_obj = SavedQuery.get(query_id) if query_obj.created_dmp_user_id == current_user_id: query_info = query_obj.__json__() result = {"data": query_info} return resp_hanlder(code=0, result=result) else: return resp_hanlder(code=999, msg="非改查询的所有者,无权查看") else: return resp_hanlder(code=999, msg="查询不存在或已被删除")
def icon(desc): ''' 说明:修改用户头像接口 参数:Authorization,说明:修改指定用户的头像信息,数据类型:JSON 返回值:成功返回状态码、对应提示信息及头像地址,数据类型:JSON,数据格式:{'msg':'pass','results':'http://pass','status':xxx} ''' if request.method == 'POST': try: auth_token = request.headers.get('Authorization') res = PuttingData.get_obj_data(Users, auth_token) data = request.json if data == None: return resp_hanlder(code=999) icon_obj_str = data.get('bin') icon_obj_str = icon_obj_str.split(',')[-1] current_obj = Users.query.filter(Users.id == res['id']).first() icon_data = base64.b64decode(icon_obj_str) icon_name = uuid_str() + '.jpg' save_url = current_app.config.get("SAVE_URL") origin_icon = current_obj.icon if origin_icon: origin_icon_path = os.path.join(save_url, origin_icon) if os.path.exists(origin_icon_path): os.remove(origin_icon_path) new_icon_path = os.path.join(save_url, icon_name) with open(new_icon_path, 'wb') as new_icon: new_icon.write(icon_data) current_obj.icon = current_app.config.get("ICON_URL") + icon_name current_obj.put() icon_url = current_obj.icon return resp_hanlder(code=4001, msg=RET.alert_code[4001], result=icon_url) except Exception as err: db.session.rollback() return resp_hanlder(code=999, msg=str(err))
def cpost(desc): """添加修改案例""" case_info = request.json if request.method == "POST": if not case_info.get("case_id"): try: # current_app.logger.info(request.json) new_case = Case(dmp_case_name=case_info.get("dmp_case_name"), description=case_info.get("description"), url=case_info.get("url"), url_name=case_info.get("url_name")) new_case.save() # current_app.logger.info("add case") return resp_hanlder(result="OK") except Exception as err: return resp_hanlder(code=202, err=err) elif request.method == "PUT": if case_info.get("case_id"): try: modify_case = Case.query.get(case_info.get("case_id")) # current_app.logger.info(modify_case) if "dmp_case_name" in case_info.keys(): modify_case.dmp_case_name = case_info.get("dmp_case_name") if "description" in case_info.keys(): modify_case.description = case_info.get("description") if "url" in case_info.keys(): modify_case.url = case_info.get("url") if "url_name" in case_info.keys(): modify_case.url_name = case_info.get("url_name") modify_case.put() # current_app.logger.info("修改完成") return resp_hanlder(result={"update": "OK!"}) except Exception as err: return resp_hanlder(cdoe=203, err=err)
def frozen_user(desc): ''' 说明:冻结用户接口 参数:Authorization,dmp_user_id,说明:指定用户标识token,没有dmp_user_id默认指定冻结自己,有dmp_user_id冻结指定id的用户,将confirmed改为false,数据类型:JSON 返回值:成功返回状态码及对应提示信息,数据类型:JSON,数据格式:{'msg':'pass','results':null,'status':xxx} ''' if request.method == 'POST': data = request.json auth_token = request.headers.get('Authorization') res = PuttingData.get_obj_data(Users, auth_token) try: # 没有dmp_user_id,默认冻结自己 if data == None: frozen_user_obj = Users.query.filter( Users.id == res['id']).first() else: dmp_user_id = data.get('dmp_user_id') frozen_user_obj = Users.query.filter( Users.id == dmp_user_id).first() # 超级管理员不可以冻结 if frozen_user_obj.id == 1: return resp_hanlder(code=4003, msg=RET.alert_code[4003]) frozen_user_obj.confirmed = False db.session.commit() return resp_hanlder(code=4004, msg=RET.alert_code[4004]) except Exception as err: return resp_hanlder(code=999, msg=str(err))
def migration(desc): if request.method == "POST": try: auth_token = request.headers.get('Authorization') submit_dmp_user_id = Users.decode_auth_token(auth_token) form_info = request.json origin_dmp_table_id = form_info.get("origin_dmp_table_id") rule = form_info.get("rule") destination_dmp_database_id = form_info.get( "destination_dmp_database_id") new_table_name = form_info.get("new_table_name") method = form_info.get("method") description = form_info.get("description") new_form_info = FormMigrate( origin_dmp_table_id=origin_dmp_table_id, destination_dmp_database_id=destination_dmp_database_id, new_table_name=new_table_name, ) new_form_info.save() new_form = Forms(submit_dmp_user_id=submit_dmp_user_id, description=description, form_info_id=new_form_info.fid, form_type=3) new_form.save() current_app.logger.info(new_form.id) return resp_hanlder(result="OK") except Exception as err: return resp_hanlder(code=999, err=err)
def del_archive_star(desc, archive_id): """ 文件夹取消置顶 --- tags: - BI parameters: - name: archive_id type: int required: true description: 要取消置顶的文件夹ID responses: 0: description: ok """ auth_token = request.headers.get('Authorization') current_user_id = Users.decode_auth_token(auth_token) if DashboardArchive.exist_item_by_id(archive_id): is_exists = db.session.query(exists().where( and_(ArchiveStar.dmp_user_id == current_user_id, ArchiveStar.dmp_archive_id == archive_id))).scalar() if is_exists: ArchiveStar.query.filter_by(dmp_user_id=current_user_id, dmp_archive_id=archive_id).delete() return resp_hanlder(code=0, msg="OK") else: return resp_hanlder(code=999, msg="文件夹未置顶") else: return resp_hanlder(code=999, msg="文件夹不存在或已被删除")
def from_db(desc): if request.method == "POST": try: auth_token = request.headers.get('Authorization') submit_dmp_user_id = Users.decode_auth_token(auth_token) form_info = request.json data_tablename = form_info.get("data_tablename") db_tablename = form_info.get("db_tablename") database_id = form_info.get("database_id") dmp_data_case_id = form_info.get("dmp_data_case_id") description = form_info.get("description") new_form_info = FormAddDataTable( dmp_data_table_name=data_tablename, db_table_name=db_tablename, dmp_database_id=database_id, dmp_case_id=dmp_data_case_id, ) new_form_info.save() new_form = Forms(submit_dmp_user_id=submit_dmp_user_id, description=description, form_info_id=new_form_info.fid, form_type=1) new_form.save() return resp_hanlder(result="OK") except Exception as err: return resp_hanlder(code=999, err=err)
def dbtdel(desc): if request.method == "DELETE": try: auth_token = request.headers.get('Authorization') current_user_id = Users.decode_auth_token(auth_token) del_data_table_id = request.json.get("dmp_data_table_id") if del_data_table_id: del_data_table = DataTable.get(del_data_table_id) if del_data_table: is_user = Users.get( DataTable.get(del_data_table_id).dmp_user_id ).id == current_user_id is_user_leader = Users.get( DataTable.get(del_data_table_id).dmp_user_id ).leader_dmp_user_id == current_user_id is_admin = Users.get(current_user_id).dmp_group_id == 1 if is_user or is_user_leader or is_admin: del_data_table.delete() current_app.logger.info("del db table complete!") return resp_hanlder(result="OK") else: return resp_hanlder(code=302) else: return resp_hanlder(code=404) else: return resp_hanlder(code=101) except Exception as err: return resp_hanlder(err=err)
def del_dashboard_star(desc, dashboard_id): """ 看板取消置顶 --- tags: - BI parameters: - name: dashboard_id type: int required: true description: 要取消置顶的看板ID responses: 0: description: ok """ auth_token = request.headers.get('Authorization') current_user_id = Users.decode_auth_token(auth_token) if Dashboard.exist_item_by_id(dashboard_id): is_exists = db.session.query(exists().where( and_(DashboardStar.dmp_user_id == current_user_id, DashboardStar.dmp_dashboard_id == dashboard_id))).scalar() if is_exists: DashboardStar.query.filter_by( dmp_user_id=current_user_id, dmp_dashboard_id=dashboard_id).delete() return resp_hanlder(code=0, msg="OK") else: return resp_hanlder(code=999, msg="看板已删除置顶") else: return resp_hanlder(code=999, msg="看板不存在或已被删除")
def modelhealth(desc): if request.method == "GET": try: cm = CM_tools() res = cm.get_model_health() return resp_hanlder(result=res) except Exception as err: return resp_hanlder(err=err)
def hdfs_io(desc): if request.method == "GET": try: time_interval = request.json.get("time_interval") cm = CM_tools() res = cm.get_hdfs_IO(time_interval) return resp_hanlder(result=res) except Exception as err: return resp_hanlder(err=err)
def get_hdfs_disk_usage(desc): if request.method == "GET": try: from dmp.utils import CM_tools the_cm = CM_tools() res = the_cm.get_hdfs_disk_usage(time_interval=60) return resp_hanlder(result=res) except Exception as err: return resp_hanlder(err=err)
def cdel(desc): del_case_id = request.json.get("case_id") # current_app.logger.info("del case , case_id :%d" % del_case_id) try: del_case = Case.query.get(del_case_id) del_case.delete() return resp_hanlder(result="OK") except Exception as err: return resp_hanlder(code=203, err=err)
def add_queries(desc): """ 保存SQL --- tags: - SQL parameters: - name: query_name in: type: string required: true description: 查询的名称,最长64字符 - name: query_sql in: type: string required: true description: 查询语句,最长65535字符 - name: description in: type: string required: false description: 查询的简介,最长512字符 - name: dmp_case_id in: type: int required: true description: 原数据案例ID - name: dmp_data_table_id in: type: int required: true description: 原数据表ID responses: 0: description: OK """ auth_token = request.headers.get('Authorization') current_user_id = Users.decode_auth_token(auth_token) request_json = request.json if request.json else {} valid = Add_queries_validator(request_json) if not valid.is_valid(): return resp_hanlder(code=201, msg=valid.str_errors) new_queries = SavedQuery( query_name=request_json.get("query_name"), query_sql=request_json.get("query_sql"), description=request_json.get("description"), dmp_data_table_id=request_json.get("dmp_data_table_id"), dmp_case_id=request_json.get("dmp_case_id"), created_dmp_user_id=current_user_id, changed_dmp_user_id=current_user_id, ) new_queries.save() return resp_hanlder(code=0, result=new_queries.__json__())
def get_secret_key(desc): """ 获取个人密钥 --- tags: - Bi parameters: - name: expires in: path type: int required: true description: 到期时间 responses: 0: description: ok """ import datetime import jwt try: auth_token = request.headers.get('Authorization') res = PuttingData.get_obj_data(Users, auth_token) if not isinstance(res, dict): return resp_hanlder(code=999) data = request.json if data == None: payload = { 'exp': datetime.datetime.utcnow() + datetime.timedelta(days=0, seconds=60*60*24), 'iat': datetime.datetime.utcnow(), 'user_id': res.get('id') } jwt_token_bytes = jwt.encode( payload, current_app.config.get('SECRET_KEY'), algorithm='HS256' ) return resp_hanlder(code=0, msg='个人密钥获取成功.', result={'self_token': jwt_token_bytes.decode('utf-8')}) else: expires = data.get('expires') expires = int(expires) payload = { 'exp': datetime.datetime.utcnow() + datetime.timedelta(days=0, seconds=expires), 'iat': datetime.datetime.utcnow(), 'user_id': res.get('id') } jwt_token_bytes = jwt.encode( payload, current_app.config.get('SECRET_KEY'), algorithm='HS256' ) return resp_hanlder(code=0, msg='个人密钥获取成功.', result={'self_token': jwt_token_bytes.decode('utf-8')}) except Exception as err: return resp_hanlder(code=999, err=str(err))
def username(desc): if request.method == 'POST': data = request.json username = data.get('username') user_obj = Users.query.filter(Users.dmp_username == username).first() if user_obj: return resp_hanlder(code=1012, msg=RET.alert_code[1012], result={"exist": True}) return resp_hanlder(code=1013, msg=RET.alert_code[1013], result={"exist": False})
def email(desc): if request.method == 'POST': data = request.json email = data.get('email') user_email_obj = Users.query.filter(Users.email == email).first() if user_email_obj: return resp_hanlder(code=1010, msg=RET.alert_code[1010], result={"exist": True}) return resp_hanlder(code=1011, msg=RET.alert_code[1011], result={"exist": False})
def post(desc): db_info = request.json auth_token = request.headers.get('Authorization') current_user_id = Users.decode_auth_token(auth_token) if request.method == "POST": try: new_db = Database( dmp_database_name=db_info.get("dmp_database_name"), db_type=db_info.get("db_type"), db_host=db_info.get("db_host"), db_port=db_info.get("db_port"), db_username=db_info.get("db_username"), db_passwd=db_info.get("db_passwd"), db_name=db_info.get("db_name"), ispublic=db_info.get("ispublic"), description=db_info.get("description"), dmp_user_id=current_user_id, ) new_db.save() current_app.logger.info("add new database") return resp_hanlder(result={"add_database": "complete!"}) except Exception as err: return resp_hanlder(code=200, err=err) elif request.method == "PUT": try: if db_info.get("dmp_database_id"): modify_db = Database.get(db_info.get("dmp_database_id")) if "dmp_database_name" in db_info.keys(): modify_db.dmp_database_name = db_info.get("dmp_database_name") if "db_type" in db_info.keys(): modify_db.db_type = db_info.get("db_type") if "db_host" in db_info.keys(): modify_db.db_host = db_info.get("db_host") if "db_port" in db_info.keys(): modify_db.db_port = db_info.get("db_port") if "db_username" in db_info.keys(): modify_db.db_username = db_info.get("db_username") if "db_passwd" in db_info.keys(): modify_db.db_passwd = db_info.get("db_passwd") if "db_name" in db_info.keys(): modify_db.db_name = db_info.get("db_name") if "ispublic" in db_info.keys(): modify_db.ispublic = True if db_info.get("ispublic") else False if "description" in db_info.keys(): modify_db.description = db_info.get("description") modify_db.put() current_app.logger.info("database info modify complete!") return resp_hanlder(result={"modify": "ok!"}) else: return resp_hanlder(code=101) except Exception as err: return resp_hanlder(code=999, err=err)
def groupname(desc): if request.method == 'POST': data = request.json groupname = data.get('groupname') group_obj = Groups.query.filter( Groups.dmp_group_name == groupname).first() if group_obj: return resp_hanlder(code=1016, msg=RET.alert_code[1016], result={"exist": True}) return resp_hanlder(code=1017, msg=RET.alert_code[1017], result={"exist": False})
def exist_dataservice_by_api(desc): if request.method == 'GET': try: api_path = request.json.get("api_path") if len(api_path) > 64: return resp_hanlder(code=101, msg="数据服务API路径长度不能超过64") from dmp.models import DataService count = DataService.query.filter_by(api_path=api_path).count() if count > 0: return resp_hanlder(result={"exist": True}) else: return resp_hanlder(result={"exist": False}) except Exception as err: return resp_hanlder(code=999, err=err)
def exist_dataservice_by_name(desc): if request.method == 'GET': try: dmp_dataservice_name = request.json.get("dmp_dataservice_name") if len(dmp_dataservice_name) > 64: return resp_hanlder(code=101, msg="数据服务名称长度不能超过64") from dmp.models import DataService count = DataService.query.filter_by( data_service_name=dmp_dataservice_name).count() if count > 0: return resp_hanlder(result={"exist": True}) else: return resp_hanlder(result={"exist": False}) except Exception as err: return resp_hanlder(code=999, err=err)
def add_query_data_task(desc): """ 添加图表更新任务 --- tags: - Task parameters: - name: chart_id type: int required: true description: 要添加定时更新任务的图表ID - name: time_unit type: string enum: ['weeks','days','hours','minutes','seconds'] required: true description: 时间单位 - name: time_value type: int required: true description: 时间数值 responses: 0: description: 添加定时任务成功并返回任务ID """ _task_param = request.json valid = Add_query_data_task_validator(_task_param) if not valid.is_valid(): return resp_hanlder(code=201,msg=valid.str_errors) chart_id = _task_param.get("chart_id") time_unit = _task_param.get("time_unit") time_value = _task_param.get("time_value") task_params = {} task_params["id"] = uuid_str() task_params["kwargs"] = {"chart_id":chart_id} task_params["func"] = update_chart_data task_params["trigger"] = "interval" task_params[time_unit] = time_value print(task_params) apscheduler.add_job(**task_params) current_chart = Chart.get(chart_id) if current_chart: current_chart.update_task_id = task_params.get("id") current_chart.put() else: return resp_hanlder(code=999,msg="请确认查询正常并保存图表后再设定定时任务") return resp_hanlder(result={"task_id":task_params.get("id")})
def get_charts_by_dashboard_id(dashboard_id, desc): """ 获取图表信息 --- tags: - BI parameters: - name: dashboard_id in: path type: int required: true description: url参数id(看板ID) responses: 0: description: ok """ try: auth_token = request.headers.get('Authorization') res = PuttingData.get_obj_data(Users, auth_token) if not isinstance(res, dict): return resp_hanlder(code=999) get_chart_obj = Chart.query.filter( Chart.dmp_dashboard_id == dashboard_id).first() if get_chart_obj: change_chart_obj = Chart.query.filter( Chart.dmp_dashboard_id == dashboard_id).all() change_chart_obj_dict_list = [ c.chart_to_dict() for c in change_chart_obj ] for d in change_chart_obj_dict_list: data_table_obj = DataTable.query.filter( DataTable.id == d.get('dmp_data_table_id')).first() if d.get('dmp_data_table_id' ) != None and data_table_obj != None: table_case_id = data_table_obj.dmp_case_id d['dmp_case_id'] = table_case_id else: d['dmp_data_table_id'] = None d['dmp_case_id'] = None return resp_hanlder(code=0, msg='获取图表信息成功.', result=change_chart_obj_dict_list) return resp_hanlder(code=999, msg='看板ID获取失败.') except Exception as err: db.session.rollback() return resp_hanlder(code=999, err=str(err))
def case_name(desc): if request.method == "GET": try: case_name_ = request.json.get("dmp_case_name") from dmp.models import Case case = Case.query.filter_by(dmp_case_name=case_name_).first() if case: return resp_hanlder(result={ "exist": True, }) else: return resp_hanlder(result={ "exist": False, }) except Exception as err: return resp_hanlder(err=err)
def table_list(desc): if request.method == "GET": db_id = request.json.get("dmp_database_id") conn = auto_connect(db_id=db_id) res = conn.tables_list current_app.logger.info(res) return resp_hanlder(result=res)
def set_index(desc, dashboard_id): """ 设置首页 --- tags: - BI parameters: - name: dashboard_id type: int required: true description: 要设置为首页的看板ID responses: 0: description: ok """ auth_token = request.headers.get('Authorization') current_user_id = Users.decode_auth_token(auth_token) if Dashboard.exist_item_by_id(dashboard_id): UserDashboard.query.filter_by(dmp_user_id=current_user_id).delete() index_page = UserDashboard(dmp_user_id=current_user_id, dmp_dashboard_id=dashboard_id) index_page.save() return resp_hanlder(code=0, result="OK")
def dlcomplete(desc): if request.method == "GET": try: form_id = request.json.get("form_id") form_ = Forms.get(form_id) rm_filepath = form_.info_form.filepath if os.path.exists(rm_filepath): os.remove(rm_filepath) dlform = form_.info_form dlform.ftp_url = None dlform.filepath = None dlform.put() return resp_hanlder(result="OK") except Exception as err: return resp_hanlder(result="OK")