def get_redis_keys(): """ 查看所有kys """ cache_object = Cache_query() result = cache_object.get_redis_keys() result.sort() url = request.url return render_template('sla_cache.html', re=result, url=url)
def del_redis_key_custom(key): """ 自定义删除某些keys """ cache_object = Cache_query() print("del_redis_key_custom") if key == "all": result = cache_object.del_redis_flush() else: result = cache_object.del_redis_key_custom(key) if result: url = request.url.split('/delete')[0] return render_template('rewrite.html', url=url) else: return Response("Input Error", mimetype='application/json')
def get_redis_key_custom(key): """ 自定义查看key """ cache_object = Cache_query() print("get_redis_key_custom") if key == "all": result = cache_object.get_redis_keys() else: result = cache_object.get_redis(key) if result: return Response(json.dumps(result, indent=3, ensure_ascii=False), mimetype='application/json') else: return Response("输入错误", mimetype='application/json')
def earthworm_release(business_name, start_time, end_time, old_time, query_time): """ CI/CD 平台信息 """ mysql_object = MySQL_query() cache_object = Cache_query() """ 判断是否有缓存,再加一级缓存 """ cache_key = "008_" + query_time + "_" + start_time + "__earthworm__" + business_name cache = cache_object.get_redis(cache_key) result = [] if cache: print("OK") result = eval(cache) else: gitlab_pids = get_business_gitpid(business_name) """ 输出查询的是项目名gitgroup/gitname + 发布id + 发布人 + 发布包名 + 发布时间 """ if gitlab_pids: print("进入earthworm") earthworm_release_sql = "select project_gitlab.gitlab_pid as name, release_apply.id as release_id, release_apply.apply_user_id as user, \ release_apply.package_name as package , release_apply.create_time as create_time from project_gitlab \ left join release_apply on project_gitlab.project_id=release_apply.project_id where \ (release_apply.online_task_id is not null or release_apply.online_gray_task_id is not null ) and \ project_gitlab.gitlab_pid in (%s) and \ (release_apply.create_time>'%s 24:00' and release_apply.create_time<='%s 24:00') " % ( gitlab_pids, end_time, start_time) print(earthworm_release_sql) fields = ['name', 'release_id', 'user', 'package', 'create_time'] result = mysql_object.mysql_RowProxy_to_list( earthworm_release_sql, fields, 'earthworm') result.sort(key=lambda x: (x['name'], x['release_id']), reverse=False) print("No") cache_object.set_redis(cache_key, result) return result
def medusa_release(business_name, start_time, end_time, old_time, query_time): """ Docker环境上线平台信息 """ mysql_object = MySQL_query() cache_object = Cache_query() """ 判断是否有缓存,再加一级缓存 """ cache_key = "008_" + query_time + "_" + start_time + "__medusa__" + business_name cache = cache_object.get_redis(cache_key) result = [] if cache: print("OK") result = eval(cache) else: gitlab_pids = get_business_gitpid(business_name) if gitlab_pids: #输出查询的是项目名gitgroup/gitname + 发布id + 发布人 + 发布包名 + 发布时间 print("进入medusa") medusa_release_sql = "select app.gitlab_pid as name, app_deploy_history.id as release_id, app_deploy_history.operator as user, app_deploy_history.deploy_name as package, app_deploy_history.created_at as create_time from app \ left join app_deploy_history on app.id=app_deploy_history.app_id \ where app_deploy_history.deploy_status = 'success' and \ app_deploy_history.cluster = 'online' and \ app.gitlab_pid in (%s) and \ (app_deploy_history.created_at>'%s 24:00' and app_deploy_history.created_at<='%s 24:00') " % ( gitlab_pids, end_time, start_time) print(medusa_release_sql) fields = ['name', 'release_id', 'user', 'package', 'create_time'] result = mysql_object.mysql_RowProxy_to_list( medusa_release_sql, fields, 'medusa') result.sort(key=lambda x: (x['name'], x['release_id']), reverse=False) cache_object.set_redis(cache_key, result) return result
def release_bak(): """ 维护release信息 """ start_time, end_time, old_time, query_time = get_time() url = request.url url = url.split('?')[0] mysql_object = MySQL_query() cache_object = Cache_query() """ 判断是否有缓存,再加一级缓存 """ cache_key = "008_" + query_time + "_" + start_time + "__release_bak" cache = cache_object.get_redis(cache_key) cache = "" if cache: print("ok") result = eval(cache) else: """ 获取所有业务线 """ business_url = config_name.CMDB_DOMAIN + "/api/v1/business/" req = requests.get(business_url) business = req.json() result = [] for i in range(len(business)): result_json = {} business_name = business[i]['name'] business_person = business[i]['person_duty'] business_name_english = business[i]['name_english'] print(business_name) gitlab_pids = get_business_gitpid(business_name) if gitlab_pids: earthworm_release_sql = "select count(*) as earthworm_release_count from project_gitlab left join release_apply on project_gitlab.project_id=release_apply.project_id \ where \ (release_apply.online_task_id is not null or release_apply.online_gray_task_id is not null ) and \ project_gitlab.gitlab_pid in (%s) and \ (release_apply.create_time>'%s 24:00' and release_apply.create_time<='%s 24:00') " % ( gitlab_pids, end_time, start_time) earthworm_release = mysql_object.sql_query( earthworm_release_sql, 'earthworm') earthworm_release_count = earthworm_release[0][0] print("进入medusa") medusa_release_sql = "select count(*) as medusa_release_count from app left join app_deploy_history on app.id=app_deploy_history.app_id \ where app_deploy_history.deploy_status = 'success' and \ app_deploy_history.cluster = 'online' and \ app.gitlab_pid in (%s) and \ (app_deploy_history.created_at>'%s 24:00' and app_deploy_history.created_at<='%s 24:00') " % ( gitlab_pids, end_time, start_time) print(medusa_release_sql) medusa_release = mysql_object.sql_query( medusa_release_sql, 'medusa') medusa_release_count = medusa_release[0][0] result_json[ 'earthworm_release_count'] = earthworm_release_count result_json['medusa_release_count'] = medusa_release_count result_json['business_name'] = business_name result_json['business_person'] = business_person result_json['business_name_english'] = business_name_english result.append(result_json) result.sort(key=lambda x: (x['earthworm_release_count'], x['medusa_release_count']), reverse=False) print("No") cache_object.set_redis(cache_key, result) """ 获取release的总数 """ earthworm_release_all = 0 medusa_release_all = 0 for i in range(len(result)): earthworm_release_all = earthworm_release_all + result[i][ 'earthworm_release_count'] medusa_release_all = medusa_release_all + result[i][ 'medusa_release_count'] return render_template('release.html', re=result, url=url, query_time=query_time, earthworm_release_all=earthworm_release_all, medusa_release_all=medusa_release_all)
def release(): """ release """ start_time, end_time, old_time, query_time = get_time() url = request.url url = url.split('?')[0] cache_object = Cache_query() """ 判断是否有缓存,再加一级缓存 """ cache_key = "008_" + query_time + "_" + start_time + "__release" cache = cache_object.get_redis(cache_key) if cache: print("OK") result = eval(cache) else: """ 获取所有业务线 """ business_url = config_name.CMDB_DOMAIN + "/api/v1/business/" req = requests.get(business_url) business = req.json() result = [] for i in range(len(business)): result_json = {} business_name = business[i]['name'] business_person = business[i]['person_duty'] business_name_english = business[i]['name_english'] print(business_name) release_earthworm = earthworm_release(business_name, start_time, end_time, old_time, query_time) release_medusa = medusa_release(business_name, start_time, end_time, old_time, query_time) earthworm_release_count = len(release_earthworm) medusa_release_count = len(release_medusa) result_json['earthworm_release_count'] = earthworm_release_count result_json['medusa_release_count'] = medusa_release_count result_json['business_name'] = business_name result_json['business_person'] = business_person result_json['business_name_english'] = business_name_english result.append(result_json) result.sort(key=lambda x: (x['earthworm_release_count'], x['medusa_release_count']), reverse=False) print("No") cache_object.set_redis(cache_key, result) """ 获取release的总数 """ earthworm_release_all = 0 medusa_release_all = 0 for i in range(len(result)): earthworm_release_all = earthworm_release_all + result[i][ 'earthworm_release_count'] medusa_release_all = medusa_release_all + result[i][ 'medusa_release_count'] return render_template('release.html', re=result, url=url, query_time=query_time, earthworm_release_all=earthworm_release_all, medusa_release_all=medusa_release_all)
def html_sla_business(business_name): """ sla business页面 """ url, none_url, none_count, start_time, end_time, old_time, query_time = get_common_value( ) print("进入business") cache_object = Cache_query() """ 判断是否有缓存,再加一级缓存 """ cache_key = "007_" + query_time + "_" + start_time + "__" + business_name print(cache_key) cache = cache_object.get_redis(cache_key) if cache: print("Sla Ok") result = eval(cache) else: rlock.acquire() #锁住,防止第二个业务线请求混淆 """ 获取所有二级业务线列表 """ mysql_object = MySQL_query() sql = "select subbusiness.name , subbusiness.person, subbusiness.name_english as name_english, business.name as business, business.name as name_main from subbusiness \ left join business on business.id=subbusiness.business_id where business.name='%s'" % business_name fields = ['name', 'person', 'name_english', 'business', 'name_main'] sql_result = mysql_object.mysql_RowProxy_to_list(sql, fields, 'cmdb') """ 根据每隔二级业务线获取所有domain,计算 """ result = [] for i in range(len(sql_result)): name = sql_result[i]['name'] name_english = sql_result[i]['name_english'] person = sql_result[i]['person'] business = name_main = sql_result[i]['business'] sql = "select domain.name as domain_name, domain.person_duty as domain_person from domain \ left join subbusiness on domain.subbusiness_id=subbusiness.id \ left join business on business.id=subbusiness.business_id where business.name='%s' and subbusiness.name='%s'" % ( name_main, name) fields = ['domain_name', 'domain_person'] domains = mysql_object.mysql_RowProxy_to_list(sql, fields, 'cmdb') if domains: domain_cache = sla_business_subbusiness( domains, len(domains), name, person, start_time, end_time, old_time, query_time) if domain_cache: result.append(domain_cache) print("Business Out") cache_object.set_redis(cache_key, result) rlock.release() # 解锁 result.sort(key=lambda x: (x['count_log_ava'], x['difference_ava']), reverse=False) print(result) if result: print(url, none_url, none_count, start_time, end_time, old_time, query_time) return render_template('sla.html', re=result, url=url, none_url=none_url, none_count=none_count, query_time=query_time) else: return Response("没有数据或数据匹配不上,需要业务线简写和域名对应业务线关系", mimetype='application/json')